This commit is contained in:
sprov
2021-06-06 22:37:10 +08:00
parent 810dad53d5
commit 3d7192d0f6
29 changed files with 1131 additions and 199 deletions

View File

@@ -1,24 +1,62 @@
package xray
import (
"encoding/json"
"bytes"
"x-ui/util/json_util"
)
type Config struct {
LogConfig json.RawMessage `json:"log"`
RouterConfig json.RawMessage `json:"routing"`
DNSConfig json.RawMessage `json:"dns"`
InboundConfigs []InboundConfig `json:"inbounds"`
OutboundConfigs json.RawMessage `json:"outbounds"`
Transport json.RawMessage `json:"transport"`
Policy json.RawMessage `json:"policy"`
API json.RawMessage `json:"api"`
Stats json.RawMessage `json:"stats"`
Reverse json.RawMessage `json:"reverse"`
FakeDNS json.RawMessage `json:"fakeDns"`
LogConfig json_util.RawMessage `json:"log"`
RouterConfig json_util.RawMessage `json:"routing"`
DNSConfig json_util.RawMessage `json:"dns"`
InboundConfigs []InboundConfig `json:"inbounds"`
OutboundConfigs json_util.RawMessage `json:"outbounds"`
Transport json_util.RawMessage `json:"transport"`
Policy json_util.RawMessage `json:"policy"`
API json_util.RawMessage `json:"api"`
Stats json_util.RawMessage `json:"stats"`
Reverse json_util.RawMessage `json:"reverse"`
FakeDNS json_util.RawMessage `json:"fakeDns"`
}
func (c *Config) MarshalJSON() ([]byte, error) {
return json_util.MarshalJSON(c)
func (c *Config) Equals(other *Config) bool {
if len(c.InboundConfigs) != len(other.InboundConfigs) {
return false
}
for i, inbound := range c.InboundConfigs {
if !inbound.Equals(&other.InboundConfigs[i]) {
return false
}
}
if !bytes.Equal(c.LogConfig, other.LogConfig) {
return false
}
if !bytes.Equal(c.RouterConfig, other.RouterConfig) {
return false
}
if !bytes.Equal(c.DNSConfig, other.DNSConfig) {
return false
}
if !bytes.Equal(c.OutboundConfigs, other.OutboundConfigs) {
return false
}
if !bytes.Equal(c.Transport, other.Transport) {
return false
}
if !bytes.Equal(c.Policy, other.Policy) {
return false
}
if !bytes.Equal(c.API, other.API) {
return false
}
if !bytes.Equal(c.Stats, other.Stats) {
return false
}
if !bytes.Equal(c.Reverse, other.Reverse) {
return false
}
if !bytes.Equal(c.FakeDNS, other.FakeDNS) {
return false
}
return true
}

View File

@@ -1,20 +1,41 @@
package xray
import (
"encoding/json"
"bytes"
"x-ui/util/json_util"
)
type InboundConfig struct {
Listen json.RawMessage `json:"listen"` // listen 不能为空字符串
Port int `json:"port"`
Protocol string `json:"protocol"`
Settings json.RawMessage `json:"settings"`
StreamSettings json.RawMessage `json:"streamSettings"`
Tag string `json:"tag"`
Sniffing json.RawMessage `json:"sniffing"`
Listen json_util.RawMessage `json:"listen"` // listen 不能为空字符串
Port int `json:"port"`
Protocol string `json:"protocol"`
Settings json_util.RawMessage `json:"settings"`
StreamSettings json_util.RawMessage `json:"streamSettings"`
Tag string `json:"tag"`
Sniffing json_util.RawMessage `json:"sniffing"`
}
func (i *InboundConfig) MarshalJSON() ([]byte, error) {
return json_util.MarshalJSON(i)
func (c *InboundConfig) Equals(other *InboundConfig) bool {
if !bytes.Equal(c.Listen, other.Listen) {
return false
}
if c.Port != other.Port {
return false
}
if c.Protocol != other.Protocol {
return false
}
if !bytes.Equal(c.Settings, other.Settings) {
return false
}
if !bytes.Equal(c.StreamSettings, other.StreamSettings) {
return false
}
if c.Tag != other.Tag {
return false
}
if !bytes.Equal(c.Sniffing, other.Sniffing) {
return false
}
return true
}

View File

@@ -62,16 +62,16 @@ type process struct {
version string
apiPort int
xrayConfig *Config
lines *queue.Queue
exitErr error
config *Config
lines *queue.Queue
exitErr error
}
func newProcess(xrayConfig *Config) *process {
func newProcess(config *Config) *process {
return &process{
version: "Unknown",
xrayConfig: xrayConfig,
lines: queue.New(100),
version: "Unknown",
config: config,
lines: queue.New(100),
}
}
@@ -90,6 +90,9 @@ func (p *process) GetErr() error {
}
func (p *process) GetResult() string {
if p.lines.Empty() && p.exitErr != nil {
return p.exitErr.Error()
}
items, _ := p.lines.TakeUntil(func(item interface{}) bool {
return true
})
@@ -108,8 +111,12 @@ func (p *Process) GetAPIPort() int {
return p.apiPort
}
func (p *Process) GetConfig() *Config {
return p.config
}
func (p *process) refreshAPIPort() {
for _, inbound := range p.xrayConfig.InboundConfigs {
for _, inbound := range p.config.InboundConfigs {
if inbound.Tag == "api" {
p.apiPort = inbound.Port
break
@@ -132,19 +139,25 @@ func (p *process) refreshVersion() {
}
}
func (p *process) Start() error {
func (p *process) Start() (err error) {
if p.IsRunning() {
return errors.New("xray is already running")
}
data, err := json.MarshalIndent(p.xrayConfig, "", " ")
defer func() {
if err != nil {
p.exitErr = err
}
}()
data, err := json.MarshalIndent(p.config, "", " ")
if err != nil {
return err
return common.NewErrorf("生成 xray 配置文件失败: %v", err)
}
configPath := GetConfigPath()
err = os.WriteFile(configPath, data, fs.ModePerm)
if err != nil {
return err
return common.NewErrorf("写入配置文件失败: %v", err)
}
cmd := exec.Command(GetBinaryPath(), "-c", configPath)