| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 | 
							- package service
 
- import (
 
- 	"encoding/json"
 
- 	"errors"
 
- 	"go.uber.org/atomic"
 
- 	"sync"
 
- 	"x-ui/xray"
 
- )
 
- var p *xray.Process
 
- var lock sync.Mutex
 
- var result string
 
- type XrayService struct {
 
- 	inboundService InboundService
 
- 	settingService SettingService
 
- 	isNeedXrayRestart atomic.Bool
 
- }
 
- func (s *XrayService) IsXrayRunning() bool {
 
- 	return p != nil && p.IsRunning()
 
- }
 
- func (s *XrayService) GetXrayErr() error {
 
- 	if p == nil {
 
- 		return nil
 
- 	}
 
- 	return p.GetErr()
 
- }
 
- func (s *XrayService) GetXrayResult() string {
 
- 	if result != "" {
 
- 		return result
 
- 	}
 
- 	if s.IsXrayRunning() {
 
- 		return ""
 
- 	}
 
- 	if p == nil {
 
- 		return ""
 
- 	}
 
- 	result = p.GetResult()
 
- 	return result
 
- }
 
- func (s *XrayService) GetXrayVersion() string {
 
- 	if p == nil {
 
- 		return "Unknown"
 
- 	}
 
- 	return p.GetVersion()
 
- }
 
- func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
 
- 	templateConfig, err := s.settingService.GetXrayConfigTemplate()
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	xrayConfig := &xray.Config{}
 
- 	err = json.Unmarshal([]byte(templateConfig), xrayConfig)
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	inbounds, err := s.inboundService.GetAllInbounds()
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	for _, inbound := range inbounds {
 
- 		if !inbound.Enable {
 
- 			continue
 
- 		}
 
- 		inboundConfig := inbound.GenXrayInboundConfig()
 
- 		xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
 
- 	}
 
- 	return xrayConfig, nil
 
- }
 
- func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, error) {
 
- 	if !s.IsXrayRunning() {
 
- 		return nil, errors.New("xray is not running")
 
- 	}
 
- 	return p.GetTraffic(true)
 
- }
 
- func (s *XrayService) RestartXray() error {
 
- 	lock.Lock()
 
- 	defer lock.Unlock()
 
- 	xrayConfig, err := s.GetXrayConfig()
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	if p != nil {
 
- 		if p.GetConfig().Equals(xrayConfig) {
 
- 			return nil
 
- 		}
 
- 		p.Stop()
 
- 	}
 
- 	p = xray.NewProcess(xrayConfig)
 
- 	result = ""
 
- 	return p.Start()
 
- }
 
- func (s *XrayService) StopXray() error {
 
- 	lock.Lock()
 
- 	defer lock.Unlock()
 
- 	if s.IsXrayRunning() {
 
- 		return p.Stop()
 
- 	}
 
- 	return errors.New("xray is not running")
 
- }
 
- func (s *XrayService) SetIsNeedRestart(needRestart bool) {
 
- 	s.isNeedXrayRestart.Store(needRestart)
 
- }
 
- func (s *XrayService) IsNeedRestart() bool {
 
- 	return s.isNeedXrayRestart.Load()
 
- }
 
 
  |