process.go 4.3 KB

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