mirror of
https://github.com/certimate-go/certimate.git
synced 2026-06-13 21:01:32 +08:00
70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package ssh
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
func NewClient(conn net.Conn, host string, port int, username string) (*ssh.Client, error) {
|
|
authentications := make([]ssh.AuthMethod, 0)
|
|
return newClientWithAuthMethods(conn, host, port, username, authentications)
|
|
}
|
|
|
|
func NewClientWithPassword(conn net.Conn, host string, port int, username string, password string) (*ssh.Client, error) {
|
|
authentications := make([]ssh.AuthMethod, 0)
|
|
authentications = append(authentications, ssh.Password(password))
|
|
authentications = append(authentications, ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
|
|
answers := make([]string, len(questions))
|
|
if len(answers) == 0 {
|
|
return answers, nil
|
|
}
|
|
|
|
for i, question := range questions {
|
|
question = strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(question), ":"))
|
|
if strings.EqualFold(question, "Password") {
|
|
answers[i] = password
|
|
return answers, nil
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("unexpected keyboard interactive question '%s'", strings.Join(questions, ", "))
|
|
}))
|
|
return newClientWithAuthMethods(conn, host, port, username, authentications)
|
|
}
|
|
|
|
func NewClientWithKey(conn net.Conn, host string, port int, username string, key, keyPassphrase string) (*ssh.Client, error) {
|
|
var signer ssh.Signer
|
|
var err error
|
|
if keyPassphrase != "" {
|
|
signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(key), []byte(keyPassphrase))
|
|
} else {
|
|
signer, err = ssh.ParsePrivateKey([]byte(key))
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
authentications := make([]ssh.AuthMethod, 0)
|
|
authentications = append(authentications, ssh.PublicKeys(signer))
|
|
return newClientWithAuthMethods(conn, host, port, username, authentications)
|
|
}
|
|
|
|
func newClientWithAuthMethods(conn net.Conn, host string, port int, username string, authMethods []ssh.AuthMethod) (*ssh.Client, error) {
|
|
addr := net.JoinHostPort(host, strconv.Itoa(int(port)))
|
|
|
|
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, &ssh.ClientConfig{
|
|
User: username,
|
|
Auth: authMethods,
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ssh.NewClient(sshConn, chans, reqs), nil
|
|
}
|