1
0

process.go 4.3 KB

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