/
captcha.go
172 lines (160 loc) · 4.46 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
// Copyright 2020 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"bytes"
"context"
"fmt"
"image"
"image/color"
"image/jpeg"
"math/rand"
"strconv"
"strings"
"time"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"github.com/vicanso/forest/util"
"github.com/vicanso/hes"
"golang.org/x/image/font/gofont/goregular"
)
const (
captchaKeyPrefix = "captcha:"
errCaptchaCategory = "captcha"
)
type (
// CaptchaInfo 图形验证码
CaptchaInfo struct {
ExpiredAt time.Time `json:"expiredAt"`
Data []byte `json:"data"`
// json输出时,忽略此字段
Value string `json:"-"`
ID string `json:"id"`
Type string `json:"type"`
}
)
// createCaptcha 创建图形验证码
func createCaptcha(fontColor, bgColor color.Color, width, height int, text string) (image.Image, error) {
font, err := truetype.Parse(goregular.TTF)
if err != nil {
return nil, err
}
dc := gg.NewContext(width, height)
// 设置背景色
dc.SetColor(bgColor)
dc.Clear()
fontCount := len(text)
offset := 10
eachFontWidth := (width - 2*offset) / fontCount
fontSize := float64(eachFontWidth) * 1.8
dc.SetColor(fontColor)
// 对字符串一个个的填入图片中
for index, ch := range text {
// 随机+ -字体大小
newFontSize := float64(rand.Int63n(40)+80) / 100 * fontSize
face := truetype.NewFace(font, &truetype.Options{Size: newFontSize})
dc.SetFontFace(face)
// 随机偏移角度
angle := float64(rand.Int63n(20))/100 - 0.1
// 随机偏移位置
offsetX := float64(eachFontWidth + index*eachFontWidth + int(rand.Int63n(10)) - 10)
offsetY := float64(height) + float64(rand.Int63n(10)) - float64(15)
if offsetY > float64(height) || offsetX < float64(height)-newFontSize {
offsetY = float64(height)
}
dc.Rotate(angle)
dc.DrawString(string(ch), offsetX, offsetY)
}
dc.SetStrokeStyle(gg.NewSolidPattern(fontColor))
dc.SetLineWidth(1.5)
// 画线(用于影响机器识别)
for index := 0; index < 3; index++ {
x1 := float64(rand.Int31n(int32(width / 2)))
y1 := float64(rand.Int31n(int32(height)))
x2 := float64(rand.Int31n(int32(width/2)) + int32(width/2))
y2 := float64(rand.Int31n(int32(height)))
dc.DrawLine(x1, y1, x2, y2)
dc.Stroke()
}
return dc.Image(), nil
}
// parseColor 转换颜色
func parseColor(s string) (*color.RGBA, error) {
arr := strings.Split(s, ",")
if len(arr) != 3 {
return nil, hes.New(fmt.Sprintf("非法颜色值,格式必须为:1,1,1,当前为:%s", s), errCaptchaCategory)
}
c := &color.RGBA{}
c.A = 0xff
for index, v := range arr {
value, e := strconv.Atoi(strings.TrimSpace(v))
if e != nil {
return nil, hes.Wrap(e)
}
if value > 255 || value < 0 {
return nil, hes.New(fmt.Sprintf("非法颜色值,必须>=0 <=255,当前为:%d", value), errCaptchaCategory)
}
switch index {
case 0:
c.R = uint8(value)
case 1:
c.G = uint8(value)
default:
c.B = uint8(value)
}
}
return c, nil
}
// GetCaptcha 获取图形验证码
func GetCaptcha(ctx context.Context, fontColor, bgColor string) (*CaptchaInfo, error) {
value := util.RandomDigit(4)
fc, err := parseColor(fontColor)
if err != nil {
return nil, err
}
bc, err := parseColor(bgColor)
if err != nil {
return nil, err
}
img, err := createCaptcha(fc, bc, 80, 40, value)
if err != nil {
return nil, err
}
buffer := new(bytes.Buffer)
err = jpeg.Encode(buffer, img, nil)
if err != nil {
return nil, err
}
id := util.GenXID()
ttl := 5 * time.Minute
err = redisSrv.Set(ctx, captchaKeyPrefix+id, value, ttl+time.Minute)
if err != nil {
return nil, err
}
return &CaptchaInfo{
ExpiredAt: time.Now().Add(ttl),
Data: buffer.Bytes(),
Value: value,
ID: id,
Type: "jpeg",
}, nil
}
// ValidateCaptcha 校验图形验证码是否正确
func ValidateCaptcha(ctx context.Context, id, value string) (bool, error) {
data, err := redisSrv.GetAndDel(ctx, captchaKeyPrefix+id)
if err != nil {
return false, err
}
return string(data) == value, nil
}