diff options
Diffstat (limited to 'web/service')
| -rw-r--r-- | web/service/inbound.go | 56 | ||||
| -rw-r--r-- | web/service/tgbot.go | 80 |
2 files changed, 91 insertions, 45 deletions
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) |
