Source File
client.go
Belonging Package
golang.org/x/crypto/ssh
package ssh
import (
)
type Client struct {
Conn
handleForwardsOnce sync.Once // guards calling (*Client).handleForwards
forwards forwardList // forwarded tcpip connections from the remote side
mu sync.Mutex
channelHandlers map[string]chan NewChannel
}
:= make(chan NewChannel)
close()
return
}
:= .channelHandlers[]
if != nil {
return nil
}
= make(chan NewChannel, chanSize)
.channelHandlers[] =
return
}
func ( Conn, <-chan NewChannel, <-chan *Request) *Client {
:= &Client{
Conn: ,
channelHandlers: make(map[string]chan NewChannel, 1),
}
go .handleGlobalRequests()
go .handleChannelOpens()
go func() {
.Wait()
.forwards.closeAll()
}()
return
}
func ( net.Conn, string, *ClientConfig) (Conn, <-chan NewChannel, <-chan *Request, error) {
:= *
.SetDefaults()
if .HostKeyCallback == nil {
.Close()
return nil, nil, nil, errors.New("ssh: must specify HostKeyCallback")
}
:= &connection{
sshConn: sshConn{conn: },
}
if := .clientHandshake(, &); != nil {
.Close()
return nil, nil, nil, fmt.Errorf("ssh: handshake failed: %v", )
}
.mux = newMux(.transport)
return , .mux.incomingChannels, .mux.incomingRequests, nil
}
func ( *connection) ( string, *ClientConfig) error {
if .ClientVersion != "" {
.clientVersion = []byte(.ClientVersion)
} else {
.clientVersion = []byte(packageVersion)
}
var error
.serverVersion, = exchangeVersions(.sshConn.conn, .clientVersion)
if != nil {
return
}
.transport = newClientTransport(
newTransport(.sshConn.conn, .Rand, true /* is client */),
.clientVersion, .serverVersion, , , .sshConn.RemoteAddr())
if := .transport.waitSession(); != nil {
return
}
.sessionID = .transport.getSessionID()
return .clientAuthenticate()
}
func ( *Client) () (*Session, error) {
, , := .OpenChannel("session", nil)
if != nil {
return nil,
}
return newSession(, )
}
func ( *Client) ( <-chan *Request) {
func ( *Client) ( <-chan NewChannel) {
for := range {
.mu.Lock()
:= .channelHandlers[.ChannelType()]
.mu.Unlock()
if != nil {
<-
} else {
.Reject(UnknownChannelType, fmt.Sprintf("unknown channel type: %v", .ChannelType()))
}
}
.mu.Lock()
for , := range .channelHandlers {
close()
}
.channelHandlers = nil
.mu.Unlock()
}
func (, string, *ClientConfig) (*Client, error) {
, := net.DialTimeout(, , .Timeout)
if != nil {
return nil,
}
, , , := NewClientConn(, , )
if != nil {
return nil,
}
return NewClient(, , ), nil
}
type BannerCallback func(message string) error
func () HostKeyCallback {
return func( string, net.Addr, PublicKey) error {
return nil
}
}
type fixedHostKey struct {
key PublicKey
}
func ( *fixedHostKey) ( string, net.Addr, PublicKey) error {
if .key == nil {
return fmt.Errorf("ssh: required host key was nil")
}
if !bytes.Equal(.Marshal(), .key.Marshal()) {
return fmt.Errorf("ssh: host key mismatch")
}
return nil
}
func ( PublicKey) HostKeyCallback {
:= &fixedHostKey{}
return .check
}
func () BannerCallback {
return func( string) error {
, := os.Stderr.WriteString()
return
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |