forked from hanguofeng/gocaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (125 loc) · 3.42 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
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
// CServer project main.go
package main
import (
"flag"
"fmt"
"html"
"image/png"
"log"
"net/http"
"os"
"path/filepath"
"github.com/hanguofeng/config"
"github.com/ivysaur2019/gocaptcha"
)
var (
ccaptcha *gocaptcha.Captcha
configFile = flag.String("c", "gocaptcha.conf", "the config file")
)
const (
DEFAULT_PORT = "80"
DEFAULT_LOG = "logs/gocaptcha-server.log"
)
func ShowImageHandler(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
if len(key) >= 0 {
cimg, err := ccaptcha.GetImage(key)
log.Println("err", err)
if nil == err {
w.Header().Add("Content-Type", "image/png")
png.Encode(w, cimg)
} else {
log.Printf("show image error:%s", err.Error())
w.WriteHeader(500)
}
}
log.Printf("[cmd:showimage][remote_addr:%s][key:%s]", r.RemoteAddr, key)
}
func GetKeyHandler(w http.ResponseWriter, r *http.Request) {
callback := html.EscapeString(r.FormValue("callback"))
key, err := ccaptcha.GetKey(4)
retstr := "{\"code\":%d,\"msg\":\"%s\",\"data\":{\"key\":\"%s\"}}"
error_no := 0
error_msg := ""
if nil != err {
error_no = 1
error_msg = err.Error()
}
if callback != "" {
retstr = "%s(" + retstr + ")"
retstr = fmt.Sprintf(retstr, callback, error_no, error_msg, key)
} else {
retstr = fmt.Sprintf(retstr, error_no, error_msg, key)
}
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(retstr))
log.Printf("[cmd:getkey][remote_addr:%s][key:%s]", r.RemoteAddr, key)
}
func VerifyHandler(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
code := r.FormValue("code")
callback := html.EscapeString(r.FormValue("callback"))
retstr := "{\"code\":%d,\"msg\":\"%s\",\"data\":{\"key\":\"%s\"}}"
error_no := 0
error_msg := ""
suc, msg := ccaptcha.Verify(key, code)
if false == suc {
error_no = 1
error_msg = msg
}
if callback != "" {
retstr = "%s(" + retstr + ")"
retstr = fmt.Sprintf(retstr, callback, error_no, error_msg, key)
} else {
retstr = fmt.Sprintf(retstr, error_no, error_msg, key)
}
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(retstr))
log.Printf("[cmd:verify][remote_addr:%s][key:%s][code:%s]", r.RemoteAddr, key, code)
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
retstr := "welcome"
w.Write([]byte(retstr))
}
func main() {
flag.Parse()
/* 1.load the config file and assign port/logfile */
port := DEFAULT_PORT
logfile := DEFAULT_LOG
if _, err := os.Stat(*configFile); os.IsNotExist(err) {
log.Fatalf("config file:%s not exists!", *configFile)
os.Exit(1)
}
c, err := config.ReadDefault(*configFile)
if nil != err {
port = DEFAULT_PORT
logfile = DEFAULT_LOG
}
port, err = c.String("service", "port")
if nil != err {
port = DEFAULT_PORT
}
logfile, err = c.String("service", "logfile")
if nil != err {
logfile = DEFAULT_LOG
}
os.MkdirAll(filepath.Dir(logfile), 0777)
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE, 0666)
log.SetOutput(f)
captcha, err := gocaptcha.CreateCaptchaFromConfigFile(*configFile)
if nil != err {
log.Fatalf("config load failed:%s", err.Error())
} else {
ccaptcha = captcha
}
/* 2. bind handler */
http.HandleFunc("/showimage", ShowImageHandler)
http.HandleFunc("/getkey", GetKeyHandler)
http.HandleFunc("/verify", VerifyHandler)
http.HandleFunc("/", IndexHandler)
/* 3. run the http server */
s := &http.Server{Addr: ":" + port}
log.Printf("=======ready to serve=======")
log.Fatal(s.ListenAndServe())
f.Close()
}