From 5eb7d4d1e635cecaa21747eac6209f659eb1531a Mon Sep 17 00:00:00 2001 From: Russ Magee Date: Sun, 29 Jul 2018 00:48:42 -0700 Subject: [PATCH] Initial (buggy) support for 'fancy arg' style ala ssh: eg user@example.org:port:path --- hkexsh/hkexsh.go | 76 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/hkexsh/hkexsh.go b/hkexsh/hkexsh.go index 4f9ca23..d68e877 100755 --- a/hkexsh/hkexsh.go +++ b/hkexsh/hkexsh.go @@ -34,7 +34,8 @@ type cmdSpec struct { } var ( - wg sync.WaitGroup + wg sync.WaitGroup + defPort = "2000" ) // Get terminal size using 'stty' command @@ -52,6 +53,53 @@ func GetSize() (cols, rows int, err error) { return } +func parseFancyEndpointArg(a []string, dp string) (user, host, port, path string) { + //TODO: Look for non-option fancyArg of syntax user@host:filespec to set -r,-t and -u + // Consider: whether fancyArg is src or dst file depends on flag.Args() index; + // fancyArg as last flag.Args() element denotes dstFile + // fancyArg as not-last flag.Args() element denotes srcFile + // * throw error if >1 fancyArgs are found in flags.Args() + var fancyUser, fancyHost, fancyPort, fancyPath string + for i, arg := range flag.Args() { + if strings.Contains(arg, ":") || strings.Contains(arg, "@") { + fancyArg := strings.Split(flag.Arg(i), "@") + var fancyHostPortPath []string + if len(fancyArg) < 2 { + //TODO: no user specified, use current + fancyUser = "[default:getUser]" + fancyHostPortPath = strings.Split(fancyArg[0], ":") + } else { + // user@.... + fancyUser = fancyArg[0] + fancyHostPortPath = strings.Split(fancyArg[1], ":") + } + + if len(fancyHostPortPath) > 2 { + // [user]@host[:port]:path + fancyPath = fancyHostPortPath[2] + } + if len(fancyHostPortPath) > 1 { + // [user]@host:port[:...] or [user]@host:path (default port) + fancyPort = fancyHostPortPath[1] + } + // [user]@host[:...[:...]] + fancyHost = fancyHostPortPath[0] + + if fancyPort == "" { + fancyPort = dp + } + + if fancyPath == "" { + fancyPath = "." + } + + fmt.Println("fancyArgs: user:", fancyUser, "host:", fancyHost, "port:", fancyPort, "path:", fancyPath) + break // ignore multiple 'fancyArgs' + } + } + return fancyUser, fancyHost, fancyPort, fancyPath +} + // Demo of a simple client that dials up to a simple test server to // send data. // @@ -90,29 +138,45 @@ func main() { flag.BoolVar(&dbg, "d", false, "debug logging") flag.StringVar(&cAlg, "c", "C_AES_256", "cipher [\"C_AES_256\" | \"C_TWOFISH_128\" | \"C_BLOWFISH_64\"]") flag.StringVar(&hAlg, "m", "H_SHA256", "hmac [\"H_SHA256\"]") - flag.StringVar(&server, "s", "localhost:2000", "server hostname/address[:port]") + flag.StringVar(&server, "s", "localhost:"+defPort, "server hostname/address[:port]") flag.StringVar(&altUser, "u", "", "specify alternate user") flag.StringVar(&authCookie, "a", "", "auth cookie") flag.BoolVar(&chaffEnabled, "e", true, "enabled chaff pkts (default true)") flag.UintVar(&chaffFreqMin, "f", 100, "chaff pkt freq min (msecs)") flag.UintVar(&chaffFreqMax, "F", 5000, "chaff pkt freq max (msecs)") flag.UintVar(&chaffBytesMax, "B", 64, "chaff pkt size max (bytes)") - + // Find out what program we are (shell or copier) myPath := strings.Split(os.Args[0], string(os.PathSeparator)) - if myPath[len(myPath)-1] != "hkexcp" { + if myPath[len(myPath)-1] != "hkexcp" && myPath[len(myPath)-1] != "hkexcp.exe" { // hkexsh accepts a command (-x) but not // a srcpath (-r) or dstpath (-t) flag.StringVar(&cmdStr, "x", "", "command to run (default empty - interactive shell)") - flag.Parse() } else { // hkexcp accepts srcpath (-r) and dstpath (-t), but not // a command (-x) flag.StringVar(©Src, "r", "", "copy srcpath") flag.StringVar(©Dst, "t", "", "copy dstpath") } + flag.Parse() - if flag.NFlag() == 0 { + fancyUser, fancyHost, fancyPort, fancyPath := parseFancyEndpointArg(flag.Args(), defPort /* defPort */) + fmt.Println("fancyHost:", fancyHost) + if fancyUser != "" { + altUser = fancyUser + } + if fancyHost != "" { + server = fancyHost + ":" + fancyPort + fmt.Println("fancyHost sets server to", server) + } + if fancyPath != "" { + //TODO: srcPath or dstPath depends on other flag.Args + copyDst = fancyPath + } + + fmt.Println("server finally is:", server) + + if flag.NFlag() == 0 && server == "" { flag.Usage() os.Exit(0) }