From 4847a2d05a2bd363d255933962e18c94d78359a0 Mon Sep 17 00:00:00 2001 From: NXShock Date: Tue, 3 Jun 2025 12:17:51 +0500 Subject: [PATCH] Allow serving unix sockets --- PKGBUILD | 16 ++++++++-------- handlers.go | 13 ++++++++++--- main.go | 14 +++++++++++++- simplefileshare.conf | 2 +- simplefileshare.service | 4 +++- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 7471148..3415a46 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,19 +1,19 @@ pkgname=simplefileshare -pkgver=0.1.4 +pkgver=0.1.5 pkgrel=0 pkgdesc="Simple file share" -arch=('x86_64' 'aarch64') -license=('GPL') +arch=("x86_64" "aarch64") +license=("GPL") url="https://github.com/nxshock/$pkgname" -makedepends=('go' 'git') -options=('!strip') +makedepends=("go" "git") +options=("!strip") backup=("etc/$pkgname.conf") source=( "git+https://github.com/nxshock/$pkgname.git" - 'git+https://github.com/dmhendricks/file-icon-vectors') + "git+https://github.com/dmhendricks/file-icon-vectors") sha256sums=( - 'SKIP' - 'SKIP') + "SKIP" + "SKIP") build() { cd "$srcdir/$pkgname" diff --git a/handlers.go b/handlers.go index 1b5e353..fbe3b0a 100644 --- a/handlers.go +++ b/handlers.go @@ -109,6 +109,13 @@ func HandleUpload(w http.ResponseWriter, r *http.Request) { } func HandleDownload(w http.ResponseWriter, r *http.Request) { + root, err := os.OpenRoot(config.StoragePath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer root.Close() + filename := filepath.Base(r.FormValue("filename")) if filename == "" { @@ -116,7 +123,7 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) { return } - f, err := os.Open(filepath.Join(config.StoragePath, filename)) + f, err := root.Open(filename) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -140,8 +147,8 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) { func HandleStream(w http.ResponseWriter, r *http.Request) { filename := filepath.Base(r.FormValue("filename")) - if filename == "" { - http.Error(w, `"filename" field can't be empty`, http.StatusBadRequest) + if !filepath.IsLocal(filename) { + http.Error(w, `wrong "filename" specified`, http.StatusBadRequest) return } diff --git a/main.go b/main.go index e177df8..5c4a823 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main import ( + "net" "net/http" + "net/url" "os" "os/signal" "syscall" @@ -40,7 +42,17 @@ func init() { func main() { go func() { - err := http.ListenAndServe(config.ListenAddress, nil) + u, err := url.Parse(config.ListenAddress) + if err != nil { + log.Fatalln(err) + } + + listener, err := net.Listen(u.Scheme, u.Host+u.Path) + if err != nil { + log.Fatalln(err) + } + + err = http.Serve(listener, http.DefaultServeMux) if err != nil { log.Fatalln(err) } diff --git a/simplefileshare.conf b/simplefileshare.conf index 72a62ff..2de9c6b 100644 --- a/simplefileshare.conf +++ b/simplefileshare.conf @@ -1,5 +1,5 @@ # HTTP-server listen address -ListenAddress = ":8000" +ListenAddress = "http://0.0.0.0:8000" # File storage path StoragePath = "/var/lib/simplefileshare" diff --git a/simplefileshare.service b/simplefileshare.service index 6bc7e8f..00a0091 100644 --- a/simplefileshare.service +++ b/simplefileshare.service @@ -4,6 +4,8 @@ Description=simplefileshare service [Service] Type=simple User=simplefileshare +Group=simplefileshare +RuntimeDirectory=simplefileshare ExecStart=/usr/bin/simplefileshare Restart=on-failure RestartSec=10s @@ -27,7 +29,7 @@ ProtectKernelLogs=true ProtectKernelModules=true ProtectKernelTunables=true ProtectSystem=strict -ReadWritePaths=/var/lib/simplefileshare +ReadWritePaths=/var/lib/simplefileshare /run/simplefileshare RemoveIPC=true RestrictNamespaces=true RestrictRealtime=true