IP resolver: add v6 support
This commit is contained in:
parent
223cc8b60b
commit
e10f8da66b
1 changed files with 6 additions and 2 deletions
|
|
@ -10,14 +10,18 @@ import (
|
||||||
// Returns the request's original IP address, resolving the `x-forwarded-for`
|
// Returns the request's original IP address, resolving the `x-forwarded-for`
|
||||||
// header if the request originates from a trusted proxy.
|
// header if the request originates from a trusted proxy.
|
||||||
func ResolveIP(app *model.AppState, r *http.Request) string {
|
func ResolveIP(app *model.AppState, r *http.Request) string {
|
||||||
addr := strings.Split(r.RemoteAddr, ":")[0]
|
iSplit := strings.LastIndex(r.RemoteAddr, ":")
|
||||||
|
addr := r.RemoteAddr[0:iSplit]
|
||||||
if slices.Contains(app.Config.TrustedProxies, addr) {
|
if slices.Contains(app.Config.TrustedProxies, addr) {
|
||||||
forwardedFor := r.Header.Get("x-forwarded-for")
|
forwardedFor := r.Header.Get("x-forwarded-for")
|
||||||
if len(forwardedFor) > 0 {
|
if len(forwardedFor) > 0 {
|
||||||
// discard extra IPs; cloudflare tends to append their nodes
|
// discard extra IPs; cloudflare tends to append their nodes
|
||||||
forwardedFor = strings.Split(forwardedFor, ", ")[0]
|
forwardedFor = strings.Split(forwardedFor, ", ")[0]
|
||||||
return forwardedFor
|
addr = forwardedFor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if addr[0] == '[' && addr[len(addr) - 1] == ']' {
|
||||||
|
addr = addr[1:len(addr) - 1]
|
||||||
|
}
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue