xray.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. }
  18. func (s *XrayService) IsXrayRunning() bool {
  19. return p != nil && p.IsRunning()
  20. }
  21. func (s *XrayService) GetXrayErr() error {
  22. if p == nil {
  23. return nil
  24. }
  25. return p.GetErr()
  26. }
  27. func (s *XrayService) GetXrayResult() string {
  28. if result != "" {
  29. return result
  30. }
  31. if s.IsXrayRunning() {
  32. return ""
  33. }
  34. if p == nil {
  35. return ""
  36. }
  37. result = p.GetResult()
  38. return result
  39. }
  40. func (s *XrayService) GetXrayVersion() string {
  41. if p == nil {
  42. return "Unknown"
  43. }
  44. return p.GetVersion()
  45. }
  46. func RemoveIndex(s []interface{}, index int) []interface{} {
  47. return append(s[:index], s[index+1:]...)
  48. }
  49. func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
  50. templateConfig, err := s.settingService.GetXrayConfigTemplate()
  51. if err != nil {
  52. return nil, err
  53. }
  54. xrayConfig := &xray.Config{}
  55. err = json.Unmarshal([]byte(templateConfig), xrayConfig)
  56. if err != nil {
  57. return nil, err
  58. }
  59. s.inboundService.DisableInvalidClients()
  60. inbounds, err := s.inboundService.GetAllInbounds()
  61. if err != nil {
  62. return nil, err
  63. }
  64. for _, inbound := range inbounds {
  65. if !inbound.Enable {
  66. continue
  67. }
  68. // get settings clients
  69. settings := map[string]interface{}{}
  70. json.Unmarshal([]byte(inbound.Settings), &settings)
  71. clients, ok := settings["clients"].([]interface{})
  72. if ok {
  73. // check users active or not
  74. clientStats := inbound.ClientStats
  75. for _, clientTraffic := range clientStats {
  76. indexDecrease := 0
  77. for index, client := range clients {
  78. c := client.(map[string]interface{})
  79. if c["email"] == clientTraffic.Email {
  80. if !clientTraffic.Enable {
  81. clients = RemoveIndex(clients, index-indexDecrease)
  82. indexDecrease++
  83. logger.Info("Remove Inbound User", c["email"], "due the expire or traffic limit")
  84. }
  85. }
  86. }
  87. }
  88. // clear client config for additional parameters
  89. var final_clients []interface{}
  90. for _, client := range clients {
  91. c := client.(map[string]interface{})
  92. if c["enable"] != nil {
  93. if enable, ok := c["enable"].(bool); ok && !enable {
  94. continue
  95. }
  96. }
  97. for key := range c {
  98. if key != "email" && key != "id" && key != "password" && key != "flow" && key != "alterId" {
  99. delete(c, key)
  100. }
  101. if c["flow"] == "xtls-rprx-vision-udp443" {
  102. c["flow"] = "xtls-rprx-vision"
  103. }
  104. }
  105. final_clients = append(final_clients, interface{}(c))
  106. }
  107. settings["clients"] = final_clients
  108. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  109. if err != nil {
  110. return nil, err
  111. }
  112. inbound.Settings = string(modifiedSettings)
  113. }
  114. inboundConfig := inbound.GenXrayInboundConfig()
  115. xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
  116. }
  117. return xrayConfig, nil
  118. }
  119. func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, error) {
  120. if !s.IsXrayRunning() {
  121. return nil, nil, errors.New("xray is not running")
  122. }
  123. return p.GetTraffic(true)
  124. }
  125. func (s *XrayService) RestartXray(isForce bool) error {
  126. lock.Lock()
  127. defer lock.Unlock()
  128. logger.Debug("restart xray, force:", isForce)
  129. xrayConfig, err := s.GetXrayConfig()
  130. if err != nil {
  131. return err
  132. }
  133. if p != nil && p.IsRunning() {
  134. if !isForce && p.GetConfig().Equals(xrayConfig) {
  135. logger.Debug("not need to restart xray")
  136. return nil
  137. }
  138. p.Stop()
  139. }
  140. p = xray.NewProcess(xrayConfig)
  141. result = ""
  142. return p.Start()
  143. }
  144. func (s *XrayService) StopXray() error {
  145. lock.Lock()
  146. defer lock.Unlock()
  147. logger.Debug("stop xray")
  148. if s.IsXrayRunning() {
  149. return p.Stop()
  150. }
  151. return errors.New("xray is not running")
  152. }
  153. func (s *XrayService) SetToNeedRestart() {
  154. isNeedXrayRestart.Store(true)
  155. }
  156. func (s *XrayService) IsNeedRestartAndSetFalse() bool {
  157. return isNeedXrayRestart.CompareAndSwap(true, false)
  158. }