From 4b1c76e97252c5bcc7a9ee16b8bd0dde693cabbb Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 10 Oct 2024 17:32:50 +0200 Subject: [PATCH 1/2] [refactor] email verification method --- web/service/inbound.go | 40 ++-------------------------------------- 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/web/service/inbound.go b/web/service/inbound.go index 822a4e8d..1450dd57 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -2,9 +2,7 @@ package service import ( "encoding/json" - "errors" "fmt" - "regexp" "strconv" "strings" "time" @@ -101,8 +99,9 @@ func (s *InboundService) getAllEmails() ([]string, error) { } func (s *InboundService) contains(slice []string, str string) bool { + lowerStr := strings.ToLower(str) for _, s := range slice { - if s == str { + if strings.ToLower(s) == lowerStr { return true } } @@ -414,12 +413,6 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) (bool, error) { return false, err } - email := clients[0].Email - valid, err := validateEmail(email) - if !valid { - return false, err - } - var settings map[string]interface{} err = json.Unmarshal([]byte(data.Settings), &settings) if err != nil { @@ -610,12 +603,6 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId strin return false, err } - email := clients[0].Email - valid, err := validateEmail(email) - if !valid { - return false, err - } - var settings map[string]interface{} err = json.Unmarshal([]byte(data.Settings), &settings) if err != nil { @@ -2022,26 +2009,3 @@ func (s *InboundService) MigrateDB() { func (s *InboundService) GetOnlineClients() []string { return p.GetOnlineClients() } - -func validateEmail(email string) (bool, error) { - - if strings.Contains(email, " ") { - return false, errors.New("email contains spaces, please remove them") - } - - if email != strings.ToLower(email) { - return false, errors.New("email contains uppercase letters, please convert to lowercase") - } - - nonEnglishPattern := `[^\x00-\x7F]` - if regexp.MustCompile(nonEnglishPattern).MatchString(email) { - return false, errors.New("email contains non-English characters, please use only English") - } - - emailPattern := `^[a-z0-9@._-]+$` - if !regexp.MustCompile(emailPattern).MatchString(email) { - return false, errors.New("email contains invalid characters, please use only lowercase letters, digits, and @._-") - } - - return true, nil -} From 1454c4ebc53027df757f92e9d6d205a82b7db559 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 10 Oct 2024 17:37:06 +0200 Subject: [PATCH 2/2] [bug] fix restarting core on disabling depleted user --- web/service/inbound.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/web/service/inbound.go b/web/service/inbound.go index 1450dd57..60530143 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -1037,8 +1037,12 @@ func (s *InboundService) disableInvalidInbounds(tx *gorm.DB) (bool, int64, error if err1 == nil { logger.Debug("Inbound disabled by api:", tag) } else { - logger.Debug("Error in disabling inbound by api:", err1) - needRestart = true + if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", tag)) { + logger.Debug("User is already disabled. Nothing to do more...") + } else { + logger.Debug("Error in disabling client by api:", err1) + needRestart = true + } } } s.xrayApi.Close() @@ -1076,8 +1080,16 @@ func (s *InboundService) disableInvalidClients(tx *gorm.DB) (bool, int64, error) if err1 == nil { logger.Debug("Client disabled by api:", result.Email) } else { - logger.Debug("Error in disabling client by api:", err1) - needRestart = true + if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", result.Email)) { + logger.Debug("User is already disabled. Nothing to do more...") + } else { + if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", result.Email)) { + logger.Debug("User is already disabled. Nothing to do more...") + } else { + logger.Debug("Error in disabling client by api:", err1) + needRestart = true + } + } } } s.xrayApi.Close()