Skip to content
This repository has been archived by the owner on Jan 17, 2021. It is now read-only.

Commit

Permalink
Add a --bind flag for local port (#82)
Browse files Browse the repository at this point in the history
- Added the ability to specify the local bind address of the ssh tunnel, for example: '--bind=0.0.0.0:8080'. This enables ChromeOS clients to access the web browser
  • Loading branch information
teddy-codes authored and sreya committed May 8, 2019
1 parent 46ef157 commit dbc0ff5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
bin
.vscode
sshcode
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type rootCmd struct {
skipSync bool
syncBack bool
printVersion bool
bindAddr string
sshFlags string
}

Expand All @@ -52,6 +53,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.StringVar(&c.bindAddr, "bind", "", "local bind address for ssh tunnel")
fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags")
}

Expand All @@ -76,6 +78,7 @@ func (c *rootCmd) Run(fl *flag.FlagSet) {
err := sshCode(host, dir, options{
skipSync: c.skipSync,
sshFlags: c.sshFlags,
bindAddr: c.bindAddr,
syncBack: c.syncBack,
})

Expand Down
36 changes: 26 additions & 10 deletions sshcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type options struct {
skipSync bool
syncBack bool
noOpen bool
localPort string
bindAddr string
remotePort string
sshFlags string
}
Expand Down Expand Up @@ -79,11 +79,9 @@ func sshCode(host, dir string, o options) error {

flog.Info("starting code-server...")

if o.localPort == "" {
o.localPort, err = randomPort()
}
o.bindAddr, err = parseBindAddr(o.bindAddr)
if err != nil {
return xerrors.Errorf("failed to find available local port: %w", err)
return xerrors.Errorf("failed to parse bind address: %w", err)
}

if o.remotePort == "" {
Expand All @@ -93,11 +91,12 @@ 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 remote port %v to %v", o.remotePort, o.bindAddr)

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: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.
sshCmd = exec.Command("sh", "-c", sshCmdStr)
Expand All @@ -109,7 +108,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://%s", o.bindAddr)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()

Expand Down Expand Up @@ -168,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

Expand Down
2 changes: 1 addition & 1 deletion sshcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSSHCode(t *testing.T) {
defer wg.Done()
err := sshCode("[email protected]", "", options{
sshFlags: testSSHArgs(sshPort),
localPort: localPort,
bindAddr: net.JoinHostPort("127.0.0.1", localPort),
remotePort: remotePort,
noOpen: true,
})
Expand Down

0 comments on commit dbc0ff5

Please sign in to comment.