* Externalized config
* Add dynamic user/share handling
This commit is contained in:
@@ -27,27 +27,39 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"golang.org/x/net/webdav"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var configFile = flag.String("config", "sharedav.toml", "Config file to be used.")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
c := LoadConfig(*configFile)
|
||||
|
||||
h := &webdav.Handler{}
|
||||
h.LockSystem = webdav.NewMemLS()
|
||||
h.FileSystem = BaseDir("")
|
||||
h.FileSystem = BaseDir(c.BaseDirectory)
|
||||
|
||||
authenticatedWebdavHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || (username != "user1" && username != "user2") || password != "test" {
|
||||
log.Println("invalid user pass", ok, username, password)
|
||||
directory := ""
|
||||
if ok {
|
||||
ok, directory = c.ValidateDAVUser(username, password)
|
||||
}
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="ShareDAV"`)
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "dir", username)))
|
||||
// Use the WebDAV handler to actually serve the request. Also enhance the context
|
||||
// to contain the subdirectory (of the base directory) which contains the data for
|
||||
// the authenticated user.
|
||||
h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "dir", directory)))
|
||||
})
|
||||
|
||||
http.ListenAndServe(":3000", authenticatedWebdavHandler)
|
||||
http.ListenAndServe(c.ListenAddress, authenticatedWebdavHandler)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user