From 4fdac0116fc6d87923070080f08c7d41783129a3 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Thu, 9 Dec 2021 05:16:00 -0700 Subject: [PATCH 1/5] Store our handlers in a slice and lock them when they are being used. Hopefully this helps with #3 (at least the HTTP 423 errors). --- main.go | 62 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 8727c7e..aeab934 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "path/filepath" "regexp" "strings" + "sync" "text/template" "time" @@ -52,9 +53,22 @@ var ( templ *template.Template ) -type userHandlers struct { - dav *webdav.Handler - fs http.Handler +type userHandler struct { + mu sync.Mutex + dav *webdav.Handler + fs http.Handler + name string +} + +type userHandlers []userHandler + +func (u userHandlers) find(name string) *userHandler { + for _, usr := range u { + if usr.name == name { + return &usr + } + } + return nil } var ( @@ -62,7 +76,7 @@ var ( davDir string fullListen string genHtpass bool - handlers map[string]userHandlers + handlers userHandlers listen string passPath string tlsCert string @@ -74,7 +88,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,7 +166,10 @@ 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 } @@ -182,12 +198,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,13 +228,17 @@ func main() { if err != nil { log.Fatal(err) } - defer p.Close() ht := csv.NewReader(p) ht.Comma = ':' ht.Comment = '#' ht.TrimLeadingSpace = true + err = p.Close() + if err != nil { + log.Fatal(err) + } + entries, err := ht.ReadAll() if err != nil { log.Fatal(err) @@ -229,22 +252,24 @@ func main() { if auth { for u := range users { uPath := path.Join(davDir, u) - handlers[u] = userHandlers{ + handlers = append(handlers, userHandler{ + name: u, dav: &webdav.Handler{ LockSystem: webdav.NewMemLS(), FileSystem: webdav.Dir(uPath), }, fs: http.FileServer(http.Dir(uPath)), - } + }) } } else { - handlers[""] = userHandlers{ + handlers = append(handlers, userHandler{ + name: "", dav: &webdav.Handler{ LockSystem: webdav.NewMemLS(), FileSystem: webdav.Dir(davDir), }, fs: http.FileServer(http.Dir(davDir)), - } + }) } mux := http.NewServeMux() @@ -272,7 +297,16 @@ func main() { } } - handler := handlers[user] + handler := handlers.find(user) + 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) From f9de42e8013108ea27b33cf9185fffb96f612597 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Thu, 9 Dec 2021 05:48:34 -0700 Subject: [PATCH 2/5] Don't assign var --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index aeab934..4d81f1b 100644 --- a/main.go +++ b/main.go @@ -63,9 +63,9 @@ type userHandler struct { type userHandlers []userHandler func (u userHandlers) find(name string) *userHandler { - for _, usr := range u { - if usr.name == name { - return &usr + for i := range u { + if u[i].name == name { + return &u[i] } } return nil From 18a4ed5026d628afcca87ab1a98cb74705417ff2 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Thu, 9 Dec 2021 05:59:56 -0700 Subject: [PATCH 3/5] remove extra pledges var --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 4d81f1b..651b94d 100644 --- a/main.go +++ b/main.go @@ -175,7 +175,6 @@ func prompt(prompt string, secure bool) (string, error) { } func main() { - var pledges = "stdio wpath rpath cpath tty inet dns unveil" if genHtpass { user, err := prompt("Username: ", false) From 8c113d95c1259c34e627108be3db491b9af9d7dc Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Fri, 10 Dec 2021 06:26:31 -0700 Subject: [PATCH 4/5] tidy --- go.sum | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/go.sum b/go.sum index 0a14f2b..71d343d 100644 --- a/go.sum +++ b/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= From 68c586f80598b8aac5b5cb4c35d27a6d68592d34 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Fri, 10 Dec 2021 06:27:27 -0700 Subject: [PATCH 5/5] use RWLock on handlers, fix close bug --- main.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 651b94d..bf3f7d6 100644 --- a/main.go +++ b/main.go @@ -60,12 +60,15 @@ type userHandler struct { name string } -type userHandlers []userHandler +type userHandlers struct { + list []userHandler + mu sync.RWMutex +} -func (u userHandlers) find(name string) *userHandler { - for i := range u { - if u[i].name == name { - return &u[i] +func (u *userHandlers) find(name string) *userHandler { + for i := range u.list { + if u.list[i].name == name { + return &u.list[i] } } return nil @@ -233,12 +236,12 @@ func main() { ht.Comment = '#' ht.TrimLeadingSpace = true - err = p.Close() + entries, err := ht.ReadAll() if err != nil { log.Fatal(err) } - entries, err := ht.ReadAll() + err = p.Close() if err != nil { log.Fatal(err) } @@ -251,7 +254,7 @@ func main() { if auth { for u := range users { uPath := path.Join(davDir, u) - handlers = append(handlers, userHandler{ + handlers.list = append(handlers.list, userHandler{ name: u, dav: &webdav.Handler{ LockSystem: webdav.NewMemLS(), @@ -261,7 +264,7 @@ func main() { }) } } else { - handlers = append(handlers, userHandler{ + handlers.list = append(handlers.list, userHandler{ name: "", dav: &webdav.Handler{ LockSystem: webdav.NewMemLS(), @@ -296,7 +299,10 @@ func main() { } } + handlers.mu.RLock() handler := handlers.find(user) + handlers.mu.RUnlock() + if handler == nil { http.NotFound(w, r) return