-
Notifications
You must be signed in to change notification settings - Fork 664
/
captcha.go
173 lines (151 loc) · 3.85 KB
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package helper
import (
"errors"
"image/color"
"github.com/mojocn/base64Captcha"
"github.com/yaoapp/gou/process"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/kun/maps"
)
var captchaStore = base64Captcha.DefaultMemStore
// CaptchaOption 验证码配置
type CaptchaOption struct {
Type string
Height int
Width int
Length int
Lang string
Background string
}
// NewCaptchaOption 创建验证码配置
func NewCaptchaOption() CaptchaOption {
return CaptchaOption{
Width: 240,
Height: 80,
Length: 4,
Lang: "zh",
Background: "#FFFFFF",
}
}
// CaptchaMake 制作验证码
func CaptchaMake(option CaptchaOption) (string, string) {
if option.Width == 0 {
option.Width = 240
}
if option.Height == 0 {
option.Width = 80
}
if option.Length == 0 {
option.Length = 4
}
if option.Lang == "" {
option.Lang = "zh"
}
var driver base64Captcha.Driver
switch option.Type {
case "audio":
driver = base64Captcha.NewDriverAudio(option.Length, option.Lang)
break
case "math":
background := captchaBackground(option.Background)
driver = base64Captcha.NewDriverMath(
option.Height, option.Width, 3,
base64Captcha.OptionShowHollowLine, background,
base64Captcha.DefaultEmbeddedFonts, []string{},
)
break
default:
driver = base64Captcha.NewDriverDigit(
option.Height, option.Width, 5,
0.7, 80,
)
break
}
c := base64Captcha.NewCaptcha(driver, captchaStore)
id, content, err := c.Generate()
if err != nil {
exception.New("生成验证码出错 %s", 500, err).Throw()
}
// 打印日志
log.Debug("图形/音频 ID:%s 验证码:%s", id, captchaStore.Get(id, false))
return id, content
}
// CaptchaValidate 校验验证码
func CaptchaValidate(id string, value string) bool {
return captchaStore.Verify(id, value, true)
}
func captchaBackground(s string) *color.RGBA {
if s == "" {
s = "#555555"
}
bg, err := captchaParseHexColorFast(s)
if err != nil {
exception.New("背景色格式错误 %s", 400, s).Throw()
}
return &bg
}
func captchaParseHexColorFast(s string) (c color.RGBA, err error) {
c.A = 0xff
if s[0] != '#' {
return c, errors.New("invalid format")
}
hexToByte := func(b byte) byte {
switch {
case b >= '0' && b <= '9':
return b - '0'
case b >= 'a' && b <= 'f':
return b - 'a' + 10
case b >= 'A' && b <= 'F':
return b - 'A' + 10
}
err = errors.New("invalid format")
return 0
}
switch len(s) {
case 7:
c.R = hexToByte(s[1])<<4 + hexToByte(s[2])
c.G = hexToByte(s[3])<<4 + hexToByte(s[4])
c.B = hexToByte(s[5])<<4 + hexToByte(s[6])
case 4:
c.R = hexToByte(s[1]) * 17
c.G = hexToByte(s[2]) * 17
c.B = hexToByte(s[3]) * 17
default:
err = errors.New("invalid format")
}
return
}
// ProcessCaptchaValidate xiang.helper.CaptchaValidate 校验图形/音频验证码
func ProcessCaptchaValidate(process *process.Process) interface{} {
process.ValidateArgNums(2)
id := process.ArgsString(0)
code := process.ArgsString(1)
if code == "" {
exception.New("请输入验证码", 400).Throw()
return false
}
if !CaptchaValidate(id, code) {
exception.New("验证码不正确", 400).Throw()
return false
}
return true
}
// ProcessCaptcha xiang.helper.Captcha 校验图形/音频验证码
func ProcessCaptcha(process *process.Process) interface{} {
process.ValidateArgNums(1)
option := CaptchaOption{
Width: any.Of(process.ArgsURLValue(0, "width", "240")).CInt(),
Height: any.Of(process.ArgsURLValue(0, "height", "80")).CInt(),
Length: any.Of(process.ArgsURLValue(0, "height", "4")).CInt(),
Type: process.ArgsURLValue(0, "type", "math"),
Background: process.ArgsURLValue(0, "background", "#FFFFFF"),
Lang: process.ArgsURLValue(0, "lang", "zh"),
}
id, content := CaptchaMake(option)
return maps.Map{
"id": id,
"content": content,
}
}