process.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package xray
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io/fs"
  9. "os"
  10. "os/exec"
  11. "runtime"
  12. "strings"
  13. "sync"
  14. "x-ui/config"
  15. "x-ui/util/common"
  16. "github.com/Workiva/go-datastructures/queue"
  17. )
  18. func GetBinaryName() string {
  19. return fmt.Sprintf("xray-%s-%s", runtime.GOOS, runtime.GOARCH)
  20. }
  21. func GetBinaryPath() string {
  22. return config.GetBinFolderPath() + "/" + GetBinaryName()
  23. }
  24. func GetConfigPath() string {
  25. return config.GetBinFolderPath() + "/config.json"
  26. }
  27. func GetGeositePath() string {
  28. return config.GetBinFolderPath() + "/geosite.dat"
  29. }
  30. func GetGeoipPath() string {
  31. return config.GetBinFolderPath() + "/geoip.dat"
  32. }
  33. func GetIranPath() string {
  34. return config.GetBinFolderPath() + "/iran.dat"
  35. }
  36. func GetBlockedIPsPath() string {
  37. return config.GetBinFolderPath() + "/BlockedIps"
  38. }
  39. func stopProcess(p *Process) {
  40. p.Stop()
  41. }
  42. type Process struct {
  43. *process
  44. }
  45. func NewProcess(xrayConfig *Config) *Process {
  46. p := &Process{newProcess(xrayConfig)}
  47. runtime.SetFinalizer(p, stopProcess)
  48. return p
  49. }
  50. type process struct {
  51. cmd *exec.Cmd
  52. version string
  53. apiPort int
  54. config *Config
  55. lines *queue.Queue
  56. exitErr error
  57. }
  58. func newProcess(config *Config) *process {
  59. return &process{
  60. version: "Unknown",
  61. config: config,
  62. lines: queue.New(100),
  63. }
  64. }
  65. func (p *process) IsRunning() bool {
  66. if p.cmd == nil || p.cmd.Process == nil {
  67. return false
  68. }
  69. if p.cmd.ProcessState == nil {
  70. return true
  71. }
  72. return false
  73. }
  74. func (p *process) GetErr() error {
  75. return p.exitErr
  76. }
  77. func (p *process) GetResult() string {
  78. if p.lines.Empty() && p.exitErr != nil {
  79. return p.exitErr.Error()
  80. }
  81. items, _ := p.lines.TakeUntil(func(item interface{}) bool {
  82. return true
  83. })
  84. lines := make([]string, 0, len(items))
  85. for _, item := range items {
  86. lines = append(lines, item.(string))
  87. }
  88. return strings.Join(lines, "\n")
  89. }
  90. func (p *process) GetVersion() string {
  91. return p.version
  92. }
  93. func (p *Process) GetAPIPort() int {
  94. return p.apiPort
  95. }
  96. func (p *Process) GetConfig() *Config {
  97. return p.config
  98. }
  99. func (p *process) refreshAPIPort() {
  100. for _, inbound := range p.config.InboundConfigs {
  101. if inbound.Tag == "api" {
  102. p.apiPort = inbound.Port
  103. break
  104. }
  105. }
  106. }
  107. func (p *process) refreshVersion() {
  108. cmd := exec.Command(GetBinaryPath(), "-version")
  109. data, err := cmd.Output()
  110. if err != nil {
  111. p.version = "Unknown"
  112. } else {
  113. datas := bytes.Split(data, []byte(" "))
  114. if len(datas) <= 1 {
  115. p.version = "Unknown"
  116. } else {
  117. p.version = string(datas[1])
  118. }
  119. }
  120. }
  121. func (p *process) Start() (err error) {
  122. if p.IsRunning() {
  123. return errors.New("xray is already running")
  124. }
  125. defer func() {
  126. if err != nil {
  127. p.exitErr = err
  128. }
  129. }()
  130. data, err := json.MarshalIndent(p.config, "", " ")
  131. if err != nil {
  132. return common.NewErrorf("Failed to generate xray configuration file: %v", err)
  133. }
  134. configPath := GetConfigPath()
  135. err = os.WriteFile(configPath, data, fs.ModePerm)
  136. if err != nil {
  137. return common.NewErrorf("Failed to write configuration file: %v", err)
  138. }
  139. cmd := exec.Command(GetBinaryPath(), "-c", configPath, "-restrictedIPsPath", GetBlockedIPsPath())
  140. p.cmd = cmd
  141. stdReader, err := cmd.StdoutPipe()
  142. if err != nil {
  143. return err
  144. }
  145. errReader, err := cmd.StderrPipe()
  146. if err != nil {
  147. return err
  148. }
  149. var wg sync.WaitGroup
  150. wg.Add(2)
  151. go func() {
  152. defer wg.Done()
  153. reader := bufio.NewReaderSize(stdReader, 8192)
  154. for {
  155. line, _, err := reader.ReadLine()
  156. if err != nil {
  157. return
  158. }
  159. if p.lines.Len() >= 100 {
  160. p.lines.Get(1)
  161. }
  162. p.lines.Put(string(line))
  163. }
  164. }()
  165. go func() {
  166. defer wg.Done()
  167. reader := bufio.NewReaderSize(errReader, 8192)
  168. for {
  169. line, _, err := reader.ReadLine()
  170. if err != nil {
  171. return
  172. }
  173. if p.lines.Len() >= 100 {
  174. p.lines.Get(1)
  175. }
  176. p.lines.Put(string(line))
  177. }
  178. }()
  179. go func() {
  180. err := cmd.Run()
  181. if err != nil {
  182. p.exitErr = err
  183. }
  184. wg.Wait()
  185. }()
  186. p.refreshVersion()
  187. p.refreshAPIPort()
  188. return nil
  189. }
  190. func (p *process) Stop() error {
  191. if !p.IsRunning() {
  192. return errors.New("xray is not running")
  193. }
  194. return p.cmd.Process.Kill()
  195. }