forked from bakape/captchouli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptchouli.go
137 lines (120 loc) · 2.49 KB
/
captchouli.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
package captchouli
// #include "thumbnail.h"
import "C"
import (
"compress/gzip"
"encoding/base64"
"strings"
"fmt"
"io"
"net/http"
"github.com/bakape/captchouli/common"
"github.com/bakape/captchouli/db"
"golang.org/x/net/html"
)
const (
// Keys used as names for input elements in captcha form HTML
IDKey = common.IDKey
ColourKey = common.ColourKey
BackgroundKey = common.BackgroundKey
)
// Generic error with prefix string
type Error = common.Error
var (
// No faces detected in downloaded image
ErrNoFace = Error{fmt.Errorf("no faces detected")}
)
// Init storage and start the runtime
func Open() error {
return db.Open()
}
// Close open resources
func Close() error {
classifiersMu.Lock()
for s, c := range classifiers {
C.cpli_unload_classifier(c)
delete(classifiers, s)
}
classifiersMu.Unlock()
return db.Close()
}
// Extact captcha ID and solution from request
func ExtractSolution(r *http.Request) (solution []byte, err error) {
err = r.ParseForm()
if err != nil {
return
}
solution = make([]byte, 0, 4)
for i := 0; i < 9; i++ {
s := r.Form.Get(solutionIDs[i])
if s == "on" {
solution = append(solution, byte(i))
}
}
return
}
// Decode captcha ID from POST request
func ExtractID(r *http.Request) (id [64]byte, err error) {
err = r.ParseForm()
if err != nil {
return
}
return DecodeID(strings.Replace(r.Form.Get(common.IDKey), " ", "+", -1))
}
// Decode captcha ID from base64 string
func DecodeID(s string) (id [64]byte, err error) {
if s == "" {
err = ErrInvalidID
return
}
buf, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return
}
if len(buf) != 64 {
err = ErrInvalidID
return
}
copy(id[:], buf)
return
}
// Extract captcha from GZipped HTML body and return together with its solution
func ExtractCaptcha(r io.Reader) (id [64]byte, solution []byte, err error) {
gzr, err := gzip.NewReader(r)
if err != nil {
return
}
defer gzr.Close()
doc, err := html.Parse(gzr)
if err != nil {
return
}
id, err = DecodeID(findID(doc))
if err != nil {
return
}
solution, err = db.GetSolution(id)
return
}
func findID(n *html.Node) string {
getAttr := func(key string) string {
for _, attr := range n.Attr {
if attr.Key == key {
return attr.Val
}
}
return ""
}
if n.Type == html.ElementNode && n.Data == "input" {
if getAttr("name") == IDKey {
return getAttr("value")
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
found := findID(c)
if found != "" {
return found
}
}
return ""
}