Merge pull request #7 from qbit/muxd-handlers
Store our handlers in a slice and lock them when they are being used.
This commit is contained in:
commit
db7f06dc5b
11
go.sum
11
go.sum
@ -1,29 +1,18 @@
|
||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc=
|
||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e h1:MUP6MR3rJ7Gk9LEia0LP2ytiH6MuCfs7qYz+47jGdD8=
|
||||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211205041911-012df41ee64c h1:7SfqwP5fxEtl/P02w5IhKc86ziJ+A25yFrkVgoy2FT8=
|
||||
golang.org/x/net v0.0.0-20211205041911-012df41ee64c/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
|
||||
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
suah.dev/protect v1.1.0 h1:C24D67UlFap5t/A29Meo7V7xLdjkaFAxSQMa5yG8sS4=
|
||||
suah.dev/protect v1.1.0/go.mod h1:ZSgyBM30JUwhVPWJzVHh0jlu5W6Qz1VR6tIhAzqJZ9Y=
|
||||
suah.dev/protect v1.2.0 h1:4G4V43yVYXCjLFzaE9QJR0fLo3rf5vNBS9YxyoI19DU=
|
||||
suah.dev/protect v1.2.0/go.mod h1:Ocn1yqUskqe/is6N2bxQxtT+fegbvQsOFyHbJAQu9XE=
|
||||
|
65
main.go
65
main.go
@ -15,6 +15,7 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
@ -52,9 +53,25 @@ var (
|
||||
templ *template.Template
|
||||
)
|
||||
|
||||
type userHandlers struct {
|
||||
type userHandler struct {
|
||||
mu sync.Mutex
|
||||
dav *webdav.Handler
|
||||
fs http.Handler
|
||||
name string
|
||||
}
|
||||
|
||||
type userHandlers struct {
|
||||
list []userHandler
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func (u *userHandlers) find(name string) *userHandler {
|
||||
for i := range u.list {
|
||||
if u.list[i].name == name {
|
||||
return &u.list[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
@ -62,7 +79,7 @@ var (
|
||||
davDir string
|
||||
fullListen string
|
||||
genHtpass bool
|
||||
handlers map[string]userHandlers
|
||||
handlers userHandlers
|
||||
listen string
|
||||
passPath string
|
||||
tlsCert string
|
||||
@ -74,7 +91,6 @@ var pledges = "stdio wpath rpath cpath tty inet dns unveil"
|
||||
|
||||
func init() {
|
||||
users = make(map[string]string)
|
||||
handlers = make(map[string]userHandlers)
|
||||
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
@ -153,13 +169,15 @@ func prompt(prompt string, secure bool) (string, error) {
|
||||
}
|
||||
input = string(b)
|
||||
} else {
|
||||
fmt.Scanln(&input)
|
||||
_, err := fmt.Scanln(&input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var pledges = "stdio wpath rpath cpath tty inet dns unveil"
|
||||
|
||||
if genHtpass {
|
||||
user, err := prompt("Username: ", false)
|
||||
@ -182,12 +200,15 @@ func main() {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
if _, err := f.WriteString(fmt.Sprintf("%s:%s\n", user, hash)); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Added %q to %q\n", user, passPath)
|
||||
|
||||
os.Exit(0)
|
||||
@ -209,7 +230,6 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer p.Close()
|
||||
|
||||
ht := csv.NewReader(p)
|
||||
ht.Comma = ':'
|
||||
@ -221,6 +241,11 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = p.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, parts := range entries {
|
||||
users[parts[0]] = parts[1]
|
||||
}
|
||||
@ -229,22 +254,24 @@ func main() {
|
||||
if auth {
|
||||
for u := range users {
|
||||
uPath := path.Join(davDir, u)
|
||||
handlers[u] = userHandlers{
|
||||
handlers.list = append(handlers.list, userHandler{
|
||||
name: u,
|
||||
dav: &webdav.Handler{
|
||||
LockSystem: webdav.NewMemLS(),
|
||||
FileSystem: webdav.Dir(uPath),
|
||||
},
|
||||
fs: http.FileServer(http.Dir(uPath)),
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
handlers[""] = userHandlers{
|
||||
handlers.list = append(handlers.list, userHandler{
|
||||
name: "",
|
||||
dav: &webdav.Handler{
|
||||
LockSystem: webdav.NewMemLS(),
|
||||
FileSystem: webdav.Dir(davDir),
|
||||
},
|
||||
fs: http.FileServer(http.Dir(davDir)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
@ -272,7 +299,19 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
handler := handlers[user]
|
||||
handlers.mu.RLock()
|
||||
handler := handlers.find(user)
|
||||
handlers.mu.RUnlock()
|
||||
|
||||
if handler == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
handler.mu.Lock()
|
||||
|
||||
defer handler.mu.Unlock()
|
||||
|
||||
userPath := path.Join(davDir, user)
|
||||
fullPath := path.Join(davDir, user, r.URL.Path)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user