driver

package
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Challenge

type Challenge struct {
	ID      string // 验证码 ID
	Payload string // 图片 base64 或行为验证码 JSON(base64)
	Answer  string // 服务端答案,不对外返回
}

Challenge 表示驱动内部生成结果,Answer 只允许写入服务端缓存。

type ChineseConfig

type ChineseConfig struct {
	Height       int    `json:"height"`        // 图片高度
	Width        int    `json:"width"`         // 图片宽度
	CaptchaCount int    `json:"captcha_count"` // 验证码字符数量
	DotCount     int    `json:"dot_count"`     // 干扰点数量
	Source       string `json:"source"`        // 中文字符源,支持逗号分隔
	CaptchaLen   int    `json:"captcha_len"`   // 验证码长度(兼容字段)
}

ChineseConfig 中文验证码配置

func DefaultChineseConfig

func DefaultChineseConfig() *ChineseConfig

DefaultChineseConfig 默认中文配置

type ClickCaptchaData

type ClickCaptchaData struct {
	Type        string `json:"type"`        // 验证码类型
	Image       string `json:"image"`       // 主图 base64
	Thumb       string `json:"thumb"`       // 缩略图 base64
	Width       int    `json:"width"`       // 主图展示宽度
	Height      int    `json:"height"`      // 主图展示高度
	ThumbWidth  int    `json:"thumbWidth"`  // 缩略图展示宽度
	ThumbHeight int    `json:"thumbHeight"` // 缩略图展示高度
}

ClickCaptchaData 点击验证码数据结构。

type ClickConfig

type ClickConfig struct {
	MasterWidth   int    `json:"master_width"`    // 主图宽度
	MasterHeight  int    `json:"master_height"`   // 主图高度
	ThumbWidth    int    `json:"thumb_width"`     // 缩略图宽度
	ThumbHeight   int    `json:"thumb_height"`    // 缩略图高度
	CaptchaCount  int    `json:"captcha_count"`   // 主图字符数量
	VerifyCount   int    `json:"verify_count"`    // 验证字符数量
	DisplayShadow bool   `json:"display_shadow"`  // 是否显示阴影
	ShadowColor   string `json:"shadow_color"`    // 阴影颜色
	ShadowOffsetX int    `json:"shadow_offset_x"` // 阴影X偏移
	ShadowOffsetY int    `json:"shadow_offset_y"` // 阴影Y偏移
	Chars         string `json:"chars"`           // 字符集
	Language      string `json:"language"`        // 语言类型 zh, en
}

ClickConfig 点击文字验证码配置

func DefaultClickConfig

func DefaultClickConfig() *ClickConfig

DefaultClickConfig 默认点击文字配置

type Config

type Config struct {
	DriverType    DriverType     `json:"driver_type"`    // 驱动类型
	Expire        time.Duration  `json:"expire"`         // 过期时间
	KeyPrefix     string         `json:"key_prefix"`     // 缓存 key 前缀
	DigitConfig   *DigitConfig   `json:"digit_config"`   // 数字配置
	StringConfig  *StringConfig  `json:"string_config"`  // 字符串配置
	MathConfig    *MathConfig    `json:"math_config"`    // 算术配置
	ChineseConfig *ChineseConfig `json:"chinese_config"` // 中文配置
	SlideConfig   *SlideConfig   `json:"slide_config"`   // 滑动拼图配置
	ClickConfig   *ClickConfig   `json:"click_config"`   // 点击文字配置
	RotateConfig  *RotateConfig  `json:"rotate_config"`  // 旋转配置
}

Config 验证码总配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 默认总配置

type DigitConfig

