-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
90 lines (65 loc) · 1.85 KB
/
http.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
package main
import (
"encoding/base64"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
)
type newRequest struct {
Lang string `json:"lang,omitempty"`
TTL int64 `json:"ttl"`
Quality int `json:"quality"`
Level string `json:"level,omitempty"`
}
type newResponse struct {
ID string `json:"id"`
Image string `json:"image"`
Expire string `json:"expire"`
Value uint64 `json:"value"`
}
type solveRequest struct {
ID string `json:"id"`
Value uint64 `json:"value"`
}
func httpNewTestImage(c *fiber.Ctx, config *config, storage *storage) error {
r := new(newRequest)
r.Lang = c.Query("lang", "en")
quality, _ := strconv.Atoi(c.Query("q", "0"))
r.Level = c.Query("level", "0")
item := storage.newItem(getLevel(r.Level), r.Lang, r.TTL)
image := generateCaptcha(item, quality)
imageByte, _ := base64.StdEncoding.DecodeString(image)
c.Set("X-Value", strconv.Itoa(int(item.value)))
c.Set("Content-Type", "image/jpeg")
return c.Send(imageByte)
}
func httpNew(c *fiber.Ctx, config *config, storage *storage) error {
r := new(newRequest)
if err := c.BodyParser(r); err != nil {
return err
}
quality := 30
if r.Quality > 0 {
quality = minMaxDefault(r.Quality, 1, 95)
}
r.TTL = minMaxDefault64(r.TTL, 30, 600)
item := storage.newItem(getLevel(r.Level), r.Lang, r.TTL)
image := generateCaptcha(item, quality)
response := newResponse{ID: item.id, Image: "data:image/jpeg;base64," + image, Expire: item.expire.Format(time.RFC3339), Value: 0}
if config.returnValue {
response.Value = item.value
}
prometheusShowTotal.Inc()
return c.JSON(response)
}
func httpSolve(c *fiber.Ctx, config *config, storage *storage) error {
r := new(solveRequest)
if err := c.BodyParser(r); err != nil {
return err
}
valid := storage.validate(r.ID, r.Value)
if valid {
return c.Status(200).JSON(true)
}
return c.Status(400).JSON(false)
}