From 1fcb5ee549fa7ba4b7bfa7e62c15dbb8a01c38b5 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sat, 3 Apr 2021 13:09:45 -0400 Subject: Some changes to make supporting a demo environment easier. Docker compose demo. --- cmd/web/app.go | 1 + cmd/web/handlers.go | 16 ++++++++-------- cmd/web/main.go | 9 +++++++++ cmd/web/templates/index.html | 45 +++++++++++++++++++++++++++++++++++++++----- cmd/web/util.go | 34 +++++++++++++++++++++++++++++++-- 5 files changed, 90 insertions(+), 15 deletions(-) (limited to 'cmd/web') diff --git a/cmd/web/app.go b/cmd/web/app.go index 1050343..bc8385a 100644 --- a/cmd/web/app.go +++ b/cmd/web/app.go @@ -30,6 +30,7 @@ type statusInfo struct { type App struct { Library *steamLib Status *statusInfo + Demo bool Templates *template.Template diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 661c411..4b86b58 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -28,19 +28,19 @@ func (a *App) HandleIndex(w http.ResponseWriter, r *http.Request) { err := a.Templates.ExecuteTemplate(w, "index", struct { - Lib *steam.Library - Info *statusInfo - Local bool - HostIP string - Port string - Version string + Lib *steam.Library + Info *statusInfo + Local bool + ShareLink string + Version string + Demo bool }{ &a.Library.Library, a.Status, isLocal(r.RemoteAddr), - GetHostIP(), - getPort(), + getShareLink(), Version, + a.Demo, }) if err != nil { Logger.Printf("While Rendering template: %s", err) diff --git a/cmd/web/main.go b/cmd/web/main.go index 7475478..c799624 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -32,6 +32,11 @@ func main() { debug := fl.Bool("d", false, "Print line numbers in log") fl.StringVar(&Listen, "l", Listen, "What address do we listen on?") fl.StringVar(&DefaultLib, "L", DefaultLib, "Full path to default library") + fl.StringVar(&isLocalCIDR, "t", isLocalCIDR, + "Trusted CIDRs for additional controls, seperated by commas") + fl.StringVar(&shareLink, "s", shareLink, "Share link, if blank make an educated guess") + isDemo := fl.Bool("demo", false, + "Whether or not to run in demo mode. You probably don't want this on.") fl.Parse(os.Args[1:]) if *debug { @@ -43,6 +48,10 @@ func main() { Logger.Fatal(err) } + if *isDemo { + a.Demo = true + } + go a.installer() r := mux.NewRouter() diff --git a/cmd/web/templates/index.html b/cmd/web/templates/index.html index eaaf722..6f015af 100644 --- a/cmd/web/templates/index.html +++ b/cmd/web/templates/index.html @@ -26,15 +26,24 @@ @@ -114,28 +125,37 @@

It also allows you to import games from across the network as well if you provide an HTTP url from which to download the game file as exported - from this application. + from this application. {{ if .Demo }}Downloads are however disabled for the + demo.{{end}}

+ {{ if .Demo }} + You would normally be able to download the application from here, but it's + disabled for the demo. See + here for more info. + {{ else }} You can download this application from this UI as well here. + {{ end }}

You can give people this link to view the library remotely and download games from your computer:

- http://{{.HostIP}}:{{.Port}}/ + {{.ShareLink}}

{{ else }}

Remote Steam library access

+ {{ if .Demo }}{{ else }} If you need this program to install the games click here. + {{ end }}

{{ end }} @@ -167,8 +187,15 @@ {{$key}} {{$val.GetSize}} + {{if $.Demo}} + + Download + + {{ else }} Download - + {{ end }} + @@ -357,7 +384,8 @@ - + + @@ -415,4 +443,11 @@ toast.show() } } + diff --git a/cmd/web/util.go b/cmd/web/util.go index 2c32922..454912f 100644 --- a/cmd/web/util.go +++ b/cmd/web/util.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "net" "net/http" @@ -22,9 +23,38 @@ func UnauthorizedIfNotLocal(h http.Handler) http.Handler { }) } +var isLocalCIDR = "127.0.0.1/8" + func isLocal(addr string) bool { - _, localNet, _ := net.ParseCIDR("127.0.0.1/8") - return localNet.Contains(net.ParseIP(strings.Split(addr, ":")[0])) + if strings.Contains(isLocalCIDR, ",") { + for _, cidr := range strings.Split(isLocalCIDR, ",") { + _, localNet, err := net.ParseCIDR(cidr) + if err != nil { + panic(err) + } + if localNet.Contains(net.ParseIP(strings.Split(addr, ":")[0])) { + return true + } + } + + return false + } else { + _, localNet, err := net.ParseCIDR(isLocalCIDR) + if err != nil { + panic(err) + } + return localNet.Contains(net.ParseIP(strings.Split(addr, ":")[0])) + } +} + +var shareLink = "" + +func getShareLink() string { + if shareLink != "" { + return shareLink + } + + return fmt.Sprintf("http://%s:%s/", GetHostIP(), getPort()) } // GetHostIP attempts to guess the IP address of the current machine and -- cgit v1.2.3