home to your local SPACEGIRL 馃挮
arimelody.space
1package controller
2
3import (
4 "arimelody-web/model"
5 "net/http"
6 "slices"
7 "strings"
8)
9
10// Returns the request's original IP address, resolving the `x-forwarded-for`
11// header if the request originates from a trusted proxy.
12func ResolveIP(app *model.AppState, r *http.Request) string {
13 addr := strings.Split(r.RemoteAddr, ":")[0]
14 if slices.Contains(app.Config.TrustedProxies, addr) {
15 forwardedFor := r.Header.Get("x-forwarded-for")
16 if len(forwardedFor) > 0 {
17 // discard extra IPs; cloudflare tends to append their nodes
18 forwardedFor = strings.Split(forwardedFor, ", ")[0]
19 return forwardedFor
20 }
21 }
22 return addr
23}