/
util.go
executable file
·209 lines (168 loc) · 5.28 KB
/
util.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ssh
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/knownhosts"
)
// DefaultKnownHostsPath returns default user knows hosts file.
func DefaultKnownHostsPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return fmt.Sprintf("%s/.ssh/known_hosts", home), err
}
// DefaultKnownHosts returns host key callback from default known hosts path, and error if any.
func DefaultKnownHosts() (ssh.HostKeyCallback, error) {
path, err := DefaultKnownHostsPath()
if err != nil {
return nil, err
}
return knownhosts.New(path)
}
// CheckKnownHost checks is host in known hosts file.
// it returns is the host found in known_hosts file and error, if the host found in
// known_hosts file and error not nil that means public key mismatch, maybe MAN IN THE MIDDLE ATTACK! you should not handshake.
func CheckKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (found bool, err error) {
var keyErr *knownhosts.KeyError
// Fallback to default known_hosts file
if knownFile == "" {
path, err := DefaultKnownHostsPath()
if err != nil {
return false, err
}
knownFile = path
}
// Get host key callback
callback, err := knownhosts.New(knownFile)
if err != nil {
return false, err
}
// check if host already exists.
err = callback(host, remote, key)
// Known host already exists.
if err == nil {
return true, nil
}
// Make sure that the error returned from the callback is host not in file error.
// If keyErr.Want is greater than 0 length, that means host is in file with different key.
if errors.As(err, &keyErr) && len(keyErr.Want) > 0 {
return true, keyErr
}
// Key is not trusted because it is not in the file.
return false, nil
}
// AddKnownHost add a a host to known hosts file.
func AddKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (err error) {
// Fallback to default known_hosts file
if knownFile == "" {
path, err := DefaultKnownHostsPath()
if err != nil {
return err
}
knownFile = path
}
f, err := os.OpenFile(knownFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return err
}
defer f.Close()
remoteNormalized := knownhosts.Normalize(remote.String())
hostNormalized := knownhosts.Normalize(host)
addresses := []string{remoteNormalized}
if hostNormalized != remoteNormalized {
addresses = append(addresses, hostNormalized)
}
_, err = f.WriteString(knownhosts.Line(addresses, key) + "\n")
return err
}
// PrivateKey Loads a private and public key from "path" and returns a SSH ClientConfig to authenticate with the server
func PrivateKey(username string, path string, timeout int) (*ssh.ClientConfig, error) {
privateKey, err := os.ReadFile(path)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
return nil, err
}
callback, err := DefaultKnownHosts()
if err != nil {
return nil, err
}
return &ssh.ClientConfig{
User: username,
Timeout: time.Duration(timeout) * time.Second,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: callback,
}, nil
}
// Creates the configuration for a client that authenticates with a password protected private key
func PrivateKeyWithPassphrase(username string, timeout int, passpharase []byte, path string) (*ssh.ClientConfig, error) {
privateKey, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKeyWithPassphrase(privateKey, passpharase)
if err != nil {
return nil, err
}
callback, err := DefaultKnownHosts()
if err != nil {
return nil, err
}
return &ssh.ClientConfig{
User: username,
Timeout: time.Duration(timeout) * time.Second,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: callback,
}, nil
}
// Creates a configuration for a client that fetches public-private key from the SSH agent for authentication
func SSHAgent(username string, timeout int, keyCallBack ssh.HostKeyCallback) (*ssh.ClientConfig, error) {
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)
if err != nil {
return &ssh.ClientConfig{
Timeout: time.Duration(timeout) * time.Second,
}, err
}
return &ssh.ClientConfig{
User: username,
Timeout: time.Duration(timeout) * time.Second,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.NewClient(conn).Signers),
},
HostKeyCallback: keyCallBack,
}, nil
}
// Creates a configuration for a client that authenticates using username and password
func PasswordKey(username string, password string, timeout int) *ssh.ClientConfig {
return &ssh.ClientConfig{
User: username,
Timeout: time.Duration(timeout) * time.Second,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
}
type WriteCounter struct {
Total int64 // Total # of bytes written
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += int64(n)
fmt.Fprintf(os.Stdout, "%.2f kb transferred\n", float64(wc.Total/1024))
return n, nil
}