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
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"

View file

@ -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
}

14
main.go
View file

@ -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)
}

View file

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

View file

@ -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