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. --- .gitignore | 1 + 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 +++++++++++++++++++++++++++++++-- demo/Dockerfile | 17 +++++++++++++++++ demo/docker-compose.yml | 31 ++++++++++++++++++++++++++++++ demo/entrypoint.sh | 34 +++++++++++++++++++++++++++++++++ 9 files changed, 173 insertions(+), 15 deletions(-) create mode 100644 demo/Dockerfile create mode 100644 demo/docker-compose.yml create mode 100755 demo/entrypoint.sh diff --git a/.gitignore b/.gitignore index af43141..ac7f9e1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ test*.go *.tgz *.tbz2 *.exe +demo/client-lib 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 diff --git a/demo/Dockerfile b/demo/Dockerfile new file mode 100644 index 0000000..5b1101a --- /dev/null +++ b/demo/Dockerfile @@ -0,0 +1,17 @@ +FROM golang:1.16-alpine + +RUN apk update +RUN apk add nginx + +RUN mkdir /code /steam-lib + +COPY . /code/ + +WORKDIR /code + +RUN go build -ldflags="-X 'main.Version=Demo'" -o /bin/steam-export ./cmd/web + +COPY demo/entrypoint.sh / + +ENTRYPOINT /entrypoint.sh + diff --git a/demo/docker-compose.yml b/demo/docker-compose.yml new file mode 100644 index 0000000..da8c2b9 --- /dev/null +++ b/demo/docker-compose.yml @@ -0,0 +1,31 @@ +--- +version: '3' + +services: + + server: + image: steam-export:latest + build: + dockerfile: demo/Dockerfile + context: .. + volumes: + - /home/mitch/.steam/steam/steamapps:/steam-lib + environment: + - USER_UID=1000 + - USER_GID=1000 + - TRUSTED_CIDRS=127.0.0.1/32 + - SHARE_LINK=http://server:8899/ + + client: + image: steam-export:latest + build: + dockerfile: demo/Dockerfile + context: .. + volumes: + # - /home/mitch/.steam/steam/steamapps:/steam-lib + - ./client-lib:/steam-lib + environment: + - USER_UID=1000 + - USER_GID=1000 + - TRUSTED_CIDRS=10.0.0.0/8,192.168.0.0/16,172.16.0.0/12,127.0.0.1/8 + - SHARE_LINK=http://client:8899/ diff --git a/demo/entrypoint.sh b/demo/entrypoint.sh new file mode 100755 index 0000000..e060597 --- /dev/null +++ b/demo/entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/sh +USER_SHELL="${USER_SHELL:-/bin/ash}" +# UID and GID used by the `git` user inside of the container +USER_UID="${USER_UID:-3500}" +USER_GID="${USER_GID:-3500}" + +TRUSTED_CIDRS="${TRUSTED_CIDRS:-127.0.0.1/8}" + +# LISTEN="${LISTEN:-:8899}" + +username="steam" +userhome="/steam" + +# This is only run once in the container's lifetime unless /setup is removed +setup() { +if [ -e /setup ] ; then return ; fi + +addgroup -g "${USER_GID}" "$username" +adduser -h "$userhome" --gecos "$FULL_NAME" -D \ + -s "${USER_SHELL}" -u "${USER_UID}" -G "$username" "$username" +passwd -u "$username" + +touch /setup +} + +run_steam_export() { +mkdir -p /steam-lib/common +chown -R "$username:$username" /steam +chown -R "$username:$username" /steam-lib +su steam -c "steam-export -demo -L /steam-lib -t \"$TRUSTED_CIDRS\" -s \"$SHARE_LINK\"" +} + +setup +run_steam_export -- cgit v1.2.3