2023-02-09 19:18:06 +00:00
|
|
|
package xray
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2023-05-22 23:13:15 +00:00
|
|
|
"sync"
|
2023-06-15 21:40:49 +00:00
|
|
|
"syscall"
|
2023-08-08 21:07:05 +00:00
|
|
|
"time"
|
2023-08-08 18:51:02 +00:00
|
|
|
|
2023-04-13 19:40:01 +00:00
|
|
|
"x-ui/config"
|
2023-07-01 12:26:43 +00:00
|
|
|
"x-ui/logger"
|
2023-02-09 19:18:06 +00:00
|
|
|
"x-ui/util/common"
|
|
|
|
|
|
|
|
"github.com/Workiva/go-datastructures/queue"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetBinaryName() string {
|
|
|
|
return fmt.Sprintf("xray-%s-%s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBinaryPath() string {
|
2023-04-13 19:40:01 +00:00
|
|
|
return config.GetBinFolderPath() + "/" + GetBinaryName()
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfigPath() string {
|
2023-04-13 19:40:01 +00:00
|
|
|
return config.GetBinFolderPath() + "/config.json"
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGeositePath() string {
|
2023-04-13 19:40:01 +00:00
|
|
|
return config.GetBinFolderPath() + "/geosite.dat"
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGeoipPath() string {
|
2023-04-13 19:40:01 +00:00
|
|
|
return config.GetBinFolderPath() + "/geoip.dat"
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:26:43 +00:00
|
|
|
func GetIPLimitLogPath() string {
|
|
|
|
return config.GetLogFolder() + "/3xipl.log"
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetIPLimitBannedLogPath() string {
|
|
|
|
return config.GetLogFolder() + "/3xipl-banned.log"
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAccessPersistentLogPath() string {
|
|
|
|
return config.GetLogFolder() + "/3xipl-access-persistent.log"
|
|
|
|
}
|
|
|
|
|
|
|
|
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 19:18:06 +00: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-08-08 21:07:05 +00:00
|
|
|
config *Config
|
|
|
|
lines *queue.Queue
|
|
|
|
exitErr error
|
|
|
|
startTime time.Time
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newProcess(config *Config) *process {
|
|
|
|
return &process{
|
2023-08-08 21:07:05 +00:00
|
|
|
version: "Unknown",
|
|
|
|
config: config,
|
|
|
|
lines: queue.New(100),
|
|
|
|
startTime: time.Now(),
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) IsRunning() bool {
|
2023-11-20 01:15:51 +00:00
|
|
|
return p.cmd != nil && p.cmd.Process != nil && p.cmd.ProcessState == nil
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) GetErr() error {
|
|
|
|
return p.exitErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) GetResult() string {
|
2023-11-20 01:15:51 +00:00
|
|
|
var lines []string
|
|
|
|
for !p.lines.Empty() {
|
|
|
|
if item, err := p.lines.Get(1); err == nil {
|
|
|
|
lines = append(lines, item[0].(string))
|
|
|
|
}
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
2023-11-20 01:15:51 +00:00
|
|
|
if len(lines) == 0 && p.exitErr != nil {
|
|
|
|
return p.exitErr.Error()
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-08 21:07:05 +00:00
|
|
|
func (p *Process) GetUptime() uint64 {
|
|
|
|
return uint64(time.Since(p.startTime).Seconds())
|
|
|
|
}
|
|
|
|
|
2023-02-09 19:18:06 +00: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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 01:15:51 +00:00
|
|
|
func (p *process) Start() error {
|
2023-02-09 19:18:06 +00:00
|
|
|
if p.IsRunning() {
|
|
|
|
return errors.New("xray is already running")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.MarshalIndent(p.config, "", " ")
|
|
|
|
if err != nil {
|
2023-02-11 20:55:21 +00:00
|
|
|
return common.NewErrorf("Failed to generate xray configuration file: %v", err)
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
configPath := GetConfigPath()
|
2023-11-20 01:15:51 +00:00
|
|
|
if err = os.WriteFile(configPath, data, fs.ModePerm); err != nil {
|
2023-02-11 20:55:21 +00:00
|
|
|
return common.NewErrorf("Failed to write configuration file: %v", err)
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 01:15:51 +00:00
|
|
|
p.cmd = exec.Command(GetBinaryPath(), "-c", configPath)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
2023-11-20 01:15:51 +00:00
|
|
|
stdReader, err := p.cmd.StdoutPipe()
|
2023-02-09 19:18:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-20 01:15:51 +00:00
|
|
|
errReader, err := p.cmd.StderrPipe()
|
2023-02-09 19:18:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-22 23:13:15 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
2023-11-20 01:15:51 +00:00
|
|
|
startReader := func(reader io.Reader) {
|
2023-05-22 23:13:15 +00:00
|
|
|
defer wg.Done()
|
2023-11-20 01:15:51 +00:00
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
|
|
|
p.lines.Put(scanner.Text())
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
2023-11-20 01:15:51 +00:00
|
|
|
}
|
2023-02-09 19:18:06 +00:00
|
|
|
|
2023-11-20 01:15:51 +00:00
|
|
|
go startReader(stdReader)
|
|
|
|
go startReader(errReader)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
|
|
go func() {
|
2023-11-20 01:15:51 +00:00
|
|
|
defer wg.Wait()
|
|
|
|
p.exitErr = p.cmd.Run()
|
2023-02-09 19:18:06 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
p.refreshVersion()
|
|
|
|
p.refreshAPIPort()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *process) Stop() error {
|
|
|
|
if !p.IsRunning() {
|
|
|
|
return errors.New("xray is not running")
|
|
|
|
}
|
2023-06-15 21:40:49 +00:00
|
|
|
return p.cmd.Process.Signal(syscall.SIGTERM)
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|