1
0

xray.go 4.8 KB

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