xray.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. }
  102. final_clients = append(final_clients, interface{}(c))
  103. }
  104. settings["clients"] = final_clients
  105. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  106. if err != nil {
  107. return nil, err
  108. }
  109. inbound.Settings = string(modifiedSettings)
  110. }
  111. inboundConfig := inbound.GenXrayInboundConfig()
  112. xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
  113. }
  114. return xrayConfig, nil
  115. }
  116. func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, error) {
  117. if !s.IsXrayRunning() {
  118. return nil, nil, errors.New("xray is not running")
  119. }
  120. return p.GetTraffic(true)
  121. }
  122. func (s *XrayService) RestartXray(isForce bool) error {
  123. lock.Lock()
  124. defer lock.Unlock()
  125. logger.Debug("restart xray, force:", isForce)
  126. xrayConfig, err := s.GetXrayConfig()
  127. if err != nil {
  128. return err
  129. }
  130. if p != nil && p.IsRunning() {
  131. if !isForce && p.GetConfig().Equals(xrayConfig) {
  132. logger.Debug("not need to restart xray")
  133. return nil
  134. }
  135. p.Stop()
  136. }
  137. p = xray.NewProcess(xrayConfig)
  138. result = ""
  139. return p.Start()
  140. }
  141. func (s *XrayService) StopXray() error {
  142. lock.Lock()
  143. defer lock.Unlock()
  144. logger.Debug("stop xray")
  145. if s.IsXrayRunning() {
  146. return p.Stop()
  147. }
  148. return errors.New("xray is not running")
  149. }
  150. func (s *XrayService) SetToNeedRestart() {
  151. isNeedXrayRestart.Store(true)
  152. }
  153. func (s *XrayService) IsNeedRestartAndSetFalse() bool {
  154. return isNeedXrayRestart.CompareAndSwap(true, false)
  155. }