This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
/
captcha.go
186 lines (157 loc) · 4.28 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
174
175
176
177
178
179
180
181
182
183
184
185
186
package http
import (
"bytes"
"encoding/base64"
"image/color"
"io/ioutil"
"net/http"
"strings"
"text/template"
"time"
"github.com/ArtalkJS/ArtalkGo/config"
"github.com/ArtalkJS/ArtalkGo/lib"
"github.com/ArtalkJS/ArtalkGo/lib/captcha"
"github.com/ArtalkJS/ArtalkGo/pkged"
"github.com/eko/gocache/v2/store"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
imgCaptcha "github.com/steambap/captcha"
)
var (
CaptchaExpiration = 5 * time.Minute // 验证码 5 分钟内有效
)
// 获取当前状态,是否需要验证
func (a *action) CaptchaStatus(c echo.Context) error {
if IsReqNeedCaptchaCheck(c) {
return RespData(c, Map{"is_pass": false})
} else {
return RespData(c, Map{"is_pass": true})
}
}
// 获取验证码
func (a *action) CaptchaGet(c echo.Context) error {
ip := c.RealIP()
// ===========
// Geetest
// ===========
if config.Instance.Captcha.Geetest.Enabled {
pageFile, _ := pkged.Open("/lib/captcha/pages/geetest.html")
buf, _ := ioutil.ReadAll(pageFile)
var page bytes.Buffer
t := template.New("")
t.Parse(string(buf))
t.Execute(&page, map[string]interface{}{"gt_id": config.Instance.Captcha.Geetest.CaptchaID})
return c.HTML(http.StatusOK, page.String())
}
// ===========
// 图片验证码
// ===========
return RespData(c, Map{
"img_data": GetNewImageCaptchaBase64(ip),
})
}
type ParamsCaptchaCheck struct {
Value string `mapstructure:"value" param:"required"`
}
// 验证
func (a *action) CaptchaCheck(c echo.Context) error {
ip := c.RealIP()
var p ParamsCaptchaCheck
if isOK, resp := ParamsDecode(c, &p); !isOK {
return resp
}
inputVal := p.Value
// ===========
// Geetest
// ===========
if config.Instance.Captcha.Geetest.Enabled {
isPass, reason, err := captcha.GeetestCheck(inputVal)
if err != nil {
logrus.Error("[Geetest] 验证发生错误 ", err)
return RespError(c, "Geetest API 错误")
}
if isPass {
// 验证成功
onCaptchaPass(c)
return RespSuccess(c)
} else {
// 验证失败
onCaptchaFail(c)
return RespError(c, "验证失败", Map{
"reason": reason,
})
}
}
// ===========
// 图片验证码
// ===========
isPass := strings.ToLower(inputVal) == GetImageCaptchaRealCode(ip)
if isPass {
// 验证码正确
DisposeImageCaptcha(ip) // 销毁图片验证码
onCaptchaPass(c)
return RespSuccess(c)
} else {
// 验证码错误
DisposeImageCaptcha(ip)
onCaptchaFail(c)
return RespError(c, "验证码错误", Map{
"img_data": GetNewImageCaptchaBase64(ip),
})
}
}
// 验证成功操作
func onCaptchaPass(c echo.Context) {
ip := c.RealIP()
setActionCount(c, config.Instance.Captcha.ActionLimit-1)
SetAlwaysCaptchaMode_Pass(ip, true) // 允许 always mode pass
}
// 验证失败操作
func onCaptchaFail(c echo.Context) {
ip := c.RealIP()
RecordAction(c) // 记录操作
SetAlwaysCaptchaMode_Pass(ip, false) // 取消 always mode pass
}
//#region 图片验证码
// 获取对应 IP 图片验证码正确的值
func GetImageCaptchaRealCode(ip string) string {
var realVal string
lib.CACHE.Get(lib.Ctx, "captcha:"+ip, &realVal)
return strings.ToLower(realVal)
}
// 获取新验证码 base64 格式图片
func GetNewImageCaptchaBase64(ip string) string {
// generate a image
pngBuffer := bytes.NewBuffer([]byte{})
data, _ := imgCaptcha.New(160, 40, func(o *imgCaptcha.Options) {
o.FontScale = 1
o.CurveNumber = 2
o.FontDPI = 85.0
o.Noise = 0.7
o.BackgroundColor = color.White
})
data.WriteImage(pngBuffer)
base64 := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBuffer.Bytes())
// save real code
lib.CACHE.Set(lib.Ctx, "captcha:"+ip, data.Text, &store.Options{Expiration: CaptchaExpiration})
return base64
}
// 销毁图片验证码
func DisposeImageCaptcha(ip string) {
lib.CACHE.Delete(lib.Ctx, "captcha:"+ip)
}
//#endregion
// AlwaysMode 是否能 Pass (for 总是需要验证码的选项)
func GetAlwaysCaptchaMode_Pass(ip string) bool {
var val string
_, err := lib.CACHE.Get(lib.Ctx, "captcha-am-pass:"+ip, &val)
return err == nil && val == "1"
}
// 设置 AlwaysMode 允许 Pass (for 总是需要验证码的选项)
func SetAlwaysCaptchaMode_Pass(ip string, pass bool) {
val := "0"
if pass {
val = "1"
}
lib.CACHE.Set(lib.Ctx, "captcha-am-pass:"+ip, val, &store.Options{Expiration: CaptchaExpiration})
}