-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (140 loc) · 3.69 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
155
156
157
158
159
160
161
package main
import (
"log"
"os"
"strings"
"time"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/basicauth"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
)
func interval(storage *storage) {
ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
storage.cleanUp()
prometheusStorageCount.Set(float64(storage.count()))
case <-quit:
ticker.Stop()
return
}
}
}()
}
func runCaptchaServer(c *cli.Context) error {
config := config{}
config.returnValue = c.Bool("return-value")
config.testImage = c.Bool("test-image")
username := c.String("auth-username")
password := c.String("auth-password")
storage := newStorage(!config.returnValue)
baseURL := strings.TrimRight(c.String("base-url"), "/")
promRegistry := getPrometheusRegistry()
handler := promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{})
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Prefork: false,
UnescapePath: true,
CaseSensitive: true,
StrictRouting: true,
BodyLimit: 1 * 512,
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
ctx.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
ctx.Status(code).SendString(err.Error())
return nil
},
})
if username != "" && password != "" {
app.Use(basicauth.New(basicauth.Config{
Users: map[string]string{
username: password,
},
Realm: "REST Captcha",
}))
}
app.Get("/metrics", adaptor.HTTPHandler(handler))
app.Post(baseURL+"/new", func(c *fiber.Ctx) error {
return httpNew(c, &config, storage)
})
app.Post(baseURL+"/solve", func(c *fiber.Ctx) error {
return httpSolve(c, &config, storage)
})
if config.testImage {
app.Get(baseURL+"/test-image", func(c *fiber.Ctx) error {
return httpNewTestImage(c, &config, storage)
})
}
if !config.returnValue {
interval(storage)
}
return app.Listen(c.String("listen"))
}
func main() {
app := cli.NewApp()
app.Usage = "REST Captcha"
app.EnableBashCompletion = true
app.Commands = []*cli.Command{
{
Name: "run",
Usage: "Run captcha server",
Action: runCaptchaServer,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "base-url",
Usage: "Base URL for routes",
Value: "/",
Required: false,
EnvVars: []string{"ASM_REST_CAPTCHA_BASE_URL"},
},
&cli.StringFlag{
Name: "auth-username",
Usage: "Basic authentication username",
Value: "",
Required: false,
EnvVars: []string{"ASM_REST_CAPTCHA_AUTH_USERNAME"},
},
&cli.StringFlag{
Name: "auth-password",
Usage: "Basic authentication password",
Value: "",
Required: false,
EnvVars: []string{"ASM_REST_CAPTCHA_AUTH_PASSWORD"},
},
&cli.StringFlag{
Name: "listen",
Usage: "Application listen http ip:port address",
Value: "127.0.0.1:4000",
Required: false,
EnvVars: []string{"ASM_REST_CAPTCHA_LISTEN_ADDRESS"},
},
&cli.BoolFlag{
Name: "return-value",
Usage: "Do not store result and return the captcha value insteed",
Value: false,
Required: false,
EnvVars: []string{"ASM_REST_CAPTCHA_RETURN_VALUE"},
},
&cli.BoolFlag{
Name: "test-image",
Usage: "Expose /test-image for testing image",
Value: false,
Required: false,
EnvVars: []string{"ASM_REST_CAPTCHA_TEST_IMAGE"},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}