This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
protocol.go
329 lines (286 loc) · 8.11 KB
/
protocol.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
package scp
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
)
const (
msgCopyFile = 'C'
msgStartDirectory = 'D'
msgEndDirectory = 'E'
msgTime = 'T'
)
const (
replyOK = '\x00'
replyError = '\x01'
replyFatalError = '\x02'
)
type sourceProtocol struct {
remIn io.WriteCloser
remOut io.Reader
remReader *bufio.Reader
}
func newSourceProtocol(remIn io.WriteCloser, remOut io.Reader) (*sourceProtocol, error) {
s := &sourceProtocol{
remIn: remIn,
remOut: remOut,
remReader: bufio.NewReader(remOut),
}
return s, s.readReply()
}
func (s *sourceProtocol) WriteFile(fileInfo *FileInfo, body io.ReadCloser) error {
if !fileInfo.modTime.IsZero() || !fileInfo.accessTime.IsZero() {
err := s.setTime(fileInfo.modTime, fileInfo.accessTime)
if err != nil {
return err
}
}
return s.writeFile(fileInfo.mode, fileInfo.size, fileInfo.name, body)
}
func (s *sourceProtocol) StartDirectory(dirInfo *FileInfo) error {
if !dirInfo.modTime.IsZero() || !dirInfo.accessTime.IsZero() {
err := s.setTime(dirInfo.modTime, dirInfo.accessTime)
if err != nil {
return err
}
}
return s.startDirectory(dirInfo.mode, dirInfo.name)
}
func (s *sourceProtocol) EndDirectory() error {
return s.endDirectory()
}
func (s *sourceProtocol) setTime(mtime, atime time.Time) error {
ms, mus := toSecondsAndMicroseconds(mtime)
as, aus := toSecondsAndMicroseconds(atime)
_, err := fmt.Fprintf(s.remIn, "%c%d %d %d %d\n", msgTime, ms, mus, as, aus)
if err != nil {
return fmt.Errorf("failed to write scp time header: %w", err)
}
return s.readReply()
}
func toSecondsAndMicroseconds(t time.Time) (seconds int64, microseconds int) {
rounded := t.Round(time.Microsecond)
return rounded.Unix(), rounded.Nanosecond() / int(int64(time.Microsecond)/int64(time.Nanosecond))
}
func (s *sourceProtocol) writeFileHeader(mode os.FileMode, length int64, filename string) error {
_, err := fmt.Fprintf(s.remIn, "%c%#4o %d %s\n", msgCopyFile, mode&os.ModePerm, length, filepath.Base(filename))
if err != nil {
return fmt.Errorf("failed to write scp file header: %w", err)
}
return nil
}
func (s *sourceProtocol) writeFile(mode os.FileMode, length int64, filename string, body io.ReadCloser) error {
err := s.writeFileHeader(mode, length, filename)
if err != nil {
return err
}
_, err = io.Copy(s.remIn, body)
// NOTE: We close body whether or not copy fails and ignore an error from closing body.
body.Close()
if err != nil {
return fmt.Errorf("failed to write scp file body: %w", err)
}
err = s.readReply()
if err != nil {
return err
}
_, err = s.remIn.Write([]byte{replyOK})
if err != nil {
return fmt.Errorf("failed to write scp replyOK reply: %w", err)
}
return s.readReply()
}
func (s *sourceProtocol) startDirectory(mode os.FileMode, dirname string) error {
// length is not used.
length := 0
_, err := fmt.Fprintf(s.remIn, "%c%#4o %d %s\n", msgStartDirectory, mode&os.ModePerm, length, filepath.Base(dirname))
if err != nil {
return fmt.Errorf("failed to write scp start directory header: %w", err)
}
return s.readReply()
}
func (s *sourceProtocol) endDirectory() error {
_, err := fmt.Fprintf(s.remIn, "%c\n", msgEndDirectory)
if err != nil {
return fmt.Errorf("failed to write scp end directory header: %w", err)
}
return s.readReply()
}
func (s *sourceProtocol) readReply() error {
b, err := s.remReader.ReadByte()
if err != nil {
return fmt.Errorf("failed to read scp reply type: %w", err)
}
if b == replyOK {
return nil
}
if b != replyError && b != replyFatalError {
return fmt.Errorf("unexpected scp reply type: %v", b)
}
line, err := s.remReader.ReadString('\n')
if err != nil {
return fmt.Errorf("failed to read scp reply message: %w", err)
}
return &protocolError{
msg: line,
fatal: b == replyFatalError,
}
}
type sinkProtocol struct {
remIn io.WriteCloser
remOut io.Reader
remReader *bufio.Reader
}
func newSinkProtocol(remIn io.WriteCloser, remOut io.Reader) (*sinkProtocol, error) {
s := &sinkProtocol{
remIn: remIn,
remOut: remOut,
remReader: bufio.NewReader(remOut),
}
err := s.WriteReplyOK()
if err != nil {
return nil, err
}
return s, nil
}
type timeMsgHeader struct {
Mtime time.Time
Atime time.Time
}
type startDirectoryMsgHeader struct {
Mode os.FileMode
Name string
}
type endDirectoryMsgHeader struct{}
type fileMsgHeader struct {
Mode os.FileMode
Size int64
Name string
}
type okMsg struct{}
func fromSecondsAndMicroseconds(seconds int64, microseconds int) time.Time {
return time.Unix(seconds, int64(microseconds)*(int64(time.Microsecond)/int64(time.Nanosecond)))
}
func (s *sinkProtocol) ReadHeaderOrReply() (interface{}, error) {
b, err := s.remReader.ReadByte()
if err == io.EOF {
return nil, err
} else if err != nil {
return nil, fmt.Errorf("failed to read scp message type: %w", err)
}
switch b {
case msgCopyFile:
var h fileMsgHeader
n, err := fmt.Fscanf(s.remReader, "%04o %d ", &h.Mode, &h.Size)
if err != nil {
return nil, fmt.Errorf("failed to read scp file message header: %w", err)
}
if n != 2 {
return nil, fmt.Errorf("unexpected count in reading file message header: n=%d", n)
}
name, err := s.remReader.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("failed to read scp file message header: %w", err)
}
h.Name = strings.TrimSuffix(name, "\n")
err = s.WriteReplyOK()
if err != nil {
return nil, fmt.Errorf("failed to write scp replyOK reply: %w", err)
}
return h, nil
case msgStartDirectory:
var h startDirectoryMsgHeader
var dummySize int64
n, err := fmt.Fscanf(s.remReader, "%04o %d ", &h.Mode, &dummySize)
if err != nil {
return nil, fmt.Errorf("failed to read scp start directory message header: %w", err)
}
if n != 2 {
return nil, fmt.Errorf("unexpected count in reading start directory message header: n=%d", n)
}
name, err := s.remReader.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("failed to read scp directory message header: %w", err)
}
h.Name = strings.TrimSuffix(name, "\n")
err = s.WriteReplyOK()
if err != nil {
return nil, fmt.Errorf("failed to write scp replyOK reply: %w", err)
}
return h, nil
case msgEndDirectory:
_, err := s.remReader.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("failed to read scp end directory message: %w", err)
}
err = s.WriteReplyOK()
if err != nil {
return nil, fmt.Errorf("failed to write scp replyOK reply: %w", err)
}
return endDirectoryMsgHeader{}, nil
case msgTime:
var ms int64
var mus int
var as int64
var aus int
n, err := fmt.Fscanf(s.remReader, "%d %d %d %d\n", &ms, &mus, &as, &aus)
if err != nil {
return nil, fmt.Errorf("failed to read scp time message header: %w", err)
}
if n != 4 {
return nil, fmt.Errorf("unexpected count in reading time message header: n=%d", 3)
}
err = s.WriteReplyOK()
if err != nil {
return nil, fmt.Errorf("failed to write scp replyOK reply: %w", err)
}
h := timeMsgHeader{
Mtime: fromSecondsAndMicroseconds(ms, mus),
Atime: fromSecondsAndMicroseconds(as, aus),
}
return h, nil
case replyOK:
return okMsg{}, nil
case replyError, replyFatalError:
line, err := s.remReader.ReadString('\n')
if err != nil {
return nil, fmt.Errorf("failed to read scp reply error message: %w", err)
}
if b == replyError {
err = s.WriteReplyOK()
if err != nil {
return nil, fmt.Errorf("failed to write scp replyOK reply: %w", err)
}
}
return nil, &protocolError{
msg: line,
fatal: b == replyFatalError,
}
default:
return nil, fmt.Errorf("invalid scp message type: %v", b)
}
}
func (s *sinkProtocol) CopyFileBodyTo(h fileMsgHeader, w io.Writer) error {
lr := io.LimitReader(s.remReader, h.Size)
n, err := io.Copy(w, lr)
if err == io.EOF {
if n != h.Size {
return fmt.Errorf("unexpected EOF in CopyFileBodyTo: %w", err)
}
} else if err != nil {
return fmt.Errorf("failed to write copy file body: %w", err)
}
err = s.WriteReplyOK()
if err != nil {
return fmt.Errorf("failed to write scp replyOK reply: %w", err)
}
return nil
}
func (s *sinkProtocol) WriteReplyOK() error {
_, err := s.remIn.Write([]byte{replyOK})
return err
}