|
| 1 | +package sshclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + "golang.org/x/crypto/ssh" |
| 9 | +) |
| 10 | + |
| 11 | +type Option func(conf *Options) |
| 12 | + |
| 13 | +type Options struct { |
| 14 | + Host string |
| 15 | + Port string |
| 16 | + Username string |
| 17 | + Password string |
| 18 | + PrivateKey string |
| 19 | + Passphrase string |
| 20 | + keyboardAuth ssh.KeyboardInteractiveChallenge |
| 21 | + PrivateAuth ssh.Signer |
| 22 | + timeout int // 秒单位 |
| 23 | +} |
| 24 | + |
| 25 | +func (cfg *Options) AuthMethods() []ssh.AuthMethod { |
| 26 | + authMethods := make([]ssh.AuthMethod, 0, 3) |
| 27 | + if cfg.PrivateAuth != nil { |
| 28 | + authMethods = append(authMethods, ssh.PublicKeys(cfg.PrivateAuth)) |
| 29 | + } |
| 30 | + |
| 31 | + if cfg.PrivateKey != "" { |
| 32 | + var ( |
| 33 | + signer ssh.Signer |
| 34 | + err error |
| 35 | + ) |
| 36 | + if cfg.Passphrase != "" { |
| 37 | + // 先使用 passphrase 解析 PrivateKey |
| 38 | + if signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(cfg.PrivateKey), |
| 39 | + []byte(cfg.Passphrase)); err == nil { |
| 40 | + authMethods = append(authMethods, ssh.PublicKeys(signer)) |
| 41 | + } |
| 42 | + } |
| 43 | + if err != nil || cfg.Passphrase == "" { |
| 44 | + // 1. 如果之前使用解析失败,则去掉 passphrase,则尝试直接解析 PrivateKey 防止错误的passphrase |
| 45 | + // 2. 如果没有 Passphrase 则直接解析 PrivateKey |
| 46 | + if signer, err = ssh.ParsePrivateKey([]byte(cfg.PrivateKey)); err == nil { |
| 47 | + authMethods = append(authMethods, ssh.PublicKeys(signer)) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + if cfg.Password != "" { |
| 52 | + authMethods = append(authMethods, ssh.Password(cfg.Password)) |
| 53 | + } |
| 54 | + if cfg.keyboardAuth == nil { |
| 55 | + cfg.keyboardAuth = func(user, instruction string, questions []string, echos []bool) (answers []string, err error) { |
| 56 | + if len(questions) == 0 { |
| 57 | + return []string{}, nil |
| 58 | + } |
| 59 | + return []string{cfg.Password}, nil |
| 60 | + } |
| 61 | + } |
| 62 | + authMethods = append(authMethods, ssh.KeyboardInteractive(cfg.keyboardAuth)) |
| 63 | + |
| 64 | + return authMethods |
| 65 | +} |
| 66 | + |
| 67 | +func Username(username string) Option { |
| 68 | + return func(args *Options) { |
| 69 | + args.Username = username |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func Password(password string) Option { |
| 74 | + return func(args *Options) { |
| 75 | + args.Password = password |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func PrivateKey(privateKey string) Option { |
| 80 | + return func(args *Options) { |
| 81 | + args.PrivateKey = privateKey |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func Passphrase(passphrase string) Option { |
| 86 | + return func(args *Options) { |
| 87 | + args.Passphrase = passphrase |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func Host(host string) Option { |
| 92 | + return func(args *Options) { |
| 93 | + args.Host = host |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func Port(port int) Option { |
| 98 | + return func(args *Options) { |
| 99 | + args.Port = strconv.Itoa(port) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func Timeout(timeout int) Option { |
| 104 | + return func(args *Options) { |
| 105 | + args.timeout = timeout |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func PrivateAuth(privateAuth ssh.Signer) Option { |
| 110 | + return func(args *Options) { |
| 111 | + args.PrivateAuth = privateAuth |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func KeyboardAuth(keyboardAuth ssh.KeyboardInteractiveChallenge) Option { |
| 116 | + return func(conf *Options) { |
| 117 | + conf.keyboardAuth = keyboardAuth |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +const defaultTimeout = 15 |
| 122 | + |
| 123 | +func New(opts ...Option) (*ssh.Client, error) { |
| 124 | + cfg := &Options{ |
| 125 | + Host: "127.0.0.1", |
| 126 | + Port: "22", |
| 127 | + timeout: defaultTimeout, |
| 128 | + } |
| 129 | + for _, setter := range opts { |
| 130 | + setter(cfg) |
| 131 | + } |
| 132 | + return NewWithCfg(cfg) |
| 133 | +} |
| 134 | + |
| 135 | +func NewWithCfg(cfg *Options) (*ssh.Client, error) { |
| 136 | + sshCfg := ssh.ClientConfig{ |
| 137 | + User: cfg.Username, |
| 138 | + Auth: cfg.AuthMethods(), |
| 139 | + Timeout: time.Duration(cfg.timeout) * time.Second, |
| 140 | + HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 141 | + } |
| 142 | + destAddr := net.JoinHostPort(cfg.Host, cfg.Port) |
| 143 | + sshClient, err := ssh.Dial("tcp", destAddr, &sshCfg) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + return sshClient, nil |
| 148 | +} |
0 commit comments