summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-04 23:53:54 -0500
committerJeff Carr <[email protected]>2025-09-05 01:05:21 -0500
commit77eed08e7ac8ee5675d55c460e2c17e53f26bb4e (patch)
tree59852a21291338f9b7487bb7ba196678d7c0bd3d
parent3db711a6c07c1f09724d1ee4ff2165a5a164e196 (diff)
good stuff
-rw-r--r--http.go37
1 files 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())
}