good stuff
This commit is contained in:
parent
3db711a6c0
commit
77eed08e7a
37
http.go
37
http.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -17,6 +18,36 @@ func cleanURL(url string) string {
|
||||||
return url
|
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) {
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -24,6 +55,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
who := whoSent(r)
|
||||||
|
|
||||||
var route string
|
var route string
|
||||||
route = cleanURL(r.URL.Path)
|
route = cleanURL(r.URL.Path)
|
||||||
parts := strings.Split(route, "?")
|
parts := strings.Split(route, "?")
|
||||||
|
@ -43,7 +76,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
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 route == "/patchset" {
|
||||||
if err := savePatchset(w, msg); err != nil {
|
if err := savePatchset(w, msg); err != nil {
|
||||||
|
@ -119,7 +152,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
writeFile(w, "ipv6.png")
|
writeFile(w, "ipv6.png")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Warn("BAD URL =", requrl)
|
log.Warn("BAD URL =", requrl, "from", who)
|
||||||
badurl(w, r.URL.String())
|
badurl(w, r.URL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue