config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Package config manages the configuration.
  2. // Configuration is loaded from sftpgo.conf file.
  3. // If sftpgo.conf is not found or cannot be readed or decoded as json the default configuration is used.
  4. // The default configuration an be found inside the source tree:
  5. // https://github.com/drakkan/sftpgo/blob/master/sftpgo.conf
  6. package config
  7. import (
  8. "encoding/json"
  9. "os"
  10. "strings"
  11. "github.com/drakkan/sftpgo/api"
  12. "github.com/drakkan/sftpgo/dataprovider"
  13. "github.com/drakkan/sftpgo/logger"
  14. "github.com/drakkan/sftpgo/sftpd"
  15. )
  16. const (
  17. logSender = "config"
  18. defaultBanner = "SFTPGo"
  19. )
  20. var (
  21. globalConf globalConfig
  22. )
  23. type globalConfig struct {
  24. SFTPD sftpd.Configuration `json:"sftpd"`
  25. ProviderConf dataprovider.Config `json:"data_provider"`
  26. HTTPDConfig api.HTTPDConf `json:"httpd"`
  27. }
  28. func init() {
  29. // create a default configuration to use if no config file is provided
  30. globalConf = globalConfig{
  31. SFTPD: sftpd.Configuration{
  32. Banner: defaultBanner,
  33. BindPort: 2022,
  34. BindAddress: "",
  35. IdleTimeout: 15,
  36. MaxAuthTries: 0,
  37. Umask: "0022",
  38. Actions: sftpd.Actions{
  39. ExecuteOn: []string{},
  40. Command: "",
  41. HTTPNotificationURL: "",
  42. },
  43. },
  44. ProviderConf: dataprovider.Config{
  45. Driver: "sqlite",
  46. Name: "sftpgo.db",
  47. Host: "",
  48. Port: 5432,
  49. Username: "",
  50. Password: "",
  51. ConnectionString: "",
  52. UsersTable: "users",
  53. ManageUsers: 1,
  54. SSLMode: 0,
  55. TrackQuota: 1,
  56. },
  57. HTTPDConfig: api.HTTPDConf{
  58. BindPort: 8080,
  59. BindAddress: "127.0.0.1",
  60. },
  61. }
  62. }
  63. // GetSFTPDConfig returns the configuration for the SFTP server
  64. func GetSFTPDConfig() sftpd.Configuration {
  65. return globalConf.SFTPD
  66. }
  67. // GetHTTPDConfig returns the configuration for the HTTP server
  68. func GetHTTPDConfig() api.HTTPDConf {
  69. return globalConf.HTTPDConfig
  70. }
  71. //GetProviderConf returns the configuration for the data provider
  72. func GetProviderConf() dataprovider.Config {
  73. return globalConf.ProviderConf
  74. }
  75. // LoadConfig loads the configuration from sftpgo.conf or use the default configuration.
  76. func LoadConfig(configPath string) error {
  77. logger.Debug(logSender, "load config from path: %v", configPath)
  78. file, err := os.Open(configPath)
  79. if err != nil {
  80. logger.Warn(logSender, "error loading configuration file: %v. Default configuration will be used: %+v", err, globalConf)
  81. return err
  82. }
  83. defer file.Close()
  84. err = json.NewDecoder(file).Decode(&globalConf)
  85. if err != nil {
  86. logger.Warn(logSender, "error parsing config file: %v. Default configuration will be used: %+v", err, globalConf)
  87. return err
  88. }
  89. if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
  90. globalConf.SFTPD.Banner = defaultBanner
  91. }
  92. logger.Debug(logSender, "config loaded: %+v", globalConf)
  93. return err
  94. }