func tunnelConnectionToPortUsingClient(localConn net.Conn, rport int, client *ssh.Client) { addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:"+strconv.Itoa(rport)) if err != nil { panic(err) } remoteConn, err := client.DialTCP("tcp", nil, addr) if err != nil { panic(err) } wg := sync.WaitGroup{} wg.Add(2) copyConn := func(writer, reader net.Conn) { defer wg.Done() CopyAndMeasureThroughput(writer, reader) } go copyConn(localConn, remoteConn) go copyConn(remoteConn, localConn) go func() { wg.Wait() localConn.Close() remoteConn.Close() }() }
func NewTunnelledSSHClient(user, tunaddr, tgtaddr string, checker *HostKeyChecker, agentForwarding bool, timeout time.Duration) (*SSHForwardingClient, error) { clientConfig, err := sshClientConfig(user, checker) if err != nil { return nil, err } tunaddr = maybeAddDefaultPort(tunaddr) tgtaddr = maybeAddDefaultPort(tgtaddr) var tunnelClient *gossh.Client dialFunc := func(echan chan error) { var err error tunnelClient, err = gossh.Dial("tcp", tunaddr, clientConfig) echan <- err } err = timeoutSSHDial(dialFunc, timeout) if err != nil { return nil, err } var targetConn net.Conn dialFunc = func(echan chan error) { tgtTCPAddr, err := net.ResolveTCPAddr("tcp", tgtaddr) if err != nil { echan <- err return } targetConn, err = tunnelClient.DialTCP("tcp", nil, tgtTCPAddr) echan <- err } err = timeoutSSHDial(dialFunc, timeout) if err != nil { return nil, err } c, chans, reqs, err := gossh.NewClientConn(targetConn, tgtaddr, clientConfig) if err != nil { return nil, err } return newSSHForwardingClient(gossh.NewClient(c, chans, reqs), agentForwarding) }