func NewTunnelledSSHClient(user, tunaddr, tgtaddr string, checker *HostKeyChecker, agentForwarding bool) (*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) 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) 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) }
// ForwardToAgent routes authentication requests to the given keyring. func ForwardToAgent(client *ssh.Client, keyring Agent) error { channels := client.HandleChannelOpen(channelType) if channels == nil { return errors.New("agent: already have handler for " + channelType) } go func() { for ch := range channels { channel, reqs, err := ch.Accept() if err != nil { continue } go ssh.DiscardRequests(reqs) go func() { ServeAgent(keyring, channel) channel.Close() }() } }() return nil }
// ForwardToRemote routes authentication requests to the ssh-agent // process serving on the given unix socket. func ForwardToRemote(client *ssh.Client, addr string) error { channels := client.HandleChannelOpen(channelType) if channels == nil { return errors.New("agent: already have handler for " + channelType) } conn, err := net.Dial("unix", addr) if err != nil { return err } conn.Close() go func() { for ch := range channels { channel, reqs, err := ch.Accept() if err != nil { continue } go ssh.DiscardRequests(reqs) go forwardUnixSocket(channel, addr) } }() return nil }