initial bits for gavin - a no bs webdav server

This commit is contained in:
Aaron Bieber 2019-11-07 08:05:34 -07:00
commit 6d1e713505
Signed by: qbit
GPG Key ID: 279160AB1BE1236B
4 changed files with 82 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
gavin

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module github.com/qbit/gavin
go 1.13
require (
golang.org/x/net v0.0.0-20191105084925-a882066a44e0
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
)

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191105084925-a882066a44e0 h1:QPlSTtPE2k6PZPasQUbzuK3p9JbS+vMXYVto8g/yrsg=
golang.org/x/net v0.0.0-20191105084925-a882066a44e0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

67
main.go Normal file
View File

@ -0,0 +1,67 @@
package main
import (
"flag"
"log"
"net"
"net/http"
"os"
"path/filepath"
"golang.org/x/net/webdav"
"golang.org/x/sys/unix"
)
var listen string
var davDir string
var staticDir string
func init() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
flag.StringVar(&listen, "http", ":8080", "Listen on")
flag.StringVar(&davDir, "dir", dir, "WebDAV directory to serve.")
flag.StringVar(&staticDir, "static", dir, "Directory to serve static resources from.")
flag.Parse()
unix.Unveil(staticDir, "r")
unix.Unveil(davDir, "rwc")
err = unix.UnveilBlock()
if err != nil {
log.Fatal(err)
}
}
func main() {
wdav := &webdav.Handler{
Prefix: "/dav/",
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(davDir),
Logger: func(r *http.Request, err error) {
log.Printf("%s : %s - %s", r.Method, r.URL.Path, err)
},
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(staticDir)))
mux.HandleFunc("/dav/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !(ok == true && u == "qbit" && p == "hai") {
w.Header().Set("WWW-Authenticate", `Basic realm="davfs"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
wdav.ServeHTTP(w, r)
}))
lis, err := net.Listen("tcp", listen)
if err != nil {
log.Panic(err)
}
s := http.Server{Handler: mux}
log.Panic(s.Serve(lis))
}