diff --git a/docs/portable-mode.md b/docs/portable-mode.md index a5eee3a4..9721428b 100644 --- a/docs/portable-mode.md +++ b/docs/portable-mode.md @@ -181,3 +181,6 @@ Flags: --webdav-port int 0 means a random unprivileged port, < 0 disabled (default -1) ``` + +In portable mode you can apply further customizations using a configuration file/environment variables as for the service mode. +SFTP, FTP, HTTP and WebDAV settings configured using the CLI flags are applied to the first binding, any additional bindings will not be affected. diff --git a/internal/common/eventmanager.go b/internal/common/eventmanager.go index 38c7d5f2..b3a36202 100644 --- a/internal/common/eventmanager.go +++ b/internal/common/eventmanager.go @@ -783,13 +783,13 @@ func (p *EventParams) getStringReplacements(addObjectData, jsonEscaped bool) []s "{{FsTargetPath}}", p.getStringReplacement(p.FsTargetPath, jsonEscaped), "{{ObjectName}}", p.getStringReplacement(p.ObjectName, jsonEscaped), "{{ObjectType}}", p.ObjectType, - "{{FileSize}}", fmt.Sprintf("%d", p.FileSize), - "{{Elapsed}}", fmt.Sprintf("%d", p.Elapsed), + "{{FileSize}}", strconv.FormatInt(p.FileSize, 10), + "{{Elapsed}}", strconv.FormatInt(p.Elapsed, 10), "{{Protocol}}", p.Protocol, "{{IP}}", p.IP, "{{Role}}", p.getStringReplacement(p.Role, jsonEscaped), "{{Email}}", p.getStringReplacement(p.Email, jsonEscaped), - "{{Timestamp}}", fmt.Sprintf("%d", p.Timestamp), + "{{Timestamp}}", strconv.FormatInt(p.Timestamp, 10), "{{StatusString}}", p.getStatusString(), "{{UID}}", p.getStringReplacement(p.UID, jsonEscaped), "{{Ext}}", p.getStringReplacement(p.Extension, jsonEscaped), diff --git a/internal/service/service_portable.go b/internal/service/service_portable.go index 1c3dafcf..db3a9146 100644 --- a/internal/service/service_portable.go +++ b/internal/service/service_portable.go @@ -65,21 +65,10 @@ func (s *Service) StartPortableMode(sftpdPort, ftpPort, webdavPort, httpPort int telemetryConf.BindPort = 0 config.SetTelemetryConfig(telemetryConf) - if sftpdPort >= 0 { - configurePortableSFTPService(sftpdPort, enabledSSHCommands) - } - - if ftpPort >= 0 { - configurePortableFTPService(ftpPort, ftpsCert, ftpsKey) - } - - if webdavPort >= 0 { - configurePortableWebDAVService(webdavPort, webDavCert, webDavKey) - } - - if httpPort >= 0 { - configurePortableHTTPService(httpPort, httpsCert, httpsKey) - } + configurePortableSFTPService(sftpdPort, enabledSSHCommands) + configurePortableFTPService(ftpPort, ftpsCert, ftpsKey) + configurePortableWebDAVService(webdavPort, webDavCert, webDavKey) + configurePortableHTTPService(httpPort, httpsCert, httpsKey) err = s.Start(true) if err != nil { @@ -217,9 +206,11 @@ func configurePortableSFTPService(port int, enabledSSHCommands []string) { } if port > 0 { sftpdConf.Bindings[0].Port = port - } else { + } else if port == 0 { // dynamic ports starts from 49152 sftpdConf.Bindings[0].Port = 49152 + rand.Intn(15000) + } else { + sftpdConf.Bindings[0].Port = 0 } if util.Contains(enabledSSHCommands, "*") { sftpdConf.EnabledSSHCommands = sftpd.GetSupportedSSHCommands() @@ -236,8 +227,10 @@ func configurePortableFTPService(port int, cert, key string) { } if port > 0 { ftpConf.Bindings[0].Port = port - } else { + } else if port == 0 { ftpConf.Bindings[0].Port = 49152 + rand.Intn(15000) + } else { + ftpConf.Bindings[0].Port = 0 } if ftpConf.Banner == "" { ftpConf.Banner = fmt.Sprintf("SFTPGo portable %v ready", version.Get().Version) @@ -254,12 +247,16 @@ func configurePortableWebDAVService(port int, cert, key string) { } if port > 0 { webDavConf.Bindings[0].Port = port - } else { + } else if port == 0 { webDavConf.Bindings[0].Port = 49152 + rand.Intn(15000) + } else { + webDavConf.Bindings[0].Port = 0 } webDavConf.Bindings[0].CertificateFile = cert webDavConf.Bindings[0].CertificateKeyFile = key - webDavConf.Bindings[0].EnableHTTPS = true + if cert != "" && key != "" { + webDavConf.Bindings[0].EnableHTTPS = true + } config.SetWebDAVDConfig(webDavConf) } @@ -270,12 +267,16 @@ func configurePortableHTTPService(port int, cert, key string) { } if port > 0 { httpdConf.Bindings[0].Port = port - } else { + } else if port == 0 { httpdConf.Bindings[0].Port = 49152 + rand.Intn(15000) + } else { + httpdConf.Bindings[0].Port = 0 } httpdConf.Bindings[0].CertificateFile = cert httpdConf.Bindings[0].CertificateKeyFile = key - httpdConf.Bindings[0].EnableHTTPS = true + if cert != "" && key != "" { + httpdConf.Bindings[0].EnableHTTPS = true + } httpdConf.Bindings[0].EnableWebAdmin = false httpdConf.Bindings[0].EnableWebClient = true httpdConf.Bindings[0].EnableRESTAPI = false