-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrustproxy.go
136 lines (110 loc) · 2.6 KB
/
trustproxy.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
package main
import (
"fmt"
"log/slog"
"net"
"net/http"
"net/netip"
"strings"
)
var trustedIPs = []string{
"192.168.0.0/16",
"172.16.0.0/12",
"10.0.0.0/8",
"127.0.0.1/8",
"fd00::/8",
"::1",
}
var parsedTrustedIPs = parseIPs(trustedIPs)
var proxyIPHeaders = []string{
"X-Envoy-External-Address",
"X-Forwarded-For",
"X-Real-IP",
"True-Client-IP",
}
var schemeHeaders = []string{
"X-Forwarded-Proto",
"X-Forwarded-Scheme",
}
var xForwardedHost = "X-Forwarded-Host"
func trustProxy(logger *slog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
trusted, err := isTrustedIP(r.RemoteAddr, parsedTrustedIPs)
if err != nil {
logger.Error(err.Error(), slog.String("ip", r.RemoteAddr))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !trusted {
next.ServeHTTP(w, r)
return
}
if realIP := getRealIP(r.Header); realIP != "" {
r.RemoteAddr = realIP
}
if host := r.Header.Get(xForwardedHost); host != "" {
r.Host = host
}
if scheme := getScheme(r.Header); scheme != "" {
r.URL.Scheme = scheme
}
next.ServeHTTP(w, r)
})
}
}
func parseIPs(ips []string) []netip.Prefix {
var parsedIPs []netip.Prefix
for _, ipStr := range ips {
if strings.Contains(ipStr, "/") {
ipNet, err := netip.ParsePrefix(ipStr)
if err != nil {
panic(fmt.Sprintf("parsing CIDR expression: %s", err.Error()))
}
parsedIPs = append(parsedIPs, ipNet)
} else {
ipAddr, err := netip.ParseAddr(ipStr)
if err != nil {
panic(fmt.Sprintf("invalid IP address: '%s': %s", ipStr, err.Error()))
}
parsedIPs = append(parsedIPs, netip.PrefixFrom(ipAddr, ipAddr.BitLen()))
}
}
return parsedIPs
}
func isTrustedIP(remoteAddr string, trustedIPs []netip.Prefix) (bool, error) {
ipStr, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
ipStr = remoteAddr
}
ipAddr, err := netip.ParseAddr(ipStr)
if err != nil {
return false, err
}
for _, ipRange := range trustedIPs {
if ipRange.Contains(ipAddr) {
return true, nil
}
}
return false, nil
}
func getRealIP(headers http.Header) string {
var addr string
for _, proxyHeader := range proxyIPHeaders {
if value := headers.Get(proxyHeader); value != "" {
addr = strings.SplitN(value, ",", 2)[0]
break
}
}
return addr
}
func getScheme(headers http.Header) string {
var scheme string
for _, schemaHeader := range schemeHeaders {
if value := headers.Get(schemaHeader); value != "" {
scheme = strings.ToLower(scheme)
break
}
}
return scheme
}