@@ -25,7 +25,7 @@ type options struct {
25
25
skipSync bool
26
26
syncBack bool
27
27
noOpen bool
28
- localPort string
28
+ bindAddr string
29
29
remotePort string
30
30
sshFlags string
31
31
}
@@ -79,11 +79,9 @@ func sshCode(host, dir string, o options) error {
79
79
80
80
flog .Info ("starting code-server..." )
81
81
82
- if o .localPort == "" {
83
- o .localPort , err = randomPort ()
84
- }
82
+ o .bindAddr , err = parseBindAddr (o .bindAddr )
85
83
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 )
87
85
}
88
86
89
87
if o .remotePort == "" {
@@ -93,11 +91,12 @@ func sshCode(host, dir string, o options) error {
93
91
return xerrors .Errorf ("failed to find available remote port: %w" , err )
94
92
}
95
93
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 )
97
95
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
+ )
101
100
102
101
// Starts code-server and forwards the remote port.
103
102
sshCmd = exec .Command ("sh" , "-c" , sshCmdStr )
@@ -109,7 +108,7 @@ func sshCode(host, dir string, o options) error {
109
108
return xerrors .Errorf ("failed to start code-server: %w" , err )
110
109
}
111
110
112
- url := "http://127.0.0.1:" + o . localPort
111
+ url := fmt . Sprintf ( "http://%s" , o . bindAddr )
113
112
ctx , cancel := context .WithTimeout (context .Background (), 15 * time .Second )
114
113
defer cancel ()
115
114
@@ -168,6 +167,23 @@ func sshCode(host, dir string, o options) error {
168
167
return nil
169
168
}
170
169
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
+
171
187
func openBrowser (url string ) {
172
188
var openCmd * exec.Cmd
173
189
0 commit comments