2022-08-28 19:21:57 +08:00
|
|
|
// Package cryptdecode provides the cryptdecode command.
|
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"
|
|
|
|
|
2019-07-29 01:47:38 +08: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 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)
|
2019-10-11 23:55:04 +08:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2023-07-11 01:34:10 +08:00
|
|
|
flags.BoolVarP(cmdFlags, &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.
|
|
|
|
|
2022-06-20 01:55:37 +08:00
|
|
|
If you supply the ` + "`--reverse`" + ` flag, it will return encrypted file names.
|
2018-01-08 16:20:01 +08:00
|
|
|
|
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
|
2020-07-13 08:04:59 +08:00
|
|
|
|
2022-06-20 01:55:37 +08:00
|
|
|
Another way to accomplish this is by using the ` + "`rclone backend encode` (or `decode`)" + ` command.
|
2022-06-19 21:51:37 +08:00
|
|
|
See the documentation on the [crypt](/crypt/) overlay for more info.
|
2017-09-20 19:40:07 +08:00
|
|
|
`,
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.38",
|
|
|
|
},
|
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 {
|
2018-05-15 01:06:57 +08:00
|
|
|
fsInfo, _, _, config, err := fs.ConfigFs(args[0])
|
2018-02-25 19:57:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fsInfo.Name != "crypt" {
|
2022-06-09 04:54:39 +08:00
|
|
|
return errors.New("the remote needs to be of type \"crypt\"")
|
2018-02-25 19:57:14 +08:00
|
|
|
}
|
2018-05-15 01:06:57 +08:00
|
|
|
cipher, err := crypt.NewCipher(config)
|
2018-02-25 19:57:14 +08:00
|
|
|
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
|
2020-04-09 23:17:17 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 04:25:17 +08:00
|
|
|
fmt.Print(output)
|
2017-09-20 19:40:07 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-08 16:20:01 +08:00
|
|
|
|
|
|
|
// cryptEncode returns the encrypted file name
|
2020-04-09 23:17:17 +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)
|
|
|
|
}
|
|
|
|
|
2022-06-09 04:25:17 +08:00
|
|
|
fmt.Print(output)
|
2018-01-08 16:20:01 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|