-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathclient.go
446 lines (373 loc) · 10 KB
/
client.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
/* Copyright (c) 2024 Bram Vandenbogaerde And Contributors
* You may use, distribute or modify this code under the
* terms of the Mozilla Public License 2.0, which is distributed
* along with the source code.
*/
package scp
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sync"
"time"
"golang.org/x/crypto/ssh"
)
// Callback for freeing managed resources
type ICloseHandler interface {
Close()
}
// Close handler equivalent to a no-op. Used by default
// when no resources have to be cleaned.
type EmptyHandler struct{}
func (EmptyHandler) Close() {}
// Close handler to close an SSH client
type CloseSSHCLient struct {
// Reference to the used SSH client
sshClient *ssh.Client
}
func (scp CloseSSHCLient) Close() {
scp.sshClient.Close()
}
type PassThru func(r io.Reader, total int64) io.Reader
type Client struct {
// Host the host to connect to.
Host string
// ClientConfig the client config to use.
ClientConfig *ssh.ClientConfig
// Keep the ssh client around for generating new sessions
sshClient *ssh.Client
// Timeout the maximal amount of time to wait for a file transfer to complete.
// Deprecated: use context.Context for each function instead.
Timeout time.Duration
// RemoteBinary the absolute path to the remote SCP binary.
RemoteBinary string
// Handler called when calling `Close` to clean up any remaining
// resources managed by `Client`.
closeHandler ICloseHandler
}
// Connect connects to the remote SSH server, returns error if it couldn't establish a session to the SSH server.
func (a *Client) Connect() error {
client, err := ssh.Dial("tcp", a.Host, a.ClientConfig)
if err != nil {
return err
}
a.sshClient = client
a.closeHandler = CloseSSHCLient{sshClient: client}
return nil
}
// Returns the underlying SSH client, this should be used carefully as
// it will be closed by `client.Close`.
func (a *Client) SSHClient() *ssh.Client {
return a.sshClient
}
// CopyFromFile copies the contents of an os.File to a remote location, it will get the length of the file by looking it up from the filesystem.
func (a *Client) CopyFromFile(
ctx context.Context,
file os.File,
remotePath string,
permissions string,
) error {
return a.CopyFromFilePassThru(ctx, file, remotePath, permissions, nil)
}
// CopyFromFilePassThru copies the contents of an os.File to a remote location, it will get the length of the file by looking it up from the filesystem.
// Access copied bytes by providing a PassThru reader factory.
func (a *Client) CopyFromFilePassThru(
ctx context.Context,
file os.File,
remotePath string,
permissions string,
passThru PassThru,
) error {
stat, err := file.Stat()
if err != nil {
return fmt.Errorf("failed to stat file: %w", err)
}
return a.CopyPassThru(ctx, &file, remotePath, permissions, stat.Size(), passThru)
}
// CopyFile copies the contents of an io.Reader to a remote location, the length is determined by reading the io.Reader until EOF
// if the file length in know in advance please use "Copy" instead.
func (a *Client) CopyFile(
ctx context.Context,
fileReader io.Reader,
remotePath string,
permissions string,
) error {
return a.CopyFilePassThru(ctx, fileReader, remotePath, permissions, nil)
}
// CopyFilePassThru copies the contents of an io.Reader to a remote location, the length is determined by reading the io.Reader until EOF
// if the file length in know in advance please use "Copy" instead.
// Access copied bytes by providing a PassThru reader factory.
func (a *Client) CopyFilePassThru(
ctx context.Context,
fileReader io.Reader,
remotePath string,
permissions string,
passThru PassThru,
) error {
contentsBytes, err := ioutil.ReadAll(fileReader)
if err != nil {
return fmt.Errorf("failed to read all data from reader: %w", err)
}
bytesReader := bytes.NewReader(contentsBytes)
return a.CopyPassThru(
ctx,
bytesReader,
remotePath,
permissions,
int64(len(contentsBytes)),
passThru,
)
}
// wait waits for the waitgroup for the specified max timeout.
// Returns true if waiting timed out.
func wait(wg *sync.WaitGroup, ctx context.Context) error {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// checkResponse checks the response it reads from the remote, and will return a single error in case
// of failure.
func checkResponse(r io.Reader) error {
_, err := ParseResponse(r, nil)
if err != nil {
return err
}
return nil
}
// Copy copies the contents of an io.Reader to a remote location.
func (a *Client) Copy(
ctx context.Context,
r io.Reader,
remotePath string,
permissions string,
size int64,
) error {
return a.CopyPassThru(ctx, r, remotePath, permissions, size, nil)
}
// CopyPassThru copies the contents of an io.Reader to a remote location.
// Access copied bytes by providing a PassThru reader factory
func (a *Client) CopyPassThru(
ctx context.Context,
r io.Reader,
remotePath string,
permissions string,
size int64,
passThru PassThru,
) error {
session, err := a.sshClient.NewSession()
if err != nil {
return fmt.Errorf("Error creating ssh session in copy to remote: %v", err)
}
defer session.Close()
stdout, err := session.StdoutPipe()
if err != nil {
return err
}
w, err := session.StdinPipe()
if err != nil {
return err
}
defer w.Close()
if passThru != nil {
r = passThru(r, size)
}
filename := path.Base(remotePath)
// Start the command first and get confirmation that it has been started
// before sending anything through the pipes.
err = session.Start(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePath))
if err != nil {
return err
}
wg := sync.WaitGroup{}
wg.Add(2)
errCh := make(chan error, 2)
// SCP protocol and file sending
go func() {
defer wg.Done()
defer w.Close()
_, err = fmt.Fprintln(w, "C"+permissions, size, filename)
if err != nil {
errCh <- err
return
}
if err = checkResponse(stdout); err != nil {
errCh <- err
return
}
_, err = io.Copy(w, r)
if err != nil {
errCh <- err
return
}
_, err = fmt.Fprint(w, "\x00")
if err != nil {
errCh <- err
return
}
if err = checkResponse(stdout); err != nil {
errCh <- err
return
}
}()
// Wait for the process to exit
go func() {
defer wg.Done()
err := session.Wait()
if err != nil {
errCh <- err
return
}
}()
// If there is a timeout, stop the transfer if it has been exceeded
if a.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, a.Timeout)
defer cancel()
}
// Wait for one of the conditions (error/timeout/completion) to occur
if err := wait(&wg, ctx); err != nil {
return err
}
close(errCh)
// Collect any errors from the error channel
for err := range errCh {
if err != nil {
return err
}
}
return nil
}
// CopyFromRemote copies a file from the remote to the local file given by the `file`
// parameter. Use `CopyFromRemotePassThru` if a more generic writer
// is desired instead of writing directly to a file on the file system.
func (a *Client) CopyFromRemote(ctx context.Context, file *os.File, remotePath string) error {
return a.CopyFromRemotePassThru(ctx, file, remotePath, nil)
}
// CopyFromRemotePassThru copies a file from the remote to the given writer. The passThru parameter can be used
// to keep track of progress and how many bytes that were download from the remote.
// `passThru` can be set to nil to disable this behaviour.
func (a *Client) CopyFromRemotePassThru(
ctx context.Context,
w io.Writer,
remotePath string,
passThru PassThru,
) error {
_, err := a.copyFromRemote(ctx, w, remotePath, passThru, false)
return err
}
// CopyFroRemoteFileInfos copies a file from the remote to a given writer and return a FileInfos struct
// containing information about the file such as permissions, the file size, modification time and access time
func (a *Client) CopyFromRemoteFileInfos(
ctx context.Context,
w io.Writer,
remotePath string,
passThru PassThru,
) (*FileInfos, error) {
return a.copyFromRemote(ctx, w, remotePath, passThru, true)
}
func (a *Client) copyFromRemote(
ctx context.Context,
w io.Writer,
remotePath string,
passThru PassThru,
preserveFileTimes bool,
) (*FileInfos, error) {
session, err := a.sshClient.NewSession()
if err != nil {
return nil, fmt.Errorf("Error creating ssh session in copy from remote: %v", err)
}
defer session.Close()
wg := sync.WaitGroup{}
errCh := make(chan error, 4)
var fileInfos *FileInfos
wg.Add(1)
go func() {
var err error
defer func() {
// NOTE: this might send an already sent error another time, but since we only receive one, this is fine. On the "happy-path" of this function, the error will be `nil` therefore completing the "err<-errCh" at the bottom of the function.
errCh <- err
// We must unblock the go routine first as we block on reading the channel later
wg.Done()
}()
r, err := session.StdoutPipe()
if err != nil {
errCh <- err
return
}
in, err := session.StdinPipe()
if err != nil {
errCh <- err
return
}
defer in.Close()
if preserveFileTimes {
err = session.Start(fmt.Sprintf("%s -pf %q", a.RemoteBinary, remotePath))
} else {
err = session.Start(fmt.Sprintf("%s -f %q", a.RemoteBinary, remotePath))
}
if err != nil {
errCh <- err
return
}
err = Ack(in)
if err != nil {
errCh <- err
return
}
fileInfo, err := ParseResponse(r, in)
if err != nil {
errCh <- err
return
}
fileInfos = fileInfo
err = Ack(in)
if err != nil {
errCh <- err
return
}
if passThru != nil {
r = passThru(r, fileInfo.Size)
}
_, err = CopyN(w, r, fileInfo.Size)
if err != nil {
errCh <- err
return
}
err = Ack(in)
if err != nil {
errCh <- err
return
}
err = session.Wait()
if err != nil {
errCh <- err
return
}
}()
if a.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, a.Timeout)
defer cancel()
}
if err := wait(&wg, ctx); err != nil {
return nil, err
}
finalErr := <-errCh
close(errCh)
return fileInfos, finalErr
}
func (a *Client) Close() {
a.closeHandler.Close()
}