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

Add a --bind flag for local port #82

Merged
merged 18 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type rootCmd struct {
skipSync bool
syncBack bool
printVersion bool
noOpen bool
bindAddr string
sshFlags string
}

Expand All @@ -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.BoolVar(&c.noOpen, "no-open", false, "do not open web browser")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after speaking with a colleague let's handle the no-open in a separate PR, sorry for indicating otherwise earlier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. WIll change it

fl.StringVar(&c.bindAddr, "bind", "", "local bind address for ssh tunnel")
fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags")
}

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

Expand Down
25 changes: 17 additions & 8 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,9 +79,17 @@ func sshCode(host, dir string, o options) error {

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

if o.localPort == "" {
o.localPort, err = randomPort()
var bindHost string
var bindPort string
bindHost, bindPort, err = net.SplitHostPort(o.bindAddr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can short assign this :=

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't because of the reassignment of err

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try it :)


if err != nil {
return xerrors.Errorf("failed to parse bind address: %w", err)
}
if bindPort == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably pull this out into a separate function func parseBindAddr(bindAddr string) (bindAddr string, err error) that splits the address and ensures that neither host nor port is empty. If one is, it gets assigned a default value, 127.0.0.1 for host, and a random port for port and then join it back together with net.JoinHostPort

bindPort, err = randomPort()
}

if err != nil {
return xerrors.Errorf("failed to find available local port: %w", err)
}
Expand All @@ -93,11 +101,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 local host %v:%v to remote port %v", bindHost, bindPort, o.remotePort)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably reword to say Tunneling remote port %v to %v, o.remotePort, 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:%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.
sshCmd = exec.Command("sh", "-c", sshCmdStr)
Expand All @@ -109,7 +118,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
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("foo@127.0.0.1", "", options{
sshFlags: testSSHArgs(sshPort),
localPort: localPort,
bindAddr: fmt.Sprintf("127.0.0.1:%v", localPort),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

net.JoinHostPort("127.0.0.1", localPort)

remotePort: remotePort,
noOpen: true,
})
Expand Down