mirror of
https://github.com/alireza0/x-ui.git
synced 2026-03-14 05:23:09 +00:00
完成xray启动
This commit is contained in:
100
web/service/xray.go
Normal file
100
web/service/xray.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"x-ui/util/common"
|
||||
"x-ui/xray"
|
||||
)
|
||||
|
||||
var p *xray.Process
|
||||
var result string
|
||||
|
||||
type XrayService struct {
|
||||
inboundService InboundService
|
||||
settingService SettingService
|
||||
}
|
||||
|
||||
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 {
|
||||
inboundConfig := inbound.GenXrayInboundConfig()
|
||||
xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
|
||||
}
|
||||
return xrayConfig, nil
|
||||
}
|
||||
|
||||
func (s *XrayService) StartXray() error {
|
||||
if s.IsXrayRunning() {
|
||||
return nil
|
||||
}
|
||||
|
||||
xrayConfig, err := s.GetXrayConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p = xray.NewProcess(xrayConfig)
|
||||
err = p.Start()
|
||||
result = ""
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *XrayService) StopXray() error {
|
||||
if s.IsXrayRunning() {
|
||||
return p.Stop()
|
||||
}
|
||||
return errors.New("xray is not running")
|
||||
}
|
||||
|
||||
func (s *XrayService) RestartXray() error {
|
||||
err1 := s.StopXray()
|
||||
err2 := s.StartXray()
|
||||
return common.Combine(err1, err2)
|
||||
}
|
||||
Reference in New Issue
Block a user