From dc1011bb5f035473bb017e2922e5aa24570ec0ee Mon Sep 17 00:00:00 2001 From: Pierre Dubouilh Date: Fri, 13 Dec 2019 00:45:21 +0100 Subject: [PATCH] remove remote state --- readme.md | 2 +- src/gossa.go | 46 ++++++++-------------------------------- src/gossa_test.go | 17 +-------------- support/Caddyfile | 4 ++-- support/build.Dockerfile | 4 ++-- support/caddy.Dockerfile | 11 +++------- support/readme.md | 4 ++-- 7 files changed, 20 insertions(+), 68 deletions(-) diff --git a/readme.md b/readme.md index 1853fce..41249f4 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ gossa [![docker pulls](https://img.shields.io/docker/pulls/pldubouilh/gossa.svg?logo=docker)](https://hub.docker.com/r/pldubouilh/gossa) [![github downloads](https://img.shields.io/github/downloads/pldubouilh/gossa/total.svg?logo=github)](https://github.com/pldubouilh/gossa/releases) -a fast and simple webserver for your files, that's dependency-free and with 200 lines of code, easy to review. +a fast and simple webserver for your files, that's dependency-free and with under 200 lines of code, easy to review. a [simple UI](https://github.com/pldubouilh/gossa-ui) comes as default, featuring : diff --git a/src/gossa.go b/src/gossa.go index f4d1904..f937094 100755 --- a/src/gossa.go +++ b/src/gossa.go @@ -1,7 +1,6 @@ package main import ( - "crypto/sha256" "encoding/json" "errors" "flag" @@ -17,7 +16,6 @@ import ( "path/filepath" "strconv" "strings" - "sync" ) var host = flag.String("h", "127.0.0.1", "host to listen to") @@ -26,21 +24,17 @@ var history = flag.Bool("history", true, "keep history for paths visited. defaul var extraPath = flag.String("prefix", "/", "url prefix at which gossa can be reached, e.g. /gossa/ (slashes of importance)") var symlinks = flag.Bool("symlinks", false, "follow symlinks \033[4mWARNING\033[0m: symlinks will by nature allow to escape the defined path (default: false)") var verb = flag.Bool("verb", false, "verbosity") -var skipHidden = flag.Bool("k", true, "skip hidden files") +var skipHidden = flag.Bool("k", true, "\nskip hidden files") var initPath = "." -var historyPath = "" -var state = make(map[string]string) -var stateLock = sync.RWMutex{} var fs http.Handler var page, _ = template.New("pageTemplate").Parse(`template_will_be_here`) type rowTemplate struct { - Name string - Href template.HTML - Size string - Ext string - Selected bool + Name string + Href template.HTML + Size string + Ext string } type pageTemplate struct { @@ -61,10 +55,6 @@ func check(e error) { } } -func hash(s string) string { - return fmt.Sprintf("%x", sha256.Sum256([]byte(s))) -} - func exitPath(w http.ResponseWriter, s ...interface{}) { if r := recover(); r != nil { log.Println("error", s, r) @@ -97,13 +87,10 @@ func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path str title := "/" + strings.TrimPrefix(path, *extraPath) p := pageTemplate{} if path != *extraPath { - p.RowsFolders = append(p.RowsFolders, rowTemplate{"../", "../", "", "folder", false}) + p.RowsFolders = append(p.RowsFolders, rowTemplate{"../", "../", "", "folder"}) } p.ExtraPath = template.HTML(html.EscapeString(*extraPath)) p.Title = template.HTML(html.EscapeString(title)) - stateLock.Lock() - loc := state[hash(r.Header.Get("Authorization")+path)] - stateLock.Unlock() for _, el := range _files { if *skipHidden && strings.HasPrefix(el.Name(), ".") { @@ -115,10 +102,10 @@ func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path str href = strings.Replace(href, "/", "", 1) } if el.IsDir() { - p.RowsFolders = append(p.RowsFolders, rowTemplate{el.Name() + "/", template.HTML(href), "", "folder", loc == hash(el.Name()+"/")}) + p.RowsFolders = append(p.RowsFolders, rowTemplate{el.Name() + "/", template.HTML(href), "", "folder"}) } else { sl := strings.Split(el.Name(), ".") - p.RowsFiles = append(p.RowsFiles, rowTemplate{el.Name(), template.HTML(href), humanize(el.Size()), strings.ToLower(sl[len(sl)-1]), loc == hash(el.Name())}) + p.RowsFiles = append(p.RowsFiles, rowTemplate{el.Name(), template.HTML(href), humanize(el.Size()), strings.ToLower(sl[len(sl)-1])}) } } @@ -160,10 +147,7 @@ func rpc(w http.ResponseWriter, r *http.Request) { bodyBytes, _ := ioutil.ReadAll(r.Body) json.Unmarshal(bodyBytes, &rpc) defer exitPath(w, "rpc", rpc) - stateLock.Lock() - defer stateLock.Unlock() ret := "ok" - key := hash(r.Header.Get("Authorization") + rpc.Args[0]) if rpc.Call == "mkdirp" { err = os.MkdirAll(checkPath(rpc.Args[0]), os.ModePerm) @@ -171,16 +155,6 @@ func rpc(w http.ResponseWriter, r *http.Request) { err = os.Rename(checkPath(rpc.Args[0]), checkPath(rpc.Args[1])) } else if rpc.Call == "rm" { err = os.RemoveAll(checkPath(rpc.Args[0])) - } else if rpc.Call == "historySet" && *history { - if rpc.Args[2] == "hash" { - state[key] = hash(rpc.Args[1]) - } else { - state[key] = rpc.Args[1] - } - f, _ := json.MarshalIndent(state, "", " ") - ioutil.WriteFile(historyPath, f, 0644) - } else if rpc.Call == "historyGet" && *history { - ret = state[key] } check(err) @@ -205,14 +179,12 @@ func main() { fmt.Printf("\nusage: ./gossa ~/directory-to-share\n\n") flag.PrintDefaults() } + flag.Parse() if len(flag.Args()) > 0 { initPath = flag.Args()[0] } - historyPath = filepath.Join(initPath, ".gossa_history") - h, _ := ioutil.ReadFile(historyPath) - _ = json.Unmarshal(h, &state) initPath, err = filepath.Abs(initPath) check(err) diff --git a/src/gossa_test.go b/src/gossa_test.go index 1bf4160..f755e97 100644 --- a/src/gossa_test.go +++ b/src/gossa_test.go @@ -61,7 +61,7 @@ func fetchAndTestDefault(t *testing.T, url string) string { t.Fatal("error title") } - if !strings.Contains(body0, `

./

`) { + if !strings.Contains(body0, `

./

`) { t.Fatal("error header") } @@ -163,21 +163,6 @@ func doTest(t *testing.T, url string, testExtra bool) { t.Fatal("mv rpc errored") } - // ~~~~~~~~~~~~~~~~~ - // Test where auth header unset, otherwise it'd be apended to the first arg - fmt.Println("\r\n~~~~~~~~~~ test history rpc") - body0 = postJSON(t, url+"rpc", `{"call":"historySet","args":["a", "123", "skipHash"]}`) - body1 = postJSON(t, url+"rpc", `{"call":"historyGet","args":["a"]}`) - if body0 != `ok` || body1 != `123` { - t.Fatal("error history get/set unhashed") - } - - body0 = postJSON(t, url+"rpc", `{"call":"historySet","args":["b", "a", "hash"]}`) - body1 = postJSON(t, url+"rpc", `{"call":"historyGet","args":["b"]}`) - if body0 != `ok` || body1 != `ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb` { - t.Fatal("error history get/set hashed") - } - // ~~~~~~~~~~~~~~~~~ fmt.Println("\r\n~~~~~~~~~~ test upload in new folder") payload = "test" diff --git a/support/Caddyfile b/support/Caddyfile index 588f78c..05f97ee 100644 --- a/support/Caddyfile +++ b/support/Caddyfile @@ -1,8 +1,8 @@ # Caddy config -# to enable https just set a valid domain instead of mydomain.com - how simple ! +# to enable https just set a valid domain (e.g. mydomain.com) instead of :8080 - how simple ! # authentication has been setup with 2 users, alice and bob -mydomain.com +:8080 basicauth / alice paul basicauth / bob dylan proxy / 127.0.0.1:8001 \ No newline at end of file diff --git a/support/build.Dockerfile b/support/build.Dockerfile index 43a4cbe..de6f990 100644 --- a/support/build.Dockerfile +++ b/support/build.Dockerfile @@ -3,9 +3,9 @@ COPY . /gossaSrc RUN cd /gossaSrc && make FROM alpine -ENV UID="1000" GID="1000" HOST="0.0.0.0" PORT="8001" PREFIX="/" HISTORY="true" FOLLOW_SYMLINKS="false" SKIP_HIDDEN_FILES="true" DATADIR="/shared" +ENV UID="1000" GID="1000" HOST="0.0.0.0" PORT="8001" PREFIX="/" FOLLOW_SYMLINKS="false" SKIP_HIDDEN_FILES="true" DATADIR="/shared" EXPOSE 8001 RUN apk add --no-cache su-exec COPY --from=builder /gossaSrc/gossa /gossa -RUN echo -e 'exec su-exec ${UID}:${GID} /gossa -h ${HOST} -p ${PORT} -k=${SKIP_HIDDEN_FILES} --history=${HISTORY} --symlinks=${FOLLOW_SYMLINKS} --prefix=${PREFIX} ${DATADIR}'>> /start.sh +RUN echo -e 'exec su-exec ${UID}:${GID} /gossa -h ${HOST} -p ${PORT} -k=${SKIP_HIDDEN_FILES} --symlinks=${FOLLOW_SYMLINKS} --prefix=${PREFIX} ${DATADIR}'>> /start.sh ENTRYPOINT [ "sh", "/start.sh" ] diff --git a/support/caddy.Dockerfile b/support/caddy.Dockerfile index b558102..6773196 100644 --- a/support/caddy.Dockerfile +++ b/support/caddy.Dockerfile @@ -1,13 +1,8 @@ FROM pldubouilh/gossa -# download and prepare caddy -RUN apk update && apk add curl ca-certificates -RUN curl -L -o caddy.tar.gz "https://github.com/caddyserver/caddy/releases/download/v1.0.3/caddy_v1.0.3_linux_amd64.tar.gz" -RUN tar xvzf caddy.tar.gz && mv caddy /caddy +RUN apk update && apk add curl ca-certificates caddy -COPY Caddyfile / - -ENV UID="1000" GID="1000" HOST="127.0.0.1" PORT="8001" PREFIX="/" HISTORY="true" FOLLOW_SYMLINKS="false" SKIP_HIDDEN_FILES="true" DATADIR="/shared" +ENV UID="1000" GID="1000" HOST="127.0.0.1" PORT="8001" PREFIX="/" FOLLOW_SYMLINKS="false" SKIP_HIDDEN_FILES="true" DATADIR="/shared" EXPOSE 443 -RUN echo -e 'exec su-exec ${UID}:${GID} /gossa -h ${HOST} -p ${PORT} -k=${SKIP_HIDDEN_FILES} --history=${HISTORY} --symlinks=${FOLLOW_SYMLINKS} --prefix=${PREFIX} ${DATADIR} & \n /caddy'>> /start.sh +RUN echo -e 'exec su-exec ${UID}:${GID} /gossa -h ${HOST} -p ${PORT} -k=${SKIP_HIDDEN_FILES} --symlinks=${FOLLOW_SYMLINKS} --prefix=${PREFIX} ${DATADIR} & \n caddy' > /start.sh ENTRYPOINT [ "sh", "/start.sh" ] diff --git a/support/readme.md b/support/readme.md index 0ebad1b..39c141d 100644 --- a/support/readme.md +++ b/support/readme.md @@ -15,13 +15,13 @@ if you prefer building the image yourself : the options are settable through environment variables that can be passed starting off the docker image. -a fancy docker image using [Caddy](https://caddyserver.com/) is also provided. a simple config is embedded in the docker file, and shows how to use http basic authentication, and automatic TLS for hands-free https 🎉 +a fancy docker image using [Caddy](https://caddyserver.com/) is also provided. have a look at the simple config file `Caddyfile`, it shows how to use http basic authentication, and automatic TLS for hands-free https 🎉 ```sh # checkout the caddy config, build, and run docker image % vim caddy.Dockerfile % docker build -t gossa-caddy -f caddy.Dockerfile . -% sudo docker run -v ~/LocalDirToShare:/shared --net=host gossa-caddy +% sudo docker run -v ~/LocalDirToShare:/shared -v `pwd`/Caddyfile:/Caddyfile --net=host gossa-caddy ``` a docker-compose example image is also provided. running docker compose should be straightforward : `docker-compose up .` have a look in `docker-compose.yml` for further configuration. \ No newline at end of file