Skip to content

Commit

Permalink
Fixed unnecessary Xray restarts in Tgbot #2076
Browse files Browse the repository at this point in the history
Fixed unnecessary Xray restarts in Tgbot
  • Loading branch information
somebodywashere committed Mar 15, 2024
2 parents 2b420bd + abd69a2 commit a7418d9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 58 deletions.
94 changes: 47 additions & 47 deletions web/service/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,20 +1152,20 @@ func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraff
return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
}

func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId string) error {
func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId string) (bool, error) {
traffic, inbound, err := s.GetClientInboundByTrafficID(trafficId)
if err != nil {
return err
return false, err
}
if inbound == nil {
return common.NewError("Inbound Not Found For Traffic ID:", trafficId)
return false, common.NewError("Inbound Not Found For Traffic ID:", trafficId)
}

clientEmail := traffic.Email

oldClients, err := s.GetClients(inbound)
if err != nil {
return err
return false, err
}

clientId := ""
Expand All @@ -1184,13 +1184,13 @@ func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId string) err
}

if len(clientId) == 0 {
return common.NewError("Client Not Found For Email:", clientEmail)
return false, common.NewError("Client Not Found For Email:", clientEmail)
}

var settings map[string]interface{}
err = json.Unmarshal([]byte(inbound.Settings), &settings)
if err != nil {
return err
return false, err
}
clients := settings["clients"].([]interface{})
var newClients []interface{}
Expand All @@ -1204,11 +1204,11 @@ func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId string) err
settings["clients"] = newClients
modifiedSettings, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
return false, err
}
inbound.Settings = string(modifiedSettings)
_, err = s.UpdateInboundClient(inbound, clientId)
return err
needRestart, err := s.UpdateInboundClient(inbound, clientId)
return needRestart, err
}

func (s *InboundService) checkIsEnabledByEmail(clientEmail string) (bool, error) {
Expand Down Expand Up @@ -1237,18 +1237,18 @@ func (s *InboundService) checkIsEnabledByEmail(clientEmail string) (bool, error)
return isEnable, err
}

func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, error) {
func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, bool, error) {
_, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil {
return false, err
return false, false, err
}
if inbound == nil {
return false, common.NewError("Inbound Not Found For Email:", clientEmail)
return false, false, common.NewError("Inbound Not Found For Email:", clientEmail)
}

oldClients, err := s.GetClients(inbound)
if err != nil {
return false, err
return false, false, err
}

clientId := ""
Expand All @@ -1269,13 +1269,13 @@ func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, er
}

if len(clientId) == 0 {
return false, common.NewError("Client Not Found For Email:", clientEmail)
return false, false, common.NewError("Client Not Found For Email:", clientEmail)
}

var settings map[string]interface{}
err = json.Unmarshal([]byte(inbound.Settings), &settings)
if err != nil {
return false, err
return false, false, err
}
clients := settings["clients"].([]interface{})
var newClients []interface{}
Expand All @@ -1289,30 +1289,30 @@ func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, er
settings["clients"] = newClients
modifiedSettings, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return false, err
return false, false, err
}
inbound.Settings = string(modifiedSettings)

_, err = s.UpdateInboundClient(inbound, clientId)
needRestart, err := s.UpdateInboundClient(inbound, clientId)
if err != nil {
return false, err
return false, needRestart, err
}

return !clientOldEnabled, nil
return !clientOldEnabled, needRestart, nil
}

func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) error {
func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) (bool, error) {
_, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil {
return err
return false, err
}
if inbound == nil {
return common.NewError("Inbound Not Found For Email:", clientEmail)
return false, common.NewError("Inbound Not Found For Email:", clientEmail)
}

oldClients, err := s.GetClients(inbound)
if err != nil {
return err
return false, err
}

clientId := ""
Expand All @@ -1331,13 +1331,13 @@ func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int
}

if len(clientId) == 0 {
return common.NewError("Client Not Found For Email:", clientEmail)
return false, common.NewError("Client Not Found For Email:", clientEmail)
}

var settings map[string]interface{}
err = json.Unmarshal([]byte(inbound.Settings), &settings)
if err != nil {
return err
return false, err
}
clients := settings["clients"].([]interface{})
var newClients []interface{}
Expand All @@ -1351,25 +1351,25 @@ func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int
settings["clients"] = newClients
modifiedSettings, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
return false, err
}
inbound.Settings = string(modifiedSettings)
_, err = s.UpdateInboundClient(inbound, clientId)
return err
needRestart, err := s.UpdateInboundClient(inbound, clientId)
return needRestart, err
}

func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) error {
func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) (bool, error) {
_, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil {
return err
return false, err
}
if inbound == nil {
return common.NewError("Inbound Not Found For Email:", clientEmail)
return false, common.NewError("Inbound Not Found For Email:", clientEmail)
}

oldClients, err := s.GetClients(inbound)
if err != nil {
return err
return false, err
}

clientId := ""
Expand All @@ -1388,13 +1388,13 @@ func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry
}

if len(clientId) == 0 {
return common.NewError("Client Not Found For Email:", clientEmail)
return false, common.NewError("Client Not Found For Email:", clientEmail)
}

