2019-10-24 16:50:35 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-01-31 18:04:00 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-10-24 16:50:35 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/service"
|
2019-11-18 22:30:37 +00:00
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2020-01-19 06:41:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-10-24 16:50:35 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-10-26 16:25:53 +00:00
|
|
|
directoryToServe string
|
|
|
|
portableSFTPDPort int
|
|
|
|
portableAdvertiseService bool
|
|
|
|
portableAdvertiseCredentials bool
|
|
|
|
portableUsername string
|
|
|
|
portablePassword string
|
|
|
|
portableLogFile string
|
|
|
|
portablePublicKeys []string
|
|
|
|
portablePermissions []string
|
2019-11-18 22:30:37 +00:00
|
|
|
portableSSHCommands []string
|
2020-01-19 06:41:05 +00:00
|
|
|
portableFsProvider int
|
|
|
|
portableS3Bucket string
|
|
|
|
portableS3Region string
|
|
|
|
portableS3AccessKey string
|
|
|
|
portableS3AccessSecret string
|
|
|
|
portableS3Endpoint string
|
|
|
|
portableS3StorageClass string
|
2020-01-19 22:23:09 +00:00
|
|
|
portableS3KeyPrefix string
|
2020-01-31 18:04:00 +00:00
|
|
|
portableGCSBucket string
|
|
|
|
portableGCSCredentialsFile string
|
|
|
|
portableGCSStorageClass string
|
|
|
|
portableGCSKeyPrefix string
|
2019-10-26 16:25:53 +00:00
|
|
|
portableCmd = &cobra.Command{
|
2019-10-24 16:50:35 +00:00
|
|
|
Use: "portable",
|
|
|
|
Short: "Serve a single directory",
|
|
|
|
Long: `To serve the current working directory with auto generated credentials simply use:
|
|
|
|
|
|
|
|
sftpgo portable
|
|
|
|
|
|
|
|
Please take a look at the usage below to customize the serving parameters`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
portableDir := directoryToServe
|
|
|
|
if !filepath.IsAbs(portableDir) {
|
|
|
|
portableDir, _ = filepath.Abs(portableDir)
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = portablePermissions
|
2020-01-31 18:04:00 +00:00
|
|
|
portableGCSCredentials := ""
|
|
|
|
if portableFsProvider == 2 {
|
|
|
|
fi, err := os.Stat(portableGCSCredentialsFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Invalid GCS credentials file: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fi.Size() > 1048576 {
|
|
|
|
fmt.Printf("Invalid GCS credentials file: %#v is too big %v/1048576 bytes\n", portableGCSCredentialsFile,
|
|
|
|
fi.Size())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
creds, err := ioutil.ReadFile(portableGCSCredentialsFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unable to read credentials file: %v\n", err)
|
|
|
|
}
|
|
|
|
portableGCSCredentials = base64.StdEncoding.EncodeToString(creds)
|
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
service := service.Service{
|
|
|
|
ConfigDir: defaultConfigDir,
|
|
|
|
ConfigFile: defaultConfigName,
|
2019-10-26 16:25:53 +00:00
|
|
|
LogFilePath: portableLogFile,
|
2019-10-24 16:50:35 +00:00
|
|
|
LogMaxSize: defaultLogMaxSize,
|
|
|
|
LogMaxBackups: defaultLogMaxBackup,
|
|
|
|
LogMaxAge: defaultLogMaxAge,
|
|
|
|
LogCompress: defaultLogCompress,
|
|
|
|
LogVerbose: defaultLogVerbose,
|
|
|
|
Shutdown: make(chan bool),
|
|
|
|
PortableMode: 1,
|
|
|
|
PortableUser: dataprovider.User{
|
|
|
|
Username: portableUsername,
|
|
|
|
Password: portablePassword,
|
|
|
|
PublicKeys: portablePublicKeys,
|
2019-12-25 17:20:19 +00:00
|
|
|
Permissions: permissions,
|
2019-10-24 16:50:35 +00:00
|
|
|
HomeDir: portableDir,
|
2019-11-13 10:36:21 +00:00
|
|
|
Status: 1,
|
2020-01-19 06:41:05 +00:00
|
|
|
FsConfig: dataprovider.Filesystem{
|
|
|
|
Provider: portableFsProvider,
|
|
|
|
S3Config: vfs.S3FsConfig{
|
|
|
|
Bucket: portableS3Bucket,
|
|
|
|
Region: portableS3Region,
|
|
|
|
AccessKey: portableS3AccessKey,
|
|
|
|
AccessSecret: portableS3AccessSecret,
|
|
|
|
Endpoint: portableS3Endpoint,
|
|
|
|
StorageClass: portableS3StorageClass,
|
2020-01-19 22:23:09 +00:00
|
|
|
KeyPrefix: portableS3KeyPrefix,
|
2020-01-19 06:41:05 +00:00
|
|
|
},
|
2020-01-31 18:04:00 +00:00
|
|
|
GCSConfig: vfs.GCSFsConfig{
|
|
|
|
Bucket: portableGCSBucket,
|
|
|
|
Credentials: portableGCSCredentials,
|
|
|
|
StorageClass: portableGCSStorageClass,
|
|
|
|
KeyPrefix: portableGCSKeyPrefix,
|
|
|
|
},
|
2020-01-19 06:41:05 +00:00
|
|
|
},
|
2019-10-24 16:50:35 +00:00
|
|
|
},
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
if err := service.StartPortableMode(portableSFTPDPort, portableSSHCommands, portableAdvertiseService,
|
2019-10-26 16:25:53 +00:00
|
|
|
portableAdvertiseCredentials); err == nil {
|
2019-10-24 16:50:35 +00:00
|
|
|
service.Wait()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
portableCmd.Flags().StringVarP(&directoryToServe, "directory", "d", ".",
|
|
|
|
"Path to the directory to serve. This can be an absolute path or a path relative to the current directory")
|
|
|
|
portableCmd.Flags().IntVarP(&portableSFTPDPort, "sftpd-port", "s", 0, "0 means a random non privileged port")
|
2019-11-18 22:30:37 +00:00
|
|
|
portableCmd.Flags().StringSliceVarP(&portableSSHCommands, "ssh-commands", "c", sftpd.GetDefaultSSHCommands(),
|
|
|
|
"SSH commands to enable. \"*\" means any supported SSH command including scp")
|
2019-10-24 16:50:35 +00:00
|
|
|
portableCmd.Flags().StringVarP(&portableUsername, "username", "u", "", "Leave empty to use an auto generated value")
|
|
|
|
portableCmd.Flags().StringVarP(&portablePassword, "password", "p", "", "Leave empty to use an auto generated value")
|
2019-10-26 16:25:53 +00:00
|
|
|
portableCmd.Flags().StringVarP(&portableLogFile, logFilePathFlag, "l", "", "Leave empty to disable logging")
|
2019-10-24 16:50:35 +00:00
|
|
|
portableCmd.Flags().StringSliceVarP(&portablePublicKeys, "public-key", "k", []string{}, "")
|
|
|
|
portableCmd.Flags().StringSliceVarP(&portablePermissions, "permissions", "g", []string{"list", "download"},
|
|
|
|
"User's permissions. \"*\" means any permission")
|
2019-10-26 16:25:53 +00:00
|
|
|
portableCmd.Flags().BoolVarP(&portableAdvertiseService, "advertise-service", "S", true,
|
2019-11-08 07:43:27 +00:00
|
|
|
"Advertise SFTP service using multicast DNS")
|
2019-10-26 16:25:53 +00:00
|
|
|
portableCmd.Flags().BoolVarP(&portableAdvertiseCredentials, "advertise-credentials", "C", false,
|
2019-11-14 13:18:43 +00:00
|
|
|
"If the SFTP service is advertised via multicast DNS this flag allows to put username/password inside the advertised TXT record")
|
2020-01-31 18:04:00 +00:00
|
|
|
portableCmd.Flags().IntVarP(&portableFsProvider, "fs-provider", "f", 0, "0 means local filesystem, 1 Amazon S3 compatible, "+
|
|
|
|
"2 Google Cloud Storage")
|
2020-01-19 06:41:05 +00:00
|
|
|
portableCmd.Flags().StringVar(&portableS3Bucket, "s3-bucket", "", "")
|
|
|
|
portableCmd.Flags().StringVar(&portableS3Region, "s3-region", "", "")
|
|
|
|
portableCmd.Flags().StringVar(&portableS3AccessKey, "s3-access-key", "", "")
|
|
|
|
portableCmd.Flags().StringVar(&portableS3AccessSecret, "s3-access-secret", "", "")
|
|
|
|
portableCmd.Flags().StringVar(&portableS3Endpoint, "s3-endpoint", "", "")
|
|
|
|
portableCmd.Flags().StringVar(&portableS3StorageClass, "s3-storage-class", "", "")
|
2020-01-19 22:23:09 +00:00
|
|
|
portableCmd.Flags().StringVar(&portableS3KeyPrefix, "s3-key-prefix", "", "Allows to restrict access to the virtual folder "+
|
|
|
|
"identified by this prefix and its contents")
|
2020-01-31 18:04:00 +00:00
|
|
|
portableCmd.Flags().StringVar(&portableGCSBucket, "gcs-bucket", "", "")
|
|
|
|
portableCmd.Flags().StringVar(&portableGCSStorageClass, "gcs-storage-class", "", "")
|
|
|
|
portableCmd.Flags().StringVar(&portableGCSKeyPrefix, "gcs-key-prefix", "", "Allows to restrict access to the virtual folder "+
|
|
|
|
"identified by this prefix and its contents")
|
|
|
|
portableCmd.Flags().StringVar(&portableGCSCredentialsFile, "gcs-credentials-file", "", "Google Cloud Storage JSON credentials file")
|
2019-10-24 16:50:35 +00:00
|
|
|
rootCmd.AddCommand(portableCmd)
|
|
|
|
}
|