handle x-forwarded-for in IP logs

This commit is contained in:
ari melody 2025-02-07 16:54:36 +00:00
parent d9b71381b0
commit 23dbbf26e3
Signed by: ari
GPG key ID: CF99829C92678188
5 changed files with 17 additions and 13 deletions

View file

@ -21,6 +21,7 @@ func GetConfig() model.Config {
BaseUrl: "https://arimelody.me",
Host: "0.0.0.0",
Port: 8080,
TrustedProxies: []string{ "127.0.0.1" },
DB: model.DBConfig{
Host: "127.0.0.1",
Port: 5432,

View file

@ -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
}