-
Notifications
You must be signed in to change notification settings - Fork 0
/
smb2.go
274 lines (228 loc) · 6.99 KB
/
smb2.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
package rippers
import (
"fmt"
"io"
"log"
"net"
"net/url"
"os"
"path"
"strings"
"github.com/cyops-se/safe-import/si-outer/types"
"github.com/hirochachacha/go-smb2"
)
type SmbContext struct {
Context
processed map[string]bool
Url *url.URL
rfs *smb2.RemoteFileSystem
}
func CreateSmbContext(job *types.Job, repo *types.Repository) *SmbContext {
context := &SmbContext{}
context.Job = job
context.processed = make(map[string]bool, 1)
context.Url, _ = url.Parse(repo.URL)
context.Repository = repo
return context
}
func (context *SmbContext) DownloadDirectoryCifs() error {
job := context.Job
outerpath := context.Repository.OuterPath
if len(strings.TrimSpace(outerpath)) == 0 {
if u, _ := url.Parse(context.Repository.URL); u != nil {
outerpath = u.RequestURI()
} else {
outerpath = "."
}
}
context.MkdirIfNotExists(outerpath)
log.Printf("Downloading files from '%s' to '%s'\n", context.Repository.URL, outerpath)
conn, err := net.Dial("tcp", context.Url.Host+":445")
if err != nil {
// fmt.Println("DownloadDirectoryCifs net.Dial ERROR:", err)
return err
}
defer conn.Close()
di := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: context.Repository.Username,
Password: context.Repository.Password,
},
}
di.Negotiator.RequireMessageSigning = false
c, err := di.Dial(conn)
if err != nil {
// fmt.Println("DownloadDirectoryCifs di.Dial ERROR:", err)
return err
}
defer c.Logoff()
share := strings.ReplaceAll(context.Url.Path, "/", `\`)
share = fmt.Sprintf(`\\%s%s`, context.Url.Host, share)
// fmt.Println("c.Mount(" + share + ")")
rfs, err := c.Mount(share)
if err != nil {
// fmt.Println("DownloadDirectoryCifs c.Mount ERROR:", err)
return err
}
defer rfs.Umount()
context.rfs = rfs
// DownloadDirCifs(p, ".", rfs, job)
// save()
// tree = &Folder{Files: make(map[string]*File)}
context.RemoteTree = &Folder{Name: "/"}
err = context.buildTreeFromURL("", context.RemoteTree)
if err != nil {
// fmt.Println("DownloadDirectoryCifs buildTreeFromURL ERROR:", err)
return err
}
err = context.CheckDestinationPath(context.RemoteTree)
if err != nil {
// fmt.Println("DownloadDirectoryCifs CheckDestinationPath ERROR:", err)
return err
}
// fmt.Println("Copying missing or modified files: ", context.Files)
for f := context.Files.Front(); f != nil; f = f.Next() {
select {
case job.Command = <-job.Commands:
err := fmt.Errorf("Job aborted by command: %d", job.Command)
// fmt.Println("DownloadDirectoryCifs aborted ERROR:", err)
return err
default:
file := f.Value.(*MissingFile)
// fmt.Println("Processing file: ", file.Fullname)
context.copyFile(file)
}
if job.Progress.Error != nil {
return job.Progress.Error
}
}
return nil
}
func (context *SmbContext) buildTreeFromURL(urlstr string, folder *Folder) error {
job := context.Job
urlstr = strings.ReplaceAll(urlstr, "/", `\`)
if job.Command > 0 {
return nil
}
select {
case job.Command = <-job.Commands:
err := fmt.Errorf("Job aborted by command: %d", job.Command)
// fmt.Println("buildTreeFromURL aborted:", err)
return err
default:
files, err := context.rfs.ReadDir(urlstr)
if err == nil {
for _, f := range files {
href := f.Name()
if f.IsDir() {
if _, ok := context.processed[href]; !ok {
if folder.Folders == nil {
folder.Folders = make(map[string]*Folder, 1)
}
child := &Folder{Name: href}
folder.Folders[child.Name] = child
context.processed[href] = true
// // fmt.Println("Folder", child.Name, "added to folder", folder.Name)
if err := context.buildTreeFromURL(path.Join(urlstr, href), child); err != nil {
job.Progress.Error = err
job.Progress.ErrorMessage = err.Error()
return err
}
}
} else {
if _, ok := context.processed[href]; !ok {
if folder.Files == nil {
folder.Files = make(map[string]*File, 1)
}
context.processed[href] = true
// // fmt.Println("File", href, "added to folder", folder.Name)
folder.Files[href] = &File{Name: href, Size: f.Size()}
}
}
}
}
if job.Progress.Error != nil {
err = job.Progress.Error
}
return err
}
}
func (context *SmbContext) copyFile(file *MissingFile) error {
job := context.Job
target := path.Join(context.Repository.OuterPath, file.Fullname)
// // fmt.Println("Copying file, from:", file.Fullname, ", to:", target)
file.Fullname = strings.TrimLeft(file.Fullname, `/\`)
file.Fullname = strings.ReplaceAll(file.Fullname, "/", `\`)
d, err := context.rfs.Open(file.Fullname)
if err == nil {
defer d.Close()
context.MkdirIfNotExists(path.Dir(target))
out, err := os.Create(target)
if err != nil {
// fmt.Println("copyFile os.Create ERROR:", err)
return err
}
defer out.Close()
done := make(chan int64)
job.Progress.CurrentPath = file.Fullname
job.Progress.Current.Percent = 0.0
job.Progress.Current.Total = int64(file.File.Size)
go context.PrintDownloadPercent(done)
n, err := io.Copy(out, d)
out.Close()
done <- n
// fmt.Println("copyFile io.Copy ERROR:", err)
} else {
// fmt.Println("copyFile rfs.Open ERROR:", err)
}
return err
}
/*
// DownloadDirCifs downloads the directory
func DownloadDirCifs(base string, startDir string, rfs *smb2.RemoteFileSystem, job *types.JobRequest) {
b := strings.ReplaceAll(base, `\\`, ``)
b = strings.ReplaceAll(b, `\`, `/`)
files, err := rfs.ReadDir(strings.ReplaceAll(startDir, `/`, `\`))
if err == nil {
for _, f := range files {
if f.IsDir() {
createDir(path.Join(b, startDir, f.Name()))
tree.Files[path.Join(b, startDir, f.Name())] = &File{}
compareTree.Files[path.Join(b, startDir, f.Name())] = tree.Files[path.Join(b, startDir, f.Name())]
DownloadDirCifs(base, path.Join(startDir, f.Name()), rfs, job)
} else {
p := strings.ReplaceAll(path.Join(startDir, f.Name()), `/`, `\`)
d, err := rfs.Open(p)
if err == nil {
defer d.Close()
size := f.Size()
tree.Files[path.Join(b, startDir, f.Name())] = &File{Name: f.Name(), Date: f.ModTime(), Bytes: size}
if compareTree.Files[path.Join(b, startDir, f.Name())] == nil || !compareTree.Files[path.Join(b, startDir, f.Name())].equals(tree.Files[path.Join(b, startDir, f.Name())]) {
compareTree.Files[path.Join(b, startDir, f.Name())] = tree.Files[path.Join(b, startDir, f.Name())]
out, err := os.Create(path.Join(b, startDir, f.Name()))
if err != nil {
// fmt.Println(path.Join(b, startDir, f.Name()))
panic(err)
}
defer out.Close()
done := make(chan int64)
job.Progress.CurrentPath = path.Join(b, startDir, f.Name())
job.Progress.Current.Percent = 0.0
job.Progress.Current.Total = int64(size)
go printDownloadPercent(done, job)
n, err := io.Copy(out, d)
job.ReportError(err, "DownloadDirCifs:io.Copy()")
// fmt.Println(n, "bytes copied to file")
out.Close()
done <- n
}
} else {
// fmt.Println(p, "error")
}
}
}
} else {
// fmt.Println(startDir, "error")
}
}
*/