-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtcha.go
285 lines (234 loc) · 4.91 KB
/
gtcha.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
275
276
277
278
279
280
281
282
283
284
285
//
// gitcha application logic
//
package GTCHA
import (
"bytes"
"errors"
"math/rand"
"net/http"
"sync"
"github.com/pborman/uuid"
"appengine"
)
const gifType = "image/gif"
// GtchaApp represents an app that is using our GIF captcha.
type GtchaApp struct {
Name string `json:"name,omitempty"`
// Secret is an app's secret key.
Secret string `json:"secret,omitempty"`
// public key of an app for use in its front-end.
APIKey string `json:"api_key,omitempty"`
Domains []string `json:"domains,omitempty"`
}
// Captcha represents our user-facing GIF captcha.
type Captcha struct {
ID string `json:"id,omitempty"`
Tag string `json:"tag"`
Images []GImg `json:"images"`
}
type GImg struct {
ID string `json:"id"`
URI string `json:"uri"`
}
type gtcha struct {
Tag string
In []gimg
Out []gimg
Maybe []gimg
IsHuman bool
}
type gimg struct {
ID string `json:"id"`
GID string `json:"gid"` // the ID on the giphy backend
GURI string `json:"guri"`
}
// newGtcha returns the internal representation of the GIF captcha.
// This function works by making a lot of API calls in parrallel.
func newGtcha(c *http.Client) (*gtcha, error) {
tag, err := GetTag(c)
if err != nil {
return nil, err
}
var (
in []gimg
out []gimg
maybe []gimg
wg sync.WaitGroup
errOnce sync.Once
errCh = make(chan error)
)
// closes over some variables, so that we can get all the imges in parrallel
processImages := func(
fn func(*http.Client, string, int) ([]*Image, error),
) []gimg {
apiImgs, err := fn(c, tag, 0)
if err != nil {
errOnce.Do(func() { errCh <- err })
return nil
}
imgs := make([]gimg, len(apiImgs))
for i, img := range apiImgs[:2] {
imgs[i] = gimg{
ID: uuid.New(),
GID: img.ID,
GURI: img.Images.FixedWidthSmall.URL,
}
}
return imgs
}
wg.Add(3)
go func() {
in = processImages(GetImagesTagged)
wg.Done()
}()
go func() {
out = processImages(GetImagesNotTagged)
wg.Done()
}()
go func() {
maybe = processImages(GetImagesMaybeTagged)
wg.Done()
}()
go func() {
wg.Wait()
close(errCh)
}()
err = <-errCh
if err != nil {
return nil, err
}
g := >cha{
Tag: tag,
In: in,
Out: out,
Maybe: maybe,
}
return g, nil
}
func verifyGtcha(c *http.Client, g *gtcha, in []string) bool {
n := len(g.In)
correctSelections := 0
found := make(map[string]bool) // for making sure duplicates aren't sent
for _, img := range in {
if isImageIn(g.Out, img) {
return false
}
if isImageIn(g.In, img) && !found[img] {
correctSelections++
found[img] = true
}
}
if correctSelections < n {
return false
}
// check the images that might be tagged g's tag against the user's submitted images
// to let the giphy API know that a human verified the tag
for _, img := range in {
go func(img string) {
if isImageIn(g.Maybe, img) {
ConfirmTag(c, g.Tag, img)
}
}(img)
}
return true
}
func (img *gimg) toGImg(c appengine.Context, httpC *http.Client) (*GImg, error) {
gotten := false
wait := make(chan struct{})
f1 := func() interface{} {
defer close(wait)
uri, err := GetImageURI(c, img.GID)
if err != nil {
return nil
}
gotten = true
return &GImg{img.ID, uri}
}
f2 := func() (interface{}, error) {
<-wait
if gotten {
return nil, errors.New("done")
}
req, err := http.NewRequest("GET", img.GURI, nil)
if err != nil {
return nil, err
}
res, err := httpC.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
buf := new(bytes.Buffer)
if _, err = buf.ReadFrom(res.Body); err != nil {
return nil, err
}
uri := dataURI(buf.Bytes(), gifType)
go CacheImageURI(c, img.GID, uri)
return &GImg{img.ID, uri}, nil
}
i, err := Get(f1, f2)
if err != nil {
return nil, err
}
return i.(*GImg), nil
}
func (g *gtcha) toCaptcha(c appengine.Context, httpC *http.Client) (*Captcha, error) {
var (
o sync.Once
wg sync.WaitGroup
errCh = make(chan error)
imgCh = make(chan *GImg) // saves overhead to use pointer
n = len(g.In) + len(g.Out) + len(g.Maybe)
)
wg.Add(n)
for _, l := range [][]gimg{g.In, g.Out, g.Maybe} {
for _, img := range l {
go func(img gimg) {
defer wg.Done()
i, err := img.toGImg(c, httpC)
if err != nil {
o.Do(func() { errCh <- err })
return
}
imgCh <- i
}(img)
}
}
go func() {
wg.Wait()
close(imgCh)
}()
imgs := make([]*GImg, 0, n)
LOOP:
for {
select {
case err := <-errCh:
return nil, err // guaranteed to not be nil
case img, ok := <-imgCh:
if !ok {
break LOOP
}
imgs = append(imgs, img)
}
}
imgsShuf := make([]GImg, n)
perm := rand.Perm(n)
for i, idx := range perm {
imgsShuf[i] = *imgs[idx]
}
captcha := &Captcha{
ID: uuid.New(),
Tag: g.Tag,
Images: imgsShuf,
}
return captcha, nil
}
func isImageIn(imgs []gimg, img string) bool {
for _, i := range imgs {
if i.ID == img {
return true
}
}
return false
}