xray.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. for index, client := range clients {
  77. c := client.(map[string]interface{})
  78. if c["email"] == clientTraffic.Email {
  79. if ! clientTraffic.Enable {
  80. clients = RemoveIndex(clients,index)
  81. logger.Info("Remove Inbound User",c["email"] ,"due the expire or traffic limit")
  82. }
  83. }
  84. }
  85. }
  86. settings["clients"] = clients
  87. modifiedSettings, err := json.Marshal(settings)
  88. if err != nil {
  89. return nil, err
  90. }
  91. inbound.Settings = string(modifiedSettings)
  92. }
  93. inboundConfig := inbound.GenXrayInboundConfig()
  94. xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
  95. }
  96. return xrayConfig, nil
  97. }
  98. func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, error) {
  99. if !s.IsXrayRunning() {
  100. return nil, nil, errors.New("xray is not running")
  101. }
  102. return p.GetTraffic(true)
  103. }
  104. func (s *XrayService) RestartXray(isForce bool) error {
  105. lock.Lock()
  106. defer lock.Unlock()
  107. logger.Debug("restart xray, force:", isForce)
  108. xrayConfig, err := s.GetXrayConfig()
  109. if err != nil {
  110. return err
  111. }
  112. if p != nil && p.IsRunning() {
  113. if !isForce && p.GetConfig().Equals(xrayConfig) {
  114. logger.Debug("not need to restart xray")
  115. return nil
  116. }
  117. p.Stop()
  118. }
  119. p = xray.NewProcess(xrayConfig)
  120. result = ""
  121. return p.Start()
  122. }
  123. func (s *XrayService) StopXray() error {
  124. lock.Lock()
  125. defer lock.Unlock()
  126. logger.Debug("stop xray")
  127. if s.IsXrayRunning() {
  128. return p.Stop()
  129. }
  130. return errors.New("xray is not running")
  131. }
  132. func (s *XrayService) SetToNeedRestart() {
  133. isNeedXrayRestart.Store(true)
  134. }
  135. func (s *XrayService) IsNeedRestartAndSetFalse() bool {
  136. return isNeedXrayRestart.CAS(true, false)
  137. }