forked from povsister/scp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
57 lines (51 loc) · 1.59 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package scp
import (
"time"
"golang.org/x/crypto/ssh"
)
// defaultConnTimeout is the default timeout for establishing a TCP connection to server
const defaultConnTimeout = 3 * time.Second
// NewSSHConfigFromPassword returns a *ssh.ClientConfig with ssh.Password AuthMethod
// and 3 seconds timeout for connecting the server.
//
// It *insecurely* ignores server's host key validation.
func NewSSHConfigFromPassword(username, password string) *ssh.ClientConfig {
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: defaultConnTimeout,
}
}
// NewSSHConfigFromPrivateKey returns a *ssh.ClientConfig with ssh.PublicKey AuthMethod
// and 3 seconds timeout for connecting the server.
//
// The passphrase is optional.
// If multiple passphrase are provided, only the first will be used.
//
// If the private key is encrypted, it will return a ssh.PassphraseMissingError.
//
// It *insecurely* ignores server's host key validation.
func NewSSHConfigFromPrivateKey(username string, privPEM []byte, passphrase ...string) (cfg *ssh.ClientConfig, err error) {
var priv ssh.Signer
if len(passphrase) > 0 && len(passphrase[0]) > 0 {
pw := passphrase[0]
priv, err = ssh.ParsePrivateKeyWithPassphrase(privPEM, []byte(pw))
} else {
priv, err = ssh.ParsePrivateKey(privPEM)
}
if err != nil {
return
}
cfg = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(priv),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: defaultConnTimeout,
}
return
}