From 9ca32999e4dd30eb80240adfdf82d55ecd47e6e4 Mon Sep 17 00:00:00 2001 From: Chetan Sarva Date: Fri, 29 Oct 2021 11:13:00 -0400 Subject: [PATCH] feat: allow passing binding as first arg instead of using a flag --- bin/vproxy/main.go | 41 +++++++++++++++++++++++++++++------------ client.go | 5 +++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/bin/vproxy/main.go b/bin/vproxy/main.go index 51f8d15..82ca56b 100644 --- a/bin/vproxy/main.go +++ b/bin/vproxy/main.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "errors" "fmt" "net/http" "os" @@ -25,6 +24,8 @@ var ( var listenDefaultAddr = "127.0.0.1" var listenAnyIP = "0.0.0.0" +var reBinding = regexp.MustCompile("^.*?:[0-9]+$") + func verbose(c *cli.Context, a ...interface{}) { if c.IsSet("verbose") { fmt.Fprintf(os.Stderr, "[+] "+a[0].(string)+"\n", a[1:]...) @@ -39,26 +40,42 @@ func printVersion(c *cli.Context) error { func startClient(c *cli.Context) error { host := c.String("host") httpPort := c.Int("http") + + // collect and validate binds + args := c.Args().Slice() binds := c.StringSlice("bind") if len(binds) == 0 { - return fmt.Errorf("must bind at least one hostname") + // see if one was passed as the first arg + if c.Args().Present() { + if b := c.Args().First(); b != "" && validateBinding(b) == nil { + binds = append(binds, b) + } else { + return fmt.Errorf("must bind at least one hostname") + } + args = c.Args().Tail() + } else { + return fmt.Errorf("must bind at least one hostname") + } + } + for _, bind := range binds { + if err := validateBinding(bind); err != nil { + return err + } } addr := fmt.Sprintf("%s:%d", host, httpPort) if !vproxy.IsDaemonRunning(addr) { - return errors.New("daemon not running on localhost") + return fmt.Errorf("daemon not running on localhost") } - // check all binds - reBind := regexp.MustCompile("^.*?:[0-9]+$") - for _, bind := range binds { - if bind == "" || !reBind.MatchString(bind) { - return fmt.Errorf("invalid binding: '%s' (expected format 'app.local.com:7000')", bind) - } - } + vproxy.StartClientMode(addr, binds, args) + return nil +} - verbose(c, "Found existing daemon, starting in client mode") - vproxy.StartClientMode(addr, binds, c.Args().Slice()) +func validateBinding(bind string) error { + if bind == "" || !reBinding.MatchString(bind) { + return fmt.Errorf("invalid binding: '%s' (expected format 'host:port', e.g., 'app.local.com:7000')", bind) + } return nil } diff --git a/client.go b/client.go index 4334ce5..f42ecde 100644 --- a/client.go +++ b/client.go @@ -15,6 +15,11 @@ import ( ) func StartClientMode(addr string, binds []string, args []string) { + if len(binds) == 0 { + fmt.Println("error: must bind at least one hostname") + os.Exit(1) + } + // run command, if given var cmd *exec.Cmd if len(args) > 0 {