--- a/middleware.go Sat Jan 04 05:14:53 2020 -0600
+++ b/middleware.go Sat Jan 04 09:17:21 2020 -0600
@@ -7,6 +7,29 @@
+func getAddr(r *http.Request) string { + // check for the X-Forwarded-For header and use if it we have it + if fwd := r.Header.Get("X-Forwarded-For"); fwd != "" { + // Request.RemoteAddr is ipv4:port or [ipv6]:port so we have to trim off + // we use LastIndex so we can parse IPv6 addresses + if idx := strings.LastIndex(addr, ":"); idx != -1 { + // if the ipv6 was wrapped in [] we need to unwrap it + if idx := strings.Index(addr, "]"); idx != -1 { func basicHandler(h http.Handler, notFoundFile string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -18,11 +41,7 @@
// implement the NCSA Combined Log Format
// http://publib.boulder.ibm.com/tividd/td/ITWSA/ITWSA_info45/en_US/HTML/guide/c-logs.html#combined
- // we use LastIndex so we can parse IPv6 addresses
- if idx := strings.LastIndex(addr, ":"); idx != -1 {
if user, _, ok := r.BasicAuth(); ok && username != "" {