diff options
Diffstat (limited to 'cmd/web/util.go')
| -rw-r--r-- | cmd/web/util.go | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/cmd/web/util.go b/cmd/web/util.go index 1252c66..2c32922 100644 --- a/cmd/web/util.go +++ b/cmd/web/util.go @@ -2,19 +2,21 @@ package main import ( "io" - "os" - "net/http" "net" + "net/http" + "os" "strings" ) +// UnauthorizedIfNotLocal is a middleware that returns unauthorized if not +// being accessed from loopback, as a basic form of host authentication. func UnauthorizedIfNotLocal(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !isLocal(r.RemoteAddr) { http.Error(w, "Unauthorized", http.StatusUnauthorized) Logger.Printf("Unauthorized request from: %s for %s", r.RemoteAddr, r.RequestURI) - return + return } h.ServeHTTP(w, r) }) @@ -25,10 +27,10 @@ func isLocal(addr string) bool { return localNet.Contains(net.ParseIP(strings.Split(addr, ":")[0])) } -// getHostIP attempts to guess the IP address of the current machine and +// GetHostIP attempts to guess the IP address of the current machine and // returns that. Simply bails at the first non sane looking IP and returns it. // Not ideal but it should work well enough most of the time -func getHostIP() string { +func GetHostIP() string { iFaces, err := net.Interfaces() if err != nil { return "127.0.0.1" @@ -40,7 +42,7 @@ func getHostIP() string { for _, iFace := range iFaces { addrs, err := iFace.Addrs() if err != nil { - return "127.0.0.1" + continue } for _, a := range addrs { @@ -68,7 +70,9 @@ func getPort() string { return s[1] } -func serveSelf(w http.ResponseWriter, r *http.Request) { +// ServeSelf tries to locate the currently running executable and serve +// it down to the client. +func ServeSelf(w http.ResponseWriter, r *http.Request) { s, err := os.Executable() if err != nil { Logger.Println("While trying to get my executable path: ", err) |
