package socks5 import "strings" // SimpleAuthHandler 简单认证处理器 type SimpleAuthHandler struct { username string password string methods []byte } // NewAuthHandler 创建认证处理器 func NewAuthHandler(config AuthConfig) AuthHandler { handler := &SimpleAuthHandler{ username: config.Username, password: config.Password, } // 设置支持的认证方法 if config.Username != "" && config.Password != "" { handler.methods = []byte{AuthPassword} } else { handler.methods = []byte{AuthNone} } // 如果配置中指定了方法,使用配置的方法 if len(config.Methods) > 0 { handler.methods = []byte{} for _, method := range config.Methods { switch strings.ToLower(method) { case "none": handler.methods = append(handler.methods, AuthNone) case "password": handler.methods = append(handler.methods, AuthPassword) } } } return handler } // Authenticate 验证用户名和密码 func (h *SimpleAuthHandler) Authenticate(username, password string) bool { // 如果支持无认证,直接返回true for _, method := range h.methods { if method == AuthNone { return true } } // 检查用户名密码 return h.username == username && h.password == password } // Methods 返回支持的认证方法 func (h *SimpleAuthHandler) Methods() []byte { return h.methods }