-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
61 lines (51 loc) · 1.27 KB
/
utils.go
File metadata and controls
61 lines (51 loc) · 1.27 KB
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
package main
import (
"log"
"net"
"net/http"
"slices"
"strings"
)
var (
trustedProxies = []string{}
)
// Tries to find the source address, especially for environments using reverse proxies such as NGINX
// If the requesting ip is trusted, we also trust headers such as X-Forwarded-For
func RealSource(r *http.Request) string {
remoteAddr := r.RemoteAddr
host, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
log.Printf("[Error] RealSource: %v", err)
host = remoteAddr // Set to default value
}
if !slices.Contains(trustedProxies, host) {
// Address is not trusted; use normal remote address
return host
}
xForwarded := r.Header.Get("X-Forwarded-For")
if xForwarded != "" {
parts := strings.Split(xForwarded, ",")
return strings.TrimSpace(parts[0])
}
xRealIP := r.Header.Get("X-Real-IP")
if xRealIP != "" {
parts := strings.Split(xRealIP, ",")
return strings.TrimSpace(parts[0])
}
return host
}
/*
Used for IPv6 addresses in ratelimitting, to prevent only blacklisting /128 ranges,
since most networks have at least a /64 address.
*/
func normalizeIP(host string) string {
ip := net.ParseIP(host)
if ip == nil {
return ""
}
if ip.To4() != nil {
return ip.String()
}
masked := ip.Mask(net.CIDRMask(64, 128))
return masked.String() + "/64"
}