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 14 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
bindFlag 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.bindFlag, "bind", "127.0.0.1", "local bind address for ssh tunnel")
sreya marked this conversation as resolved.
Show resolved Hide resolved
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,
bindHost: c.bindFlag,
noOpen: c.noOpen,
syncBack: c.syncBack,
})

Expand Down
19 changes: 14 additions & 5 deletions sshcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type options struct {
skipSync bool
syncBack bool
noOpen bool
bindHost string
sreya marked this conversation as resolved.
Show resolved Hide resolved
localPort string
sreya marked this conversation as resolved.
Show resolved Hide resolved
remotePort string
sshFlags string
Expand Down Expand Up @@ -79,12 +80,19 @@ func sshCode(host, dir string, o options) error {

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

splitHost := strings.Split(o.bindHost, ":")
sreya marked this conversation as resolved.
Show resolved Hide resolved
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)
sreya marked this conversation as resolved.
Show resolved Hide resolved

if o.remotePort == "" {
o.remotePort, err = randomPort()
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 to remote port %v", o.bindHost, 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: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.
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://127.0.0.1:%s", o.localPort)
sreya marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()

Expand Down