2017-09-20 13:40:07 +02:00
|
|
|
package cryptdecode
|
|
|
|
|
|
|
|
import (
|
2018-02-25 12:57:14 +01:00
|
|
|
"errors"
|
2017-09-20 13:40:07 +02:00
|
|
|
"fmt"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/backend/crypt"
|
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2017-09-20 13:40:07 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-01-08 09:20:01 +01:00
|
|
|
// Options set by command line flags
|
|
|
|
var (
|
|
|
|
Reverse = false
|
|
|
|
)
|
|
|
|
|
2017-09-20 13:40:07 +02:00
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 17:55:04 +02:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
|
|
|
flags.BoolVarP(cmdFlags, &Reverse, "reverse", "", Reverse, "Reverse cryptdecode, encrypts filenames")
|
2017-09-20 13:40:07 +02: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 09:20:01 +01:00
|
|
|
If you supply the --reverse flag, it will return encrypted file names.
|
|
|
|
|
2017-09-20 13:40:07 +02:00
|
|
|
use it like this
|
|
|
|
|
|
|
|
rclone cryptdecode encryptedremote: encryptedfilename1 encryptedfilename2
|
2018-01-08 09:20:01 +01:00
|
|
|
|
|
|
|
rclone cryptdecode --reverse encryptedremote: filename1 filename2
|
2017-09-20 13:40:07 +02:00
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(2, 11, command, args)
|
2018-02-25 12:57:14 +01:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2018-05-14 19:06:57 +02:00
|
|
|
fsInfo, _, _, config, err := fs.ConfigFs(args[0])
|
2018-02-25 12:57:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fsInfo.Name != "crypt" {
|
|
|
|
return errors.New("The remote needs to be of type \"crypt\"")
|
|
|
|
}
|
2018-05-14 19:06:57 +02:00
|
|
|
cipher, err := crypt.NewCipher(config)
|
2018-02-25 12:57:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if Reverse {
|
|
|
|
return cryptEncode(cipher, args[1:])
|
|
|
|
}
|
|
|
|
return cryptDecode(cipher, args[1:])
|
|
|
|
})
|
2017-09-20 13:40:07 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-08 09:20:01 +01:00
|
|
|
// cryptDecode returns the unencrypted file name
|
2020-04-09 17:17:17 +02:00
|
|
|
func cryptDecode(cipher *crypt.Cipher, args []string) error {
|
2017-09-20 13:40:07 +02:00
|
|
|
output := ""
|
|
|
|
|
|
|
|
for _, encryptedFileName := range args {
|
2018-02-25 12:57:14 +01:00
|
|
|
fileName, err := cipher.DecryptFileName(encryptedFileName)
|
2017-09-20 13:40:07 +02: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 09:20:01 +01:00
|
|
|
|
|
|
|
// cryptEncode returns the encrypted file name
|
2020-04-09 17:17:17 +02:00
|
|
|
func cryptEncode(cipher *crypt.Cipher, args []string) error {
|
2018-01-08 09:20:01 +01:00
|
|
|
output := ""
|
|
|
|
|
|
|
|
for _, fileName := range args {
|
2018-02-25 12:57:14 +01:00
|
|
|
encryptedFileName := cipher.EncryptFileName(fileName)
|
2018-01-08 09:20:01 +01:00
|
|
|
output += fmt.Sprintln(fileName, "\t", encryptedFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(output)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|