2017-09-20 19:40:07 +08:00
|
|
|
package cryptdecode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/cmd"
|
|
|
|
"github.com/ncw/rclone/crypt"
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-01-08 16:20:01 +08:00
|
|
|
// Options set by command line flags
|
|
|
|
var (
|
|
|
|
Reverse = false
|
|
|
|
)
|
|
|
|
|
2017-09-20 19:40:07 +08:00
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2018-01-08 16:20:01 +08:00
|
|
|
flags := commandDefinition.Flags()
|
|
|
|
fs.BoolVarP(flags, &Reverse, "reverse", "", Reverse, "Reverse cryptdecode, encrypts filenames")
|
2017-09-20 19:40:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var commandDefinition = &cobra.Command{
|
|
|
|
Use: "cryptdecode encryptedremote: encryptedfilename",
|
|
|
|
Short: `Cryptdecode returns unencrypted file names.`,
|
|
|
|
Long: `
|
|
|
|
rclone cryptdecode returns unencrypted file names when provided with
|
|
|
|
a list of encrypted file names. List limit is 10 items.
|
|
|
|
|
2018-01-08 16:20:01 +08:00
|
|
|
If you supply the --reverse flag, it will return encrypted file names.
|
|
|
|
|
2017-09-20 19:40:07 +08:00
|
|
|
use it like this
|
|
|
|
|
|
|
|
rclone cryptdecode encryptedremote: encryptedfilename1 encryptedfilename2
|
2018-01-08 16:20:01 +08:00
|
|
|
|
|
|
|
rclone cryptdecode --reverse encryptedremote: filename1 filename2
|
2017-09-20 19:40:07 +08:00
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(2, 11, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2018-01-08 16:20:01 +08:00
|
|
|
if Reverse {
|
|
|
|
cmd.Run(false, false, command, func() error {
|
|
|
|
return cryptEncode(fsrc, args[1:])
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
cmd.Run(false, false, command, func() error {
|
|
|
|
return cryptDecode(fsrc, args[1:])
|
|
|
|
})
|
|
|
|
}
|
2017-09-20 19:40:07 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-08 16:20:01 +08:00
|
|
|
// Check if fsrc is a crypt
|
|
|
|
func assertCryptFs(fsrc fs.Fs) (*crypt.Fs, error) {
|
2017-09-20 19:40:07 +08:00
|
|
|
fcrypt, ok := fsrc.(*crypt.Fs)
|
|
|
|
if !ok {
|
2018-01-08 16:20:01 +08:00
|
|
|
return nil, errors.Errorf("%s:%s is not a crypt remote", fsrc.Name(), fsrc.Root())
|
|
|
|
}
|
|
|
|
return fcrypt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cryptDecode returns the unencrypted file name
|
|
|
|
func cryptDecode(fsrc fs.Fs, args []string) error {
|
|
|
|
fcrypt, err := assertCryptFs(fsrc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-20 19:40:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
output := ""
|
|
|
|
|
|
|
|
for _, encryptedFileName := range args {
|
|
|
|
fileName, err := fcrypt.DecryptFileName(encryptedFileName)
|
|
|
|
if err != nil {
|
|
|
|
output += fmt.Sprintln(encryptedFileName, "\t", "Failed to decrypt")
|
|
|
|
} else {
|
|
|
|
output += fmt.Sprintln(encryptedFileName, "\t", fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(output)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-08 16:20:01 +08:00
|
|
|
|
|
|
|
// cryptEncode returns the encrypted file name
|
|
|
|
func cryptEncode(fsrc fs.Fs, args []string) error {
|
|
|
|
fcrypt, err := assertCryptFs(fsrc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ""
|
|
|
|
|
|
|
|
for _, fileName := range args {
|
|
|
|
encryptedFileName := fcrypt.EncryptFileName(fileName)
|
|
|
|
output += fmt.Sprintln(fileName, "\t", encryptedFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(output)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|