utils

package
v2.6.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2025 License: MIT Imports: 12 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyStructByJson

func CopyStructByJson(src, dst interface{})

func CopyStructToMap

func CopyStructToMap(src interface{}) map[string]interface{}

CopyStructToMap 结构体转map

func EncryptPassword added in v2.6.24

func EncryptPassword(password string) (string, error)

EncryptPassword hashes the input password using bcrypt. An error is returned if hashing fails.

func InArray

func InArray(k string, arr []string) bool

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys 泛型函数,K 是键类型,V 是值类型

func Md5

func Md5(str string) string

func RandomString

func RandomString(n int) string

RandomString 生成随机字符串

func SafeGo

func SafeGo(f interface{}, params ...interface{})

SafeGo is a common function to recover panic for goroutines

func StringConcat added in v2.6.16

func StringConcat(strs ...string) string

func Values

func Values[K comparable, V any](m map[K]V) []V

Values 泛型函数,K 是键类型,V 是值类型

func VerifyPassword added in v2.6.24

func VerifyPassword(hash, input string) (bool, string, error)

VerifyPassword checks the input password against the stored hash. When a legacy MD5 hash is provided, the password is rehashed with bcrypt and the new hash is returned. Any internal bcrypt error is returned.

Types

type B64MathCaptchaProvider added in v2.6.21

type B64MathCaptchaProvider struct{}

func (B64MathCaptchaProvider) Draw added in v2.6.21

func (p B64MathCaptchaProvider) Draw(content string) (string, error)

func (B64MathCaptchaProvider) Expiration added in v2.6.21

func (p B64MathCaptchaProvider) Expiration() time.Duration

func (B64MathCaptchaProvider) Generate added in v2.6.21

func (p B64MathCaptchaProvider) Generate() (string, string, string, error)

type B64StringCaptchaProvider added in v2.6.21

type B64StringCaptchaProvider struct{}

func (B64StringCaptchaProvider) Draw added in v2.6.21

func (p B64StringCaptchaProvider) Draw(content string) (string, error)

func (B64StringCaptchaProvider) Expiration added in v2.6.21

func (p B64StringCaptchaProvider) Expiration() time.Duration

func (B64StringCaptchaProvider) Generate added in v2.6.21

type BanRecord added in v2.6.21

type BanRecord struct {
	ExpiresAt time.Time
	Reason    string
}

IP封禁记录

type CaptchaMeta added in v2.6.21

type CaptchaMeta struct {
	Id        string
	Content   string
	Answer    string
	ExpiresAt time.Time
}

验证码元数据

type CaptchaProvider added in v2.6.21

type CaptchaProvider interface {
	Generate() (id string, content string, answer string, err error)
	//Validate(ip, code string) bool
	Expiration() time.Duration           // 验证码过期时间, 应该小于 AttemptsWindow
	Draw(content string) (string, error) // 绘制验证码
}

验证码提供者接口

type LoginLimiter added in v2.6.21

type LoginLimiter struct {
	// contains filtered or unexported fields
}

登录限制器

func NewLoginLimiter added in v2.6.21

func NewLoginLimiter(policy SecurityPolicy) *LoginLimiter

func (*LoginLimiter) CheckSecurityStatus added in v2.6.21

func (ll *LoginLimiter) CheckSecurityStatus(ip string) (banned bool, captchaRequired bool)

CheckSecurityStatus 检查安全状态

func (*LoginLimiter) DrawCaptcha added in v2.6.21

func (ll *LoginLimiter) DrawCaptcha(content string) (err error, str string)

func (*LoginLimiter) RecordFailedAttempt added in v2.6.21

func (ll *LoginLimiter) RecordFailedAttempt(ip string)

记录登录失败尝试

func (*LoginLimiter) RegisterProvider added in v2.6.21

func (ll *LoginLimiter) RegisterProvider(p CaptchaProvider)

注册验证码提供者

func (*LoginLimiter) RemoveAttempts added in v2.6.21

func (ll *LoginLimiter) RemoveAttempts(ip string)

清除记录窗口

func (*LoginLimiter) RequireCaptcha added in v2.6.21

func (ll *LoginLimiter) RequireCaptcha() (error, CaptchaMeta)

生成验证码

func (*LoginLimiter) VerifyCaptcha added in v2.6.21

func (ll *LoginLimiter) VerifyCaptcha(id, answer string) bool

验证验证码

type SecurityPolicy added in v2.6.21

type SecurityPolicy struct {
	CaptchaThreshold int // 尝试失败次数达到验证码阈值,小于0表示不启用, 0表示强制启用
	BanThreshold     int // 尝试失败次数达到封禁阈值,为0表示不启用
	AttemptsWindow   time.Duration
	BanDuration      time.Duration
}

安全策略配置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL