-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
466 lines (395 loc) · 12 KB
/
ssh.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package gossh
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/gookit/goutil/fsutil"
"github.com/melbahja/goph"
"github.com/pkg/errors"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
type Logger func(format string, v ...interface{})
func Connect(host string, port uint, username, password string) (*SSH, error) {
if port == 0 {
port = 22
}
s := &SSH{
Host: host,
Port: port,
Username: username,
Password: password,
}
err := s.connect()
if err != nil {
return nil, errors.WithStack(err)
}
return s, nil
}
type SSH struct {
Client *goph.Client
Host string
Port uint
Username string
Password string
Log Logger
}
func (s *SSH) Close() error {
return s.Client.Close()
}
func (s *SSH) Ping() error {
_, err := s.Client.Run("ls > /dev/null")
return err
}
func (s *SSH) Run(cmd string) ([]byte, error) {
s.Log("$ %s", cmd)
return s.Client.Run(cmd)
}
func (s *SSH) Runf(format string, a ...interface{}) ([]byte, error) {
return s.Run(fmt.Sprintf(format, a...))
}
func (s *SSH) Sudo(cmd string) ([]byte, error) {
if s.Username != "root" {
cmd = fmt.Sprintf(`echo '%s' | sudo -S sh -c "%s"`, s.Password, cmd)
}
return s.Run(cmd)
}
func (s *SSH) Sudof(format string, a ...interface{}) ([]byte, error) {
return s.Sudo(fmt.Sprintf(format, a...))
}
func (s *SSH) Sftp() (*sftp.Client, error) {
return s.Client.NewSftp()
}
func (s *SSH) UserHomeDir() (string, error) {
out, err := s.Run("echo $HOME")
if err != nil {
return "", err
}
return string(bytes.TrimRight(out, "\n")), nil
}
func (s *SSH) Chown(user string, path string) error {
_, err := s.Runf("chown %s %s", user, path)
return err
}
func (s *SSH) PathExists(path string) (bool, error) {
sftpClient, err := s.Client.NewSftp()
if err != nil {
return false, errors.WithMessagef(err, "failed to create SFTP client: %v", err)
}
defer sftpClient.Close()
return s.pathExists(sftpClient, path), nil
}
func (s *SSH) Download(remotePath, localDir string) (err error) {
if fsutil.IsFile(localDir) {
return errors.Errorf("local directory '%s' cannot be a file", localDir)
}
remotePath, err = s.expandTilde(strings.TrimSuffix(remotePath, "/"))
if err != nil {
return errors.WithMessagef(err, "failed to expand '~' in remote directory: %v", err)
}
sftpClient, err := s.Client.NewSftp()
if err != nil {
return errors.WithMessagef(err, "failed to create SFTP client: %v", err)
}
defer sftpClient.Close()
if !s.pathExists(sftpClient, remotePath) {
return errors.Errorf("remote path '%s' not found", remotePath)
}
if s.isFile(sftpClient, remotePath) {
err = os.MkdirAll(localDir, 0755)
if err != nil {
return errors.WithMessagef(err, "failed to create local directory '%s': %v", localDir, err)
}
return s.downloadFile(sftpClient, remotePath, filepath.Join(localDir, filepath.Base(remotePath)))
}
return s.downloadDirectory(sftpClient, remotePath, localDir)
}
func (s *SSH) downloadDirectory(sftpClient *sftp.Client, remoteDir, localDir string) error {
s.Log("download directory %s -> %s", remoteDir, localDir)
entries, err := sftpClient.ReadDir(remoteDir)
if err != nil {
return errors.WithMessagef(err, "failed to read remote directory '%s': %v", remoteDir, err)
}
for _, entry := range entries {
local := filepath.Join(localDir, entry.Name())
remote := filepath.Join(remoteDir, entry.Name())
if entry.IsDir() {
err = os.MkdirAll(local, entry.Mode())
if err != nil {
return errors.WithMessagef(err, "failed to create local directory '%s': %v", local, err)
}
err = s.downloadDirectory(sftpClient, remote, local)
if err != nil {
return errors.WithMessagef(err, "failed to download directory '%s': %v", remote, err)
}
} else {
err = s.downloadFile(sftpClient, remote, local)
if err != nil {
return errors.WithMessagef(err, "failed to download file '%s': %v", remote, err)
}
}
}
return nil
}
func (s *SSH) downloadFile(sftpClient *sftp.Client, remotePath, localPath string) error {
s.Log("download file %s -> %s", remotePath, localPath)
remoteFile, err := sftpClient.Open(remotePath)
if err != nil {
return errors.WithMessagef(err, "failed to open remote file '%s': %v", remotePath, err)
}
defer remoteFile.Close()
remoteFileInfo, err := remoteFile.Stat()
if err != nil {
return errors.WithMessagef(err, "failed to get remote file info: %v", err)
}
localFile, err := os.OpenFile(localPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, remoteFileInfo.Mode())
if err != nil {
return errors.WithMessagef(err, "failed to create local file '%s': %v", localPath, err)
}
defer localFile.Close()
_, err = io.Copy(localFile, remoteFile)
if err != nil {
return errors.WithMessagef(err, "failed to copy remote file to local: %v", err)
}
return nil
}
func (s *SSH) Upload(localPath, remoteDir string) (err error) {
if !fsutil.PathExists(localPath) {
return errors.Errorf("local path '%s' not found", localPath)
}
remoteDir, err = s.expandTilde(strings.TrimSuffix(remoteDir, "/"))
if err != nil {
return errors.WithMessagef(err, "failed to expand '~' in remote directory: %v", err)
}
sftpClient, err := s.Client.NewSftp()
if err != nil {
return errors.WithMessagef(err, "failed to create SFTP client: %v", err)
}
defer sftpClient.Close()
if s.isFile(sftpClient, remoteDir) {
return errors.Errorf("remote directory '%s' cannot be a file", remoteDir)
}
if fsutil.IsFile(localPath) {
err = sftpClient.MkdirAll(remoteDir)
if err != nil {
return errors.WithMessagef(err, "failed to create remote directory '%s': %v", remoteDir, err)
}
return s.uploadFile(sftpClient, localPath, filepath.Join(remoteDir, filepath.Base(localPath)))
}
return s.uploadDirectory(sftpClient, localPath, remoteDir)
}
func (s *SSH) uploadDirectory(sftpClient *sftp.Client, localDir, remoteDir string) error {
s.Log("upload directory %s -> %s", localDir, remoteDir)
entries, err := os.ReadDir(localDir)
if err != nil {
return errors.WithMessagef(err, "failed to read local directory '%s': %v", localDir, err)
}
for _, entry := range entries {
local := filepath.Join(localDir, entry.Name())
remote := filepath.Join(remoteDir, entry.Name())
if entry.IsDir() {
err = sftpClient.MkdirAll(remote)
if err != nil {
return errors.WithMessagef(err, "failed to create remote directory '%s': %v", remote, err)
}
err = s.uploadDirectory(sftpClient, local, remote)
if err != nil {
return errors.WithMessagef(err, "failed to upload directory '%s': %v", local, err)
}
} else {
err = s.uploadFile(sftpClient, local, remote)
if err != nil {
return errors.WithMessagef(err, "failed to upload file '%s': %v", local, err)
}
}
}
return nil
}
func (s *SSH) uploadFile(sftpClient *sftp.Client, localPath, remotePath string) error {
s.Log("upload file %s -> %s", localPath, remotePath)
localFile, err := os.Open(localPath)
if err != nil {
return errors.WithMessagef(err, "failed to open local file '%s': %v", localPath, err)
}
defer localFile.Close()
localFileInfo, err := localFile.Stat()
if err != nil {
return errors.WithMessagef(err, "failed to get local file info: %v", err)
}
remoteFile, err := sftpClient.Create(remotePath)
if err != nil {
return errors.WithMessagef(err, "failed to create remote file '%s': %v", remotePath, err)
}
defer remoteFile.Close()
err = remoteFile.Chmod(localFileInfo.Mode())
if err != nil {
return errors.WithMessagef(err, "failed to set remote file permissions: %v", err)
}
_, err = io.Copy(remoteFile, localFile)
if err != nil {
return errors.WithMessagef(err, "failed to copy local file to remote: %v", err)
}
return nil
}
// 转义 ~
func (s *SSH) expandTilde(path string) (string, error) {
if !strings.HasPrefix(path, "~") {
return path, nil
}
homeDir, err := s.UserHomeDir()
if err != nil {
return "", errors.WithStack(err)
}
expandedPath := strings.Replace(path, "~", homeDir, 1)
return expandedPath, nil
}
func (s *SSH) isFile(sftpClient *sftp.Client, path string) bool {
if fi, err := sftpClient.Stat(path); err == nil {
return !fi.IsDir()
}
return false
}
func (s *SSH) isDir(sftpClient *sftp.Client, path string) bool {
if fi, err := os.Stat(path); err == nil {
return fi.IsDir()
}
return false
}
func (s *SSH) pathExists(sftpClient *sftp.Client, path string) bool {
if _, err := sftpClient.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func (s *SSH) connect() error {
// 测试远程连接是否可通
address := fmt.Sprintf("%s:%d", s.Host, s.Port)
if _, err := isReachableAddr(address); err != nil {
return errors.WithStack(err)
}
// knownFile
knownFile, err := getKnownFile()
if err != nil {
return errors.WithStack(err)
}
// 发起 ssh 连接
client, err := goph.NewConn(&goph.Config{
Addr: s.Host,
Port: s.Port,
User: s.Username,
Auth: goph.Password(s.Password),
Callback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
hostFound, err := goph.CheckKnownHost(hostname, remote, key, knownFile)
if hostFound && err != nil {
var keyErr *knownhosts.KeyError
var revokedErr *knownhosts.RevokedError
if errors.As(err, &keyErr) {
if len(keyErr.Want) == 0 {
// 正常不会进入该语句。若 Want 为空,代表没有找到key,则 hostFound == false
return errors.WithStack(err)
}
// 删除错误的 key
lines := make([]int, 0)
for _, want := range keyErr.Want {
lines = append(lines, want.Line)
}
if err = deleteFileLines(knownFile, lines); err != nil {
return errors.WithStack(err)
}
} else if errors.As(err, revokedErr) {
// 删除被撤销的 key
if err = deleteFileLines(knownFile, []int{revokedErr.Revoked.Line}); err != nil {
return errors.WithStack(err)
}
} else {
return errors.WithMessage(err, "check knownhost error")
}
}
if hostFound && err == nil {
// 已存在
return nil
}
if err = goph.AddKnownHost(hostname, remote, key, knownFile); err != nil {
return errors.WithStack(err)
}
return nil
},
})
if err != nil {
return errors.WithStack(err)
}
s.Client = client
return nil
}
func getKnownFile() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", errors.WithMessage(err, "failed to get user home directory")
}
sshDir := filepath.Join(home, ".ssh")
err = os.MkdirAll(sshDir, 0755)
if err != nil {
return "", errors.WithMessage(err, "failed to create .ssh directory")
}
knownFile := filepath.Join(sshDir, "known_hosts")
return knownFile, nil
}
func isReachableAddr(address string) (bool, error) {
conn, err := net.DialTimeout("tcp", address, time.Second*2)
if err != nil {
return false, err
}
_ = conn.Close()
return true, nil
}
func deleteFileLines(filename string, linesToDelete []int) error {
// Open the original file for reading
file, err := os.OpenFile(filename, os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()
// Read the content of the original file and save the lines to keep in a new string slice
scanner := bufio.NewScanner(file)
var linesToKeep []string
lineNumber := 1
for scanner.Scan() {
if !contains(linesToDelete, lineNumber) {
linesToKeep = append(linesToKeep, scanner.Text())
}
lineNumber++
}
if err = scanner.Err(); err != nil {
return fmt.Errorf("failed to read file: %v", err)
}
// Join the lines from the new string slice into a single string
newContent := strings.Join(linesToKeep, "\n")
// Write the reassembled string back to the original file, overwriting the original content
if err := file.Truncate(0); err != nil {
return fmt.Errorf("failed to truncate file: %v", err)
}
if _, err := file.Seek(0, 0); err != nil {
return fmt.Errorf("failed to seek file: %v", err)
}
if _, err := file.WriteString(newContent); err != nil {
return fmt.Errorf("failed to write file: %v", err)
}
return nil
}
func contains(slice []int, value int) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}