type DigitConfig struct {
	Height       int     `json:"height"`        // 图片高度
	Width        int     `json:"width"`         // 图片宽度
	CaptchaCount int     `json:"captcha_count"` // 验证码字符数量
	MaxSkew      float64 `json:"max_skew"`      // 最大倾斜度
	DotCount     int     `json:"dot_count"`     // 干扰点数量
	BgColorR     uint8   `json:"bg_color_r"`    // 背景色R
	BgColorG     uint8   `json:"bg_color_g"`    // 背景色G
	BgColorB     uint8   `json:"bg_color_b"`    // 背景色B
	FontColorR   uint8   `json:"font_color_r"`  // 字体色R
	FontColorG   uint8   `json:"font_color_g"`  // 字体色G
	FontColorB   uint8   `json:"font_color_b"`  // 字体色B
	CaptchaLen   int     `json:"captcha_len"`   // 验证码长度(兼容字段)
}

DigitConfig 数字验证码配置

func DefaultDigitConfig

func DefaultDigitConfig() *DigitConfig

DefaultDigitConfig 默认数字配置

type Driver

type Driver interface {
	Generate() (*Challenge, error)
	Verify(expected, actual string) bool
}

Driver 定义单类验证码的生成与校验能力。

func New

func New(config *Config) Driver

New 根据配置创建对应的验证码驱动。

type DriverType

type DriverType string

DriverType 表示验证码驱动类型。

const (
	// DriverDigit 表示数字验证码。
	DriverDigit DriverType = "digit"
	// DriverString 表示字符串验证码。
	DriverString DriverType = "string"
	// DriverMath 表示算术验证码。
	DriverMath DriverType = "math"
	// DriverChinese 表示中文验证码。
	DriverChinese DriverType = "chinese"
	// DriverSlide 表示滑动拼图验证码。
	DriverSlide DriverType = "slide"
	// DriverClick 表示点击文字验证码。
	DriverClick DriverType = "click"
	// DriverRotate 表示旋转验证码。
	DriverRotate DriverType = "rotate"
)

type MathConfig

type MathConfig struct {
	Height       int     `json:"height"`        // 图片高度
	Width        int     `json:"width"`         // 图片宽度
	CaptchaCount int     `json:"captcha_count"` // 验证码字符数量
	MaxSkew      float64 `json:"max_skew"`      // 最大倾斜度
	DotCount     int     `json:"dot_count"`     // 干扰点数量
	BgColorR     uint8   `json:"bg_color_r"`    // 背景色R
	BgColorG     uint8   `json:"bg_color_g"`    // 背景色G
	BgColorB     uint8   `json:"bg_color_b"`    // 背景色B
	FontColorR   uint8   `json:"font_color_r"`  // 字体色R
	FontColorG   uint8   `json:"font_color_g"`  // 字体色G
	FontColorB   uint8   `json:"font_color_b"`  // 字体色B
	CaptchaLen   int     `json:"captcha_len"`   // 验证码长度(兼容字段)
}

MathConfig 算术验证码配置

func DefaultMathConfig

func DefaultMathConfig() *MathConfig

DefaultMathConfig 默认算术配置

type Option

type Option func(*Config)

Option 表示验证码配置选项函数。

func WithChineseChars

func WithChineseChars(chars []string) Option

WithChineseChars 设置中文验证码字符列表。

func WithChineseConfig

func WithChineseConfig(config *ChineseConfig) Option

WithChineseConfig 设置中文验证码配置

func WithChineseCount

func WithChineseCount(count int) Option

WithChineseCount 设置中文验证码字符数量

func WithChineseDotCount

func WithChineseDotCount(count int) Option

WithChineseDotCount 设置中文验证码干扰点数量

func WithChineseHeight

func WithChineseHeight(height int) Option

WithChineseHeight 设置中文验证码高度

func WithChineseSource

func WithChineseSource(source string) Option

WithChineseSource 设置中文验证码字符源

func WithChineseWidth

func WithChineseWidth(width int) Option

WithChineseWidth 设置中文验证码宽度

func WithClickCaptchaCount

func WithClickCaptchaCount(count int) Option

WithClickCaptchaCount 设置点击验证码主图字符数量

func WithClickChars

func WithClickChars(chars string) Option

WithClickChars 设置点击验证码字符集

func WithClickConfig

func WithClickConfig(config *ClickConfig) Option

WithClickConfig 设置点击文字验证码配置

func WithClickLanguage

func WithClickLanguage(language string) Option

