From 6b23b416a788aedeacd75e445f8b1a919c2cf5c8 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sun, 17 Aug 2025 13:37:49 +0200 Subject: [PATCH] minor changes --- sub/subJsonService.go | 5 ++- web/entity/entity.go | 6 +-- web/job/check_client_ip_job.go | 11 ++--- web/service/inbound.go | 56 ++++++++++++++---------- web/service/tgbot.go | 80 +++++++++++++++++++++++++--------- 5 files changed, 100 insertions(+), 58 deletions(-) diff --git a/sub/subJsonService.go b/sub/subJsonService.go index 7bc4d1db..680a01c0 100644 --- a/sub/subJsonService.go +++ b/sub/subJsonService.go @@ -209,9 +209,10 @@ func (s *SubJsonService) streamData(stream string) map[string]any { var streamSettings map[string]any json.Unmarshal([]byte(stream), &streamSettings) security, _ := streamSettings["security"].(string) - if security == "tls" { + switch security { + case "tls": streamSettings["tlsSettings"] = s.tlsData(streamSettings["tlsSettings"].(map[string]any)) - } else if security == "reality" { + case "reality": streamSettings["realitySettings"] = s.realityData(streamSettings["realitySettings"].(map[string]any)) } delete(streamSettings, "sockopt") diff --git a/web/entity/entity.go b/web/entity/entity.go index 889c9024..844a7ce0 100644 --- a/web/entity/entity.go +++ b/web/entity/entity.go @@ -2,10 +2,10 @@ package entity import ( "crypto/tls" + "math" "net" "strings" "time" - "math" "x-ui/util/common" ) @@ -39,8 +39,8 @@ type AllSetting struct { TgCpu int `json:"tgCpu" form:"tgCpu"` TgLang string `json:"tgLang" form:"tgLang"` TimeLocation string `json:"timeLocation" form:"timeLocation"` - TwoFactorEnable bool `json:"twoFactorEnable" form:"twoFactorEnable"` - TwoFactorToken string `json:"twoFactorToken" form:"twoFactorToken"` + TwoFactorEnable bool `json:"twoFactorEnable" form:"twoFactorEnable"` + TwoFactorToken string `json:"twoFactorToken" form:"twoFactorToken"` SubEnable bool `json:"subEnable" form:"subEnable"` SubTitle string `json:"subTitle" form:"subTitle"` SubListen string `json:"subListen" form:"subListen"` diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go index b95c8ee2..5a30b616 100644 --- a/web/job/check_client_ip_job.go +++ b/web/job/check_client_ip_job.go @@ -11,7 +11,6 @@ import ( "sort" "time" - "slices" "x-ui/database" "x-ui/database/model" "x-ui/logger" @@ -58,21 +57,21 @@ func (j *CheckClientIpJob) Run() { func (j *CheckClientIpJob) clearAccessLog() { logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) j.checkError(err) + defer logAccessP.Close() accessLogPath, err := xray.GetAccessLogPath() j.checkError(err) file, err := os.Open(accessLogPath) j.checkError(err) + defer file.Close() _, err = io.Copy(logAccessP, file) j.checkError(err) - logAccessP.Close() - file.Close() - err = os.Truncate(accessLogPath, 0) j.checkError(err) + j.lastClear = time.Now().Unix() } @@ -193,10 +192,6 @@ func (j *CheckClientIpJob) checkError(e error) { } } -func (j *CheckClientIpJob) contains(s []string, str string) bool { - return slices.Contains(s, str) -} - func (j *CheckClientIpJob) getInboundClientIps(clientEmail string) (*model.InboundClientIps, error) { db := database.GetDB() InboundClientIps := &model.InboundClientIps{} diff --git a/web/service/inbound.go b/web/service/inbound.go index 66e1a420..6e10e798 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -177,15 +177,16 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, boo // Secure client ID for _, client := range clients { - if inbound.Protocol == "trojan" { + switch inbound.Protocol { + case "trojan": if client.Password == "" { return inbound, false, common.NewError("empty client ID") } - } else if inbound.Protocol == "shadowsocks" { + case "shadowsocks": if client.Email == "" { return inbound, false, common.NewError("empty client ID") } - } else { + default: if client.ID == "" { return inbound, false, common.NewError("empty client ID") } @@ -436,15 +437,16 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) (bool, error) { // Secure client ID for _, client := range clients { - if oldInbound.Protocol == "trojan" { + switch oldInbound.Protocol { + case "trojan": if client.Password == "" { return false, common.NewError("empty client ID") } - } else if oldInbound.Protocol == "shadowsocks" { + case "shadowsocks": if client.Email == "" { return false, common.NewError("empty client ID") } - } else { + default: if client.ID == "" { return false, common.NewError("empty client ID") } @@ -631,13 +633,14 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId strin clientIndex := -1 for index, oldClient := range oldClients { oldClientId := "" - if oldInbound.Protocol == "trojan" { + switch oldInbound.Protocol { + case "trojan": oldClientId = oldClient.Password newClientId = clients[0].Password - } else if oldInbound.Protocol == "shadowsocks" { + case "shadowsocks": oldClientId = oldClient.Email newClientId = clients[0].Email - } else { + default: oldClientId = oldClient.ID newClientId = clients[0].ID } @@ -1244,11 +1247,12 @@ func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId int64) (boo for _, oldClient := range oldClients { if oldClient.Email == clientEmail { - if inbound.Protocol == "trojan" { + switch inbound.Protocol { + case "trojan": clientId = oldClient.Password - } else if inbound.Protocol == "shadowsocks" { + case "shadowsocks": clientId = oldClient.Email - } else { + default: clientId = oldClient.ID } break @@ -1328,11 +1332,12 @@ func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, bo for _, oldClient := range oldClients { if oldClient.Email == clientEmail { - if inbound.Protocol == "trojan" { + switch inbound.Protocol { + case "trojan": clientId = oldClient.Password - } else if inbound.Protocol == "shadowsocks" { + case "shadowsocks": clientId = oldClient.Email - } else { + default: clientId = oldClient.ID } clientOldEnabled = oldClient.Enable @@ -1391,11 +1396,12 @@ func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int for _, oldClient := range oldClients { if oldClient.Email == clientEmail { - if inbound.Protocol == "trojan" { + switch inbound.Protocol { + case "trojan": clientId = oldClient.Password - } else if inbound.Protocol == "shadowsocks" { + case "shadowsocks": clientId = oldClient.Email - } else { + default: clientId = oldClient.ID } break @@ -1448,11 +1454,12 @@ func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry for _, oldClient := range oldClients { if oldClient.Email == clientEmail { - if inbound.Protocol == "trojan" { + switch inbound.Protocol { + case "trojan": clientId = oldClient.Password - } else if inbound.Protocol == "shadowsocks" { + case "shadowsocks": clientId = oldClient.Email - } else { + default: clientId = oldClient.ID } break @@ -1508,11 +1515,12 @@ func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, tota for _, oldClient := range oldClients { if oldClient.Email == clientEmail { - if inbound.Protocol == "trojan" { + switch inbound.Protocol { + case "trojan": clientId = oldClient.Password - } else if inbound.Protocol == "shadowsocks" { + case "shadowsocks": clientId = oldClient.Email - } else { + default: clientId = oldClient.ID } break diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 301f64fd..6fdb4add 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -40,7 +40,6 @@ var ( isRunning bool hostname string hashStorage *global.HashStorage - handler *th.Handler // clients data to adding new client receiver_inbound_ID int @@ -641,13 +640,14 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool if len(dataArray) == 4 { num, err := strconv.Atoi(dataArray[3]) if err == nil { - if num == -2 { + switch num { + case -2: inputNumber = 0 - } else if num == -1 { + case -1: if inputNumber > 0 { inputNumber = (inputNumber / 10) } - } else { + default: inputNumber = (inputNumber * 10) + num } } @@ -704,6 +704,10 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool return } message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation")) @@ -715,13 +719,14 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool if len(dataArray) == 3 { num, err := strconv.Atoi(dataArray[2]) if err == nil { - if num == -2 { + switch num { + case -2: inputNumber = 0 - } else if num == -1 { + case -1: if inputNumber > 0 { inputNumber = (inputNumber / 10) } - } else { + default: inputNumber = (inputNumber * 10) + num } } @@ -844,13 +849,14 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool if len(dataArray) == 4 { num, err := strconv.Atoi(dataArray[3]) if err == nil { - if num == -2 { + switch num { + case -2: inputNumber = 0 - } else if num == -1 { + case -1: if inputNumber > 0 { inputNumber = (inputNumber / 10) } - } else { + default: inputNumber = (inputNumber * 10) + num } } @@ -919,6 +925,10 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool return } message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation")) @@ -930,13 +940,14 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool if len(dataArray) == 3 { num, err := strconv.Atoi(dataArray[2]) if err == nil { - if num == -2 { + switch num { + case -2: inputNumber = 0 - } else if num == -1 { + case -1: if inputNumber > 0 { inputNumber = (inputNumber / 10) } - } else { + default: inputNumber = (inputNumber * 10) + num } } @@ -1035,13 +1046,14 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool if len(dataArray) == 4 { num, err := strconv.Atoi(dataArray[3]) if err == nil { - if num == -2 { + switch num { + case -2: inputNumber = 0 - } else if num == -1 { + case -1: if inputNumber > 0 { inputNumber = (inputNumber / 10) } - } else { + default: inputNumber = (inputNumber * 10) + num } } @@ -1101,6 +1113,10 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool return } message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } t.addClient(callbackQuery.Message.GetChat().ID, message_text, messageId) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation")) @@ -1112,13 +1128,14 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool if len(dataArray) == 3 { num, err := strconv.Atoi(dataArray[2]) if err == nil { - if num == -2 { + switch num { + case -2: inputNumber = 0 - } else if num == -1 { + case -1: if inputNumber > 0 { inputNumber = (inputNumber / 10) } - } else { + default: inputNumber = (inputNumber * 10) + num } } @@ -1288,6 +1305,10 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool } message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } t.addClient(callbackQuery.Message.GetChat().ID, message_text) } @@ -1524,6 +1545,10 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool return } message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } t.addClient(chatId, message_text, messageId) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+client_Email)) case "add_client_default_ip_limit": @@ -1534,6 +1559,10 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool return } message_text, err := t.BuildInboundClientDataMessage(inbound.Remark, inbound.Protocol) + if err != nil { + t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error()) + return + } t.addClient(chatId, message_text, messageId) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+client_Email)) case "add_client_submit_disable": @@ -1598,6 +1627,10 @@ func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool return } valid_emails, extra_emails, err := t.inboundService.FilterAndSortClientEmails(emails) + if err != nil { + t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.errorOperation"), tu.ReplyKeyboardRemove()) + return + } for _, valid_emails := range valid_emails { traffic, err := t.inboundService.GetClientTrafficByEmail(valid_emails) @@ -1760,6 +1793,10 @@ func (t *Tgbot) SubmitAddClient() (bool, error) { } jsonString, err := t.BuildJSONForProtocol(inbound.Protocol) + if err != nil { + logger.Warning("BuildJSONForProtocol run failed:", err) + return false, errors.New("failed to build JSON for protocol") + } newInbound := &model.Inbound{ Id: receiver_inbound_ID, @@ -2008,10 +2045,11 @@ func (t *Tgbot) UserLoginNotify(username string, password string, ip string, tim } msg := "" - if status == LoginSuccess { + switch status { + case LoginSuccess: msg += t.I18nBot("tgbot.messages.loginSuccess") msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname) - } else if status == LoginFail { + case LoginFail: msg += t.I18nBot("tgbot.messages.loginFailed") msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname) msg += t.I18nBot("tgbot.messages.password", "Password=="+password)