From 77eed08e7ac8ee5675d55c460e2c17e53f26bb4e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 4 Sep 2025 23:53:54 -0500 Subject: [PATCH] good stuff --- http.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index 2bf81af..f07c495 100644 --- a/http.go +++ b/http.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "net" "net/http" "strings" "time" @@ -17,6 +18,36 @@ func cleanURL(url string) string { return url } +func getIpSimple(r *http.Request) string { + host, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + log.Printf("could not split host port: %v", err) + return r.RemoteAddr // Fallback + } + return host +} + +// getClientIP inspects the request for common headers to find the true client IP. +func getClientIP(r *http.Request) string { + // Caddy sets the X-Forwarded-For header. + if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" { + // The header can be a comma-separated list of IPs. The first one is the original client. + ips := strings.Split(forwardedFor, ",") + return strings.TrimSpace(ips[0]) + } + + // Fallback to RemoteAddr if the header is not present. + host, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + return r.RemoteAddr + } + return host +} + +func whoSent(r *http.Request) string { + return log.Sprintf("%s\t%s", getClientIP(r), r.Header.Get("hostname")) +} + func okHandler(w http.ResponseWriter, r *http.Request) { msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte if err != nil { @@ -24,6 +55,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { return } + who := whoSent(r) + var route string route = cleanURL(r.URL.Path) parts := strings.Split(route, "?") @@ -43,7 +76,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { return } - log.Warn("forged REQUEST URL =", requrl, "msg =", len(msg)) + log.Warn("forged REQUEST URL =", requrl, "msg =", len(msg), "from =", who) if route == "/patchset" { if err := savePatchset(w, msg); err != nil { @@ -119,7 +152,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { writeFile(w, "ipv6.png") return } - log.Warn("BAD URL =", requrl) + log.Warn("BAD URL =", requrl, "from", who) badurl(w, r.URL.String()) }