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

Commit dbc0ff5

Browse files
teddy-codessreya
authored andcommitted
Add a --bind flag for local port (#82)
- 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
1 parent 46ef157 commit dbc0ff5

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
bin
33
.vscode
4+
sshcode

main.go

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type rootCmd struct {
3737
skipSync bool
3838
syncBack bool
3939
printVersion bool
40+
bindAddr string
4041
sshFlags string
4142
}
4243

@@ -52,6 +53,7 @@ func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) {
5253
fl.BoolVar(&c.skipSync, "skipsync", false, "skip syncing local settings and extensions to remote host")
5354
fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination")
5455
fl.BoolVar(&c.printVersion, "version", false, "print version information and exit")
56+
fl.StringVar(&c.bindAddr, "bind", "", "local bind address for ssh tunnel")
5557
fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags")
5658
}
5759

@@ -76,6 +78,7 @@ func (c *rootCmd) Run(fl *flag.FlagSet) {
7678
err := sshCode(host, dir, options{
7779
skipSync: c.skipSync,
7880
sshFlags: c.sshFlags,
81+
bindAddr: c.bindAddr,
7982
syncBack: c.syncBack,
8083
})
8184

sshcode.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type options struct {
2525
skipSync bool
2626
syncBack bool
2727
noOpen bool
28-
localPort string
28+
bindAddr string
2929
remotePort string
3030
sshFlags string
3131
}
@@ -79,11 +79,9 @@ func sshCode(host, dir string, o options) error {
7979

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

82-
if o.localPort == "" {
83-
o.localPort, err = randomPort()
84-
}
82+
o.bindAddr, err = parseBindAddr(o.bindAddr)
8583
if err != nil {
86-
return xerrors.Errorf("failed to find available local port: %w", err)
84+
return xerrors.Errorf("failed to parse bind address: %w", err)
8785
}
8886

8987
if o.remotePort == "" {
@@ -93,11 +91,12 @@ func sshCode(host, dir string, o options) error {
9391
return xerrors.Errorf("failed to find available remote port: %w", err)
9492
}
9593

96-
flog.Info("Tunneling local port %v to remote port %v", o.localPort, o.remotePort)
94+
flog.Info("Tunneling remote port %v to %v", o.remotePort, o.bindAddr)
9795

98-
sshCmdStr = fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'",
99-
o.localPort+":localhost:"+o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort,
100-
)
96+
sshCmdStr =
97+
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'",
98+
o.bindAddr, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort,
99+
)
101100

102101
// Starts code-server and forwards the remote port.
103102
sshCmd = exec.Command("sh", "-c", sshCmdStr)
@@ -109,7 +108,7 @@ func sshCode(host, dir string, o options) error {
109108
return xerrors.Errorf("failed to start code-server: %w", err)
110109
}
111110

112-
url := "http://127.0.0.1:" + o.localPort
111+
url := fmt.Sprintf("http://%s", o.bindAddr)
113112
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
114113
defer cancel()
115114

@@ -168,6 +167,23 @@ func sshCode(host, dir string, o options) error {
168167
return nil
169168
}
170169

170+
func parseBindAddr(bindAddr string) (string, error) {
171+
host, port, err := net.SplitHostPort(bindAddr)
172+
if err != nil {
173+
return "", err
174+
}
175+
if host == "" {
176+
host = "127.0.0.1"
177+
}
178+
if port == "" {
179+
port, err = randomPort()
180+
}
181+
if err != nil {
182+
return "", err
183+
}
184+
return net.JoinHostPort(host, port), nil
185+
}
186+
171187
func openBrowser(url string) {
172188
var openCmd *exec.Cmd
173189

sshcode_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestSSHCode(t *testing.T) {
3737
defer wg.Done()
3838
err := sshCode("[email protected]", "", options{
3939
sshFlags: testSSHArgs(sshPort),
40-
localPort: localPort,
40+
bindAddr: net.JoinHostPort("127.0.0.1", localPort),
4141
remotePort: remotePort,
4242
noOpen: true,
4343
})

0 commit comments

Comments
 (0)