-
Notifications
You must be signed in to change notification settings - Fork 3
/
scp.go
320 lines (257 loc) · 6.21 KB
/
scp.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
// Package scp provides SCP functionality atop the go.crypto/ssh package.
package scp
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/kballard/go-shellquote"
"golang.org/x/crypto/ssh"
)
// File is a file being read from or written to a remote host. It implements the
// io.Reader and os.FileInfo interfaces, with the io.Reader portion delegated
// through to a bufio.Reader in the case that this is a file being read from a
// remote host.
type File struct {
io.Reader
name string
size int64
mode os.FileMode
}
// NewFile constructs a new File object with the given parameters. The size must
// be provided in advance because the remote host has to know how large the file
// is, so it can reject it in advance if there's not enough space.
func NewFile(name string, size int64, mode os.FileMode, r io.Reader) *File {
return &File{
Reader: r,
name: name,
size: size,
mode: mode,
}
}
// IsDir will always return false.
func (f File) IsDir() bool {
return false
}
// Name returns the name of the file. It does not include the full path.
func (f File) Name() string {
return f.name
}
// Size returns the size of the file in bytes.
func (f File) Size() int64 {
return f.size
}
// Mode returns the mode reported by the remote side.
func (f File) Mode() os.FileMode {
return f.mode
}
// ModTime returns the modification time of the file. It is currently not
// implemented and returns a zero value.
func (f File) ModTime() time.Time {
return time.Time{}
}
// Sys always returns nil.
func (f File) Sys() interface{} {
return nil
}
// Read opens a session on the provided ssh.Client to run the scp program
// remotely in "from" mode, and handles the SCP protocol to the degree required
// to read the content of a single file.
//
// Errors that occur before the content is being read will be returned directly
// from Read, while errors that occur during content reception will be returned
// via the Reader (e.g. from Reader.Read).
func Read(c *ssh.Client, file string) (*File, error) {
s, err := c.NewSession()
if err != nil {
return nil, err
}
stdout, err := s.StdoutPipe()
if err != nil {
return nil, err
}
stdin, err := s.StdinPipe()
if err != nil {
return nil, err
}
rw := bufio.NewReadWriter(bufio.NewReader(stdout), bufio.NewWriter(stdin))
if err := s.Start(shellquote.Join("scp", "-qf", file)); err != nil {
return nil, err
}
if err := rw.WriteByte(0); err != nil {
return nil, err
}
if err := rw.Flush(); err != nil {
return nil, err
}
b, err := rw.ReadByte()
if err != nil {
return nil, err
}
switch b {
case 0x01, 0x02:
l, err := rw.ReadBytes('\n')
if err != nil && err != io.EOF {
return nil, err
}
m := map[byte]string{
0x01: "warning",
0x02: "error",
}
return nil, fmt.Errorf("%s: %q", m[b], string(bytes.TrimRight(l, "\n")))
}
if err := rw.UnreadByte(); err != nil {
return nil, err
}
l, err := rw.ReadBytes('\n')
if err != nil {
return nil, err
}
mode, size, name, err := parseCopy(l)
if err != nil {
return nil, err
}
if err := rw.WriteByte(0); err != nil {
return nil, err
}
if err := rw.Flush(); err != nil {
return nil, err
}
r, w := io.Pipe()
go func() {
defer s.Close()
var err error
defer func() {
if err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
err = func() error {
t := 0
for {
b := make([]byte, min(1024, int(size)-t))
n, err := stdout.Read(b)
if err == io.EOF {
break
} else if err != nil {
return err
}
w.Write(b[0:n])
t += n
if int64(t) == size {
break
}
}
if err := rw.WriteByte(0); err != nil {
return err
}
if err := rw.Flush(); err != nil {
return err
}
if _, err = io.Copy(ioutil.Discard, rw); err == io.EOF {
return nil
} else if err != nil {
return err
}
return nil
}()
}()
return NewFile(name, size, mode, r), nil
}
// Write writes the given File to the directory specified. It returns a list of
// warnings and maybe an error on failure. Warnings are non-fatal, errors are
// fatal. If there are warnings returned, they're probably important.
func Write(c *ssh.Client, dir string, file *File) ([]string, error) {
s, err := c.NewSession()
if err != nil {
return nil, err
}
defer s.Close()
stdout, err := s.StdoutPipe()
if err != nil {
return nil, err
}
stdin, err := s.StdinPipe()
if err != nil {
return nil, err
}
rw := bufio.NewReadWriter(bufio.NewReader(stdout), bufio.NewWriter(stdin))
if err := s.Start(shellquote.Join("scp", "-t", dir)); err != nil {
return nil, err
}
if _, err := rw.WriteString(fmt.Sprintf("C0%s %d %s\n", strconv.FormatUint(uint64(file.Mode()), 8), file.Size(), file.Name())); err != nil {
return nil, err
}
if err := rw.Flush(); err != nil {
return nil, err
}
if err := rw.WriteByte(0); err != nil {
return nil, err
}
if err := rw.Flush(); err != nil {
return nil, err
}
var warnings []string
if b, err := rw.ReadByte(); err != nil {
return nil, err
} else if b == 1 || b == 2 {
msg, err := rw.ReadString('\n')
if err != nil {
return nil, err
}
msg = strings.TrimSpace(msg)
if b == 2 {
return nil, fmt.Errorf(msg)
}
warnings = append(warnings, msg)
}
if _, err := io.Copy(rw, file); err != nil {
return warnings, err
}
if err := rw.Flush(); err != nil {
return warnings, err
}
if b, err := rw.ReadByte(); err != nil {
return nil, err
} else if b == 1 || b == 2 {
msg, err := rw.ReadString('\n')
if err != nil {
return nil, err
}
msg = strings.TrimSpace(msg)
if b == 2 {
return nil, fmt.Errorf(msg)
}
warnings = append(warnings, msg)
}
return warnings, nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func parseCopy(l []byte) (os.FileMode, int64, string, error) {
if l[0] != 'C' {
return 0, 0, "", fmt.Errorf("invalid first byte; expected C but got %02x", l[0])
}
bits := bytes.Split(bytes.TrimRight(l, "\n"), []byte(" "))
rawMode, err := strconv.ParseUint(string(bits[0][1:]), 8, 32)
if err != nil {
return 0, 0, "", err
}
mode := os.FileMode(uint32(rawMode))
size, err := strconv.ParseInt(string(bits[1]), 10, 32)
if err != nil {
return 0, 0, "", err
}
return mode, size, string(bits[2]), nil
}