-
Notifications
You must be signed in to change notification settings - Fork 4
/
utilCaptcha.go
82 lines (70 loc) · 2.22 KB
/
utilCaptcha.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
// Copyright (c) 2023 gpress Authors.
//
// This file is part of gpress.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"image/color"
"math/rand"
"sync"
"sync/atomic"
"github.com/mojocn/base64Captcha"
)
const errCount = 3
// 配置验证码的参数
var driverString = base64Captcha.DriverString{
Height: 60,
Width: 200,
NoiseCount: 0,
ShowLineOptions: 2 | 4,
Length: 4,
Source: "34678acdefghkmnprtuvwxy",
BgColor: &color.RGBA{R: 3, G: 102, B: 214, A: 125},
Fonts: []string{"wqy-microhei.ttc"},
}
// var driver = base64Captcha.NewDriverMath(60, 200, 0, base64Captcha.OptionShowHollowLine, nil, nil, []string{"wqy-microhei.ttc"})
var driver = driverString.ConvertFonts()
var captchaQuestion, captchaAnswer, captchaBase64 string
var captchaLock = &sync.Mutex{}
var chainRandStr string
var errorLoginCount atomic.Uint32
// generateCaptcha 生成随机验证码
func generateCaptcha() {
captchaLock.Lock()
defer captchaLock.Unlock()
_, captchaQuestion, captchaAnswer = driver.GenerateIdQuestionAnswer()
item, err := driver.DrawCaptcha(captchaQuestion)
if err != nil {
captchaBase64 = ""
return
}
captchaBase64 = item.EncodeB64string()
}
// generateChainRandStr 生成区块链登录的随机字符串
func generateChainRandStr() {
captchaLock.Lock()
defer captchaLock.Unlock()
//先记录到全局变量
chainRandStr = randStr(30)
}
// randStr 生成随机字符串
func randStr(n int) string {
//rand.Seed(time.Now().UnixNano())
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}