Skip to content

Instantly share code, notes, and snippets.

@weaming
Forked from staaldraad/webdavserv.go
Created November 28, 2019 05:00
Show Gist options
  • Save weaming/7f8cc712c9040f15963554d55a503f29 to your computer and use it in GitHub Desktop.
Save weaming/7f8cc712c9040f15963554d55a503f29 to your computer and use it in GitHub Desktop.
A small webdav server in go
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"golang.org/x/net/webdav"
)
var dir string
func main() {
dirFlag := flag.String("d", "./", "Directory to serve from. Default is CWD")
httpPort := flag.Int("p", 80, "Port to serve on (Plain HTTP)")
httpsPort := flag.Int("ps", 443, "Port to serve TLS on")
serveSecure := flag.Bool("s", false, "Serve HTTPS. Default false")
flag.Parse()
dir = *dirFlag
srv := &webdav.Handler{
FileSystem: webdav.Dir(dir),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Printf("WEBDAV [%s]: %s, ERROR: %s\n", r.Method, r.URL, err)
} else {
log.Printf("WEBDAV [%s]: %s \n", r.Method, r.URL)
}
},
}
http.Handle("/", srv)
if *serveSecure == true {
if _, err := os.Stat("./cert.pem"); err != nil {
fmt.Println("[x] No cert.pem in current directory. Please provide a valid cert")
return
}
if _, er := os.Stat("./key.pem"); er != nil {
fmt.Println("[x] No key.pem in current directory. Please provide a valid cert")
return
}
go http.ListenAndServeTLS(fmt.Sprintf(":%d", *httpsPort), "cert.pem", "key.pem", nil)
}
if err := http.ListenAndServe(fmt.Sprintf(":%d", *httpPort), nil); err != nil {
log.Fatalf("Error with WebDAV server: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment