-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha.go
154 lines (127 loc) · 2.62 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
package main
import (
"bytes"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
cgen "github.com/steambap/captcha"
)
const (
CaptchaTimeLimit = 30 * time.Second
CleanupTimeout = 30 * time.Second
)
var (
captchas *Storage
)
func initCaptchas() {
captchas = NewStorage()
go captchas.Cleanup()
}
type captcha struct {
value string
image []byte
created time.Time
}
type Storage struct {
Map map[string]*captcha
Mu *sync.RWMutex
}
func NewStorage() *Storage {
return &Storage{
Map: make(map[string]*captcha),
Mu: &sync.RWMutex{},
}
}
// Get captcha by id, thread safe.
func (c *Storage) Get(id string) (*captcha, bool) {
c.Mu.RLock()
defer c.Mu.RUnlock()
v, ok := c.Map[id]
return v, ok
}
// Check if provided value is valid captcha value.
func (c *Storage) Check(value string, id string) bool {
v, ok := c.Get(id)
if !ok || v == nil {
return false
}
if time.Since(v.created) >= CaptchaTimeLimit {
return false
}
return strings.EqualFold(value, v.value)
}
// Get image for provided id.
func (c *Storage) GetImage(id string) ([]byte, error) {
v, ok := c.Get(id)
if !ok || v == nil {
return nil, errors.New("invalid id or captcha has expired")
}
return v.image, nil
}
// Create new captcha record and return it's id.
func (c *Storage) Create() (string, error) {
//opt := func(c *cgen.Options) {
// c.BackgroundColor = color.White
//}
data, err := cgen.New(170, 60)
if err != nil {
return "", err
}
log.Debug().Msgf("captcha => %s", data.Text)
buf := new(bytes.Buffer)
err = data.WriteImage(buf)
if err != nil {
return "", err
}
v := &captcha{
value: data.Text,
image: buf.Bytes(),
created: time.Now(),
}
c.Mu.Lock()
defer c.Mu.Unlock()
id := uuid.New().String()
c.Map[id] = v
return id, nil
}
// Delete record from captcha map by id.
func (c *Storage) Delete(id string) {
c.Mu.Lock()
defer c.Mu.Unlock()
_, ok := c.Map[id]
if ok {
delete(c.Map, id)
}
}
// Should be started in separate goroutine when init.
func (c *Storage) Cleanup() {
log.Info().Fields(map[string]interface{}{
"cleanup-timeout": fmt.Sprintf("%v", CleanupTimeout),
"captcha-timeout": fmt.Sprintf("%v", CaptchaTimeLimit),
}).Msg("captcha")
for {
time.Sleep(CleanupTimeout)
c.Mu.RLock()
r := []string{}
for key, value := range c.Map {
if value == nil || time.Since(value.created) >= CaptchaTimeLimit {
r = append(r, key)
}
}
c.Mu.RUnlock()
if len(r) == 0 {
continue
}
log.Info().Fields(map[string]interface{}{
"cleanup": len(r),
}).Msg("captcha")
// Removing expired captchas.
for i := range r {
c.Delete(r[i])
}
}
}