webdav.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package webdav
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "golang.org/x/net/webdav"
  8. "imuslab.com/bokofs/bokofsd/mod/bokoworker"
  9. )
  10. /*
  11. WebDAV module
  12. This module handle the interfacing to the underlying file system
  13. through the middle ware
  14. */
  15. type Options struct {
  16. ListeningAddress string //Listening address, e.g. 0.0.0.0:8443
  17. SecureServe bool //Use TLS to serve the WebDAV request
  18. PublicKeyPath string
  19. PrivateKeyPath string
  20. }
  21. type Server struct {
  22. LoadedWorkers *bokoworker.Worker
  23. }
  24. func NewWebdavInterfaceServer() *Server {
  25. return &Server{}
  26. }
  27. func (s *Server) Start() error {
  28. srv := &webdav.Handler{
  29. FileSystem: webdav.Dir(dir),
  30. LockSystem: webdav.NewMemLS(),
  31. Logger: func(r *http.Request, err error) {
  32. if err != nil {
  33. log.Printf("WEBDAV [%s]: %s, ERROR: %s\n", r.Method, r.URL, err)
  34. } else {
  35. log.Printf("WEBDAV [%s]: %s \n", r.Method, r.URL)
  36. }
  37. },
  38. }
  39. http.Handle("/", srv)
  40. if *serveSecure == true {
  41. if _, err := os.Stat("./cert.pem"); err != nil {
  42. fmt.Println("[x] No cert.pem in current directory. Please provide a valid cert")
  43. return
  44. }
  45. if _, er := os.Stat("./key.pem"); er != nil {
  46. fmt.Println("[x] No key.pem in current directory. Please provide a valid cert")
  47. return
  48. }
  49. go http.ListenAndServeTLS(fmt.Sprintf(":%d", *httpsPort), "cert.pem", "key.pem", nil)
  50. }
  51. if err := http.ListenAndServe(fmt.Sprintf(":%d", *httpPort), nil); err != nil {
  52. log.Fatalf("Error with WebDAV server: %v", err)
  53. }
  54. }