-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
702 lines (617 loc) · 18.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package captcha_protect
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/libops/captcha-protect/internal/state"
lru "github.com/patrickmn/go-cache"
)
var (
lookupAddrFunc = net.LookupAddr
lookupIPFunc = net.LookupIP
log *slog.Logger
)
type Config struct {
RateLimit uint `json:"rateLimit"`
Window int64 `json:"window"`
IPv4SubnetMask int `json:"ipv4subnetMask"`
IPv6SubnetMask int `json:"ipv6subnetMask"`
IPForwardedHeader string `json:"ipForwardedHeader"`
IPDepth int `json:"ipDepth"`
ProtectParameters string `json:"protectParameters"`
ProtectRoutes []string `json:"protectRoutes"`
ExcludeRoutes []string `json:"excludeRoutes"`
ProtectFileExtensions []string `json:"protectFileExtensions"`
ProtectHttpMethods []string `json:"protectHttpMethods"`
GoodBots []string `json:"goodBots"`
ExemptIPs []string `json:"exemptIps"`
ChallengeURL string `json:"challengeURL"`
ChallengeTmpl string `json:"challengeTmpl"`
CaptchaProvider string `json:"captchaProvider"`
SiteKey string `json:"siteKey"`
SecretKey string `json:"secretKey"`
EnableStatsPage string `json:"enableStatsPage"`
LogLevel string `json:"loglevel,omitempty"`
PersistentStateFile string `json:"persistentStateFile"`
}
type CaptchaProtect struct {
next http.Handler
name string
config *Config
rateCache *lru.Cache
verifiedCache *lru.Cache
botCache *lru.Cache
captchaConfig CaptchaConfig
exemptIps []*net.IPNet
tmpl *template.Template
ipv4Mask net.IPMask
ipv6Mask net.IPMask
}
type CaptchaConfig struct {
js string
key string
validate string
}
type captchaResponse struct {
Success bool `json:"success"`
}
func CreateConfig() *Config {
return &Config{
RateLimit: 20,
Window: 86400,
IPv4SubnetMask: 16,
IPv6SubnetMask: 64,
IPForwardedHeader: "",
ProtectParameters: "false",
ProtectRoutes: []string{},
ExcludeRoutes: []string{},
ProtectHttpMethods: []string{},
ProtectFileExtensions: []string{
"html",
},
GoodBots: []string{},
ExemptIPs: []string{
"127.0.0.0/8",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"fc00::/8",
},
ChallengeURL: "/challenge",
ChallengeTmpl: "challenge.tmpl.html",
EnableStatsPage: "false",
LogLevel: "INFO",
IPDepth: 0,
}
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
var logLevel slog.LevelVar
logLevel.Set(slog.LevelInfo)
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: &logLevel,
})
log = slog.New(handler)
level, err := ParseLogLevel(config.LogLevel)
if err != nil {
log.Warn("Unknown log level", "err", err)
}
logLevel.Set(level)
expiration := time.Duration(config.Window) * time.Second
log.Debug("Captcha config", "config", config)
if len(config.ProtectRoutes) == 0 {
return nil, fmt.Errorf("you must protect at least one route with the protectRoutes config value. / will cover your entire site")
}
if len(config.ProtectHttpMethods) == 0 {
config.ProtectHttpMethods = []string{
"GET",
"HEAD",
}
}
config.ParseHttpMethods()
var tmpl *template.Template
if _, err := os.Stat(config.ChallengeTmpl); os.IsNotExist(err) {
log.Warn("Unable to find template file. Using default template.", "challengeTmpl", config.ChallengeTmpl)
ts := getDefaultTmpl()
tmpl, err = template.New("challenge").Parse(ts)
if err != nil {
return nil, fmt.Errorf("unable to parse challenge template: %v", err)
}
} else if err != nil {
return nil, fmt.Errorf("error checking for template file %s: %v", config.ChallengeTmpl, err)
} else {
tmpl, err = template.ParseFiles(config.ChallengeTmpl)
if err != nil {
return nil, fmt.Errorf("unable to parse challenge template file %s: %v", config.ChallengeTmpl, err)
}
}
// transform exempt IP strings into what go can easily parse (net.IPNet)
var ips []*net.IPNet
for _, ip := range config.ExemptIPs {
parsedIp, err := ParseCIDR(ip)
if err != nil {
return nil, fmt.Errorf("error parsing cidr %s: %v", ip, err)
}
ips = append(ips, parsedIp)
}
bc := CaptchaProtect{
next: next,
name: name,
config: config,
rateCache: lru.New(expiration, 1*time.Minute),
botCache: lru.New(expiration, 1*time.Hour),
verifiedCache: lru.New(expiration, 1*time.Hour),
exemptIps: ips,
tmpl: tmpl,
}
err = bc.SetIpv4Mask(config.IPv4SubnetMask)
if err != nil {
return nil, err
}
err = bc.SetIpv6Mask(config.IPv6SubnetMask)
if err != nil {
return nil, err
}
// set the captcha config based on the provider
// thanks to https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/blob/4708d76854c7ae95fa7313c46fbe21959be2fff1/pkg/captcha/captcha.go#L39-L55
// for the struct/idea
if config.CaptchaProvider == "hcaptcha" {
bc.captchaConfig = CaptchaConfig{
js: "https://hcaptcha.com/1/api.js",
key: "h-captcha",
validate: "https://api.hcaptcha.com/siteverify",
}
} else if config.CaptchaProvider == "recaptcha" {
bc.captchaConfig = CaptchaConfig{
js: "https://www.google.com/recaptcha/api.js",
key: "g-recaptcha",
validate: "https://www.google.com/recaptcha/api/siteverify",
}
} else if config.CaptchaProvider == "turnstile" {
bc.captchaConfig = CaptchaConfig{
js: "https://challenges.cloudflare.com/turnstile/v0/api.js",
key: "cf-turnstile",
validate: "https://challenges.cloudflare.com/turnstile/v0/siteverify",
}
} else {
return nil, fmt.Errorf("invalid captcha provider: %s", config.CaptchaProvider)
}
if config.PersistentStateFile != "" {
bc.loadState()
childCtx, cancel := context.WithCancel(ctx)
go bc.saveState(childCtx)
go func() {
<-ctx.Done()
log.Debug("Context canceled, calling child cancel...")
cancel()
}()
}
return &bc, nil
}
func (bc *CaptchaProtect) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
clientIP, ipRange := bc.getClientIP(req)
if req.URL.Path == bc.config.ChallengeURL {
if req.Method == http.MethodGet {
log.Info("Captcha challenge", "clientIP", clientIP, "method", req.Method, "path", req.URL.Path, "useragent", req.UserAgent())
bc.serveChallengePage(rw, req)
} else if req.Method == http.MethodPost {
statusCode := bc.verifyChallengePage(rw, req, clientIP)
log.Info("Captcha challenge", "clientIP", clientIP, "method", req.Method, "path", req.URL.Path, "status", statusCode, "useragent", req.UserAgent())
} else {
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
}
return
} else if req.URL.Path == "/captcha-protect/stats" && bc.config.EnableStatsPage == "true" {
log.Info("Captcha stats", "clientIP", clientIP, "method", req.Method, "path", req.URL.Path, "useragent", req.UserAgent())
bc.serveStatsPage(rw, clientIP)
return
}
if !bc.shouldApply(req, clientIP) {
bc.next.ServeHTTP(rw, req)
return
}
bc.registerRequest(ipRange)
if bc.trippedRateLimit(ipRange) {
encodedURI := url.QueryEscape(req.RequestURI)
url := fmt.Sprintf("%s?destination=%s", bc.config.ChallengeURL, encodedURI)
http.Redirect(rw, req, url, http.StatusFound)
} else {
bc.next.ServeHTTP(rw, req)
}
}
func (bc *CaptchaProtect) serveChallengePage(rw http.ResponseWriter, req *http.Request) {
d := map[string]string{
"SiteKey": bc.config.SiteKey,
"FrontendJS": bc.captchaConfig.js,
"FrontendKey": bc.captchaConfig.key,
"ChallengeURL": bc.config.ChallengeURL,
"Destination": req.URL.Query().Get("destination"),
}
err := bc.tmpl.Execute(rw, d)
if err != nil {
log.Error("Unable to execute go template", "tmpl", bc.config.ChallengeTmpl, "err", err)
http.Error(rw, "Internal error", http.StatusInternalServerError)
return
}
rw.WriteHeader(http.StatusOK)
}
func (bc *CaptchaProtect) verifyChallengePage(rw http.ResponseWriter, req *http.Request, ip string) int {
response := req.FormValue(bc.captchaConfig.key + "-response")
if response == "" {
http.Error(rw, "Bad request", http.StatusBadRequest)
return http.StatusBadRequest
}
var body = url.Values{}
body.Add("secret", bc.config.SecretKey)
body.Add("response", response)
resp, err := http.PostForm(bc.captchaConfig.validate, body)
if err != nil {
log.Error("Unable to validate captcha", "url", bc.captchaConfig.validate, "body", body, "err", err)
http.Error(rw, "Internal error", http.StatusInternalServerError)
return http.StatusInternalServerError
}
defer resp.Body.Close()
var captchaResponse captchaResponse
err = json.NewDecoder(resp.Body).Decode(&captchaResponse)
if err != nil {
log.Error("Unable to unmarshal captcha response", "url", bc.captchaConfig.validate, "err", err)
http.Error(rw, "Internal error", http.StatusInternalServerError)
return http.StatusInternalServerError
}
if captchaResponse.Success {
bc.verifiedCache.Set(ip, true, lru.DefaultExpiration)
destination := req.FormValue("destination")
if destination == "" {
destination = "%2F"
}
u, err := url.QueryUnescape(destination)
if err != nil {
log.Error("Unable to unescape destination", "destination", destination, "err", err)
u = "/"
}
http.Redirect(rw, req, u, http.StatusFound)
return http.StatusFound
}
http.Error(rw, "Validation failed", http.StatusForbidden)
return http.StatusForbidden
}
func (bc *CaptchaProtect) serveStatsPage(rw http.ResponseWriter, ip string) {
// only allow excluded IPs from viewing
if !IsIpExcluded(ip, bc.exemptIps) {
http.Error(rw, "Forbidden", http.StatusForbidden)
return
}
state := state.GetState(bc.rateCache.Items(), bc.botCache.Items(), bc.verifiedCache.Items())
jsonData, err := json.Marshal(state)
if err != nil {
log.Error("failed to marshal JSON", "err", err)
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
return
}
rw.WriteHeader(http.StatusOK)
rw.Header().Set("Content-Type", "application/json")
_, err = rw.Write(jsonData)
if err != nil {
log.Error("failed to write JSON on stats reques", "err", err)
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func (bc *CaptchaProtect) shouldApply(req *http.Request, clientIP string) bool {
if !strInSlice(req.Method, bc.config.ProtectHttpMethods) {
return false
}
_, verified := bc.verifiedCache.Get(clientIP)
if verified {
return false
}
if IsIpExcluded(clientIP, bc.exemptIps) {
return false
}
if bc.isGoodBot(req, clientIP) {
return false
}
return bc.RouteIsProtected(req.URL.Path)
}
func (bc *CaptchaProtect) RouteIsProtected(path string) bool {
protected:
for _, route := range bc.config.ProtectRoutes {
if !strings.HasPrefix(path, route) {
continue
}
// we're on a protected route - make sure this route doesn't have an exclusion
for _, eRoute := range bc.config.ExcludeRoutes {
if strings.HasPrefix(path, eRoute) {
continue protected
}
}
// if this path isn't a file, go ahead and mark this path as protected
ext := filepath.Ext(path)
ext = strings.TrimPrefix(ext, ".")
if ext == "" {
return true
}
// if we have a file extension, see if we should protect this file extension type
for _, protectedExtensions := range bc.config.ProtectFileExtensions {
if strings.EqualFold(ext, protectedExtensions) {
return true
}
}
}
return false
}
func IsIpExcluded(clientIP string, exemptIps []*net.IPNet) bool {
ip := net.ParseIP(clientIP)
for _, block := range exemptIps {
if block.Contains(ip) {
return true
}
}
return false
}
func (bc *CaptchaProtect) trippedRateLimit(ip string) bool {
v, ok := bc.rateCache.Get(ip)
if !ok {
log.Error("IP not found, but should already be set", "ip", ip)
return false
}
return v.(uint) > bc.config.RateLimit
}
func (bc *CaptchaProtect) registerRequest(ip string) {
err := bc.rateCache.Add(ip, uint(1), lru.DefaultExpiration)
if err == nil {
return
}
_, err = bc.rateCache.IncrementUint(ip, uint(1))
if err != nil {
log.Error("Unable to set rate cache", "ip", ip)
}
}
func (bc *CaptchaProtect) getClientIP(req *http.Request) (string, string) {
ip := req.Header.Get(bc.config.IPForwardedHeader)
if bc.config.IPForwardedHeader != "" && ip != "" {
components := strings.Split(ip, ",")
depth := bc.config.IPDepth
ip = ""
for i := len(components) - 1; i >= 0; i-- {
_ip := strings.TrimSpace(components[i])
if IsIpExcluded(_ip, bc.exemptIps) {
continue
}
if depth == 0 {
ip = _ip
break
}
depth--
}
if ip == "" {
log.Debug("No non-exempt IPs in header. req.RemoteAddr", "ipDepth", bc.config.IPDepth, bc.config.IPForwardedHeader, req.Header.Get(bc.config.IPForwardedHeader))
ip = req.RemoteAddr
}
} else {
if bc.config.IPForwardedHeader != "" {
log.Debug("Received a blank header value. Defaulting to real IP")
}
ip = req.RemoteAddr
}
if strings.Contains(ip, ":") {
host, _, _ := net.SplitHostPort(ip)
ip = host
}
return bc.ParseIp(ip)
}
func (bc *CaptchaProtect) ParseIp(ip string) (string, string) {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return ip, ip
}
// For IPv4 addresses
if parsedIP.To4() != nil {
subnet := parsedIP.Mask(bc.ipv4Mask)
return ip, subnet.String()
}
// For IPv6 addresses
if parsedIP.To16() != nil {
subnet := parsedIP.Mask(bc.ipv6Mask)
return ip, subnet.String()
}
log.Warn("Unknown ip version", "ip", ip)
return ip, ip
}
func (bc *CaptchaProtect) SetIpv4Mask(m int) error {
if m < 8 || m > 32 {
return fmt.Errorf("invalid ipv4 mask: %d. Must be between 8 and 32", m)
}
bc.ipv4Mask = net.CIDRMask(m, 32)
return nil
}
func (bc *CaptchaProtect) SetIpv6Mask(m int) error {
if m < 8 || m > 128 {
return fmt.Errorf("invalid ipv6 mask: %d. Must be between 8 and 128", m)
}
bc.ipv6Mask = net.CIDRMask(m, 128)
return nil
}
func (bc *CaptchaProtect) isGoodBot(req *http.Request, clientIP string) bool {
if bc.config.ProtectParameters == "true" {
if len(req.URL.Query()) > 0 {
return false
}
}
bot, ok := bc.botCache.Get(clientIP)
if ok {
return bot.(bool)
}
v := IsIpGoodBot(clientIP, bc.config.GoodBots)
bc.botCache.Set(clientIP, v, lru.DefaultExpiration)
return v
}
func IsIpGoodBot(clientIP string, goodBots []string) bool {
if len(goodBots) == 0 {
return false
}
// lookup the hostname for a given IP
hostname, err := lookupAddrFunc(clientIP)
if err != nil || len(hostname) == 0 {
return false
}
// then nslookup that hostname to avoid spoofing
resolvedIP, err := lookupIPFunc(hostname[0])
if err != nil || len(resolvedIP) == 0 || resolvedIP[0].String() != clientIP {
return false
}
// get the sld
// will be like 194.114.135.34.bc.googleusercontent.com.
// notice the trailing period
parts := strings.Split(hostname[0], ".")
l := len(parts)
if l < 3 {
return false
}
tld := parts[l-2]
domain := parts[l-3] + "." + tld
for _, bot := range goodBots {
if domain == bot {
return true
}
}
return false
}
func (bc *CaptchaProtect) SetExemptIps(exemptIps []*net.IPNet) {
bc.exemptIps = exemptIps
}
func ParseCIDR(cidr string) (*net.IPNet, error) {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
return ipNet, nil
}
// Map string to slog.Level
func ParseLogLevel(level string) (slog.Level, error) {
switch strings.ToUpper(level) {
case "DEBUG":
return slog.LevelDebug, nil
case "INFO":
return slog.LevelInfo, nil
case "WARNING", "WARN":
return slog.LevelWarn, nil
case "ERROR":
return slog.LevelError, nil
default:
return slog.LevelInfo, fmt.Errorf("unknown logl level %s", level)
}
}
// log a warning if protected methods contains an invalid method
func (c *Config) ParseHttpMethods() {
for _, method := range c.ProtectHttpMethods {
switch method {
case "GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "CONNECT", "OPTIONS", "TRACE":
continue
default:
log.Warn("unknown http method", "method", method)
}
}
}
func (bc *CaptchaProtect) saveState(ctx context.Context) {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
file, err := os.OpenFile(bc.config.PersistentStateFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Error("Unable to save state. Could not open or create file", "stateFile", bc.config.PersistentStateFile, "err", err)
return
}
// we made sure the file is writable, we can continue in our loop
file.Close()
for {
select {
case <-ticker.C:
log.Debug("Saving state")
state := state.GetState(bc.rateCache.Items(), bc.botCache.Items(), bc.verifiedCache.Items())
jsonData, err := json.Marshal(state)
if err != nil {
log.Error("failed unmarshalling state data", "err", err)
break
}
err = os.WriteFile(bc.config.PersistentStateFile, jsonData, 0644)
if err != nil {
log.Error("failed saving state data", "err", err)
}
case <-ctx.Done():
log.Debug("Context cancelled, stopping saveState")
return
}
}
}
func (bc *CaptchaProtect) loadState() {
fileContent, err := os.ReadFile(bc.config.PersistentStateFile)
if err != nil || len(fileContent) == 0 {
log.Warn("Failed to load state file.", "err", err)
return
}
var state state.State
err = json.Unmarshal(fileContent, &state)
if err != nil {
log.Error("Failed to unmarshal state file", "err", err)
return
}
for k, v := range state.Rate {
bc.rateCache.Set(k, v, lru.DefaultExpiration)
}
for k, v := range state.Bots {
bc.botCache.Set(k, v, lru.DefaultExpiration)
}
for k, v := range state.Verified {
bc.verifiedCache.Set(k, v, lru.DefaultExpiration)
}
log.Info("Loaded previous state")
}
func getDefaultTmpl() string {
return `<html>
<head>
<title>Verifying connection</title>
<script src="{{ .FrontendJS }}" async defer referrerpolicy="no-referrer"></script>
</head>
<body>
<h1>Verifying connection</h1>
<p>One moment while we verify your network connection.</p>
<form action="{{ .ChallengeURL }}" method="post" id="captcha-form" accept-charset="UTF-8">
<div
data-callback="captchaCallback"
class="{{ .FrontendKey }}"
data-sitekey="{{ .SiteKey }}"
data-theme="auto"
data-size="normal"
data-language="auto"
data-retry="auto"
interval="8000"
data-appearance="always">
</div>
<input type="hidden" name="destination" value="{{ .Destination }}">
</form>
<script type="text/javascript">
function captchaCallback(token) {
setTimeout(function() {
document.getElementById("captcha-form").submit();
}, 1000);
}
</script>
</body>
</html>`
}
func strInSlice(s string, sl []string) bool {
for _, a := range sl {
if a == s {
return true
}
}
return false
}