webdavd.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // Cors configuration
  20. type Cors struct {
  21. AllowedOrigins []string `json:"allowed_origins" mapstructure:"allowed_origins"`
  22. AllowedMethods []string `json:"allowed_methods" mapstructure:"allowed_methods"`
  23. AllowedHeaders []string `json:"allowed_headers" mapstructure:"allowed_headers"`
  24. ExposedHeaders []string `json:"exposed_headers" mapstructure:"exposed_headers"`
  25. AllowCredentials bool `json:"allow_credentials" mapstructure:"allow_credentials"`
  26. Enabled bool `json:"enabled" mapstructure:"enabled"`
  27. MaxAge int `json:"max_age" mapstructure:"max_age"`
  28. }
  29. // UsersCacheConfig defines the cache configuration for users
  30. type UsersCacheConfig struct {
  31. ExpirationTime int `json:"expiration_time" mapstructure:"expiration_time"`
  32. MaxSize int `json:"max_size" mapstructure:"max_size"`
  33. }
  34. // MimeCacheConfig defines the cache configuration for mime types
  35. type MimeCacheConfig struct {
  36. Enabled bool `json:"enabled" mapstructure:"enabled"`
  37. MaxSize int `json:"max_size" mapstructure:"max_size"`
  38. }
  39. // Cache configuration
  40. type Cache struct {
  41. Users UsersCacheConfig `json:"users" mapstructure:"users"`
  42. MimeTypes MimeCacheConfig `json:"mime_types" mapstructure:"mime_types"`
  43. }
  44. // Configuration defines the configuration for the WevDAV server
  45. type Configuration struct {
  46. // The port used for serving FTP requests
  47. BindPort int `json:"bind_port" mapstructure:"bind_port"`
  48. // The address to listen on. A blank value means listen on all available network interfaces.
  49. BindAddress string `json:"bind_address" mapstructure:"bind_address"`
  50. // If files containing a certificate and matching private key for the server are provided the server will expect
  51. // HTTPS connections.
  52. // Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
  53. // "paramchange" request to the running service on Windows.
  54. CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
  55. CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
  56. // CORS configuration
  57. Cors Cors `json:"cors" mapstructure:"cors"`
  58. // Cache configuration
  59. Cache Cache `json:"cache" mapstructure:"cache"`
  60. }
  61. // Initialize configures and starts the WebDav server
  62. func (c *Configuration) Initialize(configDir string) error {
  63. var err error
  64. logger.Debug(logSender, "", "initializing WevDav server with config %+v", *c)
  65. mimeTypeCache = mimeCache{
  66. maxSize: c.Cache.MimeTypes.MaxSize,
  67. mimeTypes: make(map[string]string),
  68. }
  69. if !c.Cache.MimeTypes.Enabled {
  70. mimeTypeCache.maxSize = 0
  71. }
  72. server, err = newServer(c, configDir)
  73. if err != nil {
  74. return err
  75. }
  76. return server.listenAndServe()
  77. }
  78. // ReloadTLSCertificate reloads the TLS certificate and key from the configured paths
  79. func ReloadTLSCertificate() error {
  80. if server != nil && server.certMgr != nil {
  81. return server.certMgr.LoadCertificate(logSender)
  82. }
  83. return nil
  84. }
  85. func getConfigPath(name, configDir string) string {
  86. if !utils.IsFileInputValid(name) {
  87. return ""
  88. }
  89. if len(name) > 0 && !filepath.IsAbs(name) {
  90. return filepath.Join(configDir, name)
  91. }
  92. return name
  93. }