| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 | package xrayimport (	"bufio"	"bytes"	"encoding/json"	"errors"	"fmt"	"io/fs"	"os"	"os/exec"	"runtime"	"strings"	"sync"	"syscall"	"time"	"x-ui/config"	"x-ui/logger"	"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 {	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 GetIranPath() string {	return config.GetBinFolderPath() + "/iran.dat"}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 ""}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	config    *Config	lines     *queue.Queue	exitErr   error	startTime time.Time}func newProcess(config *Config) *process {	return &process{		version:   "Unknown",		config:    config,		lines:     queue.New(100),		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 p.lines.Empty() && p.exitErr != nil {		return p.exitErr.Error()	}	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) GetAPIPort() int {	return p.apiPort}func (p *Process) GetConfig() *Config {	return p.config}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("Failed to generate xray configuration file: %v", err)	}	configPath := GetConfigPath()	err = os.WriteFile(configPath, data, fs.ModePerm)	if err != nil {		return common.NewErrorf("Failed to write configuration file: %v", 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	}	var wg sync.WaitGroup	wg.Add(2)	go func() {		defer wg.Done()		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 wg.Done()		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		}		wg.Wait()	}()	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)}
 |