-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcaptcha.go
65 lines (51 loc) · 1.45 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
package main
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"strings"
"github.com/xkeyideal/captcha/pool"
"github.com/gin-gonic/gin"
)
/*
* demo
*/
const HTML_Content string = `
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form method="post" action="/v1/captcha">
<p><img src="{{.imgbase64}}" /></p>
<p><input name="captcha" placeholder="请输入验证码" type="text" /></p>
<input name="captcha_id" type="hidden" value="{{.CaptchaId}}" />
<input name="captcha_id" type="hidden" value="{{.Val}}" />
<input type="submit" />
</form>
</body>
</html>`
var CaptchaPool *pool.CaptchaPool
func GetCaptcha(c *gin.Context) {
cacheBuffer := CaptchaPool.GetImage()
base_url := base64.StdEncoding.EncodeToString(cacheBuffer.Data.Bytes())
html := strings.Replace(HTML_Content, "{{.CaptchaId}}", cacheBuffer.Id, -1)
html = strings.Replace(html, "{{.Val}}", string(cacheBuffer.Val), -1)
final_html := strings.Replace(html, "{{.imgbase64}}", "data:image/png;base64,"+base_url, -1)
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(http.StatusOK, final_html)
}
func main() {
CaptchaPool = pool.NewCaptchaPool(240, 80, 6, 2, 2, 2)
var router *gin.Engine
router = gin.Default()
router.GET("/v1/captcha", GetCaptcha)
if err := router.Run(fmt.Sprintf("0.0.0.0:%s", "7312")); err != nil {
fmt.Println("Eagle Eye Server Run Failed: ", err.Error())
os.Exit(-1)
}
CaptchaPool.Stop()
}