2019-10-08 08:29:16 +00:00
|
|
|
// Package service allows to start and stop the SFTPGo service
|
2019-09-16 06:52:58 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2020-01-31 18:04:00 +00:00
|
|
|
"path/filepath"
|
2019-10-24 16:50:35 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2019-10-21 19:10:07 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-06-19 15:08:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2019-09-16 06:52:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSender = "service"
|
|
|
|
)
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
var (
|
|
|
|
chars = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
|
|
|
)
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
// Service defines the SFTPGo service
|
|
|
|
type Service struct {
|
|
|
|
ConfigDir string
|
|
|
|
ConfigFile string
|
|
|
|
LogFilePath string
|
|
|
|
LogMaxSize int
|
|
|
|
LogMaxBackups int
|
|
|
|
LogMaxAge int
|
2019-10-24 16:50:35 +00:00
|
|
|
PortableMode int
|
|
|
|
PortableUser dataprovider.User
|
2020-04-30 12:23:55 +00:00
|
|
|
LogCompress bool
|
|
|
|
LogVerbose bool
|
2020-03-15 14:16:35 +00:00
|
|
|
Profiler bool
|
2019-09-16 06:52:58 +00:00
|
|
|
Shutdown chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start initializes the service
|
|
|
|
func (s *Service) Start() error {
|
|
|
|
logLevel := zerolog.DebugLevel
|
|
|
|
if !s.LogVerbose {
|
|
|
|
logLevel = zerolog.InfoLevel
|
|
|
|
}
|
2020-03-03 08:09:58 +00:00
|
|
|
if !filepath.IsAbs(s.LogFilePath) && utils.IsFileInputValid(s.LogFilePath) {
|
2020-03-02 23:34:06 +00:00
|
|
|
s.LogFilePath = filepath.Join(s.ConfigDir, s.LogFilePath)
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
logger.InitLogger(s.LogFilePath, s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogCompress, logLevel)
|
2019-10-26 16:25:53 +00:00
|
|
|
if s.PortableMode == 1 {
|
|
|
|
logger.EnableConsoleLogger(logLevel)
|
|
|
|
if len(s.LogFilePath) == 0 {
|
|
|
|
logger.DisableLogger()
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 19:18:45 +00:00
|
|
|
logger.Info(logSender, "", "starting SFTPGo %v, config dir: %v, config file: %v, log max size: %v log max backups: %v "+
|
2020-06-19 15:08:51 +00:00
|
|
|
"log max age: %v log verbose: %v, log compress: %v, profile: %v", version.GetAsString(), s.ConfigDir, s.ConfigFile,
|
2020-03-15 14:16:35 +00:00
|
|
|
s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogVerbose, s.LogCompress, s.Profiler)
|
2019-10-24 16:50:35 +00:00
|
|
|
// in portable mode we don't read configuration from file
|
|
|
|
if s.PortableMode != 1 {
|
2020-04-30 12:23:55 +00:00
|
|
|
err := config.LoadConfig(s.ConfigDir, s.ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error loading configuration: %v", err)
|
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
|
|
|
|
err := dataprovider.Initialize(providerConf, s.ConfigDir)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error initializing data provider: %v", err)
|
|
|
|
logger.ErrorToConsole("error initializing data provider: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:29:09 +00:00
|
|
|
httpConfig := config.GetHTTPConfig()
|
|
|
|
httpConfig.Initialize(s.ConfigDir)
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode == 1 {
|
|
|
|
// create the user for portable mode
|
2020-07-08 17:59:31 +00:00
|
|
|
err = dataprovider.AddUser(s.PortableUser)
|
2019-10-24 16:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error adding portable user: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
go func() {
|
|
|
|
logger.Debug(logSender, "", "initializing SFTP server with config %+v", sftpdConf)
|
|
|
|
if err := sftpdConf.Initialize(s.ConfigDir); err != nil {
|
|
|
|
logger.Error(logSender, "", "could not start SFTP server: %v", err)
|
|
|
|
logger.ErrorToConsole("could not start SFTP server: %v", err)
|
|
|
|
}
|
|
|
|
s.Shutdown <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
if httpdConf.BindPort > 0 {
|
|
|
|
go func() {
|
2020-03-15 14:16:35 +00:00
|
|
|
if err := httpdConf.Initialize(s.ConfigDir, s.Profiler); err != nil {
|
2019-09-16 06:52:58 +00:00
|
|
|
logger.Error(logSender, "", "could not start HTTP server: %v", err)
|
|
|
|
logger.ErrorToConsole("could not start HTTP server: %v", err)
|
|
|
|
}
|
|
|
|
s.Shutdown <- true
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
logger.Debug(logSender, "", "HTTP server not started, disabled in config file")
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
logger.DebugToConsole("HTTP server not started, disabled in config file")
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until the service exits
|
|
|
|
func (s *Service) Wait() {
|
2020-02-04 22:21:33 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
registerSigHup()
|
2020-06-22 17:11:53 +00:00
|
|
|
registerSigUSR1()
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
<-s.Shutdown
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:29:16 +00:00
|
|
|
// Stop terminates the service unblocking the Wait method
|
2019-09-16 06:52:58 +00:00
|
|
|
func (s *Service) Stop() {
|
|
|
|
close(s.Shutdown)
|
|
|
|
logger.Debug(logSender, "", "Service stopped")
|
|
|
|
}
|