-
Notifications
You must be signed in to change notification settings - Fork 18
/
captcha.go
108 lines (98 loc) · 3.13 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
package main
import (
"fmt"
"image/color"
"net/http"
"strconv"
//"github.com/mojocn/base64Captcha"
"gopkg.in/mojocn/base64Captcha.v1"
)
var (
captchaString *base64Captcha.DriverString
driver *base64Captcha.DriverString
)
type captchaJSON struct {
CaptchaID string `json:"id"`
Base64String string `json:"image"`
Result string `json:"-"`
TempPostIndex string `json:"-"`
EmailCmd string `json:"-"`
}
func initCaptcha() {
if !config.UseCaptcha {
return
}
driver = base64Captcha.NewDriverString(
config.CaptchaHeight, config.CaptchaWidth, 0, 0, 6,
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
&color.RGBA{0, 0, 0, 0}, nil).ConvertFonts()
}
func serveCaptcha(writer http.ResponseWriter, request *http.Request) {
if !config.UseCaptcha {
return
}
var err error
if err = request.ParseForm(); err != nil {
serveErrorPage(writer, err.Error())
errorLog.Println(customError(err))
return
}
tempPostIndexStr := request.FormValue("temppostindex")
var tempPostIndex int
if tempPostIndex, err = strconv.Atoi(tempPostIndexStr); err != nil {
tempPostIndexStr = "-1"
tempPostIndex = 0
}
emailCommand := request.FormValue("emailcmd")
id, b64 := getCaptchaImage()
captchaStruct := captchaJSON{id, b64, "", tempPostIndexStr, emailCommand}
useJSON := request.FormValue("json") == "1"
if useJSON {
writer.Header().Add("Content-Type", "application/json")
str, _ := marshalJSON("", captchaStruct, false)
minifyWriter(writer, []byte(str), "application/json")
return
}
if request.FormValue("reload") == "Reload" {
request.Form.Del("reload")
request.Form.Add("didreload", "1")
serveCaptcha(writer, request)
return
}
writer.Header().Add("Content-Type", "text/html")
captchaID := request.FormValue("captchaid")
captchaAnswer := request.FormValue("captchaanswer")
if captchaID != "" && request.FormValue("didreload") != "1" {
goodAnswer := base64Captcha.DefaultMemStore.Verify(captchaID, captchaAnswer, true)
if goodAnswer {
if tempPostIndex > -1 && tempPostIndex < len(tempPosts) {
// came from a /post redirect, insert the specified temporary post
// and redirect to the thread
insertPost(&tempPosts[tempPostIndex], emailCommand == "noko")
buildBoards(tempPosts[tempPostIndex].BoardID)
buildFrontPage()
url := tempPosts[tempPostIndex].GetURL(false)
// move the end Post to the current index and remove the old end Post. We don't
// really care about order as long as tempPost validation doesn't get jumbled up
tempPosts[tempPostIndex] = tempPosts[len(tempPosts)-1]
tempPosts = tempPosts[:len(tempPosts)-1]
http.Redirect(writer, request, url, http.StatusFound)
return
}
} else {
captchaStruct.Result = "Incorrect CAPTCHA"
}
}
if err = minifyTemplate(captchaTmpl, captchaStruct, writer, "text/html"); err != nil {
handleError(0, customError(err))
fmt.Fprintf(writer, "Error executing captcha template")
}
}
func getCaptchaImage() (captchaID string, chaptchaB64 string) {
if !config.UseCaptcha {
return
}
captcha := base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore)
captchaID, chaptchaB64, _ = captcha.Generate()
return
}