Passing the local_listener through to avoid a race with someone else picking up the port.

Signed-off-by: Rob Dobson <rob.dobson@citrix.com>
This commit is contained in:
Rob Dobson 2015-05-21 14:21:50 +01:00
parent e4f94fcc21
commit 437b4a3037
2 changed files with 2 additions and 14 deletions

View File

@ -142,7 +142,7 @@ func forward(local_conn net.Conn, config *gossh.ClientConfig, server, remote_des
return nil
}
func ssh_port_forward(local_port uint, remote_port uint, remote_dest, host, username, password string) error {
func ssh_port_forward(local_listener net.Listener, remote_port uint, remote_dest, host, username, password string) error {
config := &gossh.ClientConfig{
User: username,
@ -151,16 +151,6 @@ func ssh_port_forward(local_port uint, remote_port uint, remote_dest, host, user
},
}
// Listen on a local port
local_listener, err := net.Listen("tcp",
fmt.Sprintf("%s:%d",
"127.0.0.1",
local_port))
if err != nil {
log.Printf("Local listen failed: %s", err)
return err
}
for {
local_connection, err := local_listener.Accept()

View File

@ -30,14 +30,12 @@ func (self *StepForwardPortOverSSH) Run(state multistep.StateBag) multistep.Step
return multistep.ActionHalt
}
l.Close()
ui.Say(fmt.Sprintf("Creating a local port forward over SSH on local port %d", sshHostPort))
remotePort, _ := self.RemotePort(state)
remoteDest, _ := self.RemoteDest(state)
go ssh_port_forward(sshHostPort, remotePort, remoteDest, config.HostIp, config.Username, config.Password)
go ssh_port_forward(l, remotePort, remoteDest, config.HostIp, config.Username, config.Password)
ui.Say(fmt.Sprintf("Port forward setup. %d ---> %s:%d on %s", sshHostPort, remoteDest, remotePort, config.HostIp))
// Provide the local port to future steps.