mirror of
https://github.com/henrygd/beszel.git
synced 2026-08-19 16:57:47 +02:00
fix(hub): check X-Real-IP header to resolve agent's real source IP (#1973)
This commit is contained in:
@@ -317,6 +317,9 @@ func getRealIP(r *http.Request) string {
|
|||||||
if ip := r.Header.Get("CF-Connecting-IP"); ip != "" {
|
if ip := r.Header.Get("CF-Connecting-IP"); ip != "" {
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
|
if ip := r.Header.Get("X-Real-IP"); ip != "" {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
||||||
// X-Forwarded-For can contain a comma-separated list: "client_ip, proxy1, proxy2"
|
// X-Forwarded-For can contain a comma-separated list: "client_ip, proxy1, proxy2"
|
||||||
// Take the first one
|
// Take the first one
|
||||||
|
|||||||
@@ -1796,6 +1796,24 @@ func TestGetRealIP(t *testing.T) {
|
|||||||
remoteAddr: "127.0.0.1:12345",
|
remoteAddr: "127.0.0.1:12345",
|
||||||
expectedIP: "192.168.1.8",
|
expectedIP: "192.168.1.8",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "X-Real-IP header",
|
||||||
|
headers: map[string]string{"X-Real-IP": "10.8.0.4"},
|
||||||
|
remoteAddr: "172.21.0.1:12345",
|
||||||
|
expectedIP: "10.8.0.4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "X-Real-IP takes precedence over X-Forwarded-For",
|
||||||
|
headers: map[string]string{"X-Real-IP": "10.8.0.4", "X-Forwarded-For": "10.8.0.5"},
|
||||||
|
remoteAddr: "172.21.0.1:12345",
|
||||||
|
expectedIP: "10.8.0.4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CF-Connecting-IP takes precedence over X-Real-IP",
|
||||||
|
headers: map[string]string{"CF-Connecting-IP": "1.2.3.4", "X-Real-IP": "10.8.0.4"},
|
||||||
|
remoteAddr: "172.21.0.1:12345",
|
||||||
|
expectedIP: "1.2.3.4",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
|||||||
Reference in New Issue
Block a user