xray.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "runtime"
  6. "sync"
  7. "x-ui/logger"
  8. "x-ui/xray"
  9. "go.uber.org/atomic"
  10. )
  11. var (
  12. p *xray.Process
  13. lock sync.Mutex
  14. isNeedXrayRestart atomic.Bool // Indicates that restart was requested for Xray
  15. isManuallyStopped atomic.Bool // Indicates that Xray was stopped manually from the panel
  16. result string
  17. )
  18. type XrayService struct {
  19. inboundService InboundService
  20. settingService SettingService
  21. xrayAPI xray.XrayAPI
  22. }
  23. func (s *XrayService) IsXrayRunning() bool {
  24. return p != nil && p.IsRunning()
  25. }
  26. func (s *XrayService) GetXrayErr() error {
  27. if p == nil {
  28. return nil
  29. }
  30. err := p.GetErr()
  31. if runtime.GOOS == "windows" && err.Error() == "exit status 1" {
  32. // exit status 1 on Windows means that Xray process was killed
  33. // as we kill process to stop in on Windows, this is not an error
  34. return nil
  35. }
  36. return err
  37. }
  38. func (s *XrayService) GetXrayResult() string {
  39. if result != "" {
  40. return result
  41. }
  42. if s.IsXrayRunning() {
  43. return ""
  44. }
  45. if p == nil {
  46. return ""
  47. }
  48. result = p.GetResult()
  49. if runtime.GOOS == "windows" && result == "exit status 1" {
  50. // exit status 1 on Windows means that Xray process was killed
  51. // as we kill process to stop in on Windows, this is not an error
  52. return ""
  53. }
  54. return result
  55. }
  56. func (s *XrayService) GetXrayVersion() string {
  57. if p == nil {
  58. return "Unknown"
  59. }
  60. return p.GetVersion()
  61. }
  62. func RemoveIndex(s []any, index int) []any {
  63. return append(s[:index], s[index+1:]...)
  64. }
  65. func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
  66. templateConfig, err := s.settingService.GetXrayConfigTemplate()
  67. if err != nil {
  68. return nil, err
  69. }
  70. xrayConfig := &xray.Config{}
  71. err = json.Unmarshal([]byte(templateConfig), xrayConfig)
  72. if err != nil {
  73. return nil, err
  74. }
  75. s.inboundService.AddTraffic(nil, nil)
  76. inbounds, err := s.inboundService.GetAllInbounds()
  77. if err != nil {
  78. return nil, err
  79. }
  80. for _, inbound := range inbounds {
  81. if !inbound.Enable {
  82. continue
  83. }
  84. // get settings clients
  85. settings := map[string]any{}
  86. json.Unmarshal([]byte(inbound.Settings), &settings)
  87. clients, ok := settings["clients"].([]any)
  88. if ok {
  89. // check users active or not
  90. clientStats := inbound.ClientStats
  91. for _, clientTraffic := range clientStats {
  92. indexDecrease := 0
  93. for index, client := range clients {
  94. c := client.(map[string]any)
  95. if c["email"] == clientTraffic.Email {
  96. if !clientTraffic.Enable {
  97. clients = RemoveIndex(clients, index-indexDecrease)
  98. indexDecrease++
  99. logger.Infof("Remove Inbound User %s due to expiration or traffic limit", c["email"])
  100. }
  101. }
  102. }
  103. }
  104. // clear client config for additional parameters
  105. var final_clients []any
  106. for _, client := range clients {
  107. c := client.(map[string]any)
  108. if c["enable"] != nil {
  109. if enable, ok := c["enable"].(bool); ok && !enable {
  110. continue
  111. }
  112. }
  113. for key := range c {
  114. if key != "email" && key != "id" && key != "password" && key != "flow" && key != "method" {
  115. delete(c, key)
  116. }
  117. if c["flow"] == "xtls-rprx-vision-udp443" {
  118. c["flow"] = "xtls-rprx-vision"
  119. }
  120. }
  121. final_clients = append(final_clients, any(c))
  122. }
  123. settings["clients"] = final_clients
  124. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  125. if err != nil {
  126. return nil, err
  127. }
  128. inbound.Settings = string(modifiedSettings)
  129. }
  130. if len(inbound.StreamSettings) > 0 {
  131. // Unmarshal stream JSON
  132. var stream map[string]any
  133. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  134. // Remove the "settings" field under "tlsSettings" and "realitySettings"
  135. tlsSettings, ok1 := stream["tlsSettings"].(map[string]any)
  136. realitySettings, ok2 := stream["realitySettings"].(map[string]any)
  137. if ok1 || ok2 {
  138. if ok1 {
  139. delete(tlsSettings, "settings")
  140. } else if ok2 {
  141. delete(realitySettings, "settings")
  142. }
  143. }
  144. delete(stream, "externalProxy")
  145. newStream, err := json.MarshalIndent(stream, "", " ")
  146. if err != nil {
  147. return nil, err
  148. }
  149. inbound.StreamSettings = string(newStream)
  150. }
  151. inboundConfig := inbound.GenXrayInboundConfig()
  152. xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
  153. }
  154. return xrayConfig, nil
  155. }
  156. func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, error) {
  157. if !s.IsXrayRunning() {
  158. err := errors.New("xray is not running")
  159. logger.Debug("Attempted to fetch Xray traffic, but Xray is not running:", err)
  160. return nil, nil, err
  161. }
  162. apiPort := p.GetAPIPort()
  163. s.xrayAPI.Init(apiPort)
  164. defer s.xrayAPI.Close()
  165. traffic, clientTraffic, err := s.xrayAPI.GetTraffic(true)
  166. if err != nil {
  167. logger.Debug("Failed to fetch Xray traffic:", err)
  168. return nil, nil, err
  169. }
  170. return traffic, clientTraffic, nil
  171. }
  172. func (s *XrayService) RestartXray(isForce bool) error {
  173. lock.Lock()
  174. defer lock.Unlock()
  175. logger.Debug("restart Xray, force:", isForce)
  176. isManuallyStopped.Store(false)
  177. xrayConfig, err := s.GetXrayConfig()
  178. if err != nil {
  179. return err
  180. }
  181. if s.IsXrayRunning() {
  182. if !isForce && p.GetConfig().Equals(xrayConfig) && !isNeedXrayRestart.Load() {
  183. logger.Debug("It does not need to restart Xray")
  184. return nil
  185. }
  186. p.Stop()
  187. }
  188. p = xray.NewProcess(xrayConfig)
  189. result = ""
  190. err = p.Start()
  191. if err != nil {
  192. return err
  193. }
  194. return nil
  195. }
  196. func (s *XrayService) StopXray() error {
  197. lock.Lock()
  198. defer lock.Unlock()
  199. isManuallyStopped.Store(true)
  200. logger.Debug("Attempting to stop Xray...")
  201. if s.IsXrayRunning() {
  202. return p.Stop()
  203. }
  204. return errors.New("xray is not running")
  205. }
  206. func (s *XrayService) SetToNeedRestart() {
  207. isNeedXrayRestart.Store(true)
  208. }
  209. func (s *XrayService) IsNeedRestartAndSetFalse() bool {
  210. return isNeedXrayRestart.CompareAndSwap(true, false)
  211. }
  212. // Check if Xray is not running and wasn't stopped manually, i.e. crashed
  213. func (s *XrayService) DidXrayCrash() bool {
  214. return !s.IsXrayRunning() && !isManuallyStopped.Load()
  215. }