From 90fead89a6c03081e5bacd02d17a1065b05d83c4 Mon Sep 17 00:00:00 2001 From: Sahil Sarwar Date: Sun, 10 Nov 2024 21:16:36 +0530 Subject: [PATCH] Added fallback to recieve file in pwd if target_dir doesn't exist --- src/cli/cli.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index 4e29c8a3a..aa1ae4576 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -643,7 +643,13 @@ Or you can go back to the classic croc behavior by enabling classic mode: } } if c.String("out") != "" { - if err = os.Chdir(c.String("out")); err != nil { + // validate the path + destPath, err := validateDestPath(c.String("out")) + if err != nil { + return err + } + + if err = os.Chdir(destPath); err != nil { return err } } @@ -714,3 +720,20 @@ func relay(c *cli.Context) (err error) { } return tcp.Run(debugString, host, ports[0], determinePass(c), tcpPorts) } + +func validateDestPath(path string) (string, error) { + _, err := os.Stat(path) + if err != nil { + // Path doesn't exist, fallback to current directory + currentDir, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("could not get current directory: %w", err) + } + log.Debugf("Path %s does not exist, falling back to current directory: %s", path, currentDir) + fmt.Println("Target directory doesn't exist, receiving in current directory") + // flush the stdout buffer + os.Stdout.Sync() + return currentDir, nil + } + return path, nil +}