2014-03-28 17:56:04 +00:00
|
|
|
// Read, write and edit the config file
|
|
|
|
|
2014-03-15 16:06:11 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-02-16 15:25:27 +00:00
|
|
|
"bytes"
|
2016-08-14 11:04:43 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2016-02-16 15:25:27 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2015-09-01 21:33:34 +00:00
|
|
|
"encoding/base64"
|
2014-03-15 16:06:11 +00:00
|
|
|
"fmt"
|
2016-02-16 15:25:27 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-03-15 16:06:11 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
2017-01-14 03:47:55 +00:00
|
|
|
"path/filepath"
|
2016-12-20 18:03:09 +00:00
|
|
|
"regexp"
|
2014-03-15 16:06:11 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2016-02-16 15:25:27 +00:00
|
|
|
"unicode/utf8"
|
2014-03-15 16:06:11 +00:00
|
|
|
|
2016-02-17 10:45:05 +00:00
|
|
|
"github.com/Unknwon/goconfig"
|
2016-06-12 14:06:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-02-09 21:22:46 +00:00
|
|
|
"github.com/spf13/pflag"
|
2016-02-16 15:25:27 +00:00
|
|
|
"golang.org/x/crypto/nacl/secretbox"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
2014-03-15 16:06:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-14 03:47:55 +00:00
|
|
|
configFileName = "rclone.conf"
|
|
|
|
hiddenConfigFileName = "." + configFileName
|
2016-01-07 15:20:32 +00:00
|
|
|
|
|
|
|
// ConfigToken is the key used to store the token under
|
|
|
|
ConfigToken = "token"
|
|
|
|
|
|
|
|
// ConfigClientID is the config key used to store the client id
|
|
|
|
ConfigClientID = "client_id"
|
|
|
|
|
|
|
|
// ConfigClientSecret is the config key used to store the client secret
|
|
|
|
ConfigClientSecret = "client_secret"
|
|
|
|
|
|
|
|
// ConfigAutomatic indicates that we want non-interactive configuration
|
|
|
|
ConfigAutomatic = "config_automatic"
|
2014-03-15 16:06:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Global
|
|
|
|
var (
|
2016-12-20 18:03:09 +00:00
|
|
|
// configData is the config file data structure
|
|
|
|
configData *goconfig.ConfigFile
|
2015-09-22 17:47:16 +00:00
|
|
|
// ConfigPath points to the config file
|
2017-01-14 03:47:55 +00:00
|
|
|
ConfigPath = makeConfigPath()
|
2015-09-22 17:47:16 +00:00
|
|
|
// Config is the global config
|
2014-03-15 16:06:11 +00:00
|
|
|
Config = &ConfigInfo{}
|
|
|
|
// Flags
|
2017-02-09 21:22:46 +00:00
|
|
|
verbose = CountP("verbose", "v", "Print lots more stuff (repeat for more)")
|
2016-12-20 15:50:46 +00:00
|
|
|
quiet = BoolP("quiet", "q", false, "Print as little stuff as possible")
|
2017-02-09 21:22:46 +00:00
|
|
|
logLevel = StringP("log-level", "", "INFO", "Log level DEBUG|INFO|NOTICE|ERROR")
|
2016-12-20 15:50:46 +00:00
|
|
|
modifyWindow = DurationP("modify-window", "", time.Nanosecond, "Max time diff to be considered the same")
|
|
|
|
checkers = IntP("checkers", "", 8, "Number of checkers to run in parallel.")
|
|
|
|
transfers = IntP("transfers", "", 4, "Number of file transfers to run in parallel.")
|
|
|
|
configFile = StringP("config", "", ConfigPath, "Config file.")
|
|
|
|
checkSum = BoolP("checksum", "c", false, "Skip based on checksum & size, not mod-time & size")
|
|
|
|
sizeOnly = BoolP("size-only", "", false, "Skip based on size only, not mod-time or checksum")
|
|
|
|
ignoreTimes = BoolP("ignore-times", "I", false, "Don't skip files that match size and time - transfer all files")
|
|
|
|
ignoreExisting = BoolP("ignore-existing", "", false, "Skip all files that exist on destination")
|
|
|
|
dryRun = BoolP("dry-run", "n", false, "Do a trial run with no permanent changes")
|
|
|
|
connectTimeout = DurationP("contimeout", "", 60*time.Second, "Connect timeout")
|
|
|
|
timeout = DurationP("timeout", "", 5*60*time.Second, "IO idle timeout")
|
|
|
|
dumpHeaders = BoolP("dump-headers", "", false, "Dump HTTP headers - may contain sensitive info")
|
|
|
|
dumpBodies = BoolP("dump-bodies", "", false, "Dump HTTP headers and bodies - may contain sensitive info")
|
|
|
|
dumpAuth = BoolP("dump-auth", "", false, "Dump HTTP headers with auth info")
|
|
|
|
skipVerify = BoolP("no-check-certificate", "", false, "Do not verify the server SSL certificate. Insecure.")
|
|
|
|
AskPassword = BoolP("ask-password", "", true, "Allow prompt for password for encrypted configuration.")
|
|
|
|
deleteBefore = BoolP("delete-before", "", false, "When synchronizing, delete files on destination before transfering")
|
|
|
|
deleteDuring = BoolP("delete-during", "", false, "When synchronizing, delete files during transfer (default)")
|
|
|
|
deleteAfter = BoolP("delete-after", "", false, "When synchronizing, delete files on destination after transfering")
|
|
|
|
trackRenames = BoolP("track-renames", "", false, "When synchronizing, track file renames and do a server side move if possible")
|
|
|
|
lowLevelRetries = IntP("low-level-retries", "", 10, "Number of low level retries to do.")
|
|
|
|
updateOlder = BoolP("update", "u", false, "Skip files that are newer on the destination.")
|
|
|
|
noGzip = BoolP("no-gzip-encoding", "", false, "Don't set Accept-Encoding: gzip.")
|
|
|
|
maxDepth = IntP("max-depth", "", -1, "If set limits the recursion depth to this.")
|
|
|
|
ignoreSize = BoolP("ignore-size", "", false, "Ignore size when skipping use mod-time or checksum.")
|
2017-01-05 17:32:42 +00:00
|
|
|
ignoreChecksum = BoolP("ignore-checksum", "", false, "Skip post copy check of checksums.")
|
2016-12-20 15:50:46 +00:00
|
|
|
noTraverse = BoolP("no-traverse", "", false, "Don't traverse destination file system on copy.")
|
|
|
|
noUpdateModTime = BoolP("no-update-modtime", "", false, "Don't update destination mod-time if files identical.")
|
2017-01-10 21:47:03 +00:00
|
|
|
backupDir = StringP("backup-dir", "", "", "Make backups into hierarchy based in DIR.")
|
2017-01-19 17:26:29 +00:00
|
|
|
suffix = StringP("suffix", "", "", "Suffix for use with --backup-dir.")
|
2017-01-03 02:52:41 +00:00
|
|
|
bwLimit BwTimetable
|
2017-02-14 19:31:33 +00:00
|
|
|
bufferSize SizeSuffix = 16 << 20
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
// Key to use for password en/decryption.
|
|
|
|
// When nil, no encryption will be used for saving.
|
|
|
|
configKey []byte
|
2014-03-15 16:06:11 +00:00
|
|
|
)
|
|
|
|
|
2015-02-19 19:26:00 +00:00
|
|
|
func init() {
|
2016-12-20 15:50:46 +00:00
|
|
|
VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix b|k|M|G or a full timetable.")
|
2017-02-14 19:31:33 +00:00
|
|
|
VarP(&bufferSize, "buffer-size", "", "Buffer size when copying files.")
|
2017-01-03 02:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 11:04:43 +00:00
|
|
|
// crypt internals
|
|
|
|
var (
|
|
|
|
cryptKey = []byte{
|
|
|
|
0x9c, 0x93, 0x5b, 0x48, 0x73, 0x0a, 0x55, 0x4d,
|
|
|
|
0x6b, 0xfd, 0x7c, 0x63, 0xc8, 0x86, 0xa9, 0x2b,
|
|
|
|
0xd3, 0x90, 0x19, 0x8e, 0xb8, 0x12, 0x8a, 0xfb,
|
|
|
|
0xf4, 0xde, 0x16, 0x2b, 0x8b, 0x95, 0xf6, 0x38,
|
|
|
|
}
|
|
|
|
cryptBlock cipher.Block
|
|
|
|
cryptRand = rand.Reader
|
|
|
|
)
|
|
|
|
|
|
|
|
// crypt transforms in to out using iv under AES-CTR.
|
|
|
|
//
|
|
|
|
// in and out may be the same buffer.
|
|
|
|
//
|
|
|
|
// Note encryption and decryption are the same operation
|
|
|
|
func crypt(out, in, iv []byte) error {
|
|
|
|
if cryptBlock == nil {
|
|
|
|
var err error
|
|
|
|
cryptBlock, err = aes.NewCipher(cryptKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream := cipher.NewCTR(cryptBlock, iv)
|
|
|
|
stream.XORKeyStream(out, in)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obscure a value
|
|
|
|
//
|
|
|
|
// This is done by encrypting with AES-CTR
|
|
|
|
func Obscure(x string) (string, error) {
|
|
|
|
plaintext := []byte(x)
|
|
|
|
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
|
|
|
iv := ciphertext[:aes.BlockSize]
|
|
|
|
if _, err := io.ReadFull(cryptRand, iv); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to read iv")
|
|
|
|
}
|
|
|
|
if err := crypt(ciphertext[aes.BlockSize:], plaintext, iv); err != nil {
|
|
|
|
return "", errors.Wrap(err, "encrypt failed")
|
2015-09-01 21:33:34 +00:00
|
|
|
}
|
2016-08-14 11:04:43 +00:00
|
|
|
return base64.RawURLEncoding.EncodeToString(ciphertext), nil
|
2015-09-01 21:33:34 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 11:04:43 +00:00
|
|
|
// MustObscure obscures a value, exiting with a fatal error if it failed
|
|
|
|
func MustObscure(x string) string {
|
|
|
|
out, err := Obscure(x)
|
2015-09-01 21:33:34 +00:00
|
|
|
if err != nil {
|
2016-08-14 11:04:43 +00:00
|
|
|
log.Fatalf("Obscure failed: %v", err)
|
2015-09-01 21:33:34 +00:00
|
|
|
}
|
2016-08-14 11:04:43 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reveal an obscured value
|
|
|
|
func Reveal(x string) (string, error) {
|
|
|
|
ciphertext, err := base64.RawURLEncoding.DecodeString(x)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "base64 decode failed")
|
|
|
|
}
|
|
|
|
if len(ciphertext) < aes.BlockSize {
|
|
|
|
return "", errors.New("input too short")
|
|
|
|
}
|
|
|
|
buf := ciphertext[aes.BlockSize:]
|
|
|
|
iv := ciphertext[:aes.BlockSize]
|
|
|
|
if err := crypt(buf, buf, iv); err != nil {
|
|
|
|
return "", errors.Wrap(err, "decrypt failed")
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustReveal reveals an obscured value, exiting with a fatal error if it failed
|
|
|
|
func MustReveal(x string) string {
|
|
|
|
out, err := Reveal(x)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Reveal failed: %v", err)
|
2015-09-01 21:33:34 +00:00
|
|
|
}
|
2016-08-14 11:04:43 +00:00
|
|
|
return out
|
2015-09-01 21:33:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ConfigInfo is filesystem config options
|
2014-03-15 16:06:11 +00:00
|
|
|
type ConfigInfo struct {
|
2017-02-09 21:22:46 +00:00
|
|
|
LogLevel LogLevel
|
2015-10-29 15:42:25 +00:00
|
|
|
DryRun bool
|
|
|
|
CheckSum bool
|
|
|
|
SizeOnly bool
|
2016-03-22 17:02:27 +00:00
|
|
|
IgnoreTimes bool
|
2016-01-05 10:35:36 +00:00
|
|
|
IgnoreExisting bool
|
2015-10-29 15:42:25 +00:00
|
|
|
ModifyWindow time.Duration
|
|
|
|
Checkers int
|
|
|
|
Transfers int
|
|
|
|
ConnectTimeout time.Duration // Connect timeout
|
|
|
|
Timeout time.Duration // Data channel timeout
|
|
|
|
DumpHeaders bool
|
|
|
|
DumpBodies bool
|
2016-11-02 15:53:43 +00:00
|
|
|
DumpAuth bool
|
2015-10-29 15:42:25 +00:00
|
|
|
Filter *Filter
|
|
|
|
InsecureSkipVerify bool // Skip server certificate verification
|
2017-01-25 19:35:14 +00:00
|
|
|
DeleteMode DeleteMode
|
2016-12-18 10:03:56 +00:00
|
|
|
TrackRenames bool // Track file renames.
|
2016-01-12 17:38:28 +00:00
|
|
|
LowLevelRetries int
|
2016-02-29 17:46:40 +00:00
|
|
|
UpdateOlder bool // Skip files that are newer on the destination
|
2016-02-14 18:14:41 +00:00
|
|
|
NoGzip bool // Disable compression
|
2016-06-02 20:02:44 +00:00
|
|
|
MaxDepth int
|
2016-06-17 16:20:08 +00:00
|
|
|
IgnoreSize bool
|
2017-01-05 17:32:42 +00:00
|
|
|
IgnoreChecksum bool
|
2016-06-25 13:28:26 +00:00
|
|
|
NoTraverse bool
|
2016-07-12 09:46:45 +00:00
|
|
|
NoUpdateModTime bool
|
2016-11-22 04:04:05 +00:00
|
|
|
DataRateUnit string
|
2017-01-10 21:47:03 +00:00
|
|
|
BackupDir string
|
2017-01-19 17:26:29 +00:00
|
|
|
Suffix string
|
2017-02-14 19:31:33 +00:00
|
|
|
BufferSize SizeSuffix
|
2015-05-10 10:25:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 03:47:55 +00:00
|
|
|
// Return the path to the configuration file
|
|
|
|
func makeConfigPath() string {
|
|
|
|
// Find user's home directory
|
2014-03-15 17:01:13 +00:00
|
|
|
usr, err := user.Current()
|
2017-01-14 03:47:55 +00:00
|
|
|
var homedir string
|
2014-12-12 19:18:23 +00:00
|
|
|
if err == nil {
|
2017-01-14 03:47:55 +00:00
|
|
|
homedir = usr.HomeDir
|
|
|
|
} else {
|
|
|
|
// Fall back to reading $HOME - work around user.Current() not
|
|
|
|
// working for cross compiled binaries on OSX.
|
|
|
|
// https://github.com/golang/go/issues/6376
|
|
|
|
homedir = os.Getenv("HOME")
|
2014-03-15 17:01:13 +00:00
|
|
|
}
|
2017-01-14 03:47:55 +00:00
|
|
|
|
|
|
|
// Possibly find the user's XDG config paths
|
|
|
|
// See XDG Base Directory specification
|
|
|
|
// https://specifications.freedesktop.org/basedir-spec/latest/
|
|
|
|
xdgdir := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
var xdgcfgdir string
|
|
|
|
if xdgdir != "" {
|
|
|
|
xdgcfgdir = filepath.Join(xdgdir, "rclone")
|
|
|
|
} else if homedir != "" {
|
|
|
|
xdgdir = filepath.Join(homedir, ".config")
|
|
|
|
xdgcfgdir = filepath.Join(xdgdir, "rclone")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use $XDG_CONFIG_HOME/rclone/rclone.conf if already existing
|
|
|
|
var xdgconf string
|
|
|
|
if xdgcfgdir != "" {
|
|
|
|
xdgconf = filepath.Join(xdgcfgdir, configFileName)
|
|
|
|
_, err := os.Stat(xdgconf)
|
|
|
|
if err == nil {
|
|
|
|
return xdgconf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use $HOME/.rclone.conf if already existing
|
|
|
|
var homeconf string
|
|
|
|
if homedir != "" {
|
|
|
|
homeconf = filepath.Join(homedir, hiddenConfigFileName)
|
|
|
|
_, err := os.Stat(homeconf)
|
|
|
|
if err == nil {
|
|
|
|
return homeconf
|
|
|
|
}
|
2014-12-12 19:18:23 +00:00
|
|
|
}
|
2017-01-14 03:47:55 +00:00
|
|
|
|
|
|
|
// Try to create $XDG_CONFIG_HOME/rclone/rclone.conf
|
|
|
|
if xdgconf != "" {
|
|
|
|
// xdgconf != "" implies xdgcfgdir != ""
|
|
|
|
err := os.MkdirAll(xdgcfgdir, os.ModePerm)
|
|
|
|
if err == nil {
|
|
|
|
return xdgconf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create $HOME/.rclone.conf
|
|
|
|
if homeconf != "" {
|
|
|
|
return homeconf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to ./.rclone.conf (current working directory)
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Couldn't find home directory or read HOME or XDG_CONFIG_HOME environment variables.")
|
|
|
|
Errorf(nil, "Defaulting to storing config in current directory.")
|
|
|
|
Errorf(nil, "Use -config flag to workaround.")
|
|
|
|
Errorf(nil, "Error was: %v", err)
|
2017-01-14 03:47:55 +00:00
|
|
|
return hiddenConfigFileName
|
2014-03-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:35:14 +00:00
|
|
|
// DeleteMode describes the possible delete modes in the config
|
|
|
|
type DeleteMode byte
|
|
|
|
|
|
|
|
// DeleteMode constants
|
|
|
|
const (
|
|
|
|
DeleteModeOff DeleteMode = iota
|
|
|
|
DeleteModeBefore
|
|
|
|
DeleteModeDuring
|
|
|
|
DeleteModeAfter
|
|
|
|
DeleteModeOnly
|
|
|
|
DeleteModeDefault = DeleteModeAfter
|
|
|
|
)
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// LoadConfig loads the config file
|
2014-03-15 16:06:11 +00:00
|
|
|
func LoadConfig() {
|
|
|
|
// Read some flags if set
|
|
|
|
//
|
|
|
|
// FIXME read these from the config file too
|
2017-02-09 21:22:46 +00:00
|
|
|
Config.LogLevel = LogLevelNotice
|
|
|
|
if *verbose >= 2 {
|
|
|
|
Config.LogLevel = LogLevelDebug
|
|
|
|
} else if *verbose >= 1 {
|
|
|
|
Config.LogLevel = LogLevelInfo
|
|
|
|
}
|
|
|
|
if *quiet {
|
|
|
|
if *verbose > 0 {
|
|
|
|
log.Fatalf("Can't set -v and -q")
|
|
|
|
}
|
|
|
|
Config.LogLevel = LogLevelError
|
|
|
|
}
|
|
|
|
logLevelFlag := pflag.Lookup("log-level")
|
|
|
|
if logLevelFlag != nil && logLevelFlag.Changed {
|
|
|
|
if *verbose > 0 {
|
|
|
|
log.Fatalf("Can't set -v and --log-level")
|
|
|
|
}
|
|
|
|
if *quiet {
|
|
|
|
log.Fatalf("Can't set -q and --log-level")
|
|
|
|
}
|
|
|
|
switch strings.ToUpper(*logLevel) {
|
|
|
|
case "ERROR":
|
|
|
|
Config.LogLevel = LogLevelError
|
|
|
|
case "NOTICE":
|
|
|
|
Config.LogLevel = LogLevelNotice
|
|
|
|
case "INFO":
|
|
|
|
Config.LogLevel = LogLevelInfo
|
|
|
|
case "DEBUG":
|
|
|
|
Config.LogLevel = LogLevelDebug
|
|
|
|
default:
|
|
|
|
log.Fatalf("Unknown --log-level %q", *logLevel)
|
|
|
|
}
|
|
|
|
}
|
2014-03-15 16:06:11 +00:00
|
|
|
Config.ModifyWindow = *modifyWindow
|
|
|
|
Config.Checkers = *checkers
|
|
|
|
Config.Transfers = *transfers
|
2014-06-26 14:33:06 +00:00
|
|
|
Config.DryRun = *dryRun
|
2015-05-10 10:25:54 +00:00
|
|
|
Config.Timeout = *timeout
|
|
|
|
Config.ConnectTimeout = *connectTimeout
|
2015-06-03 14:08:27 +00:00
|
|
|
Config.CheckSum = *checkSum
|
2015-06-06 07:38:45 +00:00
|
|
|
Config.SizeOnly = *sizeOnly
|
2016-03-22 17:02:27 +00:00
|
|
|
Config.IgnoreTimes = *ignoreTimes
|
2016-01-05 10:35:36 +00:00
|
|
|
Config.IgnoreExisting = *ignoreExisting
|
2015-09-08 20:01:26 +00:00
|
|
|
Config.DumpHeaders = *dumpHeaders
|
|
|
|
Config.DumpBodies = *dumpBodies
|
2016-11-02 15:53:43 +00:00
|
|
|
Config.DumpAuth = *dumpAuth
|
2015-10-29 15:42:25 +00:00
|
|
|
Config.InsecureSkipVerify = *skipVerify
|
2016-01-12 17:38:28 +00:00
|
|
|
Config.LowLevelRetries = *lowLevelRetries
|
2016-02-29 17:46:40 +00:00
|
|
|
Config.UpdateOlder = *updateOlder
|
2016-02-14 18:14:41 +00:00
|
|
|
Config.NoGzip = *noGzip
|
2016-06-02 20:02:44 +00:00
|
|
|
Config.MaxDepth = *maxDepth
|
2016-06-17 16:20:08 +00:00
|
|
|
Config.IgnoreSize = *ignoreSize
|
2017-01-05 17:32:42 +00:00
|
|
|
Config.IgnoreChecksum = *ignoreChecksum
|
2016-06-25 13:28:26 +00:00
|
|
|
Config.NoTraverse = *noTraverse
|
2016-07-12 09:46:45 +00:00
|
|
|
Config.NoUpdateModTime = *noUpdateModTime
|
2017-01-10 21:47:03 +00:00
|
|
|
Config.BackupDir = *backupDir
|
2017-01-19 17:26:29 +00:00
|
|
|
Config.Suffix = *suffix
|
2017-02-14 19:31:33 +00:00
|
|
|
Config.BufferSize = bufferSize
|
2014-03-15 16:06:11 +00:00
|
|
|
|
2014-03-15 17:01:13 +00:00
|
|
|
ConfigPath = *configFile
|
2014-03-15 16:06:11 +00:00
|
|
|
|
2016-12-18 10:03:56 +00:00
|
|
|
Config.TrackRenames = *trackRenames
|
|
|
|
|
2016-01-12 13:33:03 +00:00
|
|
|
switch {
|
|
|
|
case *deleteBefore && (*deleteDuring || *deleteAfter),
|
|
|
|
*deleteDuring && *deleteAfter:
|
|
|
|
log.Fatalf(`Only one of --delete-before, --delete-during or --delete-after can be used.`)
|
2017-01-25 19:35:14 +00:00
|
|
|
case *deleteBefore:
|
|
|
|
Config.DeleteMode = DeleteModeBefore
|
|
|
|
case *deleteDuring:
|
|
|
|
Config.DeleteMode = DeleteModeDuring
|
|
|
|
case *deleteAfter:
|
|
|
|
Config.DeleteMode = DeleteModeAfter
|
|
|
|
default:
|
|
|
|
Config.DeleteMode = DeleteModeDefault
|
2016-01-12 13:33:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 16:20:08 +00:00
|
|
|
if Config.IgnoreSize && Config.SizeOnly {
|
|
|
|
log.Fatalf(`Can't use --size-only and --ignore-size together.`)
|
|
|
|
}
|
|
|
|
|
2017-01-19 17:26:29 +00:00
|
|
|
if Config.Suffix != "" && Config.BackupDir == "" {
|
|
|
|
log.Fatalf(`Can only use --suffix with --backup-dir.`)
|
|
|
|
}
|
|
|
|
|
2014-03-15 16:06:11 +00:00
|
|
|
// Load configuration file.
|
2014-03-15 17:01:13 +00:00
|
|
|
var err error
|
2016-12-20 18:03:09 +00:00
|
|
|
configData, err = loadConfigFile()
|
2016-12-19 15:04:07 +00:00
|
|
|
if err == errorConfigFileNotFound {
|
2017-02-09 11:01:20 +00:00
|
|
|
Logf(nil, "Config file %q not found - using defaults", ConfigPath)
|
2016-12-20 18:03:09 +00:00
|
|
|
configData, _ = goconfig.LoadFromReader(&bytes.Buffer{})
|
2016-12-19 15:04:07 +00:00
|
|
|
} else if err != nil {
|
|
|
|
log.Fatalf("Failed to load config file %q: %v", ConfigPath, err)
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
2015-02-19 19:26:00 +00:00
|
|
|
|
2015-09-27 15:13:20 +00:00
|
|
|
// Load filters
|
|
|
|
Config.Filter, err = NewFilter()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load filters: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-19 19:26:00 +00:00
|
|
|
// Start the token bucket limiter
|
|
|
|
startTokenBucket()
|
2017-01-03 02:52:41 +00:00
|
|
|
|
|
|
|
// Start the bandwidth update ticker
|
|
|
|
startTokenTicker()
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 15:04:07 +00:00
|
|
|
var errorConfigFileNotFound = errors.New("config file not found")
|
|
|
|
|
2016-02-16 15:25:27 +00:00
|
|
|
// loadConfigFile will load a config file, and
|
|
|
|
// automatically decrypt it.
|
|
|
|
func loadConfigFile() (*goconfig.ConfigFile, error) {
|
|
|
|
b, err := ioutil.ReadFile(ConfigPath)
|
|
|
|
if err != nil {
|
2016-12-19 15:04:07 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, errorConfigFileNotFound
|
|
|
|
}
|
|
|
|
return nil, err
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find first non-empty line
|
|
|
|
r := bufio.NewReader(bytes.NewBuffer(b))
|
|
|
|
for {
|
|
|
|
line, _, err := r.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2016-02-17 10:45:05 +00:00
|
|
|
return goconfig.LoadFromReader(bytes.NewBuffer(b))
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l := strings.TrimSpace(string(line))
|
|
|
|
if len(l) == 0 || strings.HasPrefix(l, ";") || strings.HasPrefix(l, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// First non-empty or non-comment must be ENCRYPT_V0
|
|
|
|
if l == "RCLONE_ENCRYPT_V0:" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(l, "RCLONE_ENCRYPT_V") {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.New("unsupported configuration encryption - update rclone for support")
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
2016-02-17 10:45:05 +00:00
|
|
|
return goconfig.LoadFromReader(bytes.NewBuffer(b))
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypted content is base64 encoded.
|
|
|
|
dec := base64.NewDecoder(base64.StdEncoding, r)
|
|
|
|
box, err := ioutil.ReadAll(dec)
|
|
|
|
if err != nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to load base64 encoded data")
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
if len(box) < 24+secretbox.Overhead {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.New("Configuration data too short")
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
envpw := os.Getenv("RCLONE_CONFIG_PASS")
|
|
|
|
|
|
|
|
var out []byte
|
|
|
|
for {
|
|
|
|
if len(configKey) == 0 && envpw != "" {
|
2016-08-14 16:16:06 +00:00
|
|
|
err := setConfigPassword(envpw)
|
2016-02-16 15:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Using RCLONE_CONFIG_PASS returned:", err)
|
|
|
|
envpw = ""
|
|
|
|
} else {
|
2017-02-09 11:01:20 +00:00
|
|
|
Debugf(nil, "Using RCLONE_CONFIG_PASS password.")
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(configKey) == 0 {
|
|
|
|
if !*AskPassword {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.New("unable to decrypt configuration and not allowed to ask for password - set RCLONE_CONFIG_PASS to your configuration password")
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
2016-08-14 16:16:06 +00:00
|
|
|
getConfigPassword("Enter configuration password:")
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nonce is first 24 bytes of the ciphertext
|
|
|
|
var nonce [24]byte
|
|
|
|
copy(nonce[:], box[:24])
|
|
|
|
var key [32]byte
|
|
|
|
copy(key[:], configKey[:32])
|
|
|
|
|
|
|
|
// Attempt to decrypt
|
|
|
|
var ok bool
|
|
|
|
out, ok = secretbox.Open(nil, box[24:], &nonce, &key)
|
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Couldn't decrypt configuration, most likely wrong password.")
|
2016-02-16 15:25:27 +00:00
|
|
|
configKey = nil
|
|
|
|
envpw = ""
|
|
|
|
}
|
2016-02-17 10:45:05 +00:00
|
|
|
return goconfig.LoadFromReader(bytes.NewBuffer(out))
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 16:16:06 +00:00
|
|
|
// checkPassword normalises and validates the password
|
|
|
|
func checkPassword(password string) (string, error) {
|
|
|
|
if !utf8.ValidString(password) {
|
|
|
|
return "", errors.New("password contains invalid utf8 characters")
|
|
|
|
}
|
|
|
|
// Remove leading+trailing whitespace
|
|
|
|
password = strings.TrimSpace(password)
|
|
|
|
// Normalize to reduce weird variations.
|
|
|
|
password = norm.NFKC.String(password)
|
|
|
|
if len(password) == 0 {
|
|
|
|
return "", errors.New("no characters in password")
|
|
|
|
}
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPassword asks the user for a password with the prompt given.
|
|
|
|
func GetPassword(prompt string) string {
|
|
|
|
fmt.Println(prompt)
|
|
|
|
for {
|
|
|
|
fmt.Print("password:")
|
|
|
|
password := ReadPassword()
|
|
|
|
password, err := checkPassword(password)
|
|
|
|
if err == nil {
|
|
|
|
return password
|
|
|
|
}
|
2016-08-19 19:00:05 +00:00
|
|
|
fmt.Printf("Bad password: %v\n", err)
|
2016-08-14 16:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangePassword will query the user twice for the named password. If
|
|
|
|
// the same password is entered it is returned.
|
|
|
|
func ChangePassword(name string) string {
|
|
|
|
for {
|
|
|
|
a := GetPassword(fmt.Sprintf("Enter %s password:", name))
|
|
|
|
b := GetPassword(fmt.Sprintf("Confirm %s password:", name))
|
|
|
|
if a == b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
fmt.Println("Passwords do not match!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfigPassword will query the user for a password the
|
2016-02-16 15:25:27 +00:00
|
|
|
// first time it is required.
|
2016-08-14 16:16:06 +00:00
|
|
|
func getConfigPassword(q string) {
|
2016-02-16 15:25:27 +00:00
|
|
|
if len(configKey) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
2016-08-14 16:16:06 +00:00
|
|
|
password := GetPassword(q)
|
|
|
|
err := setConfigPassword(password)
|
2016-02-16 15:25:27 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 16:16:06 +00:00
|
|
|
// setConfigPassword will set the configKey to the hash of
|
2016-02-16 15:25:27 +00:00
|
|
|
// the password. If the length of the password is
|
|
|
|
// zero after trimming+normalization, an error is returned.
|
2016-08-14 16:16:06 +00:00
|
|
|
func setConfigPassword(password string) error {
|
|
|
|
password, err := checkPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
// Create SHA256 has of the password
|
|
|
|
sha := sha256.New()
|
2016-08-14 16:16:06 +00:00
|
|
|
_, err = sha.Write([]byte("[" + password + "][rclone-config]"))
|
2016-02-16 15:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
configKey = sha.Sum(nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-14 16:16:06 +00:00
|
|
|
// changeConfigPassword will query the user twice
|
|
|
|
// for a password. If the same password is entered
|
|
|
|
// twice the key is updated.
|
|
|
|
func changeConfigPassword() {
|
|
|
|
err := setConfigPassword(ChangePassword("NEW configuration"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to set config password: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// SaveConfig saves configuration file.
|
2016-02-16 15:25:27 +00:00
|
|
|
// if configKey has been set, the file will be encrypted.
|
2014-03-15 16:06:11 +00:00
|
|
|
func SaveConfig() {
|
2016-02-16 15:25:27 +00:00
|
|
|
if len(configKey) == 0 {
|
2016-12-20 18:03:09 +00:00
|
|
|
err := goconfig.SaveConfigFile(configData, ConfigPath)
|
2016-02-16 15:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to save config file: %v", err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(ConfigPath, 0600)
|
|
|
|
if err != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Failed to set permissions on config file: %v", err)
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
2016-12-20 18:03:09 +00:00
|
|
|
err := goconfig.SaveConfigData(configData, &buf)
|
2014-03-15 16:06:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to save config file: %v", err)
|
|
|
|
}
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
f, err := os.Create(ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to save config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(f, "# Encrypted rclone configuration File")
|
|
|
|
fmt.Fprintln(f, "")
|
|
|
|
fmt.Fprintln(f, "RCLONE_ENCRYPT_V0:")
|
|
|
|
|
|
|
|
// Generate new nonce and write it to the start of the ciphertext
|
|
|
|
var nonce [24]byte
|
|
|
|
n, _ := rand.Read(nonce[:])
|
|
|
|
if n != 24 {
|
|
|
|
log.Fatalf("nonce short read: %d", n)
|
|
|
|
}
|
|
|
|
enc := base64.NewEncoder(base64.StdEncoding, f)
|
|
|
|
_, err = enc.Write(nonce[:])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to write config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var key [32]byte
|
|
|
|
copy(key[:], configKey[:32])
|
|
|
|
|
|
|
|
b := secretbox.Seal(nil, buf.Bytes(), &nonce, &key)
|
|
|
|
_, err = enc.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to write config file: %v", err)
|
|
|
|
}
|
|
|
|
_ = enc.Close()
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to close config file: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-03-16 13:53:51 +00:00
|
|
|
err = os.Chmod(ConfigPath, 0600)
|
|
|
|
if err != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Failed to set permissions on config file: %v", err)
|
2014-03-16 13:53:51 +00:00
|
|
|
}
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 15:04:07 +00:00
|
|
|
// ConfigSetValueAndSave sets the key to the value and saves just that
|
|
|
|
// value in the config file. It loads the old config file in from
|
|
|
|
// disk first and overwrites the given value only.
|
|
|
|
func ConfigSetValueAndSave(name, key, value string) (err error) {
|
|
|
|
// Set the value in config in case we fail to reload it
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.SetValue(name, key, value)
|
2016-12-19 15:04:07 +00:00
|
|
|
// Reload the config file
|
|
|
|
reloadedConfigFile, err := loadConfigFile()
|
|
|
|
if err == errorConfigFileNotFound {
|
|
|
|
// Config file not written yet so ignore reload
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = reloadedConfigFile.GetSection(name)
|
|
|
|
if err != nil {
|
|
|
|
// Section doesn't exist yet so ignore reload
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Update the config file with the reloaded version
|
2016-12-20 18:03:09 +00:00
|
|
|
configData = reloadedConfigFile
|
2016-12-19 15:04:07 +00:00
|
|
|
// Set the value in the reloaded version
|
|
|
|
reloadedConfigFile.SetValue(name, key, value)
|
|
|
|
// Save it again
|
|
|
|
SaveConfig()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ShowRemotes shows an overview of the config file
|
2014-03-15 16:52:51 +00:00
|
|
|
func ShowRemotes() {
|
2016-12-20 18:03:09 +00:00
|
|
|
remotes := configData.GetSectionList()
|
2014-03-15 16:52:51 +00:00
|
|
|
if len(remotes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-03-15 16:06:11 +00:00
|
|
|
sort.Strings(remotes)
|
|
|
|
fmt.Printf("%-20s %s\n", "Name", "Type")
|
|
|
|
fmt.Printf("%-20s %s\n", "====", "====")
|
|
|
|
for _, remote := range remotes {
|
2016-12-20 18:03:09 +00:00
|
|
|
fmt.Printf("%-20s %s\n", remote, ConfigFileGet(remote, "type"))
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChooseRemote chooses a remote name
|
|
|
|
func ChooseRemote() string {
|
2016-12-20 18:03:09 +00:00
|
|
|
remotes := configData.GetSectionList()
|
2014-03-15 16:06:11 +00:00
|
|
|
sort.Strings(remotes)
|
|
|
|
return Choose("remote", remotes, nil, false)
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ReadLine reads some input
|
2014-03-15 16:06:11 +00:00
|
|
|
func ReadLine() string {
|
|
|
|
buf := bufio.NewReader(os.Stdin)
|
|
|
|
line, err := buf.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read line: %v", err)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command - choose one
|
2014-03-15 16:52:51 +00:00
|
|
|
func Command(commands []string) byte {
|
2014-03-15 16:06:11 +00:00
|
|
|
opts := []string{}
|
|
|
|
for _, text := range commands {
|
|
|
|
fmt.Printf("%c) %s\n", text[0], text[1:])
|
|
|
|
opts = append(opts, text[:1])
|
|
|
|
}
|
|
|
|
optString := strings.Join(opts, "")
|
|
|
|
optHelp := strings.Join(opts, "/")
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", optHelp)
|
|
|
|
result := strings.ToLower(ReadLine())
|
|
|
|
if len(result) != 1 {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-26 14:18:48 +00:00
|
|
|
i := strings.Index(optString, string(result[0]))
|
2014-03-15 16:06:11 +00:00
|
|
|
if i >= 0 {
|
2014-03-15 16:52:51 +00:00
|
|
|
return result[0]
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Confirm asks the user for Yes or No and returns true or false
|
2014-03-16 13:54:43 +00:00
|
|
|
func Confirm() bool {
|
|
|
|
return Command([]string{"yYes", "nNo"}) == 'y'
|
|
|
|
}
|
|
|
|
|
2014-03-15 16:06:11 +00:00
|
|
|
// Choose one of the defaults or type a new string if newOk is set
|
|
|
|
func Choose(what string, defaults, help []string, newOk bool) string {
|
2017-01-29 15:51:26 +00:00
|
|
|
valueDescripton := "an existing"
|
2014-03-15 16:06:11 +00:00
|
|
|
if newOk {
|
2017-01-29 15:51:26 +00:00
|
|
|
valueDescripton = "your own"
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
2017-01-29 15:51:26 +00:00
|
|
|
fmt.Printf("Choose a number from below, or type in %s value\n", valueDescripton)
|
2014-03-15 16:06:11 +00:00
|
|
|
for i, text := range defaults {
|
2016-02-21 12:25:37 +00:00
|
|
|
var lines []string
|
2014-03-15 16:06:11 +00:00
|
|
|
if help != nil {
|
|
|
|
parts := strings.Split(help[i], "\n")
|
2016-02-21 12:25:37 +00:00
|
|
|
lines = append(lines, parts...)
|
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%q", text))
|
|
|
|
pos := i + 1
|
|
|
|
if len(lines) == 1 {
|
|
|
|
fmt.Printf("%2d > %s\n", pos, text)
|
|
|
|
} else {
|
|
|
|
mid := (len(lines) - 1) / 2
|
|
|
|
for i, line := range lines {
|
|
|
|
var sep rune
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
sep = '/'
|
|
|
|
case len(lines) - 1:
|
|
|
|
sep = '\\'
|
|
|
|
default:
|
|
|
|
sep = '|'
|
|
|
|
}
|
|
|
|
number := " "
|
|
|
|
if i == mid {
|
|
|
|
number = fmt.Sprintf("%2d", pos)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s %c %s\n", number, sep, line)
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
i, err := strconv.Atoi(result)
|
|
|
|
if err != nil {
|
|
|
|
if newOk {
|
|
|
|
return result
|
|
|
|
}
|
2017-01-29 15:51:26 +00:00
|
|
|
for _, v := range defaults {
|
|
|
|
if result == v {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
2014-03-15 16:06:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i >= 1 && i <= len(defaults) {
|
|
|
|
return defaults[i-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 12:58:41 +00:00
|
|
|
// ChooseNumber asks the user to enter a number between min and max
|
|
|
|
// inclusive prompting them with what.
|
|
|
|
func ChooseNumber(what string, min, max int) int {
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
i, err := strconv.Atoi(result)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Bad number: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i < min || i > max {
|
|
|
|
fmt.Printf("Out of range - %d to %d inclusive\n", min, max)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ShowRemote shows the contents of the remote
|
2014-03-15 16:06:11 +00:00
|
|
|
func ShowRemote(name string) {
|
|
|
|
fmt.Printf("--------------------\n")
|
|
|
|
fmt.Printf("[%s]\n", name)
|
2016-10-08 10:26:14 +00:00
|
|
|
fs := MustFindByName(name)
|
2016-12-20 18:03:09 +00:00
|
|
|
for _, key := range configData.GetKeyList(name) {
|
2016-10-08 10:26:14 +00:00
|
|
|
isPassword := false
|
|
|
|
for _, option := range fs.Options {
|
|
|
|
if option.Name == key && option.IsPassword {
|
|
|
|
isPassword = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 18:03:09 +00:00
|
|
|
value := ConfigFileGet(name, key)
|
2016-10-08 10:26:14 +00:00
|
|
|
if isPassword && value != "" {
|
|
|
|
fmt.Printf("%s = *** ENCRYPTED ***\n", key)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s = %s\n", key, value)
|
|
|
|
}
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("--------------------\n")
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// OkRemote prints the contents of the remote and ask if it is OK
|
2014-03-15 16:06:11 +00:00
|
|
|
func OkRemote(name string) bool {
|
|
|
|
ShowRemote(name)
|
|
|
|
switch i := Command([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}); i {
|
2014-03-15 16:52:51 +00:00
|
|
|
case 'y':
|
2014-03-15 16:06:11 +00:00
|
|
|
return true
|
2014-03-15 16:52:51 +00:00
|
|
|
case 'e':
|
2014-03-15 16:06:11 +00:00
|
|
|
return false
|
2014-03-15 16:52:51 +00:00
|
|
|
case 'd':
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.DeleteSection(name)
|
2014-03-15 16:06:11 +00:00
|
|
|
return true
|
|
|
|
default:
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Bad choice %c", i)
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-08 10:26:14 +00:00
|
|
|
// MustFindByName finds the RegInfo for the remote name passed in or
|
|
|
|
// exits with a fatal error.
|
|
|
|
func MustFindByName(name string) *RegInfo {
|
2016-12-20 18:03:09 +00:00
|
|
|
fsType := ConfigFileGet(name, "type")
|
2016-10-08 10:26:14 +00:00
|
|
|
if fsType == "" {
|
|
|
|
log.Fatalf("Couldn't find type of fs for %q", name)
|
|
|
|
}
|
|
|
|
return MustFind(fsType)
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// RemoteConfig runs the config helper for the remote if needed
|
2014-03-16 13:54:43 +00:00
|
|
|
func RemoteConfig(name string) {
|
|
|
|
fmt.Printf("Remote config\n")
|
2016-10-08 10:26:14 +00:00
|
|
|
f := MustFindByName(name)
|
2014-03-16 13:54:43 +00:00
|
|
|
if f.Config != nil {
|
|
|
|
f.Config(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ChooseOption asks the user to choose an option
|
2014-03-28 17:56:04 +00:00
|
|
|
func ChooseOption(o *Option) string {
|
|
|
|
fmt.Println(o.Help)
|
2016-08-14 16:16:06 +00:00
|
|
|
if o.IsPassword {
|
2016-08-19 19:00:05 +00:00
|
|
|
actions := []string{"yYes type in my own password", "gGenerate random password"}
|
|
|
|
if o.Optional {
|
|
|
|
actions = append(actions, "nNo leave this optional password blank")
|
|
|
|
}
|
|
|
|
var password string
|
|
|
|
switch i := Command(actions); i {
|
|
|
|
case 'y':
|
|
|
|
password = ChangePassword("the")
|
|
|
|
case 'g':
|
|
|
|
for {
|
|
|
|
fmt.Printf("Password strength in bits.\n64 is just about memorable\n128 is secure\n1024 is the maximum\n")
|
|
|
|
bits := ChooseNumber("Bits", 64, 1024)
|
|
|
|
bytes := bits / 8
|
|
|
|
if bits%8 != 0 {
|
|
|
|
bytes++
|
|
|
|
}
|
|
|
|
var pw = make([]byte, bytes)
|
|
|
|
n, _ := rand.Read(pw)
|
|
|
|
if n != bytes {
|
|
|
|
log.Fatalf("password short read: %d", n)
|
|
|
|
}
|
|
|
|
password = base64.RawURLEncoding.EncodeToString(pw)
|
|
|
|
fmt.Printf("Your password is: %s\n", password)
|
|
|
|
fmt.Printf("Use this password?\n")
|
|
|
|
if Confirm() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'n':
|
|
|
|
return ""
|
|
|
|
default:
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Bad choice %c", i)
|
2016-08-19 19:00:05 +00:00
|
|
|
}
|
|
|
|
return MustObscure(password)
|
2016-08-14 16:16:06 +00:00
|
|
|
}
|
2014-03-28 17:56:04 +00:00
|
|
|
if len(o.Examples) > 0 {
|
|
|
|
var values []string
|
|
|
|
var help []string
|
|
|
|
for _, example := range o.Examples {
|
|
|
|
values = append(values, example.Value)
|
|
|
|
help = append(help, example.Help)
|
|
|
|
}
|
|
|
|
return Choose(o.Name, values, help, true)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s> ", o.Name)
|
|
|
|
return ReadLine()
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:53 +00:00
|
|
|
// fsOption returns an Option describing the possible remotes
|
|
|
|
func fsOption() *Option {
|
|
|
|
o := &Option{
|
|
|
|
Name: "Storage",
|
|
|
|
Help: "Type of storage to configure.",
|
|
|
|
}
|
2014-03-15 16:06:11 +00:00
|
|
|
for _, item := range fsRegistry {
|
2016-02-15 18:11:53 +00:00
|
|
|
example := OptionExample{
|
|
|
|
Value: item.Name,
|
|
|
|
Help: item.Description,
|
|
|
|
}
|
|
|
|
o.Examples = append(o.Examples, example)
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
2016-02-15 18:11:53 +00:00
|
|
|
o.Examples.Sort()
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2017-01-29 15:37:44 +00:00
|
|
|
// NewRemoteName asks the user for a name for a remote
|
|
|
|
func NewRemoteName() (name string) {
|
|
|
|
for {
|
|
|
|
fmt.Printf("name> ")
|
|
|
|
name = ReadLine()
|
|
|
|
parts := matcher.FindStringSubmatch(name + ":")
|
|
|
|
switch {
|
|
|
|
case name == "":
|
|
|
|
fmt.Printf("Can't use empty name.\n")
|
|
|
|
case isDriveLetter(name):
|
|
|
|
fmt.Printf("Can't use %q as it can be confused a drive letter.\n", name)
|
|
|
|
case parts == nil:
|
|
|
|
fmt.Printf("Can't use %q as it has invalid characters in it.\n", name)
|
|
|
|
default:
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:53 +00:00
|
|
|
// NewRemote make a new remote from its name
|
|
|
|
func NewRemote(name string) {
|
|
|
|
newType := ChooseOption(fsOption())
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.SetValue(name, "type", newType)
|
2016-10-08 10:26:14 +00:00
|
|
|
fs := MustFind(newType)
|
2014-03-15 16:06:11 +00:00
|
|
|
for _, option := range fs.Options {
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.SetValue(name, option.Name, ChooseOption(&option))
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
2014-03-16 13:54:43 +00:00
|
|
|
RemoteConfig(name)
|
2014-03-15 16:06:11 +00:00
|
|
|
if OkRemote(name) {
|
|
|
|
SaveConfig()
|
|
|
|
return
|
|
|
|
}
|
2016-08-14 16:16:06 +00:00
|
|
|
EditRemote(fs, name)
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// EditRemote gets the user to edit a remote
|
2016-08-14 16:16:06 +00:00
|
|
|
func EditRemote(fs *RegInfo, name string) {
|
2014-03-15 16:06:11 +00:00
|
|
|
ShowRemote(name)
|
|
|
|
fmt.Printf("Edit remote\n")
|
|
|
|
for {
|
2016-08-14 16:16:06 +00:00
|
|
|
for _, option := range fs.Options {
|
|
|
|
key := option.Name
|
2016-12-20 18:03:09 +00:00
|
|
|
value := ConfigFileGet(name, key)
|
2016-08-14 16:16:06 +00:00
|
|
|
fmt.Printf("Value %q = %q\n", key, value)
|
|
|
|
fmt.Printf("Edit? (y/n)>\n")
|
|
|
|
if Confirm() {
|
|
|
|
newValue := ChooseOption(&option)
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.SetValue(name, key, newValue)
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 13:54:43 +00:00
|
|
|
RemoteConfig(name)
|
2014-03-15 16:06:11 +00:00
|
|
|
if OkRemote(name) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// DeleteRemote gets the user to delete a remote
|
2014-03-15 16:52:51 +00:00
|
|
|
func DeleteRemote(name string) {
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.DeleteSection(name)
|
2014-03-15 16:52:51 +00:00
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
2017-01-29 15:37:44 +00:00
|
|
|
// copyRemote asks the user for a new remote name and copies name into
|
|
|
|
// it
|
|
|
|
func copyRemote(name string) {
|
|
|
|
newName := NewRemoteName()
|
|
|
|
// Copy the keys
|
|
|
|
for _, key := range configData.GetKeyList(name) {
|
|
|
|
value := configData.MustValue(name, key, "")
|
|
|
|
configData.SetValue(newName, key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenameRemote renames a config section
|
|
|
|
func RenameRemote(name string) {
|
|
|
|
fmt.Printf("Enter new name for %q remote.\n", name)
|
|
|
|
copyRemote(name)
|
|
|
|
configData.DeleteSection(name)
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyRemote copies a config section
|
|
|
|
func CopyRemote(name string) {
|
|
|
|
fmt.Printf("Enter name for copy of %q remote.\n", name)
|
|
|
|
copyRemote(name)
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// EditConfig edits the config file interactively
|
2014-03-15 16:06:11 +00:00
|
|
|
func EditConfig() {
|
|
|
|
for {
|
2016-12-20 18:03:09 +00:00
|
|
|
haveRemotes := len(configData.GetSectionList()) != 0
|
2017-01-29 15:37:44 +00:00
|
|
|
what := []string{"eEdit existing remote", "nNew remote", "dDelete remote", "rRename remote", "cCopy remote", "sSet configuration password", "qQuit config"}
|
2014-03-15 16:52:51 +00:00
|
|
|
if haveRemotes {
|
|
|
|
fmt.Printf("Current remotes:\n\n")
|
|
|
|
ShowRemotes()
|
|
|
|
fmt.Printf("\n")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("No remotes found - make a new one\n")
|
2016-03-23 09:11:57 +00:00
|
|
|
what = append(what[1:2], what[3:]...)
|
2014-03-15 16:52:51 +00:00
|
|
|
}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'e':
|
2014-03-15 16:06:11 +00:00
|
|
|
name := ChooseRemote()
|
2016-10-08 10:26:14 +00:00
|
|
|
fs := MustFindByName(name)
|
2016-08-14 16:16:06 +00:00
|
|
|
EditRemote(fs, name)
|
2014-03-15 16:52:51 +00:00
|
|
|
case 'n':
|
2017-01-29 15:37:44 +00:00
|
|
|
NewRemote(NewRemoteName())
|
2014-03-15 16:52:51 +00:00
|
|
|
case 'd':
|
2014-03-15 16:06:11 +00:00
|
|
|
name := ChooseRemote()
|
2014-03-15 16:52:51 +00:00
|
|
|
DeleteRemote(name)
|
2017-01-29 15:37:44 +00:00
|
|
|
case 'r':
|
|
|
|
RenameRemote(ChooseRemote())
|
|
|
|
case 'c':
|
|
|
|
CopyRemote(ChooseRemote())
|
2016-02-16 15:25:27 +00:00
|
|
|
case 's':
|
|
|
|
SetPassword()
|
2014-03-15 16:52:51 +00:00
|
|
|
case 'q':
|
2014-03-15 16:06:11 +00:00
|
|
|
return
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword will allow the user to modify the current
|
|
|
|
// configuration encryption settings.
|
|
|
|
func SetPassword() {
|
|
|
|
for {
|
|
|
|
if len(configKey) > 0 {
|
|
|
|
fmt.Println("Your configuration is encrypted.")
|
|
|
|
what := []string{"cChange Password", "uUnencrypt configuration", "qQuit to main menu"}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'c':
|
2016-08-14 16:16:06 +00:00
|
|
|
changeConfigPassword()
|
2016-02-16 15:25:27 +00:00
|
|
|
SaveConfig()
|
|
|
|
fmt.Println("Password changed")
|
|
|
|
continue
|
|
|
|
case 'u':
|
|
|
|
configKey = nil
|
|
|
|
SaveConfig()
|
|
|
|
continue
|
|
|
|
case 'q':
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fmt.Println("Your configuration is not encrypted.")
|
|
|
|
fmt.Println("If you add a password, you will protect your login information to cloud services.")
|
|
|
|
what := []string{"aAdd Password", "qQuit to main menu"}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'a':
|
2016-08-14 16:16:06 +00:00
|
|
|
changeConfigPassword()
|
2016-02-16 15:25:27 +00:00
|
|
|
SaveConfig()
|
|
|
|
fmt.Println("Password set")
|
|
|
|
continue
|
|
|
|
case 'q':
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 15:13:36 +00:00
|
|
|
// Authorize is for remote authorization of headless machines.
|
2016-01-07 15:20:32 +00:00
|
|
|
//
|
|
|
|
// It expects 1 or 3 arguments
|
|
|
|
//
|
|
|
|
// rclone authorize "fs name"
|
|
|
|
// rclone authorize "fs name" "client id" "client secret"
|
|
|
|
func Authorize(args []string) {
|
2016-01-04 15:13:36 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 1, 3:
|
|
|
|
default:
|
|
|
|
log.Fatalf("Invalid number of arguments: %d", len(args))
|
|
|
|
}
|
|
|
|
newType := args[0]
|
2016-10-08 10:26:14 +00:00
|
|
|
fs := MustFind(newType)
|
2016-01-04 15:13:36 +00:00
|
|
|
if fs.Config == nil {
|
2016-01-07 15:20:32 +00:00
|
|
|
log.Fatalf("Can't authorize fs %q", newType)
|
2016-01-04 15:13:36 +00:00
|
|
|
}
|
|
|
|
// Name used for temporary fs
|
|
|
|
name := "**temp-fs**"
|
|
|
|
|
|
|
|
// Make sure we delete it
|
|
|
|
defer DeleteRemote(name)
|
|
|
|
|
|
|
|
// Indicate that we want fully automatic configuration.
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.SetValue(name, ConfigAutomatic, "yes")
|
2016-01-04 15:13:36 +00:00
|
|
|
if len(args) == 3 {
|
2016-12-20 18:03:09 +00:00
|
|
|
configData.SetValue(name, ConfigClientID, args[1])
|
|
|
|
configData.SetValue(name, ConfigClientSecret, args[2])
|
2016-01-04 15:13:36 +00:00
|
|
|
}
|
|
|
|
fs.Config(name)
|
|
|
|
}
|
2016-12-20 18:03:09 +00:00
|
|
|
|
|
|
|
// configToEnv converts an config section and name, eg ("myremote",
|
|
|
|
// "ignore-size") into an environment name
|
|
|
|
// "RCLONE_CONFIG_MYREMOTE_IGNORE_SIZE"
|
|
|
|
func configToEnv(section, name string) string {
|
|
|
|
return "RCLONE_CONFIG_" + strings.ToUpper(strings.Replace(section+"_"+name, "-", "_", -1))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFileGet gets the config key under section returning the
|
|
|
|
// default or empty string if not set.
|
|
|
|
//
|
|
|
|
// It looks up defaults in the environment if they are present
|
|
|
|
func ConfigFileGet(section, key string, defaultVal ...string) string {
|
|
|
|
envKey := configToEnv(section, key)
|
|
|
|
newValue, found := os.LookupEnv(envKey)
|
|
|
|
if found {
|
|
|
|
defaultVal = []string{newValue}
|
|
|
|
}
|
|
|
|
return configData.MustValue(section, key, defaultVal...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFileGetBool gets the config key under section returning the
|
|
|
|
// default or false if not set.
|
|
|
|
//
|
|
|
|
// It looks up defaults in the environment if they are present
|
|
|
|
func ConfigFileGetBool(section, key string, defaultVal ...bool) bool {
|
|
|
|
envKey := configToEnv(section, key)
|
|
|
|
newValue, found := os.LookupEnv(envKey)
|
|
|
|
if found {
|
|
|
|
newBool, err := strconv.ParseBool(newValue)
|
|
|
|
if err != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Couldn't parse %q into bool - ignoring: %v", envKey, err)
|
2016-12-20 18:03:09 +00:00
|
|
|
} else {
|
|
|
|
defaultVal = []bool{newBool}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return configData.MustBool(section, key, defaultVal...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFileGetInt gets the config key under section returning the
|
|
|
|
// default or 0 if not set.
|
|
|
|
//
|
|
|
|
// It looks up defaults in the environment if they are present
|
|
|
|
func ConfigFileGetInt(section, key string, defaultVal ...int) int {
|
|
|
|
envKey := configToEnv(section, key)
|
|
|
|
newValue, found := os.LookupEnv(envKey)
|
|
|
|
if found {
|
|
|
|
newInt, err := strconv.Atoi(newValue)
|
|
|
|
if err != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
Errorf(nil, "Couldn't parse %q into int - ignoring: %v", envKey, err)
|
2016-12-20 18:03:09 +00:00
|
|
|
} else {
|
|
|
|
defaultVal = []int{newInt}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return configData.MustInt(section, key, defaultVal...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFileSet sets the key in section to value. It doesn't save
|
|
|
|
// the config file.
|
|
|
|
func ConfigFileSet(section, key, value string) {
|
|
|
|
configData.SetValue(section, key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchEnv = regexp.MustCompile(`^RCLONE_CONFIG_(.*?)_TYPE=.*$`)
|
|
|
|
|
|
|
|
// ConfigFileSections returns the sections in the config file
|
|
|
|
// including any defined by environment variables.
|
|
|
|
func ConfigFileSections() []string {
|
|
|
|
sections := configData.GetSectionList()
|
|
|
|
for _, item := range os.Environ() {
|
|
|
|
matches := matchEnv.FindStringSubmatch(item)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
sections = append(sections, strings.ToLower(matches[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sections
|
|
|
|
}
|