完成xray启动
This commit is contained in:
17
xray/config.go
Normal file
17
xray/config.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package xray
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
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"`
|
||||
}
|
||||
13
xray/inbound.go
Normal file
13
xray/inbound.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package xray
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type InboundConfig struct {
|
||||
Listen string `json:"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"`
|
||||
}
|
||||
194
xray/process.go
Normal file
194
xray/process.go
Normal file
@@ -0,0 +1,194 @@
|
||||
package xray
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Workiva/go-datastructures/queue"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"x-ui/util/common"
|
||||
)
|
||||
|
||||
func GetBinaryName() string {
|
||||
return fmt.Sprintf("xray-%s-%s", runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
|
||||
func GetBinaryPath() string {
|
||||
return "bin/" + GetBinaryName()
|
||||
}
|
||||
|
||||
func GetConfigPath() string {
|
||||
return "bin/config.json"
|
||||
}
|
||||
|
||||
func GetGeositePath() string {
|
||||
return "bin/geosite.dat"
|
||||
}
|
||||
|
||||
func GetGeoipPath() string {
|
||||
return "bin/geoip.dat"
|
||||
}
|
||||
|
||||
func stopProcess(p *Process) {
|
||||
p.Stop()
|
||||
}
|
||||
|
||||
type Process struct {
|
||||
*process
|
||||
}
|
||||
|
||||
func NewProcess(xrayConfig *Config) *Process {
|
||||
p := &Process{newProcess(xrayConfig)}
|
||||
runtime.SetFinalizer(p, stopProcess)
|
||||
return p
|
||||
}
|
||||
|
||||
type process struct {
|
||||
cmd *exec.Cmd
|
||||
|
||||
version string
|
||||
|
||||
xrayConfig *Config
|
||||
lines *queue.Queue
|
||||
exitErr error
|
||||
}
|
||||
|
||||
func newProcess(xrayConfig *Config) *process {
|
||||
return &process{
|
||||
version: "Unknown",
|
||||
xrayConfig: xrayConfig,
|
||||
lines: queue.New(100),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *process) IsRunning() bool {
|
||||
if p.cmd == nil || p.cmd.Process == nil {
|
||||
return false
|
||||
}
|
||||
if p.cmd.ProcessState == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *process) GetErr() error {
|
||||
return p.exitErr
|
||||
}
|
||||
|
||||
func (p *process) GetResult() string {
|
||||
items, _ := p.lines.TakeUntil(func(item interface{}) bool {
|
||||
return true
|
||||
})
|
||||
lines := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
lines = append(lines, item.(string))
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (p *process) GetVersion() string {
|
||||
return p.version
|
||||
}
|
||||
|
||||
func (p *process) refreshVersion() {
|
||||
cmd := exec.Command(GetBinaryPath(), "-version")
|
||||
data, err := cmd.Output()
|
||||
if err != nil {
|
||||
p.version = "Unknown"
|
||||
} else {
|
||||
datas := bytes.Split(data, []byte(" "))
|
||||
if len(datas) <= 1 {
|
||||
p.version = "Unknown"
|
||||
} else {
|
||||
p.version = string(datas[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *process) Start() error {
|
||||
if p.IsRunning() {
|
||||
return errors.New("xray is already running")
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(p.xrayConfig, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
configPath := GetConfigPath()
|
||||
err = os.WriteFile(configPath, data, fs.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := exec.Command(GetBinaryPath(), "-c", configPath)
|
||||
p.cmd = cmd
|
||||
|
||||
stdReader, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
errReader, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
common.Recover("")
|
||||
stdReader.Close()
|
||||
}()
|
||||
reader := bufio.NewReaderSize(stdReader, 8192)
|
||||
for {
|
||||
line, _, err := reader.ReadLine()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if p.lines.Len() >= 100 {
|
||||
p.lines.Get(1)
|
||||
}
|
||||
p.lines.Put(string(line))
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
common.Recover("")
|
||||
errReader.Close()
|
||||
}()
|
||||
reader := bufio.NewReaderSize(errReader, 8192)
|
||||
for {
|
||||
line, _, err := reader.ReadLine()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if p.lines.Len() >= 100 {
|
||||
p.lines.Get(1)
|
||||
}
|
||||
p.lines.Put(string(line))
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
p.exitErr = err
|
||||
}
|
||||
}()
|
||||
|
||||
p.refreshVersion()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *process) Stop() error {
|
||||
if !p.IsRunning() {
|
||||
return errors.New("xray is not running")
|
||||
}
|
||||
return p.cmd.Process.Kill()
|
||||
}
|
||||
Reference in New Issue
Block a user