2017-09-20 19:40:07 +08:00
|
|
|
package cryptdecode
|
|
|
|
|
|
|
|
import (
|
2018-02-25 19:57:14 +08:00
|
|
|
"errors"
|
2017-09-20 19:40:07 +08:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-12 00:05:41 +08:00
|
|
|
"github.com/ncw/rclone/backend/crypt"
|
2017-09-20 19:40:07 +08:00
|
|
|
"github.com/ncw/rclone/cmd"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-13 00:30:54 +08:00
|
|
|
"github.com/ncw/rclone/fs/config/flags"
|
2017-09-20 19:40:07 +08:00
|
|
|
"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-13 00:30:54 +08:00
|
|
|
flagSet := commandDefinition.Flags()
|
|
|
|
flags.BoolVarP(flagSet, &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)
|
2018-02-25 19:57:14 +08:00
|
|
|
cmd.Run(false, false, command, func() error {
|
|
|
|
fsInfo, configName, _, err := fs.ParseRemote(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fsInfo.Name != "crypt" {
|
|
|
|
return errors.New("The remote needs to be of type \"crypt\"")
|
|
|
|
}
|
|
|
|
cipher, err := crypt.NewCipher(configName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if Reverse {
|
|
|
|
return cryptEncode(cipher, args[1:])
|
|
|
|
}
|
|
|
|
return cryptDecode(cipher, args[1:])
|
|
|
|
})
|
2017-09-20 19:40:07 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-08 16:20:01 +08:00
|
|
|
// cryptDecode returns the unencrypted file name
|
2018-02-25 19:57:14 +08:00
|
|
|
func cryptDecode(cipher crypt.Cipher, args []string) error {
|
2017-09-20 19:40:07 +08:00
|
|
|
output := ""
|
|
|
|
|
|
|
|
for _, encryptedFileName := range args {
|
2018-02-25 19:57:14 +08:00
|
|
|
fileName, err := cipher.DecryptFileName(encryptedFileName)
|
2017-09-20 19:40:07 +08:00
|
|
|
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
|
2018-02-25 19:57:14 +08:00
|
|
|
func cryptEncode(cipher crypt.Cipher, args []string) error {
|
2018-01-08 16:20:01 +08:00
|
|
|
output := ""
|
|
|
|
|
|
|
|
for _, fileName := range args {
|
2018-02-25 19:57:14 +08:00
|
|
|
encryptedFileName := cipher.EncryptFileName(fileName)
|
2018-01-08 16:20:01 +08:00
|
|
|
output += fmt.Sprintln(fileName, "\t", encryptedFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(output)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|