/
main.go
129 lines (112 loc) · 3.21 KB
/
main.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
package main
import (
"crypto/md5"
"fmt"
"github.com/dchest/captcha"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/thbourlove/eCaptcha/store"
"io"
"log"
"net/http"
"strconv"
)
var userStore *store.StringStore
var validationStore *store.StringStore
func newCaptcha(writer http.ResponseWriter, request *http.Request) {
clientId := request.URL.Query().Get("clientId")
privateKey := userStore.Get(clientId, false)
if privateKey == "" {
writer.WriteHeader(401)
return
}
captchaId := captcha.New()
validationStore.Set(captchaId, clientId)
io.WriteString(writer, captchaId)
}
func reloadCaptcha(writer http.ResponseWriter, request *http.Request) {
captchaId := request.URL.Query().Get("captchaId")
result := captcha.Reload(captchaId)
if result {
io.WriteString(writer, "success")
} else {
io.WriteString(writer, "fail")
}
}
func verifyCaptcha(writer http.ResponseWriter, request *http.Request) {
captchaId := request.URL.Query().Get("captchaId")
verifyString := request.URL.Query().Get("verifyString")
clientId := validationStore.Get(captchaId, true)
result := captcha.VerifyString(captchaId, verifyString)
if result {
privateKey := userStore.Get(clientId, false)
md5String := fmt.Sprintf("%x", md5.Sum([]byte(privateKey+captchaId)))
validationStore.Set(md5String, clientId)
io.WriteString(writer, md5String)
} else {
io.WriteString(writer, "fail")
}
}
func validateCaptcha(writer http.ResponseWriter, request *http.Request) {
secret := request.URL.Query().Get("secret")
clientId := validationStore.Get(secret, true)
if clientId != "" {
privateKey := userStore.Get(clientId, false)
md5String := fmt.Sprintf("%x", md5.Sum([]byte(privateKey+secret)))
io.WriteString(writer, md5String)
} else {
io.WriteString(writer, "fail")
}
}
func imageCaptcha(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
captchaId := vars["captchaId"]
width, err := strconv.Atoi(request.URL.Query().Get("w"))
if err != nil {
width = 200
}
height, err := strconv.Atoi(request.URL.Query().Get("h"))
if err != nil {
height = 100
}
writer.Header().Set("Content-Type", "image/png")
captcha.WriteImage(writer, captchaId, width, height)
}
func createRedisConn(address string, db int) (c redis.Conn) {
c, err := redis.Dial("tcp", address)
if err != nil {
log.Fatal(err)
}
c.Do("SELECT", db)
return c
}
func initRedisCaptchaStore() {
c := createRedisConn(":6379", 4)
rs := store.NewBytesStore(c)
captcha.SetCustomStore(rs)
}
func initRedisUserStore() {
c := createRedisConn(":6379", 5)
userStore = store.NewStringStore(c)
}
func initRedisValidationStore() {
c := createRedisConn(":6379", 6)
validationStore = store.NewStringStore(c)
}
func main() {
initRedisCaptchaStore()
initRedisUserStore()
initRedisValidationStore()
router := mux.NewRouter()
router.HandleFunc("/captcha/new", newCaptcha)
router.HandleFunc("/captcha/reload", reloadCaptcha)
router.HandleFunc("/captcha/verify", verifyCaptcha)
router.HandleFunc("/captcha/validate", validateCaptcha)
router.HandleFunc("/captcha/{captchaId}.png", imageCaptcha)
handler := cors.Default().Handler(router)
err := http.ListenAndServe(":8888", handler)
if err != nil {
log.Fatal(err)
}
}