Fixed unnecessary Xray restarts in Tgbot

This commit is contained in:
somebodywashere 2024-03-15 21:13:20 +03:00
parent 2b420bd517
commit abd69a253a
2 changed files with 76 additions and 63 deletions

View File

@ -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 := ""
@ -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{}
@ -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) {
@ -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 := ""
@ -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{}
@ -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, needRestart, err
}
return !clientOldEnabled, needRestart, nil
}
func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) (bool, error) {
_, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil {
return false, err
}
return !clientOldEnabled, nil
}
func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) error {
_, inbound, err := s.GetClientInboundByEmail(clientEmail)
if err != nil {
return 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 := ""
@ -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{}
@ -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 := ""
@ -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{}
@ -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 := ""
@ -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{}
@ -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 {

View File

@ -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")
@ -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 {
@ -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
@ -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
@ -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
@ -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())
@ -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 {