xray.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 (s *XrayService) GetXrayConfig() (*xray.Config, error) {
  47. templateConfig, err := s.settingService.GetXrayConfigTemplate()
  48. if err != nil {
  49. return nil, err
  50. }
  51. xrayConfig := &xray.Config{}
  52. err = json.Unmarshal([]byte(templateConfig), xrayConfig)
  53. if err != nil {
  54. return nil, err
  55. }
  56. inbounds, err := s.inboundService.GetAllInbounds()
  57. if err != nil {
  58. return nil, err
  59. }
  60. for _, inbound := range inbounds {
  61. if !inbound.Enable {
  62. continue
  63. }
  64. inboundConfig := inbound.GenXrayInboundConfig()
  65. xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
  66. }
  67. return xrayConfig, nil
  68. }
  69. func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, error) {
  70. if !s.IsXrayRunning() {
  71. return nil, errors.New("xray is not running")
  72. }
  73. return p.GetTraffic(true)
  74. }
  75. func (s *XrayService) RestartXray(isForce bool) error {
  76. lock.Lock()
  77. defer lock.Unlock()
  78. logger.Debug("restart xray, force:", isForce)
  79. xrayConfig, err := s.GetXrayConfig()
  80. if err != nil {
  81. return err
  82. }
  83. if p != nil && p.IsRunning() {
  84. if !isForce && p.GetConfig().Equals(xrayConfig) {
  85. logger.Debug("not need to restart xray")
  86. return nil
  87. }
  88. p.Stop()
  89. }
  90. p = xray.NewProcess(xrayConfig)
  91. result = ""
  92. return p.Start()
  93. }
  94. func (s *XrayService) StopXray() error {
  95. lock.Lock()
  96. defer lock.Unlock()
  97. logger.Debug("stop xray")
  98. if s.IsXrayRunning() {
  99. return p.Stop()
  100. }
  101. return errors.New("xray is not running")
  102. }
  103. func (s *XrayService) SetToNeedRestart() {
  104. isNeedXrayRestart.Store(true)
  105. }
  106. func (s *XrayService) IsNeedRestartAndSetFalse() bool {
  107. return isNeedXrayRestart.CAS(true, false)
  108. }