-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp.go
442 lines (373 loc) · 10 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
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
// Copyright (c) 2019 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
/*
Package scplib is a library for exchanging data with scp in golang.
*/
package scplib
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"regexp"
"strconv"
"strings"
"golang.org/x/crypto/ssh"
)
// TODO(blacknon): bufferを可能な限り使わないようにして、io.Pipeなどを利用したReader/Writerでの対応を主流とする
// SCPClient save credentials and use scp from method.
type SCPClient struct {
Connection *ssh.Client
Session *ssh.Session
Permission bool
}
func unset(s []string, i int) []string {
if i >= len(s) {
return s
}
return append(s[:i], s[i+1:]...)
}
func getFullPath(path string) (fullPath string) {
usr, _ := user.Current()
fullPath = strings.Replace(path, "~", usr.HomeDir, 1)
fullPath, _ = filepath.Abs(fullPath)
return fullPath
}
// TODO(blacknon): walker用の関数もあるので、こういうのを使うほうがいい??
// https://godoc.org/github.com/kr/fs
func walkDir(dir string) (files []string, err error) {
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
path = path + "/"
}
files = append(files, path)
return nil
})
return
}
// pushDirData is Write directory data to remote.
func pushDirData(w io.WriteCloser, baseDir string, paths []string, toName string, perm bool) {
baseDirSlice := strings.Split(baseDir, "/")
baseDirSlice = unset(baseDirSlice, len(baseDirSlice)-1)
baseDir = strings.Join(baseDirSlice, "/")
for _, path := range paths {
relPath, _ := filepath.Rel(baseDir, path)
dir := filepath.Dir(relPath)
if len(dir) > 0 && dir != "." {
dirList := strings.Split(dir, "/")
dirpath := baseDir
for _, dirName := range dirList {
dirpath = dirpath + "/" + dirName
dInfo, _ := os.Lstat(dirpath)
dPerm := fmt.Sprintf("%04o", dInfo.Mode().Perm())
// push directory information
fmt.Fprintln(w, "D"+dPerm, 0, dirName)
}
}
fInfo, _ := os.Lstat(path)
if !fInfo.IsDir() {
// check symlink
if fInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
fmt.Fprintf(os.Stderr, "'%v' is Symlink, Do not copy.\n", path)
} else {
pushFileData(w, []string{path}, toName, perm)
}
}
if len(dir) > 0 && dir != "." {
dirList := strings.Split(dir, "/")
endStr := strings.Repeat("E\n", len(dirList))
fmt.Fprintf(w, endStr)
}
}
return
}
// pushFileData is exchange local file data, to scp format
func pushFileData(w io.WriteCloser, paths []string, toName string, perm bool) {
for _, path := range paths {
fInfo, _ := os.Lstat(path)
content, err := os.Open(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
stat, _ := content.Stat()
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
// default permission(0644)
fPerm := "0644"
if perm == true {
fPerm = fmt.Sprintf("%04o", fInfo.Mode())
}
// push file information
fmt.Fprintln(w, "C"+fPerm, stat.Size(), toName)
io.Copy(w, content)
fmt.Fprint(w, "\x00")
}
return
}
// writeData is write to local file, from scp data.
// TODO(blacknon): Bufferで処理すると、どうしても速度が遅くなったりするので対策を考える
func writeData(data *bufio.Reader, path string, perm bool) {
pwd := path
checkloop:
for {
// Get file or dir information (1st line)
line, err := data.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
fmt.Println(err)
}
line = strings.TrimRight(line, "\n")
if line == "E" {
pwdArray := strings.Split(pwd, "/")
if len(pwdArray) > 0 {
pwdArray = pwdArray[:len(pwdArray)-2]
}
pwd = strings.Join(pwdArray, "/") + "/"
continue
}
lineSlice := strings.SplitN(line, " ", 3)
scpType := lineSlice[0][:1]
scpPerm := lineSlice[0][1:]
scpPerm32, _ := strconv.ParseUint(scpPerm, 8, 32)
scpSize, _ := strconv.Atoi(lineSlice[1])
scpObjName := lineSlice[2]
switch {
case scpType == "C":
scpPath := path
// Check pwd
check, _ := regexp.MatchString("/$", pwd)
if check || pwd != path {
scpPath = pwd + scpObjName
}
// set permission
if perm == false {
scpPerm32, _ = strconv.ParseUint("0644", 8, 32)
}
// 1st write to file
file, err := os.OpenFile(scpPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(uint32(scpPerm32)))
if err != nil {
fmt.Println(err)
break checkloop
}
file.WriteString("")
file.Close()
fileData := []byte{}
readedSize := 0
remainingSize := scpSize
outFile, _ := os.OpenFile(scpPath, os.O_WRONLY|os.O_APPEND, os.FileMode(uint32(scpPerm32)))
for {
readBuffer := make([]byte, remainingSize)
readSize, _ := data.Read(readBuffer)
remainingSize -= readSize
readedSize += readSize
// check readedSize
if readSize == 0 {
break
}
readBuffer = readBuffer[:readSize]
fileData = append(fileData, readBuffer...)
if readedSize == scpSize {
outFile.Write(fileData)
fileData = []byte{}
break
} else if len(fileData) >= 10485760 {
// write file over 10MB
outFile.Write(fileData)
fileData = []byte{}
}
}
// write file to path
os.Chmod(scpPath, os.FileMode(uint32(scpPerm32)))
// read last nUll character
_, _ = data.ReadByte()
case scpType == "D":
// Check pwd
check, _ := regexp.MatchString("/$", pwd)
if !check {
pwd = pwd + "/"
}
if perm == false {
scpPerm32, _ = strconv.ParseUint("0755", 8, 32)
}
pwd = pwd + scpObjName + "/"
err := os.Mkdir(pwd, os.FileMode(uint32(scpPerm32)))
if err != nil {
fmt.Println(err)
os.Chmod(pwd, os.FileMode(uint32(scpPerm32)))
}
default:
fmt.Fprintln(os.Stderr, line)
continue checkloop
// break checkloop
}
}
return
}
// GetFile get file data to file (remote to Local).
//
// example:
// scp.GetFile("/From/Remote/Path","/To/Local/Path")
func (s *SCPClient) GetFile(fromPaths []string, toPath string) (err error) {
session := s.Session
if s.Connection != nil {
session, err = s.Connection.NewSession()
if err != nil {
return
}
}
defer session.Close()
fin := make(chan bool)
go func() {
w, _ := session.StdinPipe()
defer w.Close()
// Null Characters(10,000 char)
nc := strings.Repeat("\x00", 100000)
fmt.Fprintf(w, nc)
}()
go func() {
r, _ := session.StdoutPipe()
b := bufio.NewReader(r)
writeData(b, toPath, s.Permission)
fin <- true
}()
// Create scp command
fromPathList := []string{}
for _, fromPath := range fromPaths {
fromPathList = append(fromPathList, fromPath)
}
fromPathString := strings.Join(fromPathList, " ")
// TODO(blacknon): scpしてる時点でセキュリティもクソもないのだが、OS Command Injectionへの対策を考える
scpCmd := "/usr/bin/scp -rf " + fromPathString
// Run scp
err = session.Run(scpCmd)
<-fin
return
}
// PutFile is put file to remote path.
//
// example:
// scp.PutFile("/From/Local/Path","/To/Remote/Path")
func (s *SCPClient) PutFile(fromPaths []string, toPath string) (err error) {
session := s.Session
if s.Connection != nil {
session, _ = s.Connection.NewSession()
if err != nil {
return
}
}
defer session.Close()
// Read Dir or File
go func() {
w, _ := session.StdinPipe()
defer w.Close()
for _, fromPath := range fromPaths {
// Get full path
fromPath = getFullPath(fromPath)
// File or Dir exits check
pInfo, err := os.Lstat(fromPath)
if err != nil {
return
}
if pInfo.IsDir() {
// Directory
pList, _ := walkDir(fromPath)
for _, i := range pList {
pushDirData(w, fromPath, []string{i}, filepath.Base(i), s.Permission)
}
} else {
// single files
toFile := filepath.Base(toPath)
if toFile == "." {
toFile = filepath.Base(fromPath)
}
pushFileData(w, []string{fromPath}, toFile, s.Permission)
}
}
}()
// Create scp command
// TODO(blacknon): scpしてる時点でセキュリティもクソもないのだが、OS Command Injectionへの対策を考える
scpCmd := "/usr/bin/scp -tr '" + toPath + "'"
if s.Permission == true {
scpCmd = "/usr/bin/scp -ptr '" + toPath + "'"
}
// Run scp
err = session.Run(scpCmd)
return
}
// GetData get and return scp format data(remote to local).
//
// example:
// scp.GetData("/path/remote/path")
func (s *SCPClient) GetData(fromPaths []string) (data *bytes.Buffer, err error) {
session := s.Session
if s.Connection != nil {
session, _ = s.Connection.NewSession()
if err != nil {
return
}
}
defer session.Close()
fin := make(chan bool)
go func() {
w, _ := session.StdinPipe()
defer w.Close()
// Null Characters(10,000 char)
nc := strings.Repeat("\x00", 100000)
fmt.Fprintf(w, nc)
}()
buf := new(bytes.Buffer)
go func() {
r, _ := session.StdoutPipe()
buf.ReadFrom(r)
fin <- true
}()
// Create scp command
fromPathList := []string{}
for _, fromPath := range fromPaths {
fromPathList = append(fromPathList, fromPath)
}
fromPathString := strings.Join(fromPathList, " ")
// TODO(blacknon): scpしてる時点でセキュリティもクソもないのだが、OS Command Injectionへの対策を考える
scpCmd := "/usr/bin/scp -fr " + fromPathString
// Run scp
err = session.Run(scpCmd)
<-fin
data = buf
return data, err
}
// PutData put data of scp format as a file(local to remote).
//
// example:
// scp.PutData(buffer(scp format data),"/path/remote/path")
func (s *SCPClient) PutData(fromData *bytes.Buffer, toPath string) (err error) {
session := s.Session
if s.Connection != nil {
session, _ = s.Connection.NewSession()
if err != nil {
return
}
}
defer session.Close()
// Read Dir or File
go func() {
w, _ := session.StdinPipe()
defer w.Close()
w.Write(fromData.Bytes())
}()
// Create scp command
// TODO(blacknon): scpしてる時点でセキュリティもクソもないのだが、OS Command Injectionへの対策を考える
scpCmd := "/usr/bin/scp -tr '" + toPath + "'"
if s.Permission == true {
scpCmd = "/usr/bin/scp -ptr '" + toPath + "'"
}
err = session.Run(scpCmd)
return
}