xray.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.DisableInvalidClients()
  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. inboundConfig := inbound.GenXrayInboundConfig()
  116. xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
  117. }
  118. return xrayConfig, nil
  119. }
  120. func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, error) {
  121. if !s.IsXrayRunning() {
  122. return nil, nil, errors.New("xray is not running")
  123. }
  124. s.xrayAPI.Init(p.GetAPIPort())
  125. defer s.xrayAPI.Close()
  126. return s.xrayAPI.GetTraffic(true)
  127. }
  128. func (s *XrayService) RestartXray(isForce bool) error {
  129. lock.Lock()
  130. defer lock.Unlock()
  131. logger.Debug("restart xray, force:", isForce)
  132. xrayConfig, err := s.GetXrayConfig()
  133. if err != nil {
  134. return err
  135. }
  136. if p != nil && p.IsRunning() {
  137. if !isForce && p.GetConfig().Equals(xrayConfig) {
  138. logger.Debug("It does not need to restart xray")
  139. return nil
  140. }
  141. p.Stop()
  142. }
  143. p = xray.NewProcess(xrayConfig)
  144. result = ""
  145. err = p.Start()
  146. if err != nil {
  147. return err
  148. }
  149. return nil
  150. }
  151. func (s *XrayService) StopXray() error {
  152. lock.Lock()
  153. defer lock.Unlock()
  154. logger.Debug("stop xray")
  155. if s.IsXrayRunning() {
  156. return p.Stop()
  157. }
  158. return errors.New("xray is not running")
  159. }
  160. func (s *XrayService) SetToNeedRestart() {
  161. isNeedXrayRestart.Store(true)
  162. }
  163. func (s *XrayService) IsNeedRestartAndSetFalse() bool {
  164. return isNeedXrayRestart.CompareAndSwap(true, false)
  165. }