From 4642896271ae9ab8038a26f47c5706f4436813c9 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 30 Apr 2019 19:53:08 -0400 Subject: [PATCH 01/17] added fix for chromeos host --- .gitignore | 1 + main.go | 8 +++++--- sshcode.go | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 6eec620..dc0daa9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor bin .vscode +sshcode diff --git a/main.go b/main.go index fd7ead3..69f72ce 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ func main() { skipSyncFlag = flag.Bool("skipsync", false, "skip syncing local settings and extensions to remote host") sshFlags = flag.String("ssh-flags", "", "custom SSH flags") syncBack = flag.Bool("b", false, "sync extensions back on termination") + hostFlag = flag.String("host", "127.0.0.1", "custom host dns") printVersion = flag.Bool("version", false, "print version information and exit") ) @@ -53,9 +54,10 @@ func main() { } err := sshCode(host, dir, options{ - skipSync: *skipSyncFlag, - sshFlags: *sshFlags, - syncBack: *syncBack, + skipSync: *skipSyncFlag, + sshFlags: *sshFlags, + localHost: *hostFlag, + syncBack: *syncBack, }) if err != nil { diff --git a/sshcode.go b/sshcode.go index c644c7d..8238ab6 100644 --- a/sshcode.go +++ b/sshcode.go @@ -25,6 +25,7 @@ type options struct { skipSync bool syncBack bool localPort string + localHost string remotePort string sshFlags string } @@ -99,8 +100,8 @@ func sshCode(host, dir string, o options) error { if err != nil { return xerrors.Errorf("failed to start code-server: %w", err) } - - url := "http://127.0.0.1:" + o.localPort + flog.Info("Using local host value: " + o.localHost) + url := "http://" + o.localHost + ":" + o.localPort ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() From 72c42b1f16a1e0580d9f3a83d17ce29306db504d Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Sat, 4 May 2019 17:26:55 -0400 Subject: [PATCH 02/17] Removed host options - Added check for gentoo on remote instance. --- main.go | 8 +++----- sshcode.go | 12 +++++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 69f72ce..fd7ead3 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,6 @@ func main() { skipSyncFlag = flag.Bool("skipsync", false, "skip syncing local settings and extensions to remote host") sshFlags = flag.String("ssh-flags", "", "custom SSH flags") syncBack = flag.Bool("b", false, "sync extensions back on termination") - hostFlag = flag.String("host", "127.0.0.1", "custom host dns") printVersion = flag.Bool("version", false, "print version information and exit") ) @@ -54,10 +53,9 @@ func main() { } err := sshCode(host, dir, options{ - skipSync: *skipSyncFlag, - sshFlags: *sshFlags, - localHost: *hostFlag, - syncBack: *syncBack, + skipSync: *skipSyncFlag, + sshFlags: *sshFlags, + syncBack: *syncBack, }) if err != nil { diff --git a/sshcode.go b/sshcode.go index 8238ab6..c7b4600 100644 --- a/sshcode.go +++ b/sshcode.go @@ -87,9 +87,11 @@ func sshCode(host, dir string, o options) error { flog.Info("Tunneling local port %v to remote port %v", o.localPort, o.remotePort) - sshCmdStr = fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", - o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, - ) + sshCmdStr = + + fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host $(lsb_release -a | grep 'Gentoo' | [ ! -t 0 ] && echo '0.0.0.0' || echo '127.0.0.1') --allow-http --no-auth --port=%v'", + o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, + ) // Starts code-server and forwards the remote port. sshCmd = exec.Command("sh", "-c", sshCmdStr) @@ -100,8 +102,8 @@ func sshCode(host, dir string, o options) error { if err != nil { return xerrors.Errorf("failed to start code-server: %w", err) } - flog.Info("Using local host value: " + o.localHost) - url := "http://" + o.localHost + ":" + o.localPort + + url := "http://127.0.0.1:" + o.localPort ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() From f622dad27f8aa0c5c77af2d4ce15450ddaf27656 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Sat, 4 May 2019 17:28:22 -0400 Subject: [PATCH 03/17] Removed host --- sshcode.go | 1 - 1 file changed, 1 deletion(-) diff --git a/sshcode.go b/sshcode.go index c7b4600..82325bb 100644 --- a/sshcode.go +++ b/sshcode.go @@ -25,7 +25,6 @@ type options struct { skipSync bool syncBack bool localPort string - localHost string remotePort string sshFlags string } From 9d262bca20e209c348302a1d642580f936da38ca Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 15:14:10 -0400 Subject: [PATCH 04/17] added remoteHost option --- main.go | 8 +++++--- sshcode.go | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index fd7ead3..5388a2e 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ func main() { skipSyncFlag = flag.Bool("skipsync", false, "skip syncing local settings and extensions to remote host") sshFlags = flag.String("ssh-flags", "", "custom SSH flags") syncBack = flag.Bool("b", false, "sync extensions back on termination") + bindFlag = flag.String("bind", "127.0.0.1", "local bind address for ssh tunnel") printVersion = flag.Bool("version", false, "print version information and exit") ) @@ -53,9 +54,10 @@ func main() { } err := sshCode(host, dir, options{ - skipSync: *skipSyncFlag, - sshFlags: *sshFlags, - syncBack: *syncBack, + skipSync: *skipSyncFlag, + sshFlags: *sshFlags, + remoteHost: *bindFlag, + syncBack: *syncBack, }) if err != nil { diff --git a/sshcode.go b/sshcode.go index 82325bb..aae1caf 100644 --- a/sshcode.go +++ b/sshcode.go @@ -25,6 +25,7 @@ type options struct { skipSync bool syncBack bool localPort string + remoteHost string remotePort string sshFlags string } @@ -86,10 +87,12 @@ func sshCode(host, dir string, o options) error { flog.Info("Tunneling local port %v to remote port %v", o.localPort, o.remotePort) + if o.remoteHost != "127.0.0.1" { + flog.Info("Binding remote to %v", o.remoteHost) + } sshCmdStr = - - fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host $(lsb_release -a | grep 'Gentoo' | [ ! -t 0 ] && echo '0.0.0.0' || echo '127.0.0.1') --allow-http --no-auth --port=%v'", - o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, + fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host %v --allow-http --no-auth --port=%v'", + o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remoteHost, o.remotePort, ) // Starts code-server and forwards the remote port. From 8df5e51337cd406d23ac0cb90412b0e14bce5e74 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 15:18:30 -0400 Subject: [PATCH 05/17] expose localPort --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index 5388a2e..0483e63 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ func main() { sshFlags = flag.String("ssh-flags", "", "custom SSH flags") syncBack = flag.Bool("b", false, "sync extensions back on termination") bindFlag = flag.String("bind", "127.0.0.1", "local bind address for ssh tunnel") + localPort = flag.String("local-port", "", "local port to bind to") printVersion = flag.Bool("version", false, "print version information and exit") ) @@ -56,6 +57,7 @@ func main() { err := sshCode(host, dir, options{ skipSync: *skipSyncFlag, sshFlags: *sshFlags, + localPort: *localPort, remoteHost: *bindFlag, syncBack: *syncBack, }) From fe26cffbc78379016af59b525b51be0f5a77e7dd Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 30 Apr 2019 19:53:08 -0400 Subject: [PATCH 06/17] added fix for chromeos host --- .gitignore | 1 + sshcode.go | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6eec620..dc0daa9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor bin .vscode +sshcode diff --git a/sshcode.go b/sshcode.go index 1d539e5..2ab4556 100644 --- a/sshcode.go +++ b/sshcode.go @@ -26,6 +26,7 @@ type options struct { syncBack bool noOpen bool localPort string + localHost string remotePort string sshFlags string } @@ -108,8 +109,8 @@ func sshCode(host, dir string, o options) error { if err != nil { return xerrors.Errorf("failed to start code-server: %w", err) } - - url := "http://127.0.0.1:" + o.localPort + flog.Info("Using local host value: " + o.localHost) + url := "http://" + o.localHost + ":" + o.localPort ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() From 8e4a734349e1a15bfcc0e872e75b42c7f25d355e Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Sat, 4 May 2019 17:26:55 -0400 Subject: [PATCH 07/17] Removed host options - Added check for gentoo on remote instance. --- sshcode.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sshcode.go b/sshcode.go index 2ab4556..9bd402e 100644 --- a/sshcode.go +++ b/sshcode.go @@ -96,9 +96,11 @@ func sshCode(host, dir string, o options) error { flog.Info("Tunneling local port %v to remote port %v", o.localPort, o.remotePort) - sshCmdStr = fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", - o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, - ) + sshCmdStr = + + fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host $(lsb_release -a | grep 'Gentoo' | [ ! -t 0 ] && echo '0.0.0.0' || echo '127.0.0.1') --allow-http --no-auth --port=%v'", + o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, + ) // Starts code-server and forwards the remote port. sshCmd = exec.Command("sh", "-c", sshCmdStr) @@ -109,8 +111,8 @@ func sshCode(host, dir string, o options) error { if err != nil { return xerrors.Errorf("failed to start code-server: %w", err) } - flog.Info("Using local host value: " + o.localHost) - url := "http://" + o.localHost + ":" + o.localPort + + url := "http://127.0.0.1:" + o.localPort ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() From 7c9e2762a742bd6630febbfcd3ec75252a5aecd8 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Sat, 4 May 2019 17:28:22 -0400 Subject: [PATCH 08/17] Removed host --- sshcode.go | 1 - 1 file changed, 1 deletion(-) diff --git a/sshcode.go b/sshcode.go index 9bd402e..f628d56 100644 --- a/sshcode.go +++ b/sshcode.go @@ -26,7 +26,6 @@ type options struct { syncBack bool noOpen bool localPort string - localHost string remotePort string sshFlags string } From 7c4434dc8d61c836dd596c8ba3f29996f1745194 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 15:14:10 -0400 Subject: [PATCH 09/17] added remoteHost option --- sshcode.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sshcode.go b/sshcode.go index f628d56..69b0d87 100644 --- a/sshcode.go +++ b/sshcode.go @@ -26,6 +26,7 @@ type options struct { syncBack bool noOpen bool localPort string + remoteHost string remotePort string sshFlags string } @@ -95,10 +96,12 @@ func sshCode(host, dir string, o options) error { flog.Info("Tunneling local port %v to remote port %v", o.localPort, o.remotePort) + if o.remoteHost != "127.0.0.1" { + flog.Info("Binding remote to %v", o.remoteHost) + } sshCmdStr = - - fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host $(lsb_release -a | grep 'Gentoo' | [ ! -t 0 ] && echo '0.0.0.0' || echo '127.0.0.1') --allow-http --no-auth --port=%v'", - o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, + fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host %v --allow-http --no-auth --port=%v'", + o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remoteHost, o.remotePort, ) // Starts code-server and forwards the remote port. From fbede729f91bf005f113198e9a41ff4ff684ffe6 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 16:14:05 -0400 Subject: [PATCH 10/17] changes --- main.go | 12 +++++++++--- sshcode.go | 12 ++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index a09df53..62efade 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,8 @@ type rootCmd struct { skipSync bool syncBack bool printVersion bool + bindFlag string + localPort string sshFlags string } @@ -52,6 +54,8 @@ func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) { fl.BoolVar(&c.skipSync, "skipsync", false, "skip syncing local settings and extensions to remote host") fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination") fl.BoolVar(&c.printVersion, "version", false, "print version information and exit") + fl.StringVar(&c.bindFlag, "bind", "127.0.0.1", "local bind address for ssh tunnel") + fl.StringVar(&c.localPort, "local-port", "", "local port for viewing outside of app mode") fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags") } @@ -74,9 +78,11 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { } err := sshCode(host, dir, options{ - skipSync: c.skipSync, - sshFlags: c.sshFlags, - syncBack: c.syncBack, + skipSync: c.skipSync, + sshFlags: c.sshFlags, + bindHost: c.bindFlag, + localPort: c.localPort, + syncBack: c.syncBack, }) if err != nil { diff --git a/sshcode.go b/sshcode.go index 69b0d87..a86c46b 100644 --- a/sshcode.go +++ b/sshcode.go @@ -26,7 +26,7 @@ type options struct { syncBack bool noOpen bool localPort string - remoteHost string + bindHost string remotePort string sshFlags string } @@ -96,12 +96,12 @@ func sshCode(host, dir string, o options) error { flog.Info("Tunneling local port %v to remote port %v", o.localPort, o.remotePort) - if o.remoteHost != "127.0.0.1" { - flog.Info("Binding remote to %v", o.remoteHost) + if o.bindHost != "127.0.0.1" { + flog.Info("Binding remote to %v", o.bindHost) } sshCmdStr = - fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host %v --allow-http --no-auth --port=%v'", - o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remoteHost, o.remotePort, + fmt.Sprintf("ssh -tt -q -L %v:%v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", + o.bindHost, o.localPort, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, ) // Starts code-server and forwards the remote port. @@ -114,7 +114,7 @@ func sshCode(host, dir string, o options) error { return xerrors.Errorf("failed to start code-server: %w", err) } - url := "http://127.0.0.1:" + o.localPort + url := fmt.Sprintf("http://127.0.0.1:%s", o.localPort) ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() From bec6ac7bea40b9ba012e2de4407ef73b3406902b Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 16:19:04 -0400 Subject: [PATCH 11/17] description change --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 62efade..2099d0a 100644 --- a/main.go +++ b/main.go @@ -55,7 +55,7 @@ func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) { fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination") fl.BoolVar(&c.printVersion, "version", false, "print version information and exit") fl.StringVar(&c.bindFlag, "bind", "127.0.0.1", "local bind address for ssh tunnel") - fl.StringVar(&c.localPort, "local-port", "", "local port for viewing outside of app mode") + fl.StringVar(&c.localPort, "local-port", "", "local forwarded port for code-server") fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags") } From 56cb6487a5144af97c8d57c1b24ae1a57a066f95 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 16:27:44 -0400 Subject: [PATCH 12/17] expose noOpen to the user --- main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.go b/main.go index 2099d0a..895e1bf 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ type rootCmd struct { skipSync bool syncBack bool printVersion bool + noOpen bool bindFlag string localPort string sshFlags string @@ -54,6 +55,7 @@ func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) { fl.BoolVar(&c.skipSync, "skipsync", false, "skip syncing local settings and extensions to remote host") fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination") fl.BoolVar(&c.printVersion, "version", false, "print version information and exit") + fl.BoolVar(&c.noOpen, "no-open", false, "do not open web browser") fl.StringVar(&c.bindFlag, "bind", "127.0.0.1", "local bind address for ssh tunnel") fl.StringVar(&c.localPort, "local-port", "", "local forwarded port for code-server") fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags") @@ -81,6 +83,7 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { skipSync: c.skipSync, sshFlags: c.sshFlags, bindHost: c.bindFlag, + noOpen: c.noOpen, localPort: c.localPort, syncBack: c.syncBack, }) From f9bb3747e4c02f3c8993f70815ff3f390b7c2cab Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 17:44:24 -0400 Subject: [PATCH 13/17] remove local-port from being exposed --- main.go | 13 +++++-------- sshcode.go | 18 +++++++++++------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 895e1bf..a4eb1e6 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,6 @@ type rootCmd struct { printVersion bool noOpen bool bindFlag string - localPort string sshFlags string } @@ -57,7 +56,6 @@ func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) { fl.BoolVar(&c.printVersion, "version", false, "print version information and exit") fl.BoolVar(&c.noOpen, "no-open", false, "do not open web browser") fl.StringVar(&c.bindFlag, "bind", "127.0.0.1", "local bind address for ssh tunnel") - fl.StringVar(&c.localPort, "local-port", "", "local forwarded port for code-server") fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags") } @@ -80,12 +78,11 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { } err := sshCode(host, dir, options{ - skipSync: c.skipSync, - sshFlags: c.sshFlags, - bindHost: c.bindFlag, - noOpen: c.noOpen, - localPort: c.localPort, - syncBack: c.syncBack, + skipSync: c.skipSync, + sshFlags: c.sshFlags, + bindHost: c.bindFlag, + noOpen: c.noOpen, + syncBack: c.syncBack, }) if err != nil { diff --git a/sshcode.go b/sshcode.go index a86c46b..8409bd2 100644 --- a/sshcode.go +++ b/sshcode.go @@ -25,8 +25,8 @@ type options struct { skipSync bool syncBack bool noOpen bool - localPort string bindHost string + localPort string remotePort string sshFlags string } @@ -80,12 +80,19 @@ func sshCode(host, dir string, o options) error { flog.Info("starting code-server...") + splitHost := strings.Split(o.bindHost, ":") + if len(splitHost) == 2 { + o.localPort = splitHost[1] + } + if o.localPort == "" { o.localPort, err = randomPort() } + if err != nil { return xerrors.Errorf("failed to find available local port: %w", err) } + o.bindHost = fmt.Sprintf("%s:%s", o.bindHost, o.localPort) if o.remotePort == "" { o.remotePort, err = randomPort() @@ -94,14 +101,11 @@ func sshCode(host, dir string, o options) error { return xerrors.Errorf("failed to find available remote port: %w", err) } - flog.Info("Tunneling local port %v to remote port %v", o.localPort, o.remotePort) + flog.Info("Tunneling local host %v to remote port %v", o.bindHost, o.remotePort) - if o.bindHost != "127.0.0.1" { - flog.Info("Binding remote to %v", o.bindHost) - } sshCmdStr = - fmt.Sprintf("ssh -tt -q -L %v:%v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", - o.bindHost, o.localPort, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, + fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", + o.bindHost, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, ) // Starts code-server and forwards the remote port. From 757daeb7eb09823e02ab45ff178df0b93c7cc500 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 19:17:41 -0400 Subject: [PATCH 14/17] updated code based on review. --- main.go | 6 +++--- sshcode.go | 26 +++++++++++++------------- sshcode_test.go | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index a4eb1e6..d8db144 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ type rootCmd struct { syncBack bool printVersion bool noOpen bool - bindFlag string + bindAddr string sshFlags string } @@ -55,7 +55,7 @@ func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) { fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination") fl.BoolVar(&c.printVersion, "version", false, "print version information and exit") fl.BoolVar(&c.noOpen, "no-open", false, "do not open web browser") - fl.StringVar(&c.bindFlag, "bind", "127.0.0.1", "local bind address for ssh tunnel") + fl.StringVar(&c.bindAddr, "bind", "", "local bind address for ssh tunnel") fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags") } @@ -80,7 +80,7 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { err := sshCode(host, dir, options{ skipSync: c.skipSync, sshFlags: c.sshFlags, - bindHost: c.bindFlag, + bindAddr: c.bindAddr, noOpen: c.noOpen, syncBack: c.syncBack, }) diff --git a/sshcode.go b/sshcode.go index 8409bd2..b73a524 100644 --- a/sshcode.go +++ b/sshcode.go @@ -25,8 +25,7 @@ type options struct { skipSync bool syncBack bool noOpen bool - bindHost string - localPort string + bindAddr string remotePort string sshFlags string } @@ -80,19 +79,20 @@ func sshCode(host, dir string, o options) error { flog.Info("starting code-server...") - splitHost := strings.Split(o.bindHost, ":") - if len(splitHost) == 2 { - o.localPort = splitHost[1] - } + var bindHost string + var bindPort string + bindHost, bindPort, err = net.SplitHostPort(o.bindAddr) - if o.localPort == "" { - o.localPort, err = randomPort() + if err != nil { + return xerrors.Errorf("failed to parse bind address: %w", err) + } + if bindPort == "" { + bindPort, err = randomPort() } if err != nil { return xerrors.Errorf("failed to find available local port: %w", err) } - o.bindHost = fmt.Sprintf("%s:%s", o.bindHost, o.localPort) if o.remotePort == "" { o.remotePort, err = randomPort() @@ -101,11 +101,11 @@ func sshCode(host, dir string, o options) error { return xerrors.Errorf("failed to find available remote port: %w", err) } - flog.Info("Tunneling local host %v to remote port %v", o.bindHost, o.remotePort) + flog.Info("Tunneling local host %v:%v to remote port %v", bindHost, bindPort, o.remotePort) sshCmdStr = - fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", - o.bindHost, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, + fmt.Sprintf("ssh -tt -q -L %v:%v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", + bindHost, bindPort, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, ) // Starts code-server and forwards the remote port. @@ -118,7 +118,7 @@ func sshCode(host, dir string, o options) error { return xerrors.Errorf("failed to start code-server: %w", err) } - url := fmt.Sprintf("http://127.0.0.1:%s", o.localPort) + url := fmt.Sprintf("http://%s", o.bindAddr) ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() diff --git a/sshcode_test.go b/sshcode_test.go index b2ae89d..2809185 100644 --- a/sshcode_test.go +++ b/sshcode_test.go @@ -37,7 +37,7 @@ func TestSSHCode(t *testing.T) { defer wg.Done() err := sshCode("foo@127.0.0.1", "", options{ sshFlags: testSSHArgs(sshPort), - localPort: localPort, + bindAddr: fmt.Sprintf("127.0.0.1:%v", localPort), remotePort: remotePort, noOpen: true, }) From 367f17716e9f4e59c5fb0c25753768963eafdbbe Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 19:57:32 -0400 Subject: [PATCH 15/17] Changed based on review --- sshcode.go | 35 +++++++++++++++++++++-------------- sshcode_test.go | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/sshcode.go b/sshcode.go index b73a524..f7a7b6b 100644 --- a/sshcode.go +++ b/sshcode.go @@ -79,20 +79,10 @@ func sshCode(host, dir string, o options) error { flog.Info("starting code-server...") - var bindHost string - var bindPort string - bindHost, bindPort, err = net.SplitHostPort(o.bindAddr) - + o.bindAddr, err = parseBindAddr(o.bindAddr) if err != nil { return xerrors.Errorf("failed to parse bind address: %w", err) } - if bindPort == "" { - bindPort, err = randomPort() - } - - if err != nil { - return xerrors.Errorf("failed to find available local port: %w", err) - } if o.remotePort == "" { o.remotePort, err = randomPort() @@ -101,11 +91,11 @@ func sshCode(host, dir string, o options) error { return xerrors.Errorf("failed to find available remote port: %w", err) } - flog.Info("Tunneling local host %v:%v to remote port %v", bindHost, bindPort, o.remotePort) + flog.Info("Tunneling remote port %v to %v", o.remotePort, o.bindAddr) sshCmdStr = - fmt.Sprintf("ssh -tt -q -L %v:%v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", - bindHost, bindPort, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, + fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'", + o.bindAddr, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, ) // Starts code-server and forwards the remote port. @@ -177,6 +167,23 @@ func sshCode(host, dir string, o options) error { return nil } +func parseBindAddr(bindAddr string) (string, error) { + host, port, err := net.SplitHostPort(bindAddr) + if err != nil { + return "", err + } + if host == "" { + host = "127.0.0.1" + } + if port == "" { + port, err = randomPort() + } + if err != nil { + return "", err + } + return net.JoinHostPort(host, port), nil +} + func openBrowser(url string) { var openCmd *exec.Cmd diff --git a/sshcode_test.go b/sshcode_test.go index 2809185..fc6eb7d 100644 --- a/sshcode_test.go +++ b/sshcode_test.go @@ -37,7 +37,7 @@ func TestSSHCode(t *testing.T) { defer wg.Done() err := sshCode("foo@127.0.0.1", "", options{ sshFlags: testSSHArgs(sshPort), - bindAddr: fmt.Sprintf("127.0.0.1:%v", localPort), + bindAddr: net.JoinHostPort("127.0.0.1", localPort), remotePort: remotePort, noOpen: true, }) From 37086a0ab8494a968946364d6f35fc15c3111c32 Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 19:59:07 -0400 Subject: [PATCH 16/17] Do not expose no-open --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index d8db144..7983594 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,6 @@ type rootCmd struct { skipSync bool syncBack bool printVersion bool - noOpen bool bindAddr string sshFlags string } @@ -54,7 +53,6 @@ func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) { fl.BoolVar(&c.skipSync, "skipsync", false, "skip syncing local settings and extensions to remote host") fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination") fl.BoolVar(&c.printVersion, "version", false, "print version information and exit") - fl.BoolVar(&c.noOpen, "no-open", false, "do not open web browser") fl.StringVar(&c.bindAddr, "bind", "", "local bind address for ssh tunnel") fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags") } From 3741703b76cacb476ba41c68ccc6cb3a1f21466d Mon Sep 17 00:00:00 2001 From: Robert Miller Date: Tue, 7 May 2019 20:01:28 -0400 Subject: [PATCH 17/17] Removed noOpen from options --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 7983594..bc0de4a 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,6 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { skipSync: c.skipSync, sshFlags: c.sshFlags, bindAddr: c.bindAddr, - noOpen: c.noOpen, syncBack: c.syncBack, })