fix(honeypot/naive): optimize hilariously

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2025-12-15 23:11:21 -05:00
parent ade8505b26
commit e0f4468b03
8 changed files with 124 additions and 40 deletions

View File

@@ -1,6 +1,7 @@
package internal
import (
"context"
"errors"
"fmt"
"log/slog"
@@ -13,6 +14,13 @@ import (
"github.com/sebest/xff"
)
type realIPKey struct{}
func RealIP(r *http.Request) (netip.Addr, bool) {
result, ok := r.Context().Value(realIPKey{}).(netip.Addr)
return result, ok
}
// TODO: move into config
type XFFComputePreferences struct {
StripPrivate bool
@@ -77,6 +85,9 @@ func RemoteXRealIP(useRemoteAddress bool, bindNetwork string, next http.Handler)
panic(err) // this should never happen
}
r.Header.Set("X-Real-Ip", host)
if addr, err := netip.ParseAddr(host); err == nil {
r = r.WithContext(context.WithValue(r.Context(), realIPKey{}, addr))
}
next.ServeHTTP(w, r)
})
}
@@ -129,8 +140,6 @@ func XForwardedForUpdate(stripPrivate bool, next http.Handler) http.Handler {
} else {
r.Header.Set("X-Forwarded-For", xffHeaderString)
}
slog.Debug("updating X-Forwarded-For", "original", origXFFHeader, "new", xffHeaderString)
})
}