Files
x-ui/xray/process.go
Hamidreza b6ccb2e076 Some fixes and improvements (#1077)
* [refactor] api controller

* [update] use status code for jsonMsg and 401 to unauthorize

* [update] handle response status code via axios

* [lint] all .go files
2024-03-10 18:00:44 +01:00

191 lines
3.3 KiB
Go

package xray
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"runtime"
"syscall"
"time"
"x-ui/config"
"x-ui/util/common"
)
func GetBinaryName() string {
return fmt.Sprintf("xray-%s-%s", runtime.GOOS, runtime.GOARCH)
}
func GetBinaryPath() string {
return config.GetBinFolderPath() + "/" + GetBinaryName()
}
func GetConfigPath() string {
return config.GetBinFolderPath() + "/config.json"
}
func GetGeositePath() string {
return config.GetBinFolderPath() + "/geosite.dat"
}
func GetGeoipPath() string {
return config.GetBinFolderPath() + "/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
apiPort int
onlineClients []string
config *Config
logWriter *LogWriter
exitErr error
startTime time.Time
}
func newProcess(config *Config) *process {
return &process{
version: "Unknown",
config: config,
logWriter: NewLogWriter(),
startTime: time.Now(),
}
}
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 {
if len(p.logWriter.lastLine) == 0 && p.exitErr != nil {
return p.exitErr.Error()
}
return p.logWriter.lastLine
}
func (p *process) GetVersion() string {
return p.version
}
func (p *Process) GetAPIPort() int {
return p.apiPort
}
func (p *Process) GetConfig() *Config {
return p.config
}
func (p *Process) GetOnlineClients() []string {
return p.onlineClients
}
func (p *Process) SetOnlineClients(users []string) {
p.onlineClients = users
}
func (p *Process) GetUptime() uint64 {
return uint64(time.Since(p.startTime).Seconds())
}
func (p *process) refreshAPIPort() {
for _, inbound := range p.config.InboundConfigs {
if inbound.Tag == "api" {
p.apiPort = inbound.Port
break
}
}
}
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() (err error) {
if p.IsRunning() {
return errors.New("xray is already running")
}
defer func() {
if err != nil {
p.exitErr = err
}
}()
data, err := json.MarshalIndent(p.config, "", " ")
if err != nil {
return common.NewErrorf("Failure to generate XRAY configuration files: %v", err)
}
configPath := GetConfigPath()
err = os.WriteFile(configPath, data, fs.ModePerm)
if err != nil {
return common.NewErrorf("Write the configuration file failed: %v", err)
}
cmd := exec.Command(GetBinaryPath(), "-c", configPath)
p.cmd = cmd
cmd.Stdout = p.logWriter
cmd.Stderr = p.logWriter
go func() {
err := cmd.Run()
if err != nil {
p.exitErr = err
}
}()
p.refreshVersion()
p.refreshAPIPort()
return nil
}
func (p *process) Stop() error {
if !p.IsRunning() {
return errors.New("xray is not running")
}
return p.cmd.Process.Signal(syscall.SIGTERM)
}