handle x-forwarded-for in IP logs
This commit is contained in:
parent
d9b71381b0
commit
23dbbf26e3
5 changed files with 17 additions and 13 deletions
|
@ -1,19 +1,21 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Returns the request's original IP address, resolving the `x-forwarded-for`
|
||||
// header if the request originates from a trusted proxy.
|
||||
func ResolveIP(r *http.Request) string {
|
||||
trustedProxies := []string{ "10.4.20.69" }
|
||||
if slices.Contains(trustedProxies, r.RemoteAddr) {
|
||||
func ResolveIP(app *model.AppState, r *http.Request) string {
|
||||
addr := strings.Split(r.RemoteAddr, ":")[0]
|
||||
if slices.Contains(app.Config.TrustedProxies, addr) {
|
||||
forwardedFor := r.Header.Get("x-forwarded-for")
|
||||
if len(forwardedFor) > 0 {
|
||||
return forwardedFor
|
||||
}
|
||||
}
|
||||
return r.RemoteAddr
|
||||
return addr
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue