Import x-panel source
This commit is contained in:
98
web/service/config.json
Normal file
98
web/service/config.json
Normal file
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"log": {
|
||||
"access": "./access.log",
|
||||
"dnsLog": true,
|
||||
"error": "./error.log",
|
||||
"loglevel": "debug",
|
||||
"maskAddress": ""
|
||||
},
|
||||
"api": {
|
||||
"tag": "api",
|
||||
"services": [
|
||||
"HandlerService",
|
||||
"LoggerService",
|
||||
"StatsService"
|
||||
]
|
||||
},
|
||||
"inbounds": [
|
||||
{
|
||||
"tag": "api",
|
||||
"listen": "127.0.0.1",
|
||||
"port": 62789,
|
||||
"protocol": "tunnel",
|
||||
"settings": {
|
||||
"address": "127.0.0.1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"outbounds": [
|
||||
{
|
||||
"tag": "direct",
|
||||
"protocol": "freedom",
|
||||
"settings": {
|
||||
"domainStrategy": "AsIs",
|
||||
"redirect": "",
|
||||
"noises": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"tag": "blocked",
|
||||
"protocol": "blackhole",
|
||||
"settings": {}
|
||||
},
|
||||
{
|
||||
"tag": "api",
|
||||
"protocol": "blackhole"
|
||||
}
|
||||
],
|
||||
"policy": {
|
||||
"levels": {
|
||||
"0": {
|
||||
"handshake": 4,
|
||||
"connIdle": 300,
|
||||
"uplinkOnly": 0,
|
||||
"downlinkOnly": 0,
|
||||
"statsUserUplink": true,
|
||||
"statsUserDownlink": true,
|
||||
"statsUserOnline": true
|
||||
}
|
||||
},
|
||||
"system": {
|
||||
"statsInboundDownlink": true,
|
||||
"statsInboundUplink": true,
|
||||
"statsOutboundDownlink": true,
|
||||
"statsOutboundUplink": true
|
||||
}
|
||||
},
|
||||
"routing": {
|
||||
"domainStrategy": "AsIs",
|
||||
"rules": [
|
||||
{
|
||||
"type": "field",
|
||||
"inboundTag": [
|
||||
"api"
|
||||
],
|
||||
"outboundTag": "api"
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"outboundTag": "blocked",
|
||||
"ip": [
|
||||
"geoip:private"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "field",
|
||||
"outboundTag": "blocked",
|
||||
"protocol": [
|
||||
"bittorrent"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"stats": {},
|
||||
"metrics": {
|
||||
"tag": "metrics_out",
|
||||
"listen": "127.0.0.1:11111"
|
||||
}
|
||||
}
|
||||
2339
web/service/inbound.go
Normal file
2339
web/service/inbound.go
Normal file
File diff suppressed because it is too large
Load Diff
100
web/service/outbound.go
Normal file
100
web/service/outbound.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"x-ui/database"
|
||||
"x-ui/database/model"
|
||||
"x-ui/logger"
|
||||
"x-ui/xray"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type OutboundService struct{}
|
||||
|
||||
func (s *OutboundService) AddTraffic(traffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (error, bool) {
|
||||
var err error
|
||||
db := database.GetDB()
|
||||
tx := db.Begin()
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
} else {
|
||||
tx.Commit()
|
||||
}
|
||||
}()
|
||||
|
||||
err = s.addOutboundTraffic(tx, traffics)
|
||||
if err != nil {
|
||||
return err, false
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (s *OutboundService) addOutboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
|
||||
if len(traffics) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
for _, traffic := range traffics {
|
||||
if traffic.IsOutbound {
|
||||
|
||||
var outbound model.OutboundTraffics
|
||||
|
||||
err = tx.Model(&model.OutboundTraffics{}).Where("tag = ?", traffic.Tag).
|
||||
FirstOrCreate(&outbound).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outbound.Tag = traffic.Tag
|
||||
outbound.Up = outbound.Up + traffic.Up
|
||||
outbound.Down = outbound.Down + traffic.Down
|
||||
outbound.Total = outbound.Up + outbound.Down
|
||||
|
||||
err = tx.Save(&outbound).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *OutboundService) GetOutboundsTraffic() ([]*model.OutboundTraffics, error) {
|
||||
db := database.GetDB()
|
||||
var traffics []*model.OutboundTraffics
|
||||
|
||||
err := db.Model(model.OutboundTraffics{}).Find(&traffics).Error
|
||||
if err != nil {
|
||||
logger.Warning("Error retrieving OutboundTraffics: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return traffics, nil
|
||||
}
|
||||
|
||||
func (s *OutboundService) ResetOutboundTraffic(tag string) error {
|
||||
db := database.GetDB()
|
||||
|
||||
whereText := "tag "
|
||||
if tag == "-alltags-" {
|
||||
whereText += " <> ?"
|
||||
} else {
|
||||
whereText += " = ?"
|
||||
}
|
||||
|
||||
result := db.Model(model.OutboundTraffics{}).
|
||||
Where(whereText, tag).
|
||||
Updates(map[string]any{"up": 0, "down": 0, "total": 0})
|
||||
|
||||
err := result.Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
26
web/service/panel.go
Normal file
26
web/service/panel.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"x-ui/logger"
|
||||
)
|
||||
|
||||
type PanelService struct{}
|
||||
|
||||
func (s *PanelService) RestartPanel(delay time.Duration) error {
|
||||
p, err := os.FindProcess(syscall.Getpid())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(delay)
|
||||
err := p.Signal(syscall.SIGHUP)
|
||||
if err != nil {
|
||||
logger.Error("failed to send SIGHUP signal:", err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
1392
web/service/server.go
Normal file
1392
web/service/server.go
Normal file
File diff suppressed because it is too large
Load Diff
634
web/service/setting.go
Normal file
634
web/service/setting.go
Normal file
@@ -0,0 +1,634 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"x-ui/database"
|
||||
"x-ui/database/model"
|
||||
"x-ui/logger"
|
||||
"x-ui/util/common"
|
||||
"x-ui/util/random"
|
||||
"x-ui/util/reflect_util"
|
||||
"x-ui/web/entity"
|
||||
"x-ui/xray"
|
||||
)
|
||||
|
||||
//go:embed config.json
|
||||
var xrayTemplateConfig string
|
||||
|
||||
var defaultValueMap = map[string]string{
|
||||
"xrayTemplateConfig": xrayTemplateConfig,
|
||||
"webListen": "",
|
||||
"webDomain": "",
|
||||
"webPort": "13688",
|
||||
"webCertFile": "",
|
||||
"webKeyFile": "",
|
||||
"secret": random.Seq(32),
|
||||
"webBasePath": "/",
|
||||
"sessionMaxAge": "360",
|
||||
"pageSize": "50",
|
||||
"expireDiff": "0",
|
||||
"trafficDiff": "0",
|
||||
"remarkModel": "-ieo",
|
||||
"timeLocation": "Local",
|
||||
"tgBotEnable": "false",
|
||||
"tgBotToken": "",
|
||||
"tgBotProxy": "",
|
||||
"tgBotAPIServer": "",
|
||||
"tgBotChatId": "",
|
||||
"tgRunTime": "@daily",
|
||||
"tgBotBackup": "false",
|
||||
"tgBotLoginNotify": "true",
|
||||
"tgCpu": "80",
|
||||
"tgLang": "zh-CN",
|
||||
"twoFactorEnable": "false",
|
||||
"twoFactorToken": "",
|
||||
"subEnable": "false",
|
||||
"subTitle": "",
|
||||
"subListen": "",
|
||||
"subPort": "13788",
|
||||
"subPath": "/sub/",
|
||||
"subDomain": "",
|
||||
"subCertFile": "",
|
||||
"subKeyFile": "",
|
||||
"subUpdates": "12",
|
||||
"subEncrypt": "true",
|
||||
"subShowInfo": "true",
|
||||
"subURI": "",
|
||||
"subJsonPath": "/json/",
|
||||
"subJsonURI": "",
|
||||
"subJsonFragment": "",
|
||||
"subJsonNoises": "",
|
||||
"subJsonMux": "",
|
||||
"subJsonRules": "",
|
||||
"datepicker": "gregorian",
|
||||
"warp": "",
|
||||
"externalTrafficInformEnable": "false",
|
||||
"externalTrafficInformURI": "",
|
||||
}
|
||||
|
||||
type SettingService struct{}
|
||||
|
||||
func (s *SettingService) GetDefaultJsonConfig() (any, error) {
|
||||
var jsonData any
|
||||
err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jsonData, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
|
||||
db := database.GetDB()
|
||||
settings := make([]*model.Setting, 0)
|
||||
err := db.Model(model.Setting{}).Not("key = ?", "xrayTemplateConfig").Find(&settings).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allSetting := &entity.AllSetting{}
|
||||
t := reflect.TypeOf(allSetting).Elem()
|
||||
v := reflect.ValueOf(allSetting).Elem()
|
||||
fields := reflect_util.GetFields(t)
|
||||
|
||||
setSetting := func(key, value string) (err error) {
|
||||
defer func() {
|
||||
panicErr := recover()
|
||||
if panicErr != nil {
|
||||
err = errors.New(fmt.Sprint(panicErr))
|
||||
}
|
||||
}()
|
||||
|
||||
var found bool
|
||||
var field reflect.StructField
|
||||
for _, f := range fields {
|
||||
if f.Tag.Get("json") == key {
|
||||
field = f
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
// Some settings are automatically generated, no need to return to the front end to modify the user
|
||||
return nil
|
||||
}
|
||||
|
||||
fieldV := v.FieldByName(field.Name)
|
||||
switch t := fieldV.Interface().(type) {
|
||||
case int:
|
||||
n, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fieldV.SetInt(n)
|
||||
case string:
|
||||
fieldV.SetString(value)
|
||||
case bool:
|
||||
fieldV.SetBool(value == "true")
|
||||
default:
|
||||
return common.NewErrorf("unknown field %v type %v", key, t)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
keyMap := map[string]bool{}
|
||||
for _, setting := range settings {
|
||||
err := setSetting(setting.Key, setting.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyMap[setting.Key] = true
|
||||
}
|
||||
|
||||
for key, value := range defaultValueMap {
|
||||
if keyMap[key] {
|
||||
continue
|
||||
}
|
||||
err := setSetting(key, value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return allSetting, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) ResetSettings() error {
|
||||
db := database.GetDB()
|
||||
err := db.Where("1 = 1").Delete(model.Setting{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Model(model.User{}).
|
||||
Where("1 = 1").Error
|
||||
}
|
||||
|
||||
func (s *SettingService) getSetting(key string) (*model.Setting, error) {
|
||||
db := database.GetDB()
|
||||
setting := &model.Setting{}
|
||||
err := db.Model(model.Setting{}).Where("key = ?", key).First(setting).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return setting, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) saveSetting(key string, value string) error {
|
||||
setting, err := s.getSetting(key)
|
||||
db := database.GetDB()
|
||||
if database.IsNotFound(err) {
|
||||
return db.Create(&model.Setting{
|
||||
Key: key,
|
||||
Value: value,
|
||||
}).Error
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
setting.Key = key
|
||||
setting.Value = value
|
||||
return db.Save(setting).Error
|
||||
}
|
||||
|
||||
func (s *SettingService) getString(key string) (string, error) {
|
||||
setting, err := s.getSetting(key)
|
||||
if database.IsNotFound(err) {
|
||||
value, ok := defaultValueMap[key]
|
||||
if !ok {
|
||||
return "", common.NewErrorf("key <%v> not in defaultValueMap", key)
|
||||
}
|
||||
return value, nil
|
||||
} else if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return setting.Value, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) setString(key string, value string) error {
|
||||
return s.saveSetting(key, value)
|
||||
}
|
||||
|
||||
func (s *SettingService) getBool(key string) (bool, error) {
|
||||
str, err := s.getString(key)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return strconv.ParseBool(str)
|
||||
}
|
||||
|
||||
func (s *SettingService) setBool(key string, value bool) error {
|
||||
return s.setString(key, strconv.FormatBool(value))
|
||||
}
|
||||
|
||||
func (s *SettingService) getInt(key string) (int, error) {
|
||||
str, err := s.getString(key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return strconv.Atoi(str)
|
||||
}
|
||||
|
||||
func (s *SettingService) setInt(key string, value int) error {
|
||||
return s.setString(key, strconv.Itoa(value))
|
||||
}
|
||||
|
||||
func (s *SettingService) GetXrayConfigTemplate() (string, error) {
|
||||
return s.getString("xrayTemplateConfig")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetListen() (string, error) {
|
||||
return s.getString("webListen")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetListen(ip string) error {
|
||||
return s.setString("webListen", ip)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetWebDomain() (string, error) {
|
||||
return s.getString("webDomain")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgBotToken() (string, error) {
|
||||
return s.getString("tgBotToken")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTgBotToken(token string) error {
|
||||
return s.setString("tgBotToken", token)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgBotProxy() (string, error) {
|
||||
return s.getString("tgBotProxy")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTgBotProxy(token string) error {
|
||||
return s.setString("tgBotProxy", token)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgBotAPIServer() (string, error) {
|
||||
return s.getString("tgBotAPIServer")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTgBotAPIServer(token string) error {
|
||||
return s.setString("tgBotAPIServer", token)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgBotChatId() (string, error) {
|
||||
return s.getString("tgBotChatId")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTgBotChatId(chatIds string) error {
|
||||
return s.setString("tgBotChatId", chatIds)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgbotEnabled() (bool, error) {
|
||||
return s.getBool("tgBotEnable")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTgbotEnabled(value bool) error {
|
||||
return s.setBool("tgBotEnable", value)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgbotRuntime() (string, error) {
|
||||
return s.getString("tgRunTime")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTgbotRuntime(time string) error {
|
||||
return s.setString("tgRunTime", time)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgBotBackup() (bool, error) {
|
||||
return s.getBool("tgBotBackup")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgBotLoginNotify() (bool, error) {
|
||||
return s.getBool("tgBotLoginNotify")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgCpu() (int, error) {
|
||||
return s.getInt("tgCpu")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTgLang() (string, error) {
|
||||
return s.getString("tgLang")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTwoFactorEnable() (bool, error) {
|
||||
return s.getBool("twoFactorEnable")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTwoFactorEnable(value bool) error {
|
||||
return s.setBool("twoFactorEnable", value)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTwoFactorToken() (string, error) {
|
||||
return s.getString("twoFactorToken")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetTwoFactorToken(value string) error {
|
||||
return s.setString("twoFactorToken", value)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetPort() (int, error) {
|
||||
return s.getInt("webPort")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetPort(port int) error {
|
||||
return s.setInt("webPort", port)
|
||||
}
|
||||
|
||||
func (s *SettingService) SetCertFile(webCertFile string) error {
|
||||
return s.setString("webCertFile", webCertFile)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetCertFile() (string, error) {
|
||||
return s.getString("webCertFile")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetKeyFile(webKeyFile string) error {
|
||||
return s.setString("webKeyFile", webKeyFile)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetKeyFile() (string, error) {
|
||||
return s.getString("webKeyFile")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetExpireDiff() (int, error) {
|
||||
return s.getInt("expireDiff")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTrafficDiff() (int, error) {
|
||||
return s.getInt("trafficDiff")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSessionMaxAge() (int, error) {
|
||||
return s.getInt("sessionMaxAge")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetRemarkModel() (string, error) {
|
||||
return s.getString("remarkModel")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSecret() ([]byte, error) {
|
||||
secret, err := s.getString("secret")
|
||||
if secret == defaultValueMap["secret"] {
|
||||
err := s.saveSetting("secret", secret)
|
||||
if err != nil {
|
||||
logger.Warning("save secret failed:", err)
|
||||
}
|
||||
}
|
||||
return []byte(secret), err
|
||||
}
|
||||
|
||||
func (s *SettingService) SetBasePath(basePath string) error {
|
||||
if !strings.HasPrefix(basePath, "/") {
|
||||
basePath = "/" + basePath
|
||||
}
|
||||
if !strings.HasSuffix(basePath, "/") {
|
||||
basePath += "/"
|
||||
}
|
||||
return s.setString("webBasePath", basePath)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetBasePath() (string, error) {
|
||||
basePath, err := s.getString("webBasePath")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !strings.HasPrefix(basePath, "/") {
|
||||
basePath = "/" + basePath
|
||||
}
|
||||
if !strings.HasSuffix(basePath, "/") {
|
||||
basePath += "/"
|
||||
}
|
||||
return basePath, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) GetTimeLocation() (*time.Location, error) {
|
||||
l, err := s.getString("timeLocation")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
location, err := time.LoadLocation(l)
|
||||
if err != nil {
|
||||
defaultLocation := defaultValueMap["timeLocation"]
|
||||
logger.Errorf("location <%v> not exist, using default location: %v", l, defaultLocation)
|
||||
return time.LoadLocation(defaultLocation)
|
||||
}
|
||||
return location, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubEnable() (bool, error) {
|
||||
return s.getBool("subEnable")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubTitle() (string, error) {
|
||||
return s.getString("subTitle")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubListen() (string, error) {
|
||||
return s.getString("subListen")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubPort() (int, error) {
|
||||
return s.getInt("subPort")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubPath() (string, error) {
|
||||
return s.getString("subPath")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubJsonPath() (string, error) {
|
||||
return s.getString("subJsonPath")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubDomain() (string, error) {
|
||||
return s.getString("subDomain")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubCertFile() (string, error) {
|
||||
return s.getString("subCertFile")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubKeyFile() (string, error) {
|
||||
return s.getString("subKeyFile")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubUpdates() (string, error) {
|
||||
return s.getString("subUpdates")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubEncrypt() (bool, error) {
|
||||
return s.getBool("subEncrypt")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubShowInfo() (bool, error) {
|
||||
return s.getBool("subShowInfo")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetPageSize() (int, error) {
|
||||
return s.getInt("pageSize")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubURI() (string, error) {
|
||||
return s.getString("subURI")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubJsonURI() (string, error) {
|
||||
return s.getString("subJsonURI")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubJsonFragment() (string, error) {
|
||||
return s.getString("subJsonFragment")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubJsonNoises() (string, error) {
|
||||
return s.getString("subJsonNoises")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubJsonMux() (string, error) {
|
||||
return s.getString("subJsonMux")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetSubJsonRules() (string, error) {
|
||||
return s.getString("subJsonRules")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetDatepicker() (string, error) {
|
||||
return s.getString("datepicker")
|
||||
}
|
||||
|
||||
func (s *SettingService) GetWarp() (string, error) {
|
||||
return s.getString("warp")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetWarp(data string) error {
|
||||
return s.setString("warp", data)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetExternalTrafficInformEnable() (bool, error) {
|
||||
return s.getBool("externalTrafficInformEnable")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetExternalTrafficInformEnable(value bool) error {
|
||||
return s.setBool("externalTrafficInformEnable", value)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetExternalTrafficInformURI() (string, error) {
|
||||
return s.getString("externalTrafficInformURI")
|
||||
}
|
||||
|
||||
func (s *SettingService) SetExternalTrafficInformURI(InformURI string) error {
|
||||
return s.setString("externalTrafficInformURI", InformURI)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetIpLimitEnable() (bool, error) {
|
||||
accessLogPath, err := xray.GetAccessLogPath()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return (accessLogPath != "none" && accessLogPath != ""), nil
|
||||
}
|
||||
|
||||
func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error {
|
||||
if err := allSetting.CheckValid(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(allSetting).Elem()
|
||||
t := reflect.TypeOf(allSetting).Elem()
|
||||
fields := reflect_util.GetFields(t)
|
||||
errs := make([]error, 0)
|
||||
for _, field := range fields {
|
||||
key := field.Tag.Get("json")
|
||||
fieldV := v.FieldByName(field.Name)
|
||||
value := fmt.Sprint(fieldV.Interface())
|
||||
err := s.saveSetting(key, value)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return common.Combine(errs...)
|
||||
}
|
||||
|
||||
func (s *SettingService) GetDefaultXrayConfig() (any, error) {
|
||||
var jsonData any
|
||||
err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jsonData, nil
|
||||
}
|
||||
|
||||
func (s *SettingService) GetDefaultSettings(host string) (any, error) {
|
||||
type settingFunc func() (any, error)
|
||||
settings := map[string]settingFunc{
|
||||
"expireDiff": func() (any, error) { return s.GetExpireDiff() },
|
||||
"trafficDiff": func() (any, error) { return s.GetTrafficDiff() },
|
||||
"pageSize": func() (any, error) { return s.GetPageSize() },
|
||||
"defaultCert": func() (any, error) { return s.GetCertFile() },
|
||||
"defaultKey": func() (any, error) { return s.GetKeyFile() },
|
||||
"tgBotEnable": func() (any, error) { return s.GetTgbotEnabled() },
|
||||
"subEnable": func() (any, error) { return s.GetSubEnable() },
|
||||
"subTitle": func() (any, error) { return s.GetSubTitle() },
|
||||
"subURI": func() (any, error) { return s.GetSubURI() },
|
||||
"subJsonURI": func() (any, error) { return s.GetSubJsonURI() },
|
||||
"remarkModel": func() (any, error) { return s.GetRemarkModel() },
|
||||
"datepicker": func() (any, error) { return s.GetDatepicker() },
|
||||
"ipLimitEnable": func() (any, error) { return s.GetIpLimitEnable() },
|
||||
}
|
||||
|
||||
result := make(map[string]any)
|
||||
|
||||
for key, fn := range settings {
|
||||
value, err := fn()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result[key] = value
|
||||
}
|
||||
|
||||
if result["subEnable"].(bool) && (result["subURI"].(string) == "" || result["subJsonURI"].(string) == "") {
|
||||
subURI := ""
|
||||
subTitle, _ := s.GetSubTitle()
|
||||
subPort, _ := s.GetSubPort()
|
||||
subPath, _ := s.GetSubPath()
|
||||
subJsonPath, _ := s.GetSubJsonPath()
|
||||
subDomain, _ := s.GetSubDomain()
|
||||
subKeyFile, _ := s.GetSubKeyFile()
|
||||
subCertFile, _ := s.GetSubCertFile()
|
||||
subTLS := false
|
||||
if subKeyFile != "" && subCertFile != "" {
|
||||
subTLS = true
|
||||
}
|
||||
if subDomain == "" {
|
||||
subDomain = strings.Split(host, ":")[0]
|
||||
}
|
||||
if subTLS {
|
||||
subURI = "https://"
|
||||
} else {
|
||||
subURI = "http://"
|
||||
}
|
||||
if (subPort == 443 && subTLS) || (subPort == 80 && !subTLS) {
|
||||
subURI += subDomain
|
||||
} else {
|
||||
subURI += fmt.Sprintf("%s:%d", subDomain, subPort)
|
||||
}
|
||||
if result["subURI"].(string) == "" {
|
||||
result["subURI"] = subURI + subPath
|
||||
}
|
||||
if result["subTitle"].(string) == "" {
|
||||
result["subTitle"] = subTitle
|
||||
}
|
||||
if result["subJsonURI"].(string) == "" {
|
||||
result["subJsonURI"] = subURI + subJsonPath
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
4314
web/service/tgbot.go
Normal file
4314
web/service/tgbot.go
Normal file
File diff suppressed because it is too large
Load Diff
123
web/service/user.go
Normal file
123
web/service/user.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"x-ui/database"
|
||||
"x-ui/database/model"
|
||||
"x-ui/logger"
|
||||
"x-ui/util/crypto"
|
||||
|
||||
"github.com/xlzd/gotp"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
settingService SettingService
|
||||
}
|
||||
|
||||
func (s *UserService) GetFirstUser() (*model.User, error) {
|
||||
db := database.GetDB()
|
||||
|
||||
user := &model.User{}
|
||||
err := db.Model(model.User{}).
|
||||
First(user).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *UserService) CheckUser(username string, password string, twoFactorCode string) *model.User {
|
||||
db := database.GetDB()
|
||||
|
||||
user := &model.User{}
|
||||
|
||||
err := db.Model(model.User{}).
|
||||
Where("username = ?", username).
|
||||
First(user).
|
||||
Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
logger.Warning("check user err:", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if !crypto.CheckPasswordHash(user.Password, password) {
|
||||
return nil
|
||||
}
|
||||
|
||||
twoFactorEnable, err := s.settingService.GetTwoFactorEnable()
|
||||
if err != nil {
|
||||
logger.Warning("check two factor err:", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if twoFactorEnable {
|
||||
twoFactorToken, err := s.settingService.GetTwoFactorToken()
|
||||
|
||||
if err != nil {
|
||||
logger.Warning("check two factor token err:", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if gotp.NewDefaultTOTP(twoFactorToken).Now() != twoFactorCode {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateUser(id int, username string, password string) error {
|
||||
db := database.GetDB()
|
||||
hashedPassword, err := crypto.HashPasswordAsBcrypt(password)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
twoFactorEnable, err := s.settingService.GetTwoFactorEnable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if twoFactorEnable {
|
||||
s.settingService.SetTwoFactorEnable(false)
|
||||
s.settingService.SetTwoFactorToken("")
|
||||
}
|
||||
|
||||
return db.Model(model.User{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]any{"username": username, "password": hashedPassword}).
|
||||
Error
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateFirstUser(username string, password string) error {
|
||||
if username == "" {
|
||||
return errors.New("username can not be empty")
|
||||
} else if password == "" {
|
||||
return errors.New("password can not be empty")
|
||||
}
|
||||
hashedPassword, er := crypto.HashPasswordAsBcrypt(password)
|
||||
|
||||
if er != nil {
|
||||
return er
|
||||
}
|
||||
|
||||
db := database.GetDB()
|
||||
user := &model.User{}
|
||||
err := db.Model(model.User{}).First(user).Error
|
||||
if database.IsNotFound(err) {
|
||||
user.Username = username
|
||||
user.Password = hashedPassword
|
||||
return db.Model(model.User{}).Create(user).Error
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
user.Username = username
|
||||
user.Password = hashedPassword
|
||||
return db.Save(user).Error
|
||||
}
|
||||
170
web/service/warp.go
Normal file
170
web/service/warp.go
Normal file
@@ -0,0 +1,170 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
"x-ui/logger"
|
||||
"x-ui/util/common"
|
||||
)
|
||||
|
||||
type WarpService struct {
|
||||
SettingService
|
||||
}
|
||||
|
||||
func (s *WarpService) GetWarpData() (string, error) {
|
||||
warp, err := s.SettingService.GetWarp()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return warp, nil
|
||||
}
|
||||
|
||||
func (s *WarpService) DelWarpData() error {
|
||||
err := s.SettingService.SetWarp("")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *WarpService) GetWarpConfig() (string, error) {
|
||||
var warpData map[string]string
|
||||
warp, err := s.SettingService.GetWarp()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = json.Unmarshal([]byte(warp), &warpData)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s", warpData["device_id"])
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
buffer := &bytes.Buffer{}
|
||||
_, err = buffer.ReadFrom(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
func (s *WarpService) RegWarp(secretKey string, publicKey string) (string, error) {
|
||||
tos := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
|
||||
hostName, _ := os.Hostname()
|
||||
data := fmt.Sprintf(`{"key":"%s","tos":"%s","type": "PC","model": "x-ui", "name": "%s"}`, publicKey, tos, hostName)
|
||||
|
||||
url := "https://api.cloudflareclient.com/v0a2158/reg"
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req.Header.Add("CF-Client-Version", "a-7.21-0721")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
buffer := &bytes.Buffer{}
|
||||
_, err = buffer.ReadFrom(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var rspData map[string]any
|
||||
err = json.Unmarshal(buffer.Bytes(), &rspData)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
deviceId := rspData["id"].(string)
|
||||
token := rspData["token"].(string)
|
||||
license, ok := rspData["account"].(map[string]any)["license"].(string)
|
||||
if !ok {
|
||||
logger.Debug("Error accessing license value.")
|
||||
return "", err
|
||||
}
|
||||
|
||||
warpData := fmt.Sprintf("{\n \"access_token\": \"%s\",\n \"device_id\": \"%s\",", token, deviceId)
|
||||
warpData += fmt.Sprintf("\n \"license_key\": \"%s\",\n \"private_key\": \"%s\"\n}", license, secretKey)
|
||||
|
||||
s.SettingService.SetWarp(warpData)
|
||||
|
||||
result := fmt.Sprintf("{\n \"data\": %s,\n \"config\": %s\n}", warpData, buffer.String())
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *WarpService) SetWarpLicense(license string) (string, error) {
|
||||
var warpData map[string]string
|
||||
warp, err := s.SettingService.GetWarp()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = json.Unmarshal([]byte(warp), &warpData)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://api.cloudflareclient.com/v0a2158/reg/%s/account", warpData["device_id"])
|
||||
data := fmt.Sprintf(`{"license": "%s"}`, license)
|
||||
|
||||
req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(data)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+warpData["access_token"])
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
buffer := &bytes.Buffer{}
|
||||
_, err = buffer.ReadFrom(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var response map[string]any
|
||||
err = json.Unmarshal(buffer.Bytes(), &response)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if response["success"] == false {
|
||||
errorArr, _ := response["errors"].([]any)
|
||||
errorObj := errorArr[0].(map[string]any)
|
||||
return "", common.NewError(errorObj["code"], errorObj["message"])
|
||||
}
|
||||
|
||||
warpData["license_key"] = license
|
||||
newWarpData, err := json.MarshalIndent(warpData, "", " ")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
s.SettingService.SetWarp(string(newWarpData))
|
||||
|
||||
return string(newWarpData), nil
|
||||
}
|
||||
481
web/service/xray.go
Normal file
481
web/service/xray.go
Normal file
@@ -0,0 +1,481 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"runtime"
|
||||
"sync"
|
||||
"strconv"
|
||||
|
||||
"x-ui/logger"
|
||||
"x-ui/xray"
|
||||
json_util "x-ui/util/json_util"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
p *xray.Process
|
||||
lock sync.Mutex
|
||||
isNeedXrayRestart atomic.Bool // Indicates that restart was requested for Xray
|
||||
isManuallyStopped atomic.Bool // Indicates that Xray was stopped manually from the panel
|
||||
result string
|
||||
)
|
||||
|
||||
type XrayService struct {
|
||||
inboundService InboundService
|
||||
settingService SettingService
|
||||
xrayAPI xray.XrayAPI
|
||||
}
|
||||
|
||||
// SetXrayAPI 用于从外部注入 XrayAPI 实例
|
||||
func (s *XrayService) SetXrayAPI(api xray.XrayAPI) {
|
||||
s.xrayAPI = api
|
||||
}
|
||||
|
||||
// IsXrayRunning 检查 Xray 是否正在运行
|
||||
func (s *XrayService) IsXrayRunning() bool {
|
||||
return p != nil && p.IsRunning()
|
||||
}
|
||||
|
||||
// 中文注释:
|
||||
// 新增 GetApiPort 函数。
|
||||
// 这个函数的作用是安全地返回当前 Xray 进程正在监听的 API 端口号。
|
||||
// 如果 Xray 没有运行 (p == nil),则返回 0。
|
||||
// 我们的后台任务将调用这个函数来获取端口号。
|
||||
func (s *XrayService) GetApiPort() int {
|
||||
if p == nil {
|
||||
return 0
|
||||
}
|
||||
return p.GetAPIPort()
|
||||
}
|
||||
|
||||
|
||||
func (s *XrayService) GetXrayErr() error {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := p.GetErr()
|
||||
|
||||
if runtime.GOOS == "windows" && err.Error() == "exit status 1" {
|
||||
// exit status 1 on Windows means that Xray process was killed
|
||||
// as we kill process to stop in on Windows, this is not an error
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *XrayService) GetXrayResult() string {
|
||||
if result != "" {
|
||||
return result
|
||||
}
|
||||
if s.IsXrayRunning() {
|
||||
return ""
|
||||
}
|
||||
if p == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
result = p.GetResult()
|
||||
|
||||
if runtime.GOOS == "windows" && result == "exit status 1" {
|
||||
// exit status 1 on Windows means that Xray process was killed
|
||||
// as we kill process to stop in on Windows, this is not an error
|
||||
return ""
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *XrayService) GetXrayVersion() string {
|
||||
if p == nil {
|
||||
return "Unknown"
|
||||
}
|
||||
return p.GetVersion()
|
||||
}
|
||||
|
||||
func RemoveIndex(s []any, index int) []any {
|
||||
return append(s[:index], s[index+1:]...)
|
||||
}
|
||||
|
||||
func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
|
||||
templateConfig, err := s.settingService.GetXrayConfigTemplate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
xrayConfig := &xray.Config{}
|
||||
if err := json.Unmarshal([]byte(templateConfig), xrayConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
inbounds, err := s.inboundService.GetAllInbounds()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// 中文注释: 动态限速核心逻辑 - 第一步: 收集所有限速值
|
||||
// =================================================================
|
||||
// 创建一个 map 用于存储所有出现过的、不为0的限速值
|
||||
uniqueSpeeds := make(map[int]bool)
|
||||
for _, inbound := range inbounds {
|
||||
if !inbound.Enable {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取该入站下的所有客户端设置
|
||||
dbClients, _ := s.inboundService.GetClients(inbound)
|
||||
for _, dbClient := range dbClients {
|
||||
if dbClient.SpeedLimit > 0 {
|
||||
uniqueSpeeds[dbClient.SpeedLimit] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// 中文注释: 动态限速核心逻辑 - 第二步: 根据收集到的限速值,动态生成 Policy Levels
|
||||
// =================================================================
|
||||
|
||||
// 1. 先从模板中解析出已有的 policy 对象
|
||||
var finalPolicy map[string]interface{}
|
||||
if xrayConfig.Policy != nil {
|
||||
if err := json.Unmarshal(xrayConfig.Policy, &finalPolicy); err != nil {
|
||||
logger.Warningf("无法解析模板中的 policy: %v", err)
|
||||
finalPolicy = make(map[string]interface{})
|
||||
}
|
||||
} else {
|
||||
finalPolicy = make(map[string]interface{})
|
||||
}
|
||||
|
||||
// 2. 初始化 policy levels,获取或创建 policy中的 levels map
|
||||
var policyLevels map[string]interface{}
|
||||
if levels, ok := finalPolicy["levels"].(map[string]interface{}); ok {
|
||||
policyLevels = levels
|
||||
} else {
|
||||
policyLevels = make(map[string]interface{})
|
||||
}
|
||||
|
||||
// 3. 〔重要修改〕: 确保 level 0 策略的完整性,这是让设备限制和默认用户统计生效的关键
|
||||
var level0 map[string]interface{}
|
||||
if l0, ok := policyLevels["0"].(map[string]interface{}); ok {
|
||||
// 〔中文注释〕: 如果模板中已存在 level 0,使用它作为基础进行修改。
|
||||
level0 = l0
|
||||
} else {
|
||||
// 〔中文注释〕: 如果模板中不存在,则创建一个全新的 map。
|
||||
level0 = make(map[string]interface{})
|
||||
}
|
||||
// 〔中文注释〕: 无论 level 0 是否存在,都为其补充或覆盖以下关键参数。
|
||||
// handshake 和 connIdle 是激活 Xray 连接统计的前提,
|
||||
// uplinkOnly 和 downlinkOnly 设置为 0 代表不限速,这是 level 0 用户的默认行为。
|
||||
// statsUserUplink 和 statsUserDownlink 确保用户的流量能够被统计。
|
||||
level0["handshake"] = 4
|
||||
level0["connIdle"] = 300
|
||||
level0["uplinkOnly"] = 0
|
||||
level0["downlinkOnly"] = 0
|
||||
level0["statsUserUplink"] = true
|
||||
level0["statsUserDownlink"] = true
|
||||
// 〔新增〕: 增加此关键选项以启用 Xray-core 的在线 IP 统计功能。
|
||||
// 这是让【设备限制】功能正常工作的前提。
|
||||
level0["statsUserOnline"] = true
|
||||
|
||||
// 〔中文注释〕: 将完整配置好的 level 0 写回 policyLevels,确保最终生成的 config.json 是正确的。
|
||||
policyLevels["0"] = level0
|
||||
|
||||
// 4. 遍历所有收集到的限速值,为每个独立的限速值创建对应的 level
|
||||
for speed := range uniqueSpeeds {
|
||||
// 为每个速率创建一个 level,level 的名字就是速率的字符串形式
|
||||
// 例如,速率 1024 KB/s 对应 level "1024"
|
||||
policyLevels[strconv.Itoa(speed)] = map[string]interface{}{
|
||||
"downlinkOnly": speed,
|
||||
"uplinkOnly": speed,
|
||||
"handshake": 4,
|
||||
"connIdle": 300,
|
||||
"statsUserUplink": true,
|
||||
"statsUserDownlink": true,
|
||||
"statsUserOnline": true,
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 将修改后的 levels 写回 policy 对象,并序列化回 xrayConfig.Policy,将生成的 policy 应用到 Xray 配置中
|
||||
finalPolicy["levels"] = policyLevels
|
||||
policyJSON, err := json.Marshal(finalPolicy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
xrayConfig.Policy = json_util.RawMessage(policyJSON)
|
||||
|
||||
// =================================================================
|
||||
// 中文注释: 在这里增加日志,打印最终生成的限速策略
|
||||
// =================================================================
|
||||
if len(uniqueSpeeds) > 0 {
|
||||
finalPolicyLog, _ := json.Marshal(policyLevels)
|
||||
logger.Infof("已为Xray动态生成〔限速策略〕: %s", string(finalPolicyLog))
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// 中文注释: 动态限速核心逻辑 - 第三步: 为设置了限速的用户分配对应的 Level,逐个 inbound 构建 inboundConfig
|
||||
// =================================================================
|
||||
// 触发一次空调用以处理可能的残留任务
|
||||
s.inboundService.AddTraffic(nil, nil)
|
||||
|
||||
for _, inbound := range inbounds {
|
||||
if !inbound.Enable {
|
||||
continue
|
||||
}
|
||||
|
||||
// 先生成一个 inboundConfig(后面会覆盖 Settings/StreamSettings)
|
||||
inboundConfig := inbound.GenXrayInboundConfig()
|
||||
|
||||
// 从 DB clients 建立 email/id -> speedLimit 映射(优先使用 DB 的值)
|
||||
speedByEmail := make(map[string]int)
|
||||
speedById := make(map[string]int)
|
||||
dbClients, _ := s.inboundService.GetClients(inbound)
|
||||
for _, dbc := range dbClients {
|
||||
if dbc.Email != "" {
|
||||
speedByEmail[dbc.Email] = dbc.SpeedLimit
|
||||
}
|
||||
// 如果有 id 字段也建立映射(以防 email 不存在)
|
||||
if dbc.ID != "" {
|
||||
speedById[dbc.ID] = dbc.SpeedLimit
|
||||
}
|
||||
}
|
||||
|
||||
// 解析 inbound.Settings
|
||||
var settings map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
|
||||
logger.Warningf("无法解析 inbound.Settings (inbound %d): %v ,跳过该入站", inbound.Id, err)
|
||||
continue
|
||||
}
|
||||
|
||||
originalClients, ok := settings["clients"].([]interface{})
|
||||
if ok {
|
||||
clientStats := inbound.ClientStats
|
||||
|
||||
var xrayClients []interface{}
|
||||
for _, clientRaw := range originalClients {
|
||||
c, ok := clientRaw.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 中文注释: 用户过滤 - 1) settings 中的 enable 字段检查
|
||||
// -----------------------------------------------------------------
|
||||
if en, ok := c["enable"].(bool); ok && !en {
|
||||
if em, _ := c["email"].(string); em != "" {
|
||||
logger.Infof("已从Xray配置中移除被settings标记为禁用的用户: %s", em)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 中文注释: 用户过滤 - 2) inbound.ClientStats 检查 (DB/流量层禁用)
|
||||
// -----------------------------------------------------------------
|
||||
email, _ := c["email"].(string)
|
||||
idStr, _ := c["id"].(string)
|
||||
disabledByStat := false
|
||||
for _, stat := range clientStats {
|
||||
if stat.Email == email && !stat.Enable {
|
||||
disabledByStat = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if disabledByStat {
|
||||
logger.Infof("已从Xray配置中移除被禁用的用户: %s", email)
|
||||
continue
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 中文注释: 构建干净的 xrayClient(只保留白名单字段)
|
||||
// -----------------------------------------------------------------
|
||||
xrayClient := make(map[string]interface{})
|
||||
if id, ok := c["id"]; ok { xrayClient["id"] = id }
|
||||
if email != "" { xrayClient["email"] = email }
|
||||
|
||||
// 规范化 flow
|
||||
if flow, ok := c["flow"]; ok {
|
||||
if fs, ok2 := flow.(string); ok2 && fs == "xtls-rprx-vision-udp443" {
|
||||
xrayClient["flow"] = "xtls-rprx-vision"
|
||||
} else {
|
||||
xrayClient["flow"] = flow
|
||||
}
|
||||
}
|
||||
if password, ok := c["password"]; ok { xrayClient["password"] = password }
|
||||
if method, ok := c["method"]; ok { xrayClient["method"] = method }
|
||||
|
||||
// ⚠️ security 字段已移除,不再加入到 xrayClient
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 中文注释: 限速等级映射(优先 DB,再回退 settings.speedLimit)
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
// =================================================================
|
||||
// 这里的逻辑是准备将 client 对象提交给 Xray-core。
|
||||
// 我们需要将 speedLimit 转换为 Xray 认识的 level 字段。
|
||||
// 这样可以确保包含 speedLimit 的完整用户信息被用于生成配置。
|
||||
// =================================================================
|
||||
level := 0
|
||||
if email != "" {
|
||||
if v, ok := speedByEmail[email]; ok && v > 0 {
|
||||
level = v
|
||||
}
|
||||
}
|
||||
if level == 0 && idStr != "" {
|
||||
if v, ok := speedById[idStr]; ok && v > 0 {
|
||||
level = v
|
||||
}
|
||||
}
|
||||
if level == 0 {
|
||||
if sl, ok := c["speedLimit"]; ok {
|
||||
switch vv := sl.(type) {
|
||||
case float64:
|
||||
level = int(vv)
|
||||
case int:
|
||||
level = vv
|
||||
case int64:
|
||||
level = int(vv)
|
||||
case string:
|
||||
if n, err := strconv.Atoi(vv); err == nil {
|
||||
level = n
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 【新增功能】在这里添加日志记录
|
||||
// 只有当最终计算出的 level 大于 0,且 email 存在时,才记录日志
|
||||
if level > 0 && email != "" {
|
||||
logger.Infof("为用户 %s 应用〔独立限速〕: %d KB/s", email, level)
|
||||
}
|
||||
// =================================================================
|
||||
|
||||
xrayClient["level"] = level
|
||||
|
||||
xrayClients = append(xrayClients, xrayClient)
|
||||
}
|
||||
|
||||
// 把纯净的 clients 应用到 settings,并写入 inboundConfig.Settings
|
||||
settings["clients"] = xrayClients
|
||||
finalSettingsForXray, err := json.Marshal(settings)
|
||||
if err != nil {
|
||||
logger.Warningf("无法序列化用于Xray的入站设置 in GetXrayConfig for inbound %d: %v,跳过该入站", inbound.Id, err)
|
||||
continue
|
||||
}
|
||||
inboundConfig.Settings = json_util.RawMessage(finalSettingsForXray)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// 中文注释: 处理 StreamSettings(清理敏感字段)
|
||||
// -----------------------------------------------------------------
|
||||
if len(inbound.StreamSettings) > 0 {
|
||||
var stream map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(inbound.StreamSettings), &stream); err != nil {
|
||||
logger.Warningf("无法解析 StreamSettings (inbound %d): %v ,跳过该入站", inbound.Id, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if tlsSettings, ok := stream["tlsSettings"].(map[string]interface{}); ok {
|
||||
delete(tlsSettings, "settings")
|
||||
}
|
||||
if realitySettings, ok := stream["realitySettings"].(map[string]interface{}); ok {
|
||||
delete(realitySettings, "settings")
|
||||
}
|
||||
delete(stream, "externalProxy")
|
||||
|
||||
newStream, err := json.Marshal(stream)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inboundConfig.StreamSettings = json_util.RawMessage(newStream)
|
||||
}
|
||||
|
||||
xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
|
||||
}
|
||||
|
||||
return xrayConfig, nil
|
||||
}
|
||||
|
||||
|
||||
func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, error) {
|
||||
if !s.IsXrayRunning() {
|
||||
err := errors.New("xray is not running")
|
||||
logger.Debug("Attempted to fetch Xray traffic, but Xray is not running:", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
apiPort := p.GetAPIPort()
|
||||
s.xrayAPI.Init(apiPort)
|
||||
defer s.xrayAPI.Close()
|
||||
|
||||
traffic, clientTraffic, err := s.xrayAPI.GetTraffic(true)
|
||||
if err != nil {
|
||||
logger.Debug("Failed to fetch Xray traffic:", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
return traffic, clientTraffic, nil
|
||||
}
|
||||
|
||||
func (s *XrayService) RestartXray(isForce bool) error {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
logger.Debug("restart Xray, force:", isForce)
|
||||
isManuallyStopped.Store(false)
|
||||
|
||||
xrayConfig, err := s.GetXrayConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 【新功能】重启时,将完整配置打印到 Debug 日志以供验证
|
||||
configBytes, jsonErr := json.MarshalIndent(xrayConfig, "", " ")
|
||||
if jsonErr == nil {
|
||||
logger.Debugf("使用新配置重启 Xray:\n%s", string(configBytes))
|
||||
} else {
|
||||
logger.Warning("无法将 Xray 配置编组以进行日志记录:", jsonErr)
|
||||
}
|
||||
|
||||
|
||||
if s.IsXrayRunning() {
|
||||
if !isForce && p.GetConfig().Equals(xrayConfig) && !isNeedXrayRestart.Load() {
|
||||
logger.Debug("It does not need to restart Xray")
|
||||
return nil
|
||||
}
|
||||
p.Stop()
|
||||
}
|
||||
|
||||
p = xray.NewProcess(xrayConfig)
|
||||
result = ""
|
||||
err = p.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *XrayService) StopXray() error {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
isManuallyStopped.Store(true)
|
||||
logger.Debug("Attempting to stop Xray...")
|
||||
if s.IsXrayRunning() {
|
||||
return p.Stop()
|
||||
}
|
||||
return errors.New("xray is not running")
|
||||
}
|
||||
|
||||
func (s *XrayService) SetToNeedRestart() {
|
||||
isNeedXrayRestart.Store(true)
|
||||
}
|
||||
|
||||
func (s *XrayService) IsNeedRestartAndSetFalse() bool {
|
||||
return isNeedXrayRestart.CompareAndSwap(true, false)
|
||||
}
|
||||
|
||||
// Check if Xray is not running and wasn't stopped manually, i.e. crashed
|
||||
func (s *XrayService) DidXrayCrash() bool {
|
||||
return !s.IsXrayRunning() && !isManuallyStopped.Load()
|
||||
}
|
||||
29
web/service/xray_setting.go
Normal file
29
web/service/xray_setting.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
|
||||
"x-ui/util/common"
|
||||
"x-ui/xray"
|
||||
)
|
||||
|
||||
type XraySettingService struct {
|
||||
SettingService
|
||||
}
|
||||
|
||||
func (s *XraySettingService) SaveXraySetting(newXraySettings string) error {
|
||||
if err := s.CheckXrayConfig(newXraySettings); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.SettingService.saveSetting("xrayTemplateConfig", newXraySettings)
|
||||
}
|
||||
|
||||
func (s *XraySettingService) CheckXrayConfig(XrayTemplateConfig string) error {
|
||||
xrayConfig := &xray.Config{}
|
||||
err := json.Unmarshal([]byte(XrayTemplateConfig), xrayConfig)
|
||||
if err != nil {
|
||||
return common.NewError("xray template config invalid:", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user