var settings map[string]interface{}
err = json.Unmarshal([]byte(inbound.Settings), &settings)
if err != nil {
return err
return false, err
}
clients := settings["clients"].([]interface{})
var newClients []interface{}
Expand All @@ -1408,28 +1408,28 @@ func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry
settings["clients"] = newClients
modifiedSettings, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
return false, err
}
inbound.Settings = string(modifiedSettings)
_, err = s.UpdateInboundClient(inbound, clientId)
return err
needRestart, err := s.UpdateInboundClient(inbound, clientId)
return needRestart, err
}

func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, totalGB int) error {
func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, totalGB int) (bool, error) {
if totalGB < 0 {
return common.NewError("totalGB must be >= 0")
return false, common.NewError("totalGB must be >= 0")
}
_, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil {
return err
return false, err
}
if inbound == nil {
return common.NewError("Inbound Not Found For Email:", clientEmail)
return false, common.NewError("Inbound Not Found For Email:", clientEmail)
}

oldClients, err := s.GetClients(inbound)
if err != nil {
return err
return false, err
}

clientId := ""
Expand All @@ -1448,13 +1448,13 @@ func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, tota
}

if len(clientId) == 0 {
return common.NewError("Client Not Found For Email:", clientEmail)
return false, common.NewError("Client Not Found For Email:", clientEmail)
}

var settings map[string]interface{}
err = json.Unmarshal([]byte(inbound.Settings), &settings)
if err != nil {
return err
return false, err
}
clients := settings["clients"].([]interface{})
var newClients []interface{}
Expand All @@ -1468,11 +1468,11 @@ func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, tota
settings["clients"] = newClients
modifiedSettings, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
return false, err
}
inbound.Settings = string(modifiedSettings)
_, err = s.UpdateInboundClient(inbound, clientId)
return err
needRestart, err := s.UpdateInboundClient(inbound, clientId)
return needRestart, err
}

func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
Expand Down
35 changes: 24 additions & 11 deletions web/service/tgbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ func (t *Tgbot) OnReceive() {
for _, userID := range message.UsersShared.UserIDs {
userIDsStr += strconv.FormatInt(userID, 10) + " "
}
err := t.inboundService.SetClientTelegramUserID(message.UsersShared.RequestID, userIDsStr)
needRestart, err := t.inboundService.SetClientTelegramUserID(message.UsersShared.RequestID, userIDsStr)
if needRestart {
t.xrayService.SetToNeedRestart()
}
output := ""
if err != nil {
output += t.I18nBot("tgbot.messages.selectUserFailed")
Expand Down Expand Up @@ -331,7 +334,6 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
case "reset_traffic_c":
err := t.inboundService.ResetClientTrafficByEmail(email)
if err == nil {
t.xrayService.SetToNeedRestart()
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetTrafficSuccess", "Email=="+email))
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
} else {
Expand Down Expand Up @@ -372,9 +374,11 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
if len(dataArray) == 3 {
limitTraffic, err := strconv.Atoi(dataArray[2])
if err == nil {
err := t.inboundService.ResetClientTrafficLimitByEmail(email, limitTraffic)
if err == nil {
needRestart, err := t.inboundService.ResetClientTrafficLimitByEmail(email, limitTraffic)
if needRestart {
t.xrayService.SetToNeedRestart()
}
if err == nil {
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.setTrafficLimitSuccess", "Email=="+email))
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
return
Expand Down Expand Up @@ -501,9 +505,11 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
}

}
err := t.inboundService.ResetClientExpiryTimeByEmail(email, date)
if err == nil {
needRestart, err := t.inboundService.ResetClientExpiryTimeByEmail(email, date)
if needRestart {
t.xrayService.SetToNeedRestart()
}
if err == nil {
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.expireResetSuccess", "Email=="+email))
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
return
Expand Down Expand Up @@ -606,9 +612,11 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
if len(dataArray) == 3 {
count, err := strconv.Atoi(dataArray[2])
if err == nil {
err := t.inboundService.ResetClientIpLimitByEmail(email, count)
if err == nil {
needRestart, err := t.inboundService.ResetClientIpLimitByEmail(email, count)
if needRestart {
t.xrayService.SetToNeedRestart()
}
if err == nil {
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetIpSuccess", "Email=="+email, "Count=="+strconv.Itoa(count)))
t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
return
Expand Down Expand Up @@ -718,7 +726,10 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
return
}
err = t.inboundService.SetClientTelegramUserID(traffic.Id, "")
needRestart, err := t.inboundService.SetClientTelegramUserID(traffic.Id, "")
if needRestart {
t.xrayService.SetToNeedRestart()
}
if err == nil {
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.removedTGUserSuccess", "Email=="+email))
t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
Expand All @@ -736,9 +747,11 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
)
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
case "toggle_enable_c":
enabled, err := t.inboundService.ToggleClientEnableByEmail(email)
if err == nil {
enabled, needRestart, err := t.inboundService.ToggleClientEnableByEmail(email)
if needRestart {
t.xrayService.SetToNeedRestart()
}
if err == nil {
if enabled {
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.enableSuccess", "Email=="+email))
} else {
Expand Down

0 comments on commit a7418d9

Please sign in to comment.