-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol.go
197 lines (153 loc) · 3.73 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
package scp
import (
"bufio"
"errors"
"fmt"
"github.com/alessio/shellescape"
"golang.org/x/crypto/ssh"
"io"
"os"
"strconv"
"strings"
)
type RemoteClient struct {
session *ssh.Session
stdout io.Reader
stdin io.WriteCloser
verbose bool
options *Options
}
func (o *Options) NewClient(session *ssh.Session) (*RemoteClient, error) {
stdout, err := session.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("cannot catch stdout: %w", err)
}
stdin, err := session.StdinPipe()
if err != nil {
return nil, fmt.Errorf("cannot set stdin: %w", err)
}
return &RemoteClient{options: o.applyDefault(), session: session, stdout: stdout, stdin: stdin}, nil
}
func NewClient(session *ssh.Session) (*RemoteClient, error) {
return DefaultOptions.NewClient(session)
}
func (c *RemoteClient) Start(filename string, isDirectory bool) error {
commandBuilder := strings.Builder{}
commandBuilder.WriteString(c.options.scpPath + " -t")
if isDirectory {
commandBuilder.WriteString(" -r")
}
if c.verbose {
commandBuilder.WriteString(" -v")
// just for debug
stderr, err := c.session.StderrPipe()
if err != nil {
fmt.Println("Cannot connect to stderr")
} else {
f, err := os.Create("protocol.log")
if err != nil {
fmt.Println("Cannot create test file")
} else {
go func() {
buffer := make([]byte, 1024)
for {
read, _ := stderr.Read(buffer)
if read != 0 {
_, err := f.Write(buffer[:read])
if err != nil {
fmt.Println("Cannot write verbose info")
}
}
}
}()
}
}
}
commandBuilder.WriteByte(' ')
commandBuilder.WriteString(shellescape.Quote(filename))
command := commandBuilder.String()
err := c.session.Start(command)
if err != nil {
return err
}
return c.checkResponse()
}
func (c *RemoteClient) checkResponse() error {
buffer := make([]byte, 1)
_, err := c.stdout.Read(buffer)
if err != nil {
return fmt.Errorf("cannot read from remote stdout: %w", err)
}
if buffer[0] == 0 {
return nil
}
errorMessageBuilder := strings.Builder{}
if buffer[0] == 1 {
errorMessageBuilder.WriteString("Error (1): ")
} else if buffer[0] == 2 {
errorMessageBuilder.WriteString("Fatal (2): ")
} else {
errorMessageBuilder.WriteString("UnknownError (")
errorMessageBuilder.WriteString(strconv.Itoa(int(buffer[0])))
errorMessageBuilder.WriteString("): ")
}
reader := bufio.NewReader(c.stdout)
all, err := reader.ReadString('\n')
if err != nil {
errorMessageBuilder.WriteString("[ERROR during get reason] (error: ")
errorMessageBuilder.WriteString(err.Error())
errorMessageBuilder.WriteString(")")
} else {
errorMessageBuilder.WriteString(all)
}
return errors.New(errorMessageBuilder.String())
}
func (c *RemoteClient) WriteFile(perm string, size int64, filename string, data io.Reader) error {
_, err := fmt.Fprintln(c.stdin, "C"+perm, size, filename)
if err != nil {
return err
}
err = c.checkResponse()
if err != nil {
return err
}
_, err = io.Copy(c.stdin, data)
if err != nil {
return err
}
_, err = c.stdin.Write([]byte{0})
if err != nil {
return err
}
err = c.checkResponse()
if err != nil {
return err
}
return nil
}
func (c *RemoteClient) WriteDirectoryStart(perm string, filename string) error {
if len(filename) > 1024 {
// Unknown reason, just as same as https://github.com/openssh/openssh-portable/blob/master/scp.c#L1204
filename = filename[:1024]
}
_, err := fmt.Fprintln(c.stdin, "D"+perm, 0, filename)
if err != nil {
return err
}
err = c.checkResponse()
if err != nil {
return err
}
return nil
}
func (c *RemoteClient) WriteDirectoryEnd() error {
_, err := fmt.Fprintln(c.stdin, "E")
if err != nil {
return err
}
err = c.checkResponse()
if err != nil {
return err
}
return nil
}