WithClickLanguage 设置点击验证码语言

func WithClickMasterSize

func WithClickMasterSize(width, height int) Option

WithClickMasterSize 设置点击验证码主图尺寸

func WithClickShadow

func WithClickShadow(display bool, color string, offsetX, offsetY int) Option

WithClickShadow 设置点击验证码阴影效果

func WithClickThumbSize

func WithClickThumbSize(width, height int) Option

WithClickThumbSize 设置点击验证码缩略图尺寸

func WithClickVerifyCount

func WithClickVerifyCount(count int) Option

WithClickVerifyCount 设置点击验证码验证字符数量

func WithDigitConfig

func WithDigitConfig(config *DigitConfig) Option

WithDigitConfig 设置数字验证码配置

func WithDigitCount

func WithDigitCount(count int) Option

WithDigitCount 设置数字验证码字符数量

func WithDigitDotCount

func WithDigitDotCount(count int) Option

WithDigitDotCount 设置数字验证码干扰点数量

func WithDigitHeight

func WithDigitHeight(height int) Option

WithDigitHeight 设置数字验证码高度

func WithDigitMaxSkew

func WithDigitMaxSkew(skew float64) Option

WithDigitMaxSkew 设置数字验证码最大倾斜度

func WithDigitWidth

func WithDigitWidth(width int) Option

WithDigitWidth 设置数字验证码宽度

func WithDriverType

func WithDriverType(driverType DriverType) Option

WithDriverType 设置驱动类型

func WithExpire

func WithExpire(expire time.Duration) Option

WithExpire 设置过期时间

func WithKeyPrefix

func WithKeyPrefix(prefix string) Option

WithKeyPrefix 设置缓存 key 前缀

func WithMathConfig

func WithMathConfig(config *MathConfig) Option

WithMathConfig 设置算术验证码配置

func WithMathDotCount

func WithMathDotCount(count int) Option

WithMathDotCount 设置算术验证码干扰点数量

func WithMathHeight

func WithMathHeight(height int) Option

WithMathHeight 设置算术验证码高度

func WithMathWidth

func WithMathWidth(width int) Option

WithMathWidth 设置算术验证码宽度

func WithRotateConfig

func WithRotateConfig(config *RotateConfig) Option

WithRotateConfig 设置旋转验证码配置

func WithRotateMasterSize

func WithRotateMasterSize(width, height int) Option

WithRotateMasterSize 设置旋转验证码主图尺寸

func WithRotateThumbSize

func WithRotateThumbSize(width, height int) Option

WithRotateThumbSize 设置旋转验证码缩略图尺寸

func WithSlideConfig

func WithSlideConfig(config *SlideConfig) Option

WithSlideConfig 设置滑动拼图验证码配置

func WithSlideJigsawRadius

func WithSlideJigsawRadius(radius int) Option

WithSlideJigsawRadius 设置滑动拼图缺口圆角半径

func WithSlideMasterSize

func WithSlideMasterSize(width, height int) Option

WithSlideMasterSize 设置滑动拼图主图尺寸

func WithSlideShadow

func WithSlideShadow(offsetX, offsetY, blur int) Option

WithSlideShadow 设置滑动拼图阴影效果

func WithSlideTileRadius

func WithSlideTileRadius(radius int) Option

WithSlideTileRadius 设置滑动拼图滑块圆角半径

func WithSlideTileSize

func WithSlideTileSize(width, height int) Option

WithSlideTileSize 设置滑动拼图滑块尺寸

func WithStringConfig

func WithStringConfig(config *StringConfig) Option

WithStringConfig 设置字符串验证码配置

func WithStringCount

func WithStringCount(count int) Option

WithStringCount 设置字符串验证码字符数量

func WithStringDotCount

func WithStringDotCount(count int) Option

WithStringDotCount 设置字符串验证码干扰点数量

func WithStringHeight

func WithStringHeight(height int) Option

WithStringHeight 设置字符串验证码高度

func WithStringSource

func WithStringSource(source string) Option

WithStringSource 设置字符串验证码字符源

func WithStringWidth

