-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaptcha.go
274 lines (244 loc) · 8.88 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package DynCaptcha
import (
"bufio"
"bytes"
"image"
"image/color"
"image/gif"
"math"
"math/rand"
"time"
)
type traveller struct {
X, Y float64
Radius float64
TargetX, TargetY float64
Target int
Hotspot bool
FakeDirection float64
}
func (p *traveller) Travel(per float64, clr color.RGBA) (traveller, color.RGBA) {
const th float64 = 0.2
fakeX := p.X + math.Cos(p.FakeDirection)*p.Radius*2
fakeY := p.Y + math.Sin(p.FakeDirection)*p.Radius*2
var x, y float64
if per >= th {
x = (p.TargetX-fakeX)*(per-th)/(1-th) + fakeX
y = (p.TargetY-fakeY)*(per-th)/(1-th) + fakeY
} else {
x = (fakeX-p.X)*(per/th) + p.X
y = (fakeY-p.Y)*(per/th) + p.Y
}
return traveller{X: x, Y: y}, clr
}
func drawNumber(img *image.Paletted, _x float64, _y float64, num int, clr color.RGBA) {
type _pos struct {
X, Y int
}
var _draw = func(x, y int, n int) {
var _buf []_pos
switch n {
case 0:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
_pos{-2, 2} /* */, _pos{2, 2},
_pos{-2, 1} /* */, _pos{2, 1},
_pos{-2, 0} /* */, _pos{2, 0},
_pos{-2, -1} /* */, _pos{2, -1},
_pos{-2, -2} /* */, _pos{2, -2},
_pos{-2, -3}, _pos{-1, -3}, _pos{0, -3}, _pos{1, -3}, _pos{2, -3},
)
case 1:
_buf = append(_buf,
_pos{0, 3},
_pos{0, 2},
_pos{0, 1},
_pos{0, 0},
_pos{0, -1},
_pos{0, -2},
_pos{0, -3},
)
case 2:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
/* */ _pos{2, 2},
/* */ _pos{2, 1},
_pos{-2, 0}, _pos{-1, 0}, _pos{0, 0}, _pos{1, 0}, _pos{2, 0},
_pos{-2, -1}, /* */
_pos{-2, -2}, /* */
_pos{-2, -3}, _pos{-1, -3}, _pos{0, -3}, _pos{1, -3}, _pos{2, -3},
)
case 3:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
/* */ _pos{2, 2},
/* */ _pos{2, 1},
_pos{-2, 0}, _pos{-1, 0}, _pos{0, 0}, _pos{1, 0}, _pos{2, 0},
/* */ _pos{2, -1},
/* */ _pos{2, -2},
_pos{-2, -3}, _pos{-1, -3}, _pos{0, -3}, _pos{1, -3}, _pos{2, -3},
)
case 4:
_buf = append(_buf,
_pos{-2, 3} /* */, _pos{2, 3},
_pos{-2, 2} /* */, _pos{2, 2},
_pos{-2, 1} /* */, _pos{2, 1},
_pos{-2, 0}, _pos{-1, 0}, _pos{0, 0}, _pos{1, 0}, _pos{2, 0},
/* */ _pos{2, -1},
/* */ _pos{2, -2},
/* */ _pos{2, -3},
)
case 5:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
_pos{-2, 2}, /* */
_pos{-2, 1}, /* */
_pos{-2, 0}, _pos{-1, 0}, _pos{0, 0}, _pos{1, 0}, _pos{2, 0},
/* */ _pos{2, -1},
/* */ _pos{2, -2},
_pos{-2, -3}, _pos{-1, -3}, _pos{0, -3}, _pos{1, -3}, _pos{2, -3},
)
case 6:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
_pos{-2, 2}, /* */
_pos{-2, 1}, /* */
_pos{-2, 0}, _pos{-1, 0}, _pos{0, 0}, _pos{1, 0}, _pos{2, 0},
_pos{-2, -1} /* */, _pos{2, -1},
_pos{-2, -2} /* */, _pos{2, -2},
_pos{-2, -3}, _pos{-1, -3}, _pos{0, -3}, _pos{1, -3}, _pos{2, -3},
)
case 7:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
/* */ _pos{2, 2},
/* */ _pos{2, 1},
/* */ _pos{2, 0},
/* */ _pos{2, -1},
/* */ _pos{2, -2},
/* */ _pos{2, -3},
)
case 8:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
_pos{-2, 2} /* */, _pos{2, 2},
_pos{-2, 1} /* */, _pos{2, 1},
_pos{-2, 0}, _pos{-1, 0}, _pos{0, 0}, _pos{1, 0}, _pos{2, 0},
_pos{-2, -1} /* */, _pos{2, -1},
_pos{-2, -2} /* */, _pos{2, -2},
_pos{-2, -3}, _pos{-1, -3}, _pos{0, -3}, _pos{1, -3}, _pos{2, -3},
)
case 9:
_buf = append(_buf,
_pos{-2, 3}, _pos{-1, 3}, _pos{0, 3}, _pos{1, 3}, _pos{2, 3},
_pos{-2, 2} /* */, _pos{2, 2},
_pos{-2, 1} /* */, _pos{2, 1},
_pos{-2, 0}, _pos{-1, 0}, _pos{0, 0}, _pos{1, 0}, _pos{2, 0},
/* */ _pos{2, -1},
/* */ _pos{2, -2},
_pos{-2, -3}, _pos{-1, -3}, _pos{0, -3}, _pos{1, -3}, _pos{2, -3},
)
}
for _, p := range _buf {
img.Set(p.X+x, -p.Y+y, clr)
}
}
if num < 10 {
_draw(int(_x), int(_y), num)
} else {
n1 := int(num / 10)
n2 := num - n1*10
_draw(int(_x)-3, int(_y), n1)
_draw(int(_x)+3, int(_y), n2)
}
}
func drawCircle(img *image.Paletted, _x float64, _y float64, r float64, clr color.RGBA) {
for x := _x - r; x <= _x+r; x++ {
for y := _y - r; y <= _y+r; y++ {
d := math.Sqrt((x-_x)*(x-_x) + (y-_y)*(y-_y))
if d <= r {
img.Set(int(x), int(y), clr)
}
}
}
}
func New(seed int) ([]byte, int) {
var w, h int = 128, 128
var size = 7
var group []traveller
if seed == 0 {
seed = int(time.Now().UnixNano())
}
_rand := rand.New(rand.NewSource(int64(seed)))
shuffle := _rand.Perm(size * size)
hotspot := (_rand.Intn(size-2)+1)*size + _rand.Intn(size-2) + 1
delta := w / (size + 1)
for i := 1; i <= size; i++ {
for j := 1; j <= size; j++ {
p := traveller{X: float64(j * delta), Y: float64(i * delta)}
group = append(group, p)
}
}
var palette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0xff, 0x00, 0x00, 0xff},
color.RGBA{0xff, 0xff, 0xff, 0xff},
}
var whiteColor, redColor = color.RGBA{255, 255, 255, 255}, color.RGBA{255, 0, 0, 255}
var images []*image.Paletted
var delays []int
imgBlink1 := image.NewPaletted(image.Rect(0, 0, w, h), palette)
imgBlink2 := image.NewPaletted(image.Rect(0, 0, w, h), palette)
imgFinal := image.NewPaletted(image.Rect(0, 0, w, h), palette)
imgNum := image.NewPaletted(image.Rect(0, 0, w, h), palette)
for i, _ := range group {
group[i].Target = shuffle[i]
group[i].TargetX = group[shuffle[i]].X
group[i].TargetY = group[shuffle[i]].Y
group[i].FakeDirection = float64(_rand.Intn(12)) * math.Pi * 2 / 12
group[i].Radius = float64(_rand.Intn(2)) + 3
g := group[i]
drawCircle(imgBlink1, g.X, g.Y, g.Radius, whiteColor)
drawCircle(imgFinal, g.TargetX, g.TargetY, g.Radius, whiteColor)
if i == hotspot {
drawCircle(imgBlink2, g.X, g.Y, g.Radius, redColor)
group[i].Hotspot = true
} else {
drawCircle(imgBlink2, g.X, g.Y, g.Radius, whiteColor)
group[i].Hotspot = false
}
drawNumber(imgNum, g.X, g.Y, i+1, whiteColor)
}
images = append(images, imgBlink1, imgBlink2, imgBlink1, imgBlink2)
delays = append(delays, 100, 100, 100, 100)
steps := 48
for step := 0; step < steps; step++ {
img := image.NewPaletted(image.Rect(0, 0, w, h), palette)
images = append(images, img)
delays = append(delays, 0)
for _, g := range group {
tmp, newColor := g.Travel(float64(step)/float64(steps), whiteColor)
drawCircle(img, tmp.X, tmp.Y, g.Radius, newColor)
}
// for x := 0; x < w; x++ {
// for y := 0; y < h; y++ {
// if _rand.Intn(10) == 1 {
// img.Set(x, y, whiteColor)
// }
// }
// }
c := 0
for c < int(w*h/15) {
x := _rand.Intn(w)
y := _rand.Intn(h)
img.Set(x, y, whiteColor)
c++
}
}
images = append(images, imgFinal, imgNum)
delays = append(delays, 200, 500)
var buf bytes.Buffer
writer := bufio.NewWriter(&buf)
gif.EncodeAll(writer, &gif.GIF{Image: images, Delay: delays})
return buf.Bytes(), group[hotspot].Target + 1
}