-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
350 lines (295 loc) · 7.53 KB
/
client.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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"github.com/BurntSushi/toml" // config as Tom's Obvious, Minimal Language
"github.com/chrisfarms/yenc"
// "gopkg.in/yenc.v0"
// decode yenc
)
type Config struct {
Address string
Port string
Secure string
Username string
Password string
Connections int
}
func loadConfig() (conf Config, err error) {
b, err := ioutil.ReadFile("client.conf") // just pass the file name
if err != nil {
fmt.Print(err)
return
}
str := string(b) // convert content to a 'string'
_, err = toml.Decode(str, &conf)
if err != nil {
// handle error
return
}
return
}
/*
workers take a connection c and a job j from respective pools,
fetch segment, and send segment to results channel where it's read by the download function
*/
func worker(id int, jobs <-chan Segment, con <-chan *tls.Conn, results chan<- Segment) {
for c := range con {
for j := range jobs {
j.Connection = c
segment, err := fetchSegment(j)
if err != nil {
fmt.Print(err)
}
results <- segment
}
}
}
/*
write yenc file to disk, decode, append binary data
*/
func write(segment Segment) {
err := ioutil.WriteFile("test.yenc", segment.Data, 0644)
if err != nil {
fmt.Print(err)
return
}
d, err := os.Open("test.yenc")
if err != nil {
fmt.Print(err)
return
}
part, err := yenc.Decode(d)
if err != nil {
panic("decoding: " + err.Error())
}
fmt.Println("Decoded: Filename", part.Name)
// fmt.Println("Body Bytes", part.Body)
// var b bytes.Buffer
// write yenc to disk
/* err := ioutil.WriteFile("test.yenc", segment.Data, 0644)
if err != nil {
fmt.Print(err)
return
}
dec, err := os.Open("test.yenc")
if err != nil {
fmt.Print(err)
return
}
out, err := os.OpenFile(yread.Filename, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
fmt.Println("open " + yread.Filename + " Failed")
return
}
fmt.Println("open " + yread.Filename + " SUCCESS")
_, err = io.Copy(out, yread)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println("Successful write part: " + yread.Filename)
*/
// buf1, err := ioutil.ReadAll(yenc)
/***********************/
/* d, err := yenc.Decode(f) // , yenc.DecodeWithBufferSize(20)
if err != nil {
fmt.Print("error decoding segment")
return
// panic(err)
}
fmt.Println("Decoded: " + d.Header().Name)
_, err = io.Copy(&b, d)
if err != nil {
fmt.Print("error copying to memory")
return
}
fmt.Print("Copied decoded to memory")
*/
/*
_, err = io.Copy(&b, d)
if err != nil {
fmt.Print(err)
return
}
fmt.Print("Copied decoded to memory")
if _, err := os.Stat(d.Header().Name); os.IsNotExist(err) {
_, err := os.Create(d.Header().Name)
if err != nil {
panic(err)
}
}
out, err := os.OpenFile(d.Header().Name, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
fmt.Println("Opened file for writing: " + d.Header().Name)
_, err = b.WriteTo(out)
if err != nil {
// Handle the error
fmt.Println("Failed to write buffer to file:", err)
return
}
fmt.Println("Buffer contents written to file successfully.")
*/
/*
part, err := yenc.Decode(f)
if err != nil {
fmt.Print(err)
}
fmt.Println("Successful Decode: " + string(part.Header().Name))
// write decoded part to disk
// if file does not exist, create it
if _, err := os.Stat(string(part.Header().Name)); os.IsNotExist(err) {
_, err := os.Create(string(part.Header().Name))
if err != nil {
panic(err)
}
}
// open file
out, err := os.OpenFile(string(part.Header().Name), os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
out.Write(part.)
fmt.Print("Written") */
}
func sanitizeFilename(filename string) string {
// Define a list of invalid characters
invalidChars := []string{"\\", "/", ":", "*", "?", "\"", "<", ">", "|"}
// Replace invalid characters with underscores
for _, char := range invalidChars {
filename = strings.ReplaceAll(filename, char, "_")
}
// Remove whitespace from the filename
filename = strings.TrimSpace(filename)
return filename
}
/*
manage the download of files and segments contained in a single nzb file
*/
func download(nzb *Nzb, fileBegin int, segmentBegin int, connections chan *tls.Conn, maxWorkers int) {
jobs := make(chan Segment, 200)
results := make(chan Segment, 100)
for w := 1; w <= maxWorkers; w++ { // 3 connections
go worker(w, jobs, connections, results)
}
// for each file in nzb
for i := fileBegin; i < len(nzb.Files); i++ {
// create map to keep track of out-of-order segments
segmentMap := make(map[int]Segment)
var expected = 1
// for each segment
fmt.Println("Working on new File: " + nzb.Files[i].Subject)
// add each segment to jobs pool
for j := segmentBegin; j < len(nzb.Files[i].Segments); j++ {
jobs <- Segment{nzb.Files[i].Segments[j], nil, nil, nzb.Files[i].Groups}
}
for {
segment := <-results
size := len(segment.Data)
fmt.Println("Got segment: " + segment.Article.Id + " Article expected size (bytes): " + strconv.Itoa(segment.Article.Bytes))
fmt.Printf("Actual Size of segment.Data: %d\n", size)
if segment.Article.Number == expected {
fmt.Println("Segment " + strconv.Itoa(expected) + " expected, writing to disk")
write(segment)
expected++
// write segments stored in memory:
for expected < len(nzb.Files[i].Segments)+1 {
fmt.Println("Checking memory")
j := segmentMap[expected]
// if next segment not found in memory
if j.Article.Number == 0 {
fmt.Println("next segment not found in memory")
break
}
// if found, write to disk
fmt.Println("Found segment " + strconv.Itoa(expected) + " in memory, writing")
write(j)
delete(segmentMap, expected)
expected++
}
// check if this is last segment
if expected > len(nzb.Files[i].Segments) {
fmt.Println("This is last segment")
break
}
continue
}
fmt.Println("Segment " + strconv.Itoa(expected) + " unexpected, saving to map")
segmentMap[segment.Article.Number] = segment
}
}
close(jobs)
fmt.Println("Download Complete!")
}
func manager() {
/*
load config and define parameters
*/
config, err := loadConfig()
if err != nil {
fmt.Print("Error parsing config")
return
}
maxConnections := config.Connections
fmt.Print("Max Connections: " + strconv.Itoa(maxConnections) + "\n")
/*
make job pool and send maxConnections into pool to be multiplexed by workers
*/
connections := make(chan *tls.Conn, 20)
for c := 1; c <= maxConnections; c++ {
connections <- connect(config)
}
/*
load NZB file(s) from disk
*/
fmt.Print("Loading next nzb file...")
b, err := ioutil.ReadFile("test.nzb") // just pass the file name
if err != nil {
panic(err)
}
fmt.Println("Successfully Opened test.nzb")
nzb, err := NewString(string(b)) // marshal, returning pointer to nzb object
if err != nil {
panic(err)
}
/*
call download for each NZB opened
*/
go download(nzb, 0, 0, connections, maxConnections)
}
func main() {
log.SetFlags(log.Lshortfile)
scanner := bufio.NewScanner(os.Stdin)
go manager()
for scanner.Scan() {
text := scanner.Text()
tokens := strings.Fields(text)
if tokens[0] == "/pause" {
fmt.Print("Pausing all downloads \n")
}
}
if scanner.Err() != nil {
// handle error.
}
// test decode
/* f, err := os.Open("test.yenc")
if err != nil {
fmt.Print(err)
return
}
d, err := yenc.Decode(f)
if err != nil {
fmt.Print(err)
}
fmt.Println("Successful Decode: " + d.Header().Name)
*/
}