forked from razonyang/clevergo-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha.go
83 lines (77 loc) · 2.06 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
package core
import (
"fmt"
"time"
"github.com/clevergo/captchas"
"github.com/clevergo/captchas/drivers"
"github.com/clevergo/captchas/stores/redisstore"
"github.com/go-redis/redis/v7"
)
type CaptchaConfig struct {
Driver string `koanf:"driver"`
String struct {
Height int `koanf:"height"`
Width int `koanf:"width"`
Length int `koanf:"length"`
} `koanf:"string"`
Math struct {
Height int `koanf:"height"`
Width int `koanf:"width"`
NoiseCount int `koanf:"noise_count"`
} `koanf:"math"`
Chinese struct {
Height int `koanf:"height"`
Width int `koanf:"width"`
Length int `koanf:"length"`
} `koanf:"chinese"`
Digit struct {
Height int `koanf:"height"`
Width int `koanf:"width"`
Length int `koanf:"length"`
} `koanf:"digit"`
Audio struct {
Length int `koanf:"length"`
} `koanf:"audio"`
}
func NewCaptchaManager(cfg CaptchaConfig, store captchas.Store) *captchas.Manager {
switch cfg.Driver {
case "string":
return captchas.New(store, drivers.NewString(
drivers.StringHeight(cfg.String.Height),
drivers.StringWidth(cfg.String.Width),
drivers.StringLength(cfg.String.Length),
))
case "chinese":
return captchas.New(store, drivers.NewChinese(
drivers.ChineseHeight(cfg.Chinese.Height),
drivers.ChineseWidth(cfg.Chinese.Width),
drivers.ChineseLength(cfg.Chinese.Length),
))
case "audio":
return captchas.New(store, drivers.NewAudio(
drivers.AudioLength(cfg.Audio.Length),
))
case "math":
return captchas.New(store, drivers.NewMath(
drivers.MathHeight(cfg.Math.Height),
drivers.MathWidth(cfg.Math.Width),
drivers.MathNoiseCount(cfg.Math.NoiseCount),
))
default:
return captchas.New(store, drivers.NewDigit(
drivers.DigitHeight(cfg.Digit.Height),
drivers.DigitWidth(cfg.Digit.Width),
drivers.DigitLength(cfg.Digit.Length),
))
}
}
func NewCaptchaStore(cfg RedisConfig) captchas.Store {
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
})
return redisstore.New(
client,
redisstore.Expiration(10*time.Minute),
redisstore.Prefix("captchas"),
)
}