mirror of
https://github.com/pldubouilh/gossa.git
synced 2026-08-28 12:56:35 -04:00
Compare commits
50
Commits
state
...
rc/test-deploy
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
418af92512 | ||
|
|
a1613a08e8 | ||
|
|
99ac521aa8 | ||
|
|
f1dc8280f5 | ||
|
|
cd255a21f6 | ||
|
|
bf4be929f5 | ||
|
|
d99d975f70 | ||
|
|
177edf4791 | ||
|
|
16b31a5047 | ||
|
|
65a6087c60 | ||
|
|
5b55ab0a0a | ||
|
|
cb4631c086 | ||
|
|
de632f9ab7 | ||
|
|
4385249c94 | ||
|
|
5440b4c5a2 | ||
|
|
2f73fd9df8 | ||
|
|
688be87923 | ||
|
|
166869fc04 | ||
|
|
a2754c616d | ||
|
|
e2aa4f149e | ||
|
|
8a7a542b22 | ||
|
|
e490853baa | ||
|
|
557b3fd50d | ||
|
|
1992815b41 | ||
|
|
97e24cc513 | ||
|
|
c1eeb9820a | ||
|
|
dd6cb90e9d | ||
|
|
682e76da1f | ||
|
|
43e8e36ea2 | ||
|
|
56f418bbff | ||
|
|
c7e92cb730 | ||
|
|
5ea4838d19 | ||
|
|
50b9bc77ba | ||
|
|
2cd2753480 | ||
|
|
a39424159d | ||
|
|
32b64da0e6 | ||
|
|
2565dcca13 | ||
|
|
39611d2df3 | ||
|
|
55e729e434 | ||
|
|
7f690bc75f | ||
|
|
308ad9461e | ||
|
|
1119146cef | ||
|
|
f0abad8b78 | ||
|
|
551c334cdd | ||
|
|
49cfc28d34 | ||
|
|
be39e6aca8 | ||
|
|
b2e0396cf1 | ||
|
|
e22ef1bc12 | ||
|
|
5c9c8983e1 | ||
|
|
31775b6ae9 |
@@ -1,16 +1,15 @@
|
|||||||
name: Go
|
name: always
|
||||||
on: [push, pull_request]
|
on: [push]
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- name: Set up Go 1.12
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v1
|
||||||
with:
|
with:
|
||||||
go-version: 1.12
|
go-version: 1.19.1
|
||||||
id: go
|
id: go
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
@@ -20,3 +19,4 @@ jobs:
|
|||||||
|
|
||||||
- name: Run
|
- name: Run
|
||||||
run: make ci
|
run: make ci
|
||||||
|
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
name: deploy
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v1
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.19.1
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Set env
|
||||||
|
run: echo "GIT_TAG=`echo $(git describe --tags --abbrev=0)`" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v1.2.0
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v1.6.0
|
||||||
|
|
||||||
|
- name: Login to DockerHub
|
||||||
|
uses: docker/login-action@v1.10.0
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USER }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Build and release on dockerhub
|
||||||
|
uses: docker/build-push-action@v2.7.0
|
||||||
|
with:
|
||||||
|
file: support/build.Dockerfile
|
||||||
|
push: true
|
||||||
|
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
|
||||||
|
tags: pldubouilh/gossa:latest,pldubouilh/gossa:${{github.ref_name}}
|
||||||
|
|
||||||
|
- name: Build all artifacts
|
||||||
|
run: make build-all
|
||||||
|
|
||||||
|
- name: "Release gh release versioned"
|
||||||
|
uses: ncipollo/release-action@58ae73b360456532aafd58ee170c045abbeaee37
|
||||||
|
with:
|
||||||
|
allowUpdates: true
|
||||||
|
artifacts: "builds/*"
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: "Release gh release latest"
|
||||||
|
uses: ncipollo/release-action@58ae73b360456532aafd58ee170c045abbeaee37
|
||||||
|
with:
|
||||||
|
tag: latest
|
||||||
|
name: Latest
|
||||||
|
allowUpdates: true
|
||||||
|
artifacts: "builds/*"
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
name: rc
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- 'rc/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.19.1
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v1
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
|
||||||
|
- name: Run
|
||||||
|
run: make ci
|
||||||
|
|
||||||
|
- name: Build all artifacts
|
||||||
|
run: make build-all
|
||||||
|
|
||||||
|
- name: "Release gh release prerelease"
|
||||||
|
uses: ncipollo/release-action@58ae73b360456532aafd58ee170c045abbeaee37
|
||||||
|
with:
|
||||||
|
allowUpdates: true
|
||||||
|
prerelease: true
|
||||||
|
tag: "rc"
|
||||||
|
artifacts: "builds/*"
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
+10
@@ -4,10 +4,14 @@ gossa-linux-arm
|
|||||||
gossa-linux-arm64
|
gossa-linux-arm64
|
||||||
gossa-mac
|
gossa-mac
|
||||||
gossa-windows.exe
|
gossa-windows.exe
|
||||||
|
builds
|
||||||
|
gossa.test
|
||||||
|
|
||||||
|
*.out
|
||||||
.vscode
|
.vscode
|
||||||
test-fixture/*
|
test-fixture/*
|
||||||
test-fixture/*/*
|
test-fixture/*/*
|
||||||
|
!test-fixture/.testhidden
|
||||||
!test-fixture/compress
|
!test-fixture/compress
|
||||||
!test-fixture/compress/foo*
|
!test-fixture/compress/foo*
|
||||||
!test-fixture/gzip
|
!test-fixture/gzip
|
||||||
@@ -36,3 +40,9 @@ test-fixture/*/*
|
|||||||
!test-fixture/hols/scotland-1761292_1920.jpg
|
!test-fixture/hols/scotland-1761292_1920.jpg
|
||||||
!test-fixture/fancy-path
|
!test-fixture/fancy-path
|
||||||
!test-fixture/fancy-path/*
|
!test-fixture/fancy-path/*
|
||||||
|
!test-fixture/ext/*
|
||||||
|
!test-fixture/ext
|
||||||
|
!test-fixture/.some-hidden-file
|
||||||
|
!test-fixture/hols/.hidden-folder
|
||||||
|
!test-fixture/hols/.hidden-folder/some-file
|
||||||
|
!test-fixture/compress/hols-sym
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
language: go
|
|
||||||
go:
|
|
||||||
- "1.10"
|
|
||||||
script:
|
|
||||||
- make ci
|
|
||||||
@@ -1,56 +1,74 @@
|
|||||||
|
FLAGS := -ldflags "-s -w" -trimpath
|
||||||
|
NOCGO := CGO_ENABLED=0
|
||||||
|
|
||||||
build:
|
build:
|
||||||
cp src/gossa.go gossa.go
|
|
||||||
make -C gossa-ui/
|
|
||||||
go vet && go fmt
|
go vet && go fmt
|
||||||
CGO_ENABLED=0 go build gossa.go
|
${NOCGO} go build ${FLAGS} -o gossa
|
||||||
rm gossa.go
|
|
||||||
|
install:
|
||||||
|
sudo cp gossa /usr/local/bin
|
||||||
|
|
||||||
run:
|
run:
|
||||||
make build
|
./gossa -verb=true test-fixture
|
||||||
./gossa test-fixture
|
|
||||||
|
run-ro:
|
||||||
|
./gossa -verb=true -ro=true test-fixture
|
||||||
|
|
||||||
run-extra:
|
run-extra:
|
||||||
make build
|
./gossa -verb=true -prefix="/fancy-path/" -k=false -symlinks=true test-fixture
|
||||||
./gossa -prefix="/fancy-path/" -symlinks=true test-fixture
|
|
||||||
|
|
||||||
ci:
|
ci: build-all test
|
||||||
-@cd test-fixture && ln -s ../docker .
|
echo "done"
|
||||||
make build
|
|
||||||
timeout 10 make run &
|
test:
|
||||||
sleep 11 && timeout 10 make run-extra &
|
-@cd test-fixture && ln -s ../support .; true
|
||||||
cp src/gossa_test.go . && go test
|
go test -cover -c -tags testrunmain
|
||||||
rm gossa_test.go
|
|
||||||
|
timeout -s SIGINT 3 ./gossa.test -test.coverprofile=normal.out -test.run '^TestRunMain' -verb=true test-fixture &
|
||||||
|
sleep 2
|
||||||
|
go test -run TestNormal
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
timeout -s SIGINT 3 ./gossa.test -test.coverprofile=extra.out -test.run '^TestRunMain' -prefix='/fancy-path/' -k=false -symlinks=true test-fixture &
|
||||||
|
sleep 2
|
||||||
|
go test -run TestExtra
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
timeout -s SIGINT 3 ./gossa.test -test.coverprofile=ro.out -test.run '^TestRunMain' -ro=true test-fixture &
|
||||||
|
sleep 2
|
||||||
|
go test -run TestRo
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
# gocovmerge ro.out extra.out normal.out > all.out
|
||||||
|
# go tool cover -html all.out
|
||||||
|
# go tool cover -func=all.out | grep main | grep '9.\..\%'
|
||||||
|
|
||||||
watch:
|
watch:
|
||||||
ls src/* gossa-ui/* | entr -rc make run
|
ls gossa.go gossa_test.go gossa-ui/* | entr -rc make build run
|
||||||
|
|
||||||
watch-extra:
|
watch-extra:
|
||||||
ls src/* gossa-ui/* | entr -rc make run-extra
|
ls gossa.go gossa_test.go gossa-ui/* | entr -rc make build run-extra
|
||||||
|
|
||||||
watch-ci:
|
watch-ro:
|
||||||
ls src/* gossa-ui/* | entr -rc make ci
|
ls gossa.go gossa_test.go gossa-ui/* | entr -rc make build run-ro
|
||||||
|
|
||||||
build-all:
|
watch-test:
|
||||||
cp src/gossa.go gossa.go
|
ls gossa.go gossa_test.go gossa-ui/* | entr -rc make test
|
||||||
make -C gossa-ui/
|
|
||||||
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build gossa.go
|
build-all: build
|
||||||
mv gossa gossa-linux64
|
${NOCGO} GOOS=linux GOARCH=amd64 go build ${FLAGS} -o builds/gossa-linux-x64
|
||||||
env GOOS=linux GOARCH=arm go build gossa.go
|
${NOCGO} GOOS=linux GOARCH=arm go build ${FLAGS} -o builds/gossa-linux-arm
|
||||||
mv gossa gossa-linux-arm
|
${NOCGO} GOOS=linux GOARCH=arm64 go build ${FLAGS} -o builds/gossa-linux-arm64
|
||||||
env GOOS=linux GOARCH=arm64 go build gossa.go
|
${NOCGO} GOOS=darwin GOARCH=amd64 go build ${FLAGS} -o builds/gossa-mac-x64
|
||||||
mv gossa gossa-linux-arm64
|
${NOCGO} GOOS=darwin GOARCH=arm64 go build ${FLAGS} -o builds/gossa-mac-arm64
|
||||||
env GOOS=darwin GOARCH=amd64 go build gossa.go
|
${NOCGO} GOOS=windows GOARCH=amd64 go build ${FLAGS} -o builds/gossa-windows.exe
|
||||||
mv gossa gossa-mac
|
sha256sum builds/*
|
||||||
env GOOS=windows GOARCH=amd64 go build gossa.go
|
|
||||||
mv gossa.exe gossa-windows.exe
|
|
||||||
rm gossa.go
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
-rm gossa.go
|
|
||||||
-rm gossa_test.go
|
|
||||||
-rm gossa
|
-rm gossa
|
||||||
-rm gossa-linux64
|
-rm gossa-linux64
|
||||||
-rm gossa-linux-arm
|
-rm gossa-linux-arm
|
||||||
-rm gossa-linux-arm64
|
-rm gossa-linux-arm64
|
||||||
-rm gossa-mac
|
-rm gossa-mac
|
||||||
-rm gossa-windows.exe
|
-rm gossa-windows.exe
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
FROM golang:latest as builder
|
|
||||||
COPY . /gossaSrc
|
|
||||||
RUN cd /gossaSrc && make
|
|
||||||
|
|
||||||
FROM alpine
|
|
||||||
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} --symlinks=${FOLLOW_SYMLINKS} --prefix=${PREFIX} ${DATADIR}'>> /start.sh
|
|
||||||
ENTRYPOINT [ "sh", "/start.sh" ]
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
FROM alpine
|
|
||||||
|
|
||||||
# download and prepare caddy
|
|
||||||
RUN apk update && apk add curl ca-certificates
|
|
||||||
RUN curl -L -o caddy.tar.gz "https://github.com/mholt/caddy/releases/download/v0.11.1/caddy_v0.11.1_linux_amd64.tar.gz"
|
|
||||||
RUN tar xvzf caddy.tar.gz && mv caddy /caddy
|
|
||||||
|
|
||||||
# download and prepare gossa
|
|
||||||
RUN curl -L -o /gossa "https://github.com/pldubouilh/gossa/releases/download/v0.0.7/gossa-linux64"
|
|
||||||
RUN chmod +x /gossa /caddy
|
|
||||||
|
|
||||||
# Caddy config:
|
|
||||||
# * http basic auth is implemented here, with bob as user and dylan as password
|
|
||||||
# * to enable https just set a valid domain instead of *:8001 - how simple !
|
|
||||||
RUN echo -e '\n\
|
|
||||||
*:8001\n\
|
|
||||||
basicauth / bob dylan\n\
|
|
||||||
proxy / 127.0.0.1:8000\n\
|
|
||||||
'>> /Caddyfile
|
|
||||||
|
|
||||||
RUN echo -e '/gossa -h 127.0.0.1 -p 8000 /shared & \n /caddy'>> /start.sh
|
|
||||||
ENTRYPOINT [ "sh", "/start.sh" ]
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
FROM alpine
|
|
||||||
|
|
||||||
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
|
|
||||||
RUN wget https://github.com/pldubouilh/gossa/releases/download/v0.0.8/gossa-linux64 && mv gossa-linux64 /gossa && chmod +x /gossa
|
|
||||||
|
|
||||||
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" ]
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
the master branch is automatically built and pushed to [dockerhub](https://hub.docker.com/r/pldubouilh/gossa) under `pldubouilh/gossa`.
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# pull from dockerhub and run
|
|
||||||
% sudo docker run -v ~/LocalDirToShare:/shared -p 8001:8001 pldubouilh/gossa
|
|
||||||
```
|
|
||||||
|
|
||||||
if you prefer building the image yourself :
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# build gossa within a build container, needs to be ran within the sources, ../ from here
|
|
||||||
% docker build -t gossa -f docker/build.Dockerfile .
|
|
||||||
|
|
||||||
# and to run it simply
|
|
||||||
% sudo docker run -v ~/LocalDirToShare:/shared -p 8001:8001 gossa
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
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 🎉
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# run with caddy, checkout the config in the dockerfile
|
|
||||||
% docker build -t gossa -f caddy.Dockerfile .
|
|
||||||
|
|
||||||
# run with caddy
|
|
||||||
% sudo docker run -v ~/LocalDirToShare:/shared --net=host gossa
|
|
||||||
```
|
|
||||||
|
|
||||||
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.
|
|
||||||
+1
-1
Submodule gossa-ui updated: 1a8e83cc7a...5ec0a804c9
@@ -0,0 +1,271 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"compress/gzip"
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"html"
|
||||||
|
"html/template"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type rpcCall struct {
|
||||||
|
Call string `json:"call"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var rootPath = ""
|
||||||
|
var handler http.Handler
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func exitPath(w http.ResponseWriter, s ...interface{}) {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
log.Println("error", s, r)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte("error"))
|
||||||
|
} else if *verb {
|
||||||
|
log.Println(s...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func humanize(bytes int64) string {
|
||||||
|
b := float64(bytes)
|
||||||
|
u := 0
|
||||||
|
for {
|
||||||
|
if b < 1024 {
|
||||||
|
return strconv.FormatFloat(b, 'f', 1, 64) + [9]string{"B", "k", "M", "G", "T", "P", "E", "Z", "Y"}[u]
|
||||||
|
}
|
||||||
|
b = b / 1024
|
||||||
|
u++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path string) {
|
||||||
|
_files, err := ioutil.ReadDir(fullPath)
|
||||||
|
check(err)
|
||||||
|
sort.Slice(_files, func(i, j int) bool { return strings.ToLower(_files[i].Name()) < strings.ToLower(_files[j].Name()) })
|
||||||
|
|
||||||
|
if !strings.HasSuffix(path, "/") {
|
||||||
|
path += "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
title := "/" + strings.TrimPrefix(path, *extraPath)
|
||||||
|
p := pageTemplate{}
|
||||||
|
if path != *extraPath {
|
||||||
|
p.RowsFolders = append(p.RowsFolders, rowTemplate{"../", "../", "", "folder"})
|
||||||
|
}
|
||||||
|
p.ExtraPath = template.HTML(html.EscapeString(*extraPath))
|
||||||
|
p.Ro = *ro
|
||||||
|
p.Title = template.HTML(html.EscapeString(title))
|
||||||
|
|
||||||
|
for _, el := range _files {
|
||||||
|
if *skipHidden && strings.HasPrefix(el.Name(), ".") {
|
||||||
|
continue // dont print hidden files if we're not allowed
|
||||||
|
}
|
||||||
|
if !*symlinks && el.Mode()&os.ModeSymlink != 0 {
|
||||||
|
continue // dont print symlinks if were not allowed
|
||||||
|
}
|
||||||
|
|
||||||
|
el, err := os.Stat(fullPath + "/" + el.Name())
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error - cant stat a file", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
href := url.PathEscape(el.Name())
|
||||||
|
if el.IsDir() && strings.HasPrefix(href, "/") {
|
||||||
|
href = strings.Replace(href, "/", "", 1)
|
||||||
|
}
|
||||||
|
if el.IsDir() {
|
||||||
|
p.RowsFolders = append(p.RowsFolders, rowTemplate{el.Name() + "/", template.HTML(href), "", "folder"})
|
||||||
|
} else {
|
||||||
|
sl := strings.Split(el.Name(), ".")
|
||||||
|
ext := strings.ToLower(sl[len(sl)-1])
|
||||||
|
p.RowsFiles = append(p.RowsFiles, rowTemplate{el.Name(), template.HTML(href), humanize(el.Size()), ext})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.Header().Add("Content-Encoding", "gzip")
|
||||||
|
gz, err := gzip.NewWriterLevel(w, gzip.BestSpeed) // BestSpeed is Much Faster than default - base on a very unscientific local test, and only ~30% larger (compression remains still very effective, ~6x)
|
||||||
|
check(err)
|
||||||
|
defer gz.Close()
|
||||||
|
templateParsed.Execute(gz, p)
|
||||||
|
} else {
|
||||||
|
templateParsed.Execute(w, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func doContent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.HasPrefix(r.URL.Path, *extraPath) { // redir when were not hitting the supplementary path if one is set
|
||||||
|
http.Redirect(w, r, *extraPath, http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
path := html.UnescapeString(r.URL.Path)
|
||||||
|
defer exitPath(w, "get content", path)
|
||||||
|
fullPath := enforcePath(path)
|
||||||
|
stat, errStat := os.Stat(fullPath)
|
||||||
|
check(errStat)
|
||||||
|
|
||||||
|
if stat.IsDir() {
|
||||||
|
replyList(w, r, fullPath, path)
|
||||||
|
} else {
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func upload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path := r.Header.Get("gossa-path")
|
||||||
|
defer exitPath(w, "upload", path)
|
||||||
|
|
||||||
|
path, err := url.PathUnescape(path)
|
||||||
|
check(err)
|
||||||
|
reader, err := r.MultipartReader()
|
||||||
|
check(err)
|
||||||
|
part, err := reader.NextPart()
|
||||||
|
if err != nil && err != io.EOF { // errs EOF when no more parts to process
|
||||||
|
check(err)
|
||||||
|
}
|
||||||
|
dst, err := os.Create(enforcePath(path))
|
||||||
|
check(err)
|
||||||
|
io.Copy(dst, part)
|
||||||
|
w.Write([]byte("ok"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func zipRPC(w http.ResponseWriter, r *http.Request) {
|
||||||
|
zipPath := r.URL.Query().Get("zipPath")
|
||||||
|
zipName := r.URL.Query().Get("zipName")
|
||||||
|
defer exitPath(w, "zip", zipPath)
|
||||||
|
zipFullPath := enforcePath(zipPath)
|
||||||
|
_, err := os.Lstat(zipFullPath)
|
||||||
|
check(err)
|
||||||
|
w.Header().Add("Content-Disposition", "attachment; filename=\""+zipName+".zip\"")
|
||||||
|
zipWriter := zip.NewWriter(w)
|
||||||
|
defer zipWriter.Close()
|
||||||
|
|
||||||
|
err = filepath.Walk(zipFullPath, func(path string, f fs.FileInfo, err error) error {
|
||||||
|
check(err)
|
||||||
|
if f.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rel, err := filepath.Rel(zipFullPath, path)
|
||||||
|
check(err)
|
||||||
|
if *skipHidden && (strings.HasPrefix(rel, ".") || strings.HasPrefix(f.Name(), ".")) {
|
||||||
|
return nil // hidden files not allowed
|
||||||
|
}
|
||||||
|
if f.Mode()&os.ModeSymlink != 0 {
|
||||||
|
panic(errors.New("symlink not allowed in zip downloads")) // filepath.Walk doesnt support symlinks
|
||||||
|
}
|
||||||
|
|
||||||
|
header, err := zip.FileInfoHeader(f)
|
||||||
|
check(err)
|
||||||
|
header.Name = filepath.ToSlash(rel) // make the paths consistent between OSes
|
||||||
|
header.Method = zip.Store
|
||||||
|
headerWriter, err := zipWriter.CreateHeader(header)
|
||||||
|
check(err)
|
||||||
|
file, err := os.Open(path)
|
||||||
|
check(err)
|
||||||
|
defer file.Close()
|
||||||
|
_, err = io.Copy(headerWriter, file)
|
||||||
|
check(err)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
check(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rpc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var err error
|
||||||
|
var rpc rpcCall
|
||||||
|
defer exitPath(w, "rpc", rpc)
|
||||||
|
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||||
|
check(err)
|
||||||
|
json.Unmarshal(bodyBytes, &rpc)
|
||||||
|
|
||||||
|
if rpc.Call == "mkdirp" {
|
||||||
|
err = os.MkdirAll(enforcePath(rpc.Args[0]), os.ModePerm)
|
||||||
|
} else if rpc.Call == "mv" {
|
||||||
|
err = os.Rename(enforcePath(rpc.Args[0]), enforcePath(rpc.Args[1]))
|
||||||
|
} else if rpc.Call == "rm" {
|
||||||
|
err = os.RemoveAll(enforcePath(rpc.Args[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
check(err)
|
||||||
|
w.Write([]byte("ok"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func enforcePath(p string) string {
|
||||||
|
joined := filepath.Join(rootPath, strings.TrimPrefix(p, *extraPath))
|
||||||
|
fp, err := filepath.Abs(joined)
|
||||||
|
sl, _ := filepath.EvalSymlinks(fp) // err skipped as it would error for unexistent files (RPC check). The actual behaviour is tested below
|
||||||
|
|
||||||
|
// panic if we had a error getting absolute path,
|
||||||
|
// ... or if path doesnt contain the prefix path we expect,
|
||||||
|
// ... or if we're skipping hidden folders, and one is requested,
|
||||||
|
// ... or if we're skipping symlinks, path exists, and a symlink out of bound requested
|
||||||
|
if err != nil || !strings.HasPrefix(fp, rootPath) || *skipHidden && strings.Contains(p, "/.") || !*symlinks && len(sl) > 0 && !strings.HasPrefix(sl, rootPath) {
|
||||||
|
panic(errors.New("invalid path"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return fp
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if flag.Parse(); len(flag.Args()) > 0 {
|
||||||
|
rootPath = flag.Args()[0]
|
||||||
|
} else {
|
||||||
|
fmt.Printf("\nusage: ./gossa ~/directory-to-share\n\n")
|
||||||
|
flag.PrintDefaults()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
rootPath, err = filepath.Abs(rootPath)
|
||||||
|
check(err)
|
||||||
|
server := &http.Server{Addr: *host + ":" + *port, Handler: handler}
|
||||||
|
|
||||||
|
// clean shutdown - used only for coverage test
|
||||||
|
// go func() {
|
||||||
|
// sigchan := make(chan os.Signal, 1)
|
||||||
|
// signal.Notify(sigchan, os.Interrupt)
|
||||||
|
// <-sigchan
|
||||||
|
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
// defer cancel()
|
||||||
|
// server.Shutdown(ctx)
|
||||||
|
// }()
|
||||||
|
|
||||||
|
if !*ro {
|
||||||
|
http.HandleFunc(*extraPath+"rpc", rpc)
|
||||||
|
http.HandleFunc(*extraPath+"post", upload)
|
||||||
|
}
|
||||||
|
http.HandleFunc(*extraPath+"zip", zipRPC)
|
||||||
|
http.HandleFunc("/", doContent)
|
||||||
|
handler = http.StripPrefix(*extraPath, http.FileServer(http.Dir(rootPath)))
|
||||||
|
|
||||||
|
fmt.Printf("Gossa starting on directory %s\nListening on http://%s:%s%s\n", rootPath, *host, *port, *extraPath)
|
||||||
|
if err = server.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
|
check(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/base64"
|
||||||
|
"flag"
|
||||||
|
"html/template"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed gossa-ui/script.js
|
||||||
|
var scriptJs string
|
||||||
|
|
||||||
|
//go:embed gossa-ui/style.css
|
||||||
|
var styleCss string
|
||||||
|
|
||||||
|
//go:embed gossa-ui/favicon.svg
|
||||||
|
var faviconSvg []byte
|
||||||
|
|
||||||
|
//go:embed gossa-ui/ui.tmpl
|
||||||
|
var templateStr string
|
||||||
|
|
||||||
|
// fill in template
|
||||||
|
var templateCss = strings.Replace(templateStr, "css_will_be_here", styleCss, 1)
|
||||||
|
var templateCssJs = strings.Replace(templateCss, "js_will_be_here", scriptJs, 1)
|
||||||
|
var templateCssJssIcon = strings.Replace(templateCssJs, "favicon_will_be_here", base64.StdEncoding.EncodeToString(faviconSvg), 2)
|
||||||
|
var templateParsed, _ = template.New("").Parse(templateCssJssIcon)
|
||||||
|
|
||||||
|
type rowTemplate struct {
|
||||||
|
Name string
|
||||||
|
Href template.HTML
|
||||||
|
Size string
|
||||||
|
Ext string
|
||||||
|
}
|
||||||
|
|
||||||
|
type pageTemplate struct {
|
||||||
|
Title template.HTML
|
||||||
|
ExtraPath template.HTML
|
||||||
|
Ro bool
|
||||||
|
RowsFiles []rowTemplate
|
||||||
|
RowsFolders []rowTemplate
|
||||||
|
}
|
||||||
|
|
||||||
|
var host = flag.String("h", "127.0.0.1", "host to listen to")
|
||||||
|
var port = flag.String("p", "8001", "port to listen to")
|
||||||
|
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, "\nskip hidden files")
|
||||||
|
var ro = flag.Bool("ro", false, "read only mode (no upload, rename, move, etc...)")
|
||||||
+372
@@ -0,0 +1,372 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dieMaybe(t *testing.T, err error) {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimSpaces(str string) string {
|
||||||
|
space := regexp.MustCompile(`\s+`)
|
||||||
|
return space.ReplaceAllString(str, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRaw(t *testing.T, url string) []byte {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
dieMaybe(t, err)
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
dieMaybe(t, err)
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
func getZip(t *testing.T, needle string, dest string) (int, bool) {
|
||||||
|
b := getRaw(t, dest)
|
||||||
|
unzipped, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
|
||||||
|
dieMaybe(t, err)
|
||||||
|
length := len(unzipped.File)
|
||||||
|
for _, file := range unzipped.File {
|
||||||
|
if file.Name == needle {
|
||||||
|
return length, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return length, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func get(t *testing.T, url string) string {
|
||||||
|
body := getRaw(t, url)
|
||||||
|
return trimSpaces(string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
func postDummyFile(t *testing.T, url string, path string, payload string) string {
|
||||||
|
// Generated by curl-to-Go: https://mholt.github.io/curl-to-go
|
||||||
|
body := strings.NewReader("------WebKitFormBoundarycCRIderiXxJWEUcU\r\nContent-Disposition: form-data; name=\"\u1112\u1161 \u1112\u1161\"; filename=\"\u1112\u1161 \u1112\u1161\"\r\nContent-Type: application/octet-stream\r\n\r\n" + payload)
|
||||||
|
req, err := http.NewRequest("POST", url+"post", body)
|
||||||
|
dieMaybe(t, err)
|
||||||
|
req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarycCRIderiXxJWEUcU")
|
||||||
|
req.Header.Set("Gossa-Path", path)
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
dieMaybe(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
bodyS, err := ioutil.ReadAll(resp.Body)
|
||||||
|
dieMaybe(t, err)
|
||||||
|
return trimSpaces(string(bodyS))
|
||||||
|
}
|
||||||
|
|
||||||
|
func postJSON(t *testing.T, url string, what string) string {
|
||||||
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer([]byte(what)))
|
||||||
|
dieMaybe(t, err)
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
dieMaybe(t, err)
|
||||||
|
return trimSpaces(string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchAndTestDefault(t *testing.T, url string) string {
|
||||||
|
body0 := get(t, url)
|
||||||
|
|
||||||
|
if !strings.Contains(body0, `<title>/</title>`) {
|
||||||
|
t.Fatal("error title")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(body0, `<h1 onclick="return titleClick(event)">./</h1>`) {
|
||||||
|
t.Fatal("error header")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(body0, `href="hols">hols/</a>`) {
|
||||||
|
t.Fatal("error hols folder")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(body0, `href="curimit@gmail.com%20%2840%25%29">curimit@gmail.com (40%)/</a>`) {
|
||||||
|
t.Fatal("error curimit@gmail.com (40%) folder")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(body0, `href="%E4%B8%AD%E6%96%87">中文/</a>`) {
|
||||||
|
t.Fatal("error 中文 folder")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(body0, `href="custom_mime_type.types">custom_mime_type.types</a>`) {
|
||||||
|
t.Fatal("error row custom_mime_type")
|
||||||
|
}
|
||||||
|
|
||||||
|
return body0
|
||||||
|
}
|
||||||
|
|
||||||
|
func doTestRegular(t *testing.T, url string, testExtra bool) {
|
||||||
|
var payload, path, body0, body1, body2 string
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching default path")
|
||||||
|
fetchAndTestDefault(t, url)
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching another page")
|
||||||
|
body0 = get(t, url+"/hols")
|
||||||
|
if !strings.Contains(body0, "glasgow.jpg") {
|
||||||
|
t.Fatal("fetching a subfolder failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root")
|
||||||
|
fetchAndTestDefault(t, url+"../../")
|
||||||
|
fetchAndTestDefault(t, url+"hols/../../")
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching regular files")
|
||||||
|
body0 = get(t, url+"subdir_with%20space/file_with%20space.html")
|
||||||
|
body1 = get(t, url+"fancy-path/a")
|
||||||
|
if body0 != `<b>spacious!!</b> ` || body1 != `fancy! ` {
|
||||||
|
t.Fatal("fetching a regular file errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching a invalid file")
|
||||||
|
path = "../../../../../../../../../../etc/passwd"
|
||||||
|
if !testExtra && get(t, url+path) != `error` {
|
||||||
|
t.Fatal("fetching a invalid file didnt errored")
|
||||||
|
} else if testExtra {
|
||||||
|
fetchAndTestDefault(t, url+path) // extra path will just redirect to root dir
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder 中文")
|
||||||
|
len, foundFile := getZip(t, "檔案.html", url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87")
|
||||||
|
if len != 1 || !foundFile {
|
||||||
|
t.Fatal("invalid zip generated")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder with hidden file")
|
||||||
|
_, foundHidden := getZip(t, ".hidden-folder/some-file", url+"zip?zipPath=%2fhols%2f&zipName=hols")
|
||||||
|
if foundHidden && !testExtra {
|
||||||
|
t.Fatal("invalid zip generated - shouldnt contain hidden folder")
|
||||||
|
} else if !foundHidden && testExtra {
|
||||||
|
t.Fatal("invalid zip generated - should contain hidden folder")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test zip invalid path")
|
||||||
|
body0 = get(t, url+"zip?zipPath=%2Ftmp&zipName=subdir")
|
||||||
|
println(body0)
|
||||||
|
if body0 != `error` {
|
||||||
|
t.Fatal("zip passed for invalid path")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test mkdir rpc")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/AAA"]}`)
|
||||||
|
if body0 != `ok` {
|
||||||
|
t.Fatal("mkdir rpc errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
body0 = fetchAndTestDefault(t, url)
|
||||||
|
if !strings.Contains(body0, `href="AAA">AAA/</a>`) {
|
||||||
|
t.Fatal("mkdir rpc folder not created")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test invalid mkdir rpc")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["../BBB"]}`)
|
||||||
|
if body0 != `error` {
|
||||||
|
t.Fatal("invalid mkdir rpc didnt errored #0")
|
||||||
|
}
|
||||||
|
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/../BBB"]}`)
|
||||||
|
if body0 != `error` {
|
||||||
|
t.Fatal("invalid mkdir rpc didnt errored #1")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test post file")
|
||||||
|
path = "%2F%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1" // "하 하" encoded
|
||||||
|
payload = "123 하"
|
||||||
|
body0 = postDummyFile(t, url, path, payload)
|
||||||
|
body1 = get(t, url+path)
|
||||||
|
body2 = fetchAndTestDefault(t, url)
|
||||||
|
if body0 != `ok` || body1 != payload || !strings.Contains(body2, `href="%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1">하 하</a>`) {
|
||||||
|
t.Fatal("post file errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test post file incorrect path")
|
||||||
|
body0 = postDummyFile(t, url, "%2E%2E"+path, payload)
|
||||||
|
if !strings.Contains(body0, `err`) {
|
||||||
|
t.Fatal("post file incorrect path didnt errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test mv rpc")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"mv","args":["/AAA", "/hols/AAA"]}`)
|
||||||
|
body1 = fetchAndTestDefault(t, url)
|
||||||
|
if body0 != `ok` || strings.Contains(body1, `href="AAA">AAA/</a></td> </tr>`) {
|
||||||
|
t.Fatal("mv rpc errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test upload in new folder")
|
||||||
|
payload = "test"
|
||||||
|
body0 = postDummyFile(t, url, "%2Fhols%2FAAA%2Fabcdef", payload)
|
||||||
|
body1 = get(t, url+"hols/AAA/abcdef")
|
||||||
|
if body0 != `ok` || body1 != payload {
|
||||||
|
t.Fatal("upload in new folder errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test symlink, should succeed: ", testExtra)
|
||||||
|
body0 = get(t, url+"/support/")
|
||||||
|
hasListing := strings.Contains(body0, `readme.md`)
|
||||||
|
body1 = get(t, url+"/support/readme.md")
|
||||||
|
hasReadme := strings.Contains(body1, `the master branch is automatically built and pushed`)
|
||||||
|
|
||||||
|
if !testExtra && hasReadme {
|
||||||
|
t.Fatal("error symlink file reached where illegal")
|
||||||
|
} else if testExtra && !hasReadme {
|
||||||
|
t.Fatal("error symlink file unreachable")
|
||||||
|
}
|
||||||
|
if !testExtra && hasListing {
|
||||||
|
t.Fatal("error symlink folder reached where illegal")
|
||||||
|
} else if testExtra && !hasListing {
|
||||||
|
t.Fatal("error symlink folder unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
if testExtra {
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test symlink mkdir & cleanup")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/support/testfolder"]}`)
|
||||||
|
if body0 != `ok` {
|
||||||
|
t.Fatal("error symlink mkdir")
|
||||||
|
}
|
||||||
|
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/support/testfolder"]}`)
|
||||||
|
if body0 != `ok` {
|
||||||
|
t.Fatal("error symlink rm")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test hidden file, should succeed: ", testExtra)
|
||||||
|
body0 = get(t, url+"/.testhidden")
|
||||||
|
hasHidden := strings.Contains(body0, `test`)
|
||||||
|
if !testExtra && hasHidden {
|
||||||
|
t.Fatal("error hidden file reached where illegal")
|
||||||
|
} else if testExtra && !hasHidden {
|
||||||
|
t.Fatal("error hidden file unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test upload in new folder")
|
||||||
|
payload = "test"
|
||||||
|
body0 = postDummyFile(t, url, "%2Fhols%2FAAA%2Fabcdef", payload)
|
||||||
|
body1 = get(t, url+"hols/AAA/abcdef")
|
||||||
|
if body0 != `ok` || body1 != payload {
|
||||||
|
t.Fatal("upload in new folder errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test rm rpc & cleanup")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/hols/AAA"]}`)
|
||||||
|
if body0 != `ok` {
|
||||||
|
t.Fatal("cleanup errored #0")
|
||||||
|
}
|
||||||
|
|
||||||
|
body0 = get(t, url+"hols/AAA")
|
||||||
|
if !strings.Contains(body0, `error`) {
|
||||||
|
t.Fatal("cleanup errored #1")
|
||||||
|
}
|
||||||
|
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/하 하"]}`)
|
||||||
|
if body0 != `ok` {
|
||||||
|
t.Fatal("cleanup errored #2")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\r\n=========\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func doTestReadonly(t *testing.T, url string) {
|
||||||
|
var payload, path, body0, body1 string
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching default path")
|
||||||
|
fetchAndTestDefault(t, url)
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root")
|
||||||
|
fetchAndTestDefault(t, url+"../../")
|
||||||
|
fetchAndTestDefault(t, url+"hols/../../")
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching regular files")
|
||||||
|
body0 = get(t, url+"subdir_with%20space/file_with%20space.html")
|
||||||
|
body1 = get(t, url+"fancy-path/a")
|
||||||
|
if body0 != `<b>spacious!!</b> ` || body1 != `fancy! ` {
|
||||||
|
t.Fatal("fetching a regular file errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test fetching a invalid file")
|
||||||
|
path = "../../../../../../../../../../etc/passwd"
|
||||||
|
if get(t, url+path) != `error` {
|
||||||
|
t.Fatal("fetching a invalid file didnt errored")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test mkdir rpc")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/AAA"]}`)
|
||||||
|
if body0 == `ok` {
|
||||||
|
t.Fatal("mkdir rpc passed - should not be allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test post file")
|
||||||
|
path = "%2F%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1" // "하 하" encoded
|
||||||
|
payload = "123 하"
|
||||||
|
body0 = postDummyFile(t, url, path, payload)
|
||||||
|
get(t, url+path)
|
||||||
|
if body0 == `ok` {
|
||||||
|
t.Fatal("post file passed - should not be allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test mv rpc")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"mv","args":["/AAA", "/hols/AAA"]}`)
|
||||||
|
fetchAndTestDefault(t, url)
|
||||||
|
if body0 == `ok` {
|
||||||
|
t.Fatal("mv rpc passed - should not be allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~
|
||||||
|
fmt.Println("\r\n~~~~~~~~~~ test rm rpc & cleanup")
|
||||||
|
body0 = postJSON(t, url+"rpc", `{"call":"rm","args":["/hols/AAA"]}`)
|
||||||
|
if body0 == `ok` {
|
||||||
|
t.Fatal("cleanup passed - should not be allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\r\n=========\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormal(t *testing.T) {
|
||||||
|
fmt.Println("========== testing normal path ============")
|
||||||
|
doTestRegular(t, "http://127.0.0.1:8001/", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtra(t *testing.T) {
|
||||||
|
fmt.Println("========== testing extras options ============")
|
||||||
|
doTestRegular(t, "http://127.0.0.1:8001/fancy-path/", true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRo(t *testing.T) {
|
||||||
|
fmt.Println("========== testing read only ============")
|
||||||
|
doTestReadonly(t, "http://127.0.0.1:8001/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunMain(t *testing.T) {
|
||||||
|
main()
|
||||||
|
}
|
||||||
@@ -1,28 +1,35 @@
|
|||||||
gossa
|
gossa
|
||||||
=============
|
=============
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
[](https://travis-ci.org/pldubouilh/gossa)
|
[](https://github.com/pldubouilh/gossa/actions)
|
||||||
[](https://hub.docker.com/r/pldubouilh/gossa)
|
|
||||||
[](https://hub.docker.com/r/pldubouilh/gossa)
|
[](https://hub.docker.com/r/pldubouilh/gossa)
|
||||||
[](https://github.com/pldubouilh/gossa/releases)
|
[](https://github.com/pldubouilh/gossa/releases)
|
||||||
|
|
||||||
a fast and simple webserver for your files, that's dependency-free and with under 200 lines of code, easy to review.
|
a fast and simple webserver for your files, that's dependency-free and with under 250 lines of code, easy to review.
|
||||||
|
|
||||||
a [simple UI](https://github.com/pldubouilh/gossa-ui) comes as default, featuring :
|
a [simple UI](https://github.com/pldubouilh/gossa-ui) comes as default, featuring :
|
||||||
|
|
||||||
* 🔍 files/directories browser
|
* 🔍 files/directories browser & handler
|
||||||
* 📩 drag-and-drop file/directory uploader
|
* 📩 drag-and-drop uploader
|
||||||
* 🗺️ files handling - move/rename/delete
|
* 🥂 fast golang static server
|
||||||
* 📸 picture browser
|
* 💾 90s web UI that prints in milliseconds
|
||||||
* 📽️ video streaming from the browser
|
* 📸 video streaming & picture browser
|
||||||
* ✍️ simple text editor
|
* ✍️ simple note editor
|
||||||
* ⌨️ keyboard shortcuts
|
* ⌨️ keyboard navigation
|
||||||
* 🥂 speed - showed rates above 100MB/s
|
* 🚀 lightweight and dependency free codebase
|
||||||
|
* 🔒 >95% test coverage and reproducible builds
|
||||||
|
* 💑 easy multi account setup, read-only mode
|
||||||
|
* ✨ PWA enabled
|
||||||
|
|
||||||
### build
|
### build
|
||||||
built blobs are available on the [release page](https://github.com/pldubouilh/gossa/releases) - or simply `make build` this repo
|
built blobs are available on the [release page](https://github.com/pldubouilh/gossa/releases) - or simply `make build` this repo.
|
||||||
|
all builds are reproducible, checkout the hashes on the release page.
|
||||||
|
|
||||||
|
arch linux users can also install through the [user repos](https://aur.archlinux.org/packages/gossa/) - e.g. `yay -S gossa`
|
||||||
|
|
||||||
|
automatic boot-time startup can be handled with a user systemd service - see [support](https://github.com/pldubouilh/gossa/tree/master/support)
|
||||||
|
|
||||||
### usage
|
### usage
|
||||||
```sh
|
```sh
|
||||||
@@ -31,33 +38,17 @@ built blobs are available on the [release page](https://github.com/pldubouilh/go
|
|||||||
% ./gossa -h 192.168.100.33 ~/storage
|
% ./gossa -h 192.168.100.33 ~/storage
|
||||||
```
|
```
|
||||||
|
|
||||||
### ui shortcuts
|
### shortcuts
|
||||||
|shortcut | action|
|
press `Ctrl/Cmd + h` to see all the UI/keyboard shortcuts.
|
||||||
| ------------- |-------------|
|
|
||||||
|click new folder icon | create new folder|
|
|
||||||
|click pad icon | open file editor|
|
|
||||||
|click file icon | rename item|
|
|
||||||
|double click file icon | delete item|
|
|
||||||
|drag-and-drop item on UI | move item|
|
|
||||||
|drag-and-drop external item | upload file/folders|
|
|
||||||
|
|
||||||
### keyboard shortcuts
|
### fancier setups
|
||||||
|shortcut | action|
|
release images are pushed to [dockerhub](https://hub.docker.com/r/pldubouilh/gossa), e.g. :
|
||||||
|-------------|-------------|
|
|
||||||
|Arrows/Enter | browse through files/directories and pictures|
|
|
||||||
|Ctrl/Meta + C | copy URL to clipboard|
|
|
||||||
|Ctrl/Meta + E | rename file/folder|
|
|
||||||
|Ctrl/Meta + Del | delete file/folder|
|
|
||||||
|Ctrl/Meta + U | upload new file/folder|
|
|
||||||
|Ctrl/Meta + D | create a new directory|
|
|
||||||
|Ctrl/Meta + X | cut selected path|
|
|
||||||
|Ctrl/Meta + V | paste previously selected paths to directory|
|
|
||||||
|\<any letter\> | search|
|
|
||||||
|
|
||||||
### using with docker
|
|
||||||
a few docker/docker-compose files are provided in the [docker folder](https://github.com/pldubouilh/gossa/tree/master/docker). release images are also pushed to [dockerhub](https://hub.docker.com/r/pldubouilh/gossa), e.g. :
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# pull from dockerhub and run
|
# pull from dockerhub and run
|
||||||
% sudo docker run -v ~/LocalDirToShare:/shared -p 8001:8001 pldubouilh/gossa
|
% sudo docker run -v ~/LocalDirToShare:/shared -p 8001:8001 pldubouilh/gossa
|
||||||
```
|
```
|
||||||
|
|
||||||
|
in a do-one-thing-well mindset, HTTPS and authentication has been left to middlewares and proxies. [sample caddy configs](https://github.com/pldubouilh/gossa/blob/master/support/) are available to quickly setup multi users setups along with https.
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
-196
@@ -1,196 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"html"
|
|
||||||
"html/template"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var host = flag.String("h", "127.0.0.1", "host to listen to")
|
|
||||||
var port = flag.String("p", "8001", "port to listen to")
|
|
||||||
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", true, "verbosity")
|
|
||||||
var skipHidden = flag.Bool("k", true, "skip hidden files")
|
|
||||||
var initPath = "."
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
type pageTemplate struct {
|
|
||||||
Title template.HTML
|
|
||||||
ExtraPath template.HTML
|
|
||||||
RowsFiles []rowTemplate
|
|
||||||
RowsFolders []rowTemplate
|
|
||||||
}
|
|
||||||
|
|
||||||
type rpcCall struct {
|
|
||||||
Call string `json:"call"`
|
|
||||||
Args []string `json:"args"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func check(e error) {
|
|
||||||
if e != nil {
|
|
||||||
panic(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func exitPath(w http.ResponseWriter, s ...interface{}) {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
log.Println("error", s, r)
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte("error"))
|
|
||||||
} else if *verb {
|
|
||||||
log.Println(s...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func humanize(bytes int64) string {
|
|
||||||
b := float64(bytes)
|
|
||||||
u := 0
|
|
||||||
for {
|
|
||||||
if b < 1024 {
|
|
||||||
return strconv.FormatFloat(b, 'f', 1, 64) + [9]string{"B", "k", "M", "G", "T", "P", "E", "Z", "Y"}[u]
|
|
||||||
}
|
|
||||||
b = b / 1024
|
|
||||||
u++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func replyList(w http.ResponseWriter, fullPath string, path string) {
|
|
||||||
_files, err := ioutil.ReadDir(fullPath)
|
|
||||||
check(err)
|
|
||||||
|
|
||||||
if !strings.HasSuffix(path, "/") {
|
|
||||||
path += "/"
|
|
||||||
}
|
|
||||||
|
|
||||||
title := "/" + strings.TrimPrefix(path, *extraPath)
|
|
||||||
p := pageTemplate{}
|
|
||||||
if path != *extraPath {
|
|
||||||
p.RowsFolders = append(p.RowsFolders, rowTemplate{"../", "../", "", "folder"})
|
|
||||||
}
|
|
||||||
p.ExtraPath = template.HTML(html.EscapeString(*extraPath))
|
|
||||||
p.Title = template.HTML(html.EscapeString(title))
|
|
||||||
|
|
||||||
for _, el := range _files {
|
|
||||||
if *skipHidden && strings.HasPrefix(el.Name(), ".") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
el, _ = os.Stat(fullPath + "/" + el.Name())
|
|
||||||
href := url.PathEscape(el.Name())
|
|
||||||
if el.IsDir() && strings.HasPrefix(href, "/") {
|
|
||||||
href = strings.Replace(href, "/", "", 1)
|
|
||||||
}
|
|
||||||
if el.IsDir() {
|
|
||||||
p.RowsFolders = append(p.RowsFolders, rowTemplate{el.Name() + "/", template.HTML(href), "", "folder"})
|
|
||||||
} else {
|
|
||||||
sl := strings.Split(el.Name(), ".")
|
|
||||||
ext := strings.ToLower(sl[len(sl)-1])
|
|
||||||
p.RowsFiles = append(p.RowsFiles, rowTemplate{el.Name(), template.HTML(href), humanize(el.Size()), ext})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
page.Execute(w, p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func doContent(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if !strings.HasPrefix(r.URL.Path, *extraPath) {
|
|
||||||
http.Redirect(w, r, *extraPath, 302)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
path := html.UnescapeString(r.URL.Path)
|
|
||||||
defer exitPath(w, "get content", path)
|
|
||||||
fullPath := checkPath(path)
|
|
||||||
stat, errStat := os.Stat(fullPath)
|
|
||||||
check(errStat)
|
|
||||||
|
|
||||||
if stat.IsDir() {
|
|
||||||
replyList(w, fullPath, path)
|
|
||||||
} else {
|
|
||||||
fs.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func upload(w http.ResponseWriter, r *http.Request) {
|
|
||||||
path, _ := url.PathUnescape(r.Header.Get("gossa-path"))
|
|
||||||
defer exitPath(w, "upload", path)
|
|
||||||
reader, _ := r.MultipartReader()
|
|
||||||
part, _ := reader.NextPart()
|
|
||||||
dst, _ := os.Create(checkPath(path))
|
|
||||||
io.Copy(dst, part)
|
|
||||||
w.Write([]byte("ok"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func rpc(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var err error
|
|
||||||
var rpc rpcCall
|
|
||||||
bodyBytes, _ := ioutil.ReadAll(r.Body)
|
|
||||||
json.Unmarshal(bodyBytes, &rpc)
|
|
||||||
defer exitPath(w, "rpc", rpc)
|
|
||||||
|
|
||||||
if rpc.Call == "mkdirp" {
|
|
||||||
err = os.MkdirAll(checkPath(rpc.Args[0]), os.ModePerm)
|
|
||||||
} else if rpc.Call == "mv" {
|
|
||||||
err = os.Rename(checkPath(rpc.Args[0]), checkPath(rpc.Args[1]))
|
|
||||||
} else if rpc.Call == "rm" {
|
|
||||||
err = os.RemoveAll(checkPath(rpc.Args[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
check(err)
|
|
||||||
w.Write([]byte("ok"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkPath(p string) string {
|
|
||||||
p = filepath.Join(initPath, strings.TrimPrefix(p, *extraPath))
|
|
||||||
fp, err := filepath.Abs(p)
|
|
||||||
sl, _ := filepath.EvalSymlinks(fp)
|
|
||||||
|
|
||||||
if err != nil || !strings.HasPrefix(fp, initPath) || len(sl) > 0 && !*symlinks && !strings.HasPrefix(sl, initPath) {
|
|
||||||
panic(errors.New("invalid path"))
|
|
||||||
}
|
|
||||||
|
|
||||||
return fp
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
flag.Parse()
|
|
||||||
if len(flag.Args()) > 0 {
|
|
||||||
initPath = flag.Args()[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
initPath, err = filepath.Abs(initPath)
|
|
||||||
check(err)
|
|
||||||
|
|
||||||
hostString := *host + ":" + *port
|
|
||||||
fmt.Println("Gossa startig on directory " + initPath)
|
|
||||||
fmt.Println("Listening on http://" + hostString + *extraPath)
|
|
||||||
|
|
||||||
http.HandleFunc(*extraPath+"rpc", rpc)
|
|
||||||
http.HandleFunc(*extraPath+"post", upload)
|
|
||||||
http.HandleFunc("/", doContent)
|
|
||||||
fs = http.StripPrefix(*extraPath, http.FileServer(http.Dir(initPath)))
|
|
||||||
err = http.ListenAndServe(hostString, nil)
|
|
||||||
check(err)
|
|
||||||
}
|
|
||||||
@@ -1,249 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func dieMaybe(t *testing.T, err error) {
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func trimSpaces(str string) string {
|
|
||||||
space := regexp.MustCompile(`\s+`)
|
|
||||||
return space.ReplaceAllString(str, " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
func get(t *testing.T, url string) string {
|
|
||||||
resp, err := http.Get(url)
|
|
||||||
dieMaybe(t, err)
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
dieMaybe(t, err)
|
|
||||||
return trimSpaces(string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
func postDummyFile(t *testing.T, url string, path string, payload string) string {
|
|
||||||
// Generated by curl-to-Go: https://mholt.github.io/curl-to-go
|
|
||||||
body := strings.NewReader("------WebKitFormBoundarycCRIderiXxJWEUcU\r\nContent-Disposition: form-data; name=\"\u1112\u1161 \u1112\u1161\"; filename=\"\u1112\u1161 \u1112\u1161\"\r\nContent-Type: application/octet-stream\r\n\r\n" + payload)
|
|
||||||
req, err := http.NewRequest("POST", url+"post", body)
|
|
||||||
dieMaybe(t, err)
|
|
||||||
req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarycCRIderiXxJWEUcU")
|
|
||||||
req.Header.Set("Gossa-Path", path)
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
dieMaybe(t, err)
|
|
||||||
defer resp.Body.Close()
|
|
||||||
bodyS, err := ioutil.ReadAll(resp.Body)
|
|
||||||
dieMaybe(t, err)
|
|
||||||
return trimSpaces(string(bodyS))
|
|
||||||
}
|
|
||||||
|
|
||||||
func postJSON(t *testing.T, url string, what string) string {
|
|
||||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer([]byte(what)))
|
|
||||||
dieMaybe(t, err)
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
dieMaybe(t, err)
|
|
||||||
return trimSpaces(string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchAndTestDefault(t *testing.T, url string) string {
|
|
||||||
bodyStr := get(t, url)
|
|
||||||
|
|
||||||
if !strings.Contains(bodyStr, `<title>/</title>`) {
|
|
||||||
t.Fatal("error title")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(bodyStr, `<h1>./</h1>`) {
|
|
||||||
t.Fatal("error header")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(bodyStr, `href="hols">hols/</a>`) {
|
|
||||||
t.Fatal("error hols folder")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(bodyStr, `href="curimit@gmail.com%20%2840%25%29">curimit@gmail.com (40%)/</a>`) {
|
|
||||||
t.Fatal("error curimit@gmail.com (40%) folder")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(bodyStr, `href="%E4%B8%AD%E6%96%87">中文/</a>`) {
|
|
||||||
t.Fatal("error 中文 folder")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(bodyStr, `href="custom_mime_type.types">custom_mime_type.types</a>`) {
|
|
||||||
t.Fatal("error row custom_mime_type")
|
|
||||||
}
|
|
||||||
|
|
||||||
return bodyStr
|
|
||||||
}
|
|
||||||
|
|
||||||
func doTest(t *testing.T, url string, symlinkEnabled bool) {
|
|
||||||
payload := ""
|
|
||||||
path := ""
|
|
||||||
bodyStr := ""
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test fetching default path")
|
|
||||||
fetchAndTestDefault(t, url)
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root")
|
|
||||||
fetchAndTestDefault(t, url+"../../")
|
|
||||||
fetchAndTestDefault(t, url+"hols/../../")
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test fetching regular files")
|
|
||||||
bodyStr = get(t, url+"subdir_with%20space/file_with%20space.html")
|
|
||||||
bodyStr2 := get(t, url+"fancy-path/a")
|
|
||||||
fmt.Println(bodyStr2)
|
|
||||||
if !strings.Contains(bodyStr, `<b>spacious!!</b>`) || !strings.Contains(bodyStr2, `fancy!`) {
|
|
||||||
t.Fatal("fetching a regular file errored")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test fetching a invalid file")
|
|
||||||
bodyStr = get(t, url+"../../../../../../../../../../etc/passwd")
|
|
||||||
if !strings.Contains(bodyStr, `error`) {
|
|
||||||
t.Fatal("fetching a invalid file didnt errored")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test mkdir rpc")
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/AAA"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `ok`) {
|
|
||||||
t.Fatal("mkdir rpc errored")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = fetchAndTestDefault(t, url)
|
|
||||||
if !strings.Contains(bodyStr, `href="AAA">AAA/</a>`) {
|
|
||||||
t.Fatal("mkdir rpc folder not created")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test invalid mkdir rpc")
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["../BBB"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `error`) {
|
|
||||||
t.Fatal("invalid mkdir rpc didnt errored #0")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/../BBB"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `error`) {
|
|
||||||
t.Fatal("invalid mkdir rpc didnt errored #1")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test post file")
|
|
||||||
path = "%2F%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1" // "하 하" encoded
|
|
||||||
payload = "123 하"
|
|
||||||
bodyStr = postDummyFile(t, url, path, payload)
|
|
||||||
if !strings.Contains(bodyStr, `ok`) {
|
|
||||||
t.Fatal("post file errored")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = get(t, url+path)
|
|
||||||
if !strings.Contains(bodyStr, payload) {
|
|
||||||
t.Fatal("post file errored reaching new file")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = fetchAndTestDefault(t, url)
|
|
||||||
if !strings.Contains(bodyStr, `href="%E1%84%92%E1%85%A1%20%E1%84%92%E1%85%A1">하 하</a>`) {
|
|
||||||
t.Fatal("post file errored checking new file row")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test post file incorrect path")
|
|
||||||
bodyStr = postDummyFile(t, url, "%2E%2E"+path, payload)
|
|
||||||
if !strings.Contains(bodyStr, `err`) {
|
|
||||||
t.Fatal("post file incorrect path didnt errored")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test mv rpc")
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"mv","args":["/AAA", "/hols/AAA"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `ok`) {
|
|
||||||
t.Fatal("mv rpc errored")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = fetchAndTestDefault(t, url)
|
|
||||||
if strings.Contains(bodyStr, `href="AAA">AAA/</a></td> </tr>`) {
|
|
||||||
t.Fatal("mv rpc folder not moved")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test upload in new folder")
|
|
||||||
payload = "abcdef1234"
|
|
||||||
bodyStr = postDummyFile(t, url, "%2Fhols%2FAAA%2Fabcdef", payload)
|
|
||||||
if strings.Contains(bodyStr, `err`) {
|
|
||||||
t.Fatal("upload in new folder errored")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = get(t, url+"hols/AAA/abcdef")
|
|
||||||
if !strings.Contains(bodyStr, payload) {
|
|
||||||
t.Fatal("upload in new folder error reaching new file")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test symlink, should succeed: ", symlinkEnabled)
|
|
||||||
bodyStr = get(t, url+"/docker/readme.md")
|
|
||||||
hasReadme := strings.Contains(bodyStr, `the master branch is automatically built and pushed`)
|
|
||||||
if !symlinkEnabled && hasReadme {
|
|
||||||
t.Fatal("error symlink reached where illegal")
|
|
||||||
} else if symlinkEnabled && !hasReadme {
|
|
||||||
t.Fatal("error symlink unreachable")
|
|
||||||
}
|
|
||||||
|
|
||||||
if symlinkEnabled {
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test symlink mkdir")
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/docker/testfolder"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `ok`) {
|
|
||||||
t.Fatal("error symlink mkdir")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test rm rpc & cleanup")
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"rm","args":["/hols/AAA"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `ok`) {
|
|
||||||
t.Fatal("cleanup errored #0")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = get(t, url+"hols/AAA")
|
|
||||||
if !strings.Contains(bodyStr, `error`) {
|
|
||||||
t.Fatal("cleanup errored #1")
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"rm","args":["/하 하"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `ok`) {
|
|
||||||
t.Fatal("cleanup errored #2")
|
|
||||||
}
|
|
||||||
|
|
||||||
if symlinkEnabled {
|
|
||||||
bodyStr = postJSON(t, url+"rpc", `{"call":"rm","args":["/docker/testfolder"]}`)
|
|
||||||
if !strings.Contains(bodyStr, `ok`) {
|
|
||||||
t.Fatal("error symlink rm")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetFolder(t *testing.T) {
|
|
||||||
time.Sleep(6 * time.Second)
|
|
||||||
fmt.Println("========== testing normal path ============")
|
|
||||||
url := "http://127.0.0.1:8001/"
|
|
||||||
doTest(t, url, false)
|
|
||||||
|
|
||||||
fmt.Printf("\r\n=========\r\n")
|
|
||||||
time.Sleep(10 * time.Second)
|
|
||||||
|
|
||||||
url = "http://127.0.0.1:8001/fancy-path/"
|
|
||||||
fmt.Println("========== testing at fancy path ============")
|
|
||||||
doTest(t, url, true)
|
|
||||||
|
|
||||||
fmt.Printf("\r\n=========\r\n")
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
FROM golang:1.18 as builder
|
||||||
|
COPY . /gossaSrc
|
||||||
|
RUN cd /gossaSrc && make
|
||||||
|
|
||||||
|
FROM alpine:3.15
|
||||||
|
ENV UID="1000" GID="1000" HOST="0.0.0.0" PORT="8001" PREFIX="/" FOLLOW_SYMLINKS="false" SKIP_HIDDEN_FILES="true" DATADIR="/shared" READONLY="false" VERB="false"
|
||||||
|
RUN apk add --no-cache su-exec
|
||||||
|
COPY ./support/entrypoint.sh /entrypoint.sh
|
||||||
|
COPY --from=builder /gossaSrc/gossa /gossa
|
||||||
|
ENTRYPOINT "/entrypoint.sh"
|
||||||
Executable
+2
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
exec su-exec ${UID}:${GID} /gossa -h ${HOST} -p ${PORT} -k=${SKIP_HIDDEN_FILES} -ro=${READONLY} --symlinks=${FOLLOW_SYMLINKS} --prefix=${PREFIX} --verb=${VERB} ${DATADIR}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
## run as a service
|
||||||
|
|
||||||
|
use the `service` file provided here to use as a systemd service.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# edit service file
|
||||||
|
|
||||||
|
# copy over our service, and start. Notice no sudo with systemctl command !
|
||||||
|
% mkdir -p ~/.config/systemd/user/
|
||||||
|
% cp service ~/.config/systemd/user/gossa.service
|
||||||
|
% systemctl --user start gossa
|
||||||
|
|
||||||
|
# enable at boot time
|
||||||
|
% systemctl --user enable gossa
|
||||||
|
```
|
||||||
|
|
||||||
|
## run with docker
|
||||||
|
|
||||||
|
the master branch is automatically built and pushed to [dockerhub](https://hub.docker.com/r/pldubouilh/gossa) under `pldubouilh/gossa`.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# pull from dockerhub and run
|
||||||
|
% mkdir ~/LocalDirToShare
|
||||||
|
% sudo docker run -v ~/LocalDirToShare:/shared -p 8001:8001 pldubouilh/gossa
|
||||||
|
|
||||||
|
# options are settable through env. variabes. all the options are the build.Dockerfile
|
||||||
|
% sudo docker run -e PREFIX="/gossa/" -v ~/LocalDirToShare:/shared -p 8001:8001 pldubouilh/gossa
|
||||||
|
```
|
||||||
|
|
||||||
|
if you prefer building the image yourself :
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# build gossa within a build container, needs to be ran within the sources, ../ from here, and run
|
||||||
|
% mkdir ~/LocalDirToShare
|
||||||
|
% docker build -t gossa -f support/build.Dockerfile .
|
||||||
|
% sudo docker run -v ~/LocalDirToShare:/shared -p 8001:8001 gossa
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## multi-account setup
|
||||||
|
|
||||||
|
authentication / user routing has been left out of the design of gossa, as simple tools are already available for this purpose. [caddy](https://caddyserver.com/v1/) is used here as an example, but other proxy can be used in a similar fashion.
|
||||||
|
|
||||||
|
### example 1 root, multiple read-only users
|
||||||
|
|
||||||
|
this sample caddy config will
|
||||||
|
+ enable https on the domain myserver.com
|
||||||
|
+ password protect the access
|
||||||
|
+ route the root user requests to 1 gossa instance
|
||||||
|
+ route user1 and user2 requests to a readonly gossa instance
|
||||||
|
|
||||||
|
```sh
|
||||||
|
myserver.com
|
||||||
|
|
||||||
|
# proxy regular and read only instance
|
||||||
|
proxy / 127.0.0.1:8001
|
||||||
|
proxy /ro 127.0.0.1:8002 { without /ro }
|
||||||
|
|
||||||
|
# reroute non-root user to read-only
|
||||||
|
# cm9... is the output of `printf "root:password" | base64`
|
||||||
|
rewrite {
|
||||||
|
if {>Authorization} not "Basic cm9vdDpwYXNzd29yZA=="
|
||||||
|
to /ro/{path}
|
||||||
|
}
|
||||||
|
|
||||||
|
# gate access
|
||||||
|
basicauth / root password
|
||||||
|
basicauth / ro_user1 passworduser1
|
||||||
|
basicauth / ro_user2 passworduser2
|
||||||
|
```
|
||||||
|
|
||||||
|
then simply start the 2 gossa instances, and caddy
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# start an instance in readonly
|
||||||
|
% ./gossa -ro=true -p 8002 ~/folder &
|
||||||
|
|
||||||
|
# start an instance with access to hidden files
|
||||||
|
% ./gossa -k=false -p 8001 ~/folder &
|
||||||
|
|
||||||
|
# start caddy
|
||||||
|
% ./caddy
|
||||||
|
```
|
||||||
|
|
||||||
|
### example 2 users on 2 different folders
|
||||||
|
|
||||||
|
this sample caddy config will
|
||||||
|
+ enable https on the domain myserver.com
|
||||||
|
+ password protect the access
|
||||||
|
+ route user1 to own folder
|
||||||
|
+ route user2 to own folder
|
||||||
|
+ share a folder between 2 users with a symlink
|
||||||
|
|
||||||
|
```sh
|
||||||
|
myserver.com
|
||||||
|
|
||||||
|
proxy /user1 127.0.0.1:8001 { without /user1 }
|
||||||
|
proxy /user2 127.0.0.1:8002 { without /user2 }
|
||||||
|
|
||||||
|
basicauth / user1 passworduser1
|
||||||
|
basicauth / user2 passworduser2
|
||||||
|
|
||||||
|
rewrite {
|
||||||
|
if {>Authorization} is "Basic dXNlcjE6cGFzc3dvcmR1c2VyMQ=="
|
||||||
|
to /user1/{path}
|
||||||
|
}
|
||||||
|
|
||||||
|
rewrite {
|
||||||
|
if {>Authorization} is "Basic dXNlcjI6cGFzc3dvcmR1c2VyMg=="
|
||||||
|
to /user2/{path}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
start 2 gossa instances, and caddy
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# create symlink to share folder between 2 users
|
||||||
|
% ln -s /path/shared test/user1
|
||||||
|
% ln -s /path/shared test/user2
|
||||||
|
|
||||||
|
# start gossa & caddy
|
||||||
|
% ./gossa -p 8001 -symlinks=true test/user1 &
|
||||||
|
% ./gossa -p 8002 -symlinks=true test/user2 &
|
||||||
|
% ./caddy
|
||||||
|
```
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Gossa service for mytestfolder
|
||||||
|
Wants=network-online.target
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
# note: absolute paths are necessary here
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/bin/gossa /home/joe/mytestfolder
|
||||||
|
Restart=always
|
||||||
|
RestartSec=30
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
hidden !
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
test
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../hols
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
I'm hidden by my directory!
|
||||||
Reference in New Issue
Block a user