Allow serving unix sockets

This commit is contained in:
NXShock 2025-06-03 12:17:51 +05:00
parent c41ff56ffa
commit 4847a2d05a
5 changed files with 35 additions and 14 deletions

View file

@ -1,19 +1,19 @@
pkgname=simplefileshare pkgname=simplefileshare
pkgver=0.1.4 pkgver=0.1.5
pkgrel=0 pkgrel=0
pkgdesc="Simple file share" pkgdesc="Simple file share"
arch=('x86_64' 'aarch64') arch=("x86_64" "aarch64")
license=('GPL') license=("GPL")
url="https://github.com/nxshock/$pkgname" url="https://github.com/nxshock/$pkgname"
makedepends=('go' 'git') makedepends=("go" "git")
options=('!strip') options=("!strip")
backup=("etc/$pkgname.conf") backup=("etc/$pkgname.conf")
source=( source=(
"git+https://github.com/nxshock/$pkgname.git" "git+https://github.com/nxshock/$pkgname.git"
'git+https://github.com/dmhendricks/file-icon-vectors') "git+https://github.com/dmhendricks/file-icon-vectors")
sha256sums=( sha256sums=(
'SKIP' "SKIP"
'SKIP') "SKIP")
build() { build() {
cd "$srcdir/$pkgname" cd "$srcdir/$pkgname"

View file

@ -109,6 +109,13 @@ func HandleUpload(w http.ResponseWriter, r *http.Request) {
} }
func HandleDownload(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")) filename := filepath.Base(r.FormValue("filename"))
if filename == "" { if filename == "" {
@ -116,7 +123,7 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) {
return return
} }
f, err := os.Open(filepath.Join(config.StoragePath, filename)) f, err := root.Open(filename)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
@ -140,8 +147,8 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) {
func HandleStream(w http.ResponseWriter, r *http.Request) { func HandleStream(w http.ResponseWriter, r *http.Request) {
filename := filepath.Base(r.FormValue("filename")) filename := filepath.Base(r.FormValue("filename"))
if filename == "" { if !filepath.IsLocal(filename) {
http.Error(w, `"filename" field can't be empty`, http.StatusBadRequest) http.Error(w, `wrong "filename" specified`, http.StatusBadRequest)
return return
} }

14
main.go
View file

@ -1,7 +1,9 @@
package main package main
import ( import (
"net"
"net/http" "net/http"
"net/url"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -40,7 +42,17 @@ func init() {
func main() { func main() {
go func() { 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 { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

View file

@ -1,5 +1,5 @@
# HTTP-server listen address # HTTP-server listen address
ListenAddress = ":8000" ListenAddress = "http://0.0.0.0:8000"
# File storage path # File storage path
StoragePath = "/var/lib/simplefileshare" StoragePath = "/var/lib/simplefileshare"

View file

@ -4,6 +4,8 @@ Description=simplefileshare service
[Service] [Service]
Type=simple Type=simple
User=simplefileshare User=simplefileshare
Group=simplefileshare
RuntimeDirectory=simplefileshare
ExecStart=/usr/bin/simplefileshare ExecStart=/usr/bin/simplefileshare
Restart=on-failure Restart=on-failure
RestartSec=10s RestartSec=10s
@ -27,7 +29,7 @@ ProtectKernelLogs=true
ProtectKernelModules=true ProtectKernelModules=true
ProtectKernelTunables=true ProtectKernelTunables=true
ProtectSystem=strict ProtectSystem=strict
ReadWritePaths=/var/lib/simplefileshare ReadWritePaths=/var/lib/simplefileshare /run/simplefileshare
RemoveIPC=true RemoveIPC=true
RestrictNamespaces=true RestrictNamespaces=true
RestrictRealtime=true RestrictRealtime=true