1
0

process.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package xray
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/fs"
  8. "os"
  9. "os/exec"
  10. "runtime"
  11. "syscall"
  12. "time"
  13. "x-ui/config"
  14. "x-ui/logger"
  15. "x-ui/util/common"
  16. )
  17. func GetBinaryName() string {
  18. return fmt.Sprintf("xray-%s-%s", runtime.GOOS, runtime.GOARCH)
  19. }
  20. func GetBinaryPath() string {
  21. return config.GetBinFolderPath() + "/" + GetBinaryName()
  22. }
  23. func GetConfigPath() string {
  24. return config.GetBinFolderPath() + "/config.json"
  25. }
  26. func GetGeositePath() string {
  27. return config.GetBinFolderPath() + "/geosite.dat"
  28. }
  29. func GetGeoipPath() string {
  30. return config.GetBinFolderPath() + "/geoip.dat"
  31. }
  32. func GetIPLimitLogPath() string {
  33. return config.GetLogFolder() + "/3xipl.log"
  34. }
  35. func GetIPLimitBannedLogPath() string {
  36. return config.GetLogFolder() + "/3xipl-banned.log"
  37. }
  38. func GetAccessPersistentLogPath() string {
  39. return config.GetLogFolder() + "/3xipl-access-persistent.log"
  40. }
  41. func GetAccessLogPath() string {
  42. config, err := os.ReadFile(GetConfigPath())
  43. if err != nil {
  44. logger.Warningf("Something went wrong: %s", err)
  45. }
  46. jsonConfig := map[string]interface{}{}
  47. err = json.Unmarshal([]byte(config), &jsonConfig)
  48. if err != nil {
  49. logger.Warningf("Something went wrong: %s", err)
  50. }
  51. if jsonConfig["log"] != nil {
  52. jsonLog := jsonConfig["log"].(map[string]interface{})
  53. if jsonLog["access"] != nil {
  54. accessLogPath := jsonLog["access"].(string)
  55. return accessLogPath
  56. }
  57. }
  58. return ""
  59. }
  60. func stopProcess(p *Process) {
  61. p.Stop()
  62. }
  63. type Process struct {
  64. *process
  65. }
  66. func NewProcess(xrayConfig *Config) *Process {
  67. p := &Process{newProcess(xrayConfig)}
  68. runtime.SetFinalizer(p, stopProcess)
  69. return p
  70. }
  71. type process struct {
  72. cmd *exec.Cmd
  73. version string
  74. apiPort int
  75. onlineClients []string
  76. config *Config
  77. logWriter *LogWriter
  78. exitErr error
  79. startTime time.Time
  80. }
  81. func newProcess(config *Config) *process {
  82. return &process{
  83. version: "Unknown",
  84. config: config,
  85. logWriter: NewLogWriter(),
  86. startTime: time.Now(),
  87. }
  88. }
  89. func (p *process) IsRunning() bool {
  90. if p.cmd == nil || p.cmd.Process == nil {
  91. return false
  92. }
  93. if p.cmd.ProcessState == nil {
  94. return true
  95. }
  96. return false
  97. }
  98. func (p *process) GetErr() error {
  99. return p.exitErr
  100. }
  101. func (p *process) GetResult() string {
  102. if len(p.logWriter.lastLine) == 0 && p.exitErr != nil {
  103. return p.exitErr.Error()
  104. }
  105. return p.logWriter.lastLine
  106. }
  107. func (p *process) GetVersion() string {
  108. return p.version
  109. }
  110. func (p *Process) GetAPIPort() int {
  111. return p.apiPort
  112. }
  113. func (p *Process) GetConfig() *Config {
  114. return p.config
  115. }
  116. func (p *Process) GetOnlineClients() []string {
  117. return p.onlineClients
  118. }
  119. func (p *Process) SetOnlineClients(users []string) {
  120. p.onlineClients = users
  121. }
  122. func (p *Process) GetUptime() uint64 {
  123. return uint64(time.Since(p.startTime).Seconds())
  124. }
  125. func (p *process) refreshAPIPort() {
  126. for _, inbound := range p.config.InboundConfigs {
  127. if inbound.Tag == "api" {
  128. p.apiPort = inbound.Port
  129. break
  130. }
  131. }
  132. }
  133. func (p *process) refreshVersion() {
  134. cmd := exec.Command(GetBinaryPath(), "-version")
  135. data, err := cmd.Output()
  136. if err != nil {
  137. p.version = "Unknown"
  138. } else {
  139. datas := bytes.Split(data, []byte(" "))
  140. if len(datas) <= 1 {
  141. p.version = "Unknown"
  142. } else {
  143. p.version = string(datas[1])
  144. }
  145. }
  146. }
  147. func (p *process) Start() (err error) {
  148. if p.IsRunning() {
  149. return errors.New("xray is already running")
  150. }
  151. defer func() {
  152. if err != nil {
  153. p.exitErr = err
  154. }
  155. }()
  156. data, err := json.MarshalIndent(p.config, "", " ")
  157. if err != nil {
  158. return common.NewErrorf("Failed to generate xray configuration file: %v", err)
  159. }
  160. configPath := GetConfigPath()
  161. err = os.WriteFile(configPath, data, fs.ModePerm)
  162. if err != nil {
  163. return common.NewErrorf("Failed to write configuration file: %v", err)
  164. }
  165. cmd := exec.Command(GetBinaryPath(), "-c", configPath)
  166. p.cmd = cmd
  167. cmd.Stdout = p.logWriter
  168. cmd.Stderr = p.logWriter
  169. go func() {
  170. err := cmd.Run()
  171. if err != nil {
  172. p.exitErr = err
  173. }
  174. }()
  175. p.refreshVersion()
  176. p.refreshAPIPort()
  177. return nil
  178. }
  179. func (p *process) Stop() error {
  180. if !p.IsRunning() {
  181. return errors.New("xray is not running")
  182. }
  183. return p.cmd.Process.Signal(syscall.SIGTERM)
  184. }