webdavd.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Package webdavd implements the WebDAV protocol
  2. package webdavd
  3. import (
  4. "path/filepath"
  5. "github.com/drakkan/sftpgo/logger"
  6. "github.com/drakkan/sftpgo/utils"
  7. )
  8. type ctxReqParams int
  9. const (
  10. requestIDKey ctxReqParams = iota
  11. requestStartKey
  12. )
  13. const (
  14. logSender = "webdavd"
  15. )
  16. var (
  17. server *webDavServer
  18. )
  19. // Configuration defines the configuration for the WevDAV server
  20. type Configuration struct {
  21. // The port used for serving FTP requests
  22. BindPort int `json:"bind_port" mapstructure:"bind_port"`
  23. // The address to listen on. A blank value means listen on all available network interfaces.
  24. BindAddress string `json:"bind_address" mapstructure:"bind_address"`
  25. // If files containing a certificate and matching private key for the server are provided the server will expect
  26. // HTTPS connections.
  27. // Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
  28. // "paramchange" request to the running service on Windows.
  29. CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
  30. CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
  31. }
  32. // Initialize configures and starts the WebDav server
  33. func (c *Configuration) Initialize(configDir string) error {
  34. var err error
  35. logger.Debug(logSender, "", "initializing WevDav server with config %+v", *c)
  36. server, err = newServer(c, configDir)
  37. if err != nil {
  38. return err
  39. }
  40. return server.listenAndServe()
  41. }
  42. // ReloadTLSCertificate reloads the TLS certificate and key from the configured paths
  43. func ReloadTLSCertificate() error {
  44. if server != nil && server.certMgr != nil {
  45. return server.certMgr.LoadCertificate(logSender)
  46. }
  47. return nil
  48. }
  49. func getConfigPath(name, configDir string) string {
  50. if !utils.IsFileInputValid(name) {
  51. return ""
  52. }
  53. if len(name) > 0 && !filepath.IsAbs(name) {
  54. return filepath.Join(configDir, name)
  55. }
  56. return name
  57. }