2023-02-09 22:18:06 +03:00
|
|
|
package xray
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2023-06-16 00:40:49 +03:00
|
|
|
"syscall"
|
2023-08-09 00:07:05 +03:00
|
|
|
"time"
|
2023-08-08 21:51:02 +03:00
|
|
|
|
2023-04-13 22:40:01 +03:00
|
|
|
"x-ui/config"
|
2023-07-01 15:26:43 +03:00
|
|
|
"x-ui/logger"
|
2023-02-09 22:18:06 +03:00
|
|
|
"x-ui/util/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetBinaryName() string {
|
|
|
|
return fmt.Sprintf("xray-%s-%s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBinaryPath() string {
|
2023-04-13 22:40:01 +03:00
|
|
|
return config.GetBinFolderPath() + "/" + GetBinaryName()
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfigPath() string {
|
2023-04-13 22:40:01 +03:00
|
|
|
return config.GetBinFolderPath() + "/config.json"
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGeositePath() string {
|
2023-04-13 22:40:01 +03:00
|
|
|
return config.GetBinFolderPath() + "/geosite.dat"
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGeoipPath() string {
|
2023-04-13 22:40:01 +03:00
|
|
|
return config.GetBinFolderPath() + "/geoip.dat"
|
|
|
|
}
|
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
func GetIPLimitLogPath() string {
|
|
|
|
return config.GetLogFolder() + "/3xipl.log"
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetIPLimitBannedLogPath() string {
|
|
|
|
return config.GetLogFolder() + "/3xipl-banned.log"
|
|
|
|
}
|
|
|
|
|
2024-01-01 18:07:56 +03:00
|
|
|
func GetIPLimitBannedPrevLogPath() string {
|
|
|
|
return config.GetLogFolder() + "/3xipl-banned.prev.log"
|
|
|
|
}
|
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
func GetAccessPersistentLogPath() string {
|
2024-01-01 18:07:56 +03:00
|
|
|
return config.GetLogFolder() + "/3xipl-ap.log"
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAccessPersistentPrevLogPath() string {
|
|
|
|
return config.GetLogFolder() + "/3xipl-ap.prev.log"
|
2023-07-01 15:26:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetAccessLogPath() string {
|
|
|
|
config, err := os.ReadFile(GetConfigPath())
|
|
|
|
if err != nil {
|
|
|
|
logger.Warningf("Something went wrong: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonConfig := map[string]interface{}{}
|
|
|
|
err = json.Unmarshal([]byte(config), &jsonConfig)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warningf("Something went wrong: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsonConfig["log"] != nil {
|
|
|
|
jsonLog := jsonConfig["log"].(map[string]interface{})
|
|
|
|
if jsonLog["access"] != nil {
|
|
|
|
|
|
|
|
accessLogPath := jsonLog["access"].(string)
|
|
|
|
|
|
|
|
return accessLogPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:18:06 +03:00
|
|
|
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
|
|
|
|
|
2023-12-04 21:13:21 +03:00
|
|
|
onlineClients []string
|
|
|
|
|
2023-08-09 00:07:05 +03:00
|
|
|
config *Config
|
2023-12-10 15:07:50 +03:00
|
|
|
logWriter *LogWriter
|
2023-08-09 00:07:05 +03:00
|
|
|
exitErr error
|
|
|
|
startTime time.Time
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newProcess(config *Config) *process {
|
|
|
|
return &process{
|
2023-08-09 00:07:05 +03:00
|
|
|
version: "Unknown",
|
|
|
|
config: config,
|
2023-12-10 15:07:50 +03:00
|
|
|
logWriter: NewLogWriter(),
|
2023-08-09 00:07:05 +03:00
|
|
|
startTime: time.Now(),
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-12-10 15:07:50 +03:00
|
|
|
if len(p.logWriter.lastLine) == 0 && p.exitErr != nil {
|
2023-02-09 22:18:06 +03:00
|
|
|
return p.exitErr.Error()
|
|
|
|
}
|
2023-12-10 15:07:50 +03:00
|
|
|
return p.logWriter.lastLine
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) GetVersion() string {
|
|
|
|
return p.version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) GetAPIPort() int {
|
|
|
|
return p.apiPort
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) GetConfig() *Config {
|
|
|
|
return p.config
|
|
|
|
}
|
|
|
|
|
2023-12-04 21:13:21 +03:00
|
|
|
func (p *Process) GetOnlineClients() []string {
|
|
|
|
return p.onlineClients
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) SetOnlineClients(users []string) {
|
|
|
|
p.onlineClients = users
|
|
|
|
}
|
|
|
|
|
2023-08-09 00:07:05 +03:00
|
|
|
func (p *Process) GetUptime() uint64 {
|
|
|
|
return uint64(time.Since(p.startTime).Seconds())
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:18:06 +03:00
|
|
|
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 {
|
2023-02-11 23:55:21 +03:00
|
|
|
return common.NewErrorf("Failed to generate xray configuration file: %v", err)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
configPath := GetConfigPath()
|
|
|
|
err = os.WriteFile(configPath, data, fs.ModePerm)
|
|
|
|
if err != nil {
|
2023-02-11 23:55:21 +03:00
|
|
|
return common.NewErrorf("Failed to write configuration file: %v", err)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
2023-07-27 10:36:27 +03:00
|
|
|
cmd := exec.Command(GetBinaryPath(), "-c", configPath)
|
2023-02-09 22:18:06 +03:00
|
|
|
p.cmd = cmd
|
|
|
|
|
2023-12-10 15:07:50 +03:00
|
|
|
cmd.Stdout = p.logWriter
|
|
|
|
cmd.Stderr = p.logWriter
|
2023-02-09 22:18:06 +03:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2023-06-16 00:40:49 +03:00
|
|
|
return p.cmd.Process.Signal(syscall.SIGTERM)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|