diff --git a/cmd/version.go b/cmd/version.go index 880936d..3b74773 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.28.1" +var Version = "0.28.2" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 1756b2d..ff2e8e2 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -206,6 +206,28 @@ func refererToUri(referer string) string { return "/" + parts[3] } +func getRealIp(r *http.Request) string { + remoteAddr := strings.SplitAfter(r.RemoteAddr, ":") + + if len(remoteAddr) <= 0 { + return r.RemoteAddr + } + + remotePort := remoteAddr[len(remoteAddr)-1] + + cfIP := r.Header.Get("Cf-Connecting-Ip") + xRealIp := r.Header.Get("X-Real-Ip") + + switch { + case cfIP != "": + return cfIP + ":" + remotePort + case xRealIp != "": + return xRealIp + ":" + remotePort + default: + return r.RemoteAddr + } +} + func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensions *Dimensions, filters *Filters) error { fileName := filepath.Base(filePath) @@ -293,22 +315,11 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err w.Write(buf) if Verbose { - remoteAddr := "" - cfIP := r.Header.Get("Cf-Connecting-Ip") - xRealIp := r.Header.Get("X-Real-Ip") - if cfIP != "" { - remoteAddr = cfIP - } else if xRealIp != "" { - remoteAddr = xRealIp - } else { - remoteAddr = r.RemoteAddr - } - fmt.Printf("%v | Served %v (%v) to %v in %v\n", startTime.Format(LogDate), filePath, humanReadableSize(len(buf)), - remoteAddr, + getRealIp(r), time.Since(startTime).Round(time.Microsecond), ) }