@@ -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+
171187func openBrowser (url string ) {
172188 var openCmd * exec.Cmd
173189
0 commit comments