refactor: 重构服务层和仓库层
This commit is contained in:
81
internal/service/yggdrasil_validator.go
Normal file
81
internal/service/yggdrasil_validator.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Validator Yggdrasil验证器
|
||||
type Validator struct{}
|
||||
|
||||
// NewValidator 创建验证器实例
|
||||
func NewValidator() *Validator {
|
||||
return &Validator{}
|
||||
}
|
||||
|
||||
var (
|
||||
// emailRegex 邮箱正则表达式
|
||||
emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
|
||||
)
|
||||
|
||||
// ValidateServerID 验证服务器ID格式
|
||||
func (v *Validator) ValidateServerID(serverID string) error {
|
||||
if serverID == "" {
|
||||
return errors.New("服务器ID不能为空")
|
||||
}
|
||||
if len(serverID) > 100 {
|
||||
return errors.New("服务器ID长度超过限制(最大100字符)")
|
||||
}
|
||||
// 防止注入攻击:检查危险字符
|
||||
if strings.ContainsAny(serverID, "<>\"'&") {
|
||||
return errors.New("服务器ID包含非法字符")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateIP 验证IP地址格式
|
||||
func (v *Validator) ValidateIP(ip string) error {
|
||||
if ip == "" {
|
||||
return nil // IP是可选的
|
||||
}
|
||||
if net.ParseIP(ip) == nil {
|
||||
return errors.New("IP地址格式无效")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateEmail 验证邮箱格式
|
||||
func (v *Validator) ValidateEmail(email string) error {
|
||||
if email == "" {
|
||||
return errors.New("邮箱不能为空")
|
||||
}
|
||||
if !emailRegex.MatchString(email) {
|
||||
return errors.New("邮箱格式不正确")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateUUID 验证UUID格式(简单验证)
|
||||
func (v *Validator) ValidateUUID(uuid string) error {
|
||||
if uuid == "" {
|
||||
return errors.New("UUID不能为空")
|
||||
}
|
||||
// UUID格式:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (32个十六进制字符 + 4个连字符)
|
||||
if len(uuid) < 32 || len(uuid) > 36 {
|
||||
return errors.New("UUID格式无效")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateAccessToken 验证访问令牌
|
||||
func (v *Validator) ValidateAccessToken(token string) error {
|
||||
if token == "" {
|
||||
return errors.New("访问令牌不能为空")
|
||||
}
|
||||
if len(token) < 10 {
|
||||
return errors.New("访问令牌格式无效")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user