func WithStringWidth(width int) Option

WithStringWidth 设置字符串验证码宽度

type RotateCaptchaData

type RotateCaptchaData struct {
	Type      string `json:"type"`      // 验证码类型
	Image     string `json:"image"`     // 主图 base64(需要旋转的图片)
	Thumb     string `json:"thumb"`     // 缩略图 base64(提示目标方向)
	Width     int    `json:"width"`     // 主图展示宽度
	Height    int    `json:"height"`    // 主图展示高度
	ThumbSize int    `json:"thumbSize"` // 内圈展示尺寸
}

RotateCaptchaData 旋转验证码数据结构。

type RotateConfig

type RotateConfig struct {
	MasterWidth  int `json:"master_width"`  // 主图宽度
	MasterHeight int `json:"master_height"` // 主图高度
	ThumbWidth   int `json:"thumb_width"`   // 缩略图宽度
	ThumbHeight  int `json:"thumb_height"`  // 缩略图高度
}

RotateConfig 旋转验证码配置

func DefaultRotateConfig

func DefaultRotateConfig() *RotateConfig

DefaultRotateConfig 默认旋转验证码配置

type SlideCaptchaData

type SlideCaptchaData struct {
	Type        string `json:"type"`        // 验证码类型
	Image       string `json:"image"`       // 主图 base64
	Thumb       string `json:"thumb"`       // 滑块图 base64
	Width       int    `json:"width"`       // 主图展示宽度
	Height      int    `json:"height"`      // 主图展示高度
	ThumbX      int    `json:"thumbX"`      // 滑块初始展示横坐标
	ThumbY      int    `json:"thumbY"`      // 滑块初始展示纵坐标
	ThumbWidth  int    `json:"thumbWidth"`  // 滑块展示宽度
	ThumbHeight int    `json:"thumbHeight"` // 滑块展示高度
}

SlideCaptchaData 滑动验证码数据结构。

type SlideConfig

type SlideConfig struct {
	MasterWidth   int `json:"master_width"`    // 主图宽度
	MasterHeight  int `json:"master_height"`   // 主图高度
	TileWidth     int `json:"tile_width"`      // 滑块宽度
	TileHeight    int `json:"tile_height"`     // 滑块高度
	TileRadius    int `json:"tile_radius"`     // 滑块圆角半径
	JigsawRadius  int `json:"jigsaw_radius"`   // 拼图缺口圆角半径
	ShadowOffsetX int `json:"shadow_offset_x"` // 阴影X偏移
	ShadowOffsetY int `json:"shadow_offset_y"` // 阴影Y偏移
	ShadowBlur    int `json:"shadow_blur"`     // 阴影模糊度
}

SlideConfig 滑动拼图验证码配置

func DefaultSlideConfig

func DefaultSlideConfig() *SlideConfig

DefaultSlideConfig 默认滑动拼图配置

type StringConfig

type StringConfig struct {
	Height       int     `json:"height"`        // 图片高度
	Width        int     `json:"width"`         // 图片宽度
	CaptchaCount int     `json:"captcha_count"` // 验证码字符数量
	MaxSkew      float64 `json:"max_skew"`      // 最大倾斜度
	DotCount     int     `json:"dot_count"`     // 干扰点数量
	BgColorR     uint8   `json:"bg_color_r"`    // 背景色R
	BgColorG     uint8   `json:"bg_color_g"`    // 背景色G
	BgColorB     uint8   `json:"bg_color_b"`    // 背景色B
	FontColorR   uint8   `json:"font_color_r"`  // 字体色R
	FontColorG   uint8   `json:"font_color_g"`  // 字体色G
	FontColorB   uint8   `json:"font_color_b"`  // 字体色B
	Source       string  `json:"source"`        // 字符源
	CaptchaLen   int     `json:"captcha_len"`   // 验证码长度(兼容字段)
}

StringConfig 字符串验证码配置

func DefaultStringConfig

func DefaultStringConfig() *StringConfig

DefaultStringConfig 默认字符串配置

Jump to

Keyboard shortcuts

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