Browse Source

Update Email Validation

mhsanaei 2 tuần trước cách đây
mục cha
commit
b4baf35ed8
1 tập tin đã thay đổi với 31 bổ sung0 xóa
  1. 31 0
      web/service/inbound.go

+ 31 - 0
web/service/inbound.go

@@ -2,7 +2,9 @@ package service
 
 import (
 	"encoding/json"
+	"errors"
 	"fmt"
+	"regexp"
 	"strconv"
 	"strings"
 	"time"
@@ -411,6 +413,12 @@ 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 {
@@ -601,6 +609,12 @@ 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 {
@@ -2007,3 +2021,20 @@ 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")
+	}
+
+	emailPattern := `^[a-z0-9._-]+$`
+	if !regexp.MustCompile(emailPattern).MatchString(email) {
+		return false, errors.New("email contains invalid characters, please use only lowercase letters, digits, dots, dashes, and underscores")
+	}
+
+	return true, nil
+}