-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.go
62 lines (51 loc) · 1.13 KB
/
detector.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
package traefik_tor_detector
import (
"context"
"errors"
"io"
"net/http"
"slices"
"strconv"
"strings"
)
type Config struct {
}
func CreateConfig() *Config {
return &Config{}
}
type Demo struct {
next http.Handler
name string
torIps []string
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
res, err := http.Get("https://check.torproject.org/torbulkexitlist")
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, errors.New("failed to get Tor exit nodes list. server returned non 200 response")
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var ips []string
for _, line := range strings.Split(string(body), "\n") {
trimmed := strings.TrimSpace(line)
if trimmed != "" {
ips = append(ips, trimmed)
}
}
return &Demo{
next: next,
name: name,
torIps: ips,
}, nil
}
func (a *Demo) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
clientIp := req.Header.Get("X-Real-Ip")
isTor := slices.Contains(a.torIps, clientIp)
req.Header.Add("X-Tor", strconv.FormatBool(isTor))
a.next.ServeHTTP(rw, req)
}