-
Notifications
You must be signed in to change notification settings - Fork 215
Add a --bind flag for local port #82
Changes from 15 commits
4642896
72c42b1
f622dad
9d262bc
8df5e51
fe26cff
8e4a734
7c9e276
7c4434d
6ad37b6
fbede72
bec6ac7
56cb648
f9bb374
757daeb
367f177
37086a0
3741703
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vendor | ||
bin | ||
.vscode | ||
sshcode |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ type options struct { | |
skipSync bool | ||
syncBack bool | ||
noOpen bool | ||
localPort string | ||
bindAddr string | ||
remotePort string | ||
sshFlags string | ||
} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can short assign this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't because of the reassignment of err There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably pull this out into a separate function |
||
bindPort, err = randomPort() | ||
} | ||
|
||
if err != nil { | ||
return xerrors.Errorf("failed to find available local port: %w", err) | ||
} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably reword to say |
||
|
||
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) | ||
|
@@ -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() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
remotePort: remotePort, | ||
noOpen: true, | ||
}) | ||
|
There was a problem hiding this comment.
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 earlierThere was a problem hiding this comment.
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