process.go 4.6 KB

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