aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/util.go
diff options
context:
space:
mode:
authorMitchell <mitch@riedstra.dev>2021-03-20 01:37:03 -0400
committerMitchell <mitch@riedstra.dev>2021-03-20 01:37:03 -0400
commitd047c36dd09b6169bf27c244196e99bb5df54c3a (patch)
tree5103e1951bd58bbef54c90a13c199e8d1d866492 /cmd/web/util.go
parent0e62a3b46b25e7c101b14ed44235f3c276982fc0 (diff)
downloadsteam-export-d047c36dd09b6169bf27c244196e99bb5df54c3a.tar.gz
steam-export-d047c36dd09b6169bf27c244196e99bb5df54c3a.tar.xz
Update documentation. Remove all traces of chdir from the steam library. Remove most linter complaints.
Diffstat (limited to 'cmd/web/util.go')
-rw-r--r--cmd/web/util.go18
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)