-
Notifications
You must be signed in to change notification settings - Fork 8
/
nzbdirectsearch.go
432 lines (408 loc) · 12.5 KB
/
nzbdirectsearch.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
package main
import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"html"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/Tensai75/nntpPool"
"github.com/Tensai75/nzbparser"
"github.com/Tensai75/subjectparser"
progressbar "github.com/schollz/progressbar/v3"
)
var directsearchHits = make(map[string]map[string]nzbparser.NzbFile)
var directsearchCounter uint64
var startDate int64
var endDate int64
var mutex = sync.Mutex{}
func nzbdirectsearch(engine SearchEngine, name string) error {
if len(results) > 0 && conf.Directsearch.Skip {
Log.Info("Results already available. Skipping search based on config settings.")
return nil
}
if conf.Directsearch.Username == "" || conf.Directsearch.Password == "" {
return fmt.Errorf("No or incomplete credentials for usenet server")
}
if len(args.Groups) == 0 {
return fmt.Errorf("No groups provided")
}
if args.UnixDate == 0 {
return fmt.Errorf("No date provided")
}
if conf.Directsearch.Connections == 0 {
conf.Directsearch.Connections = 20
}
if conf.Directsearch.Hours == 0 {
conf.Directsearch.Hours = 12
}
if conf.Directsearch.Scans == 0 {
conf.Directsearch.Scans = 50
}
if conf.Directsearch.Step == 0 {
conf.Directsearch.Step = 20000
}
var searchInGroupError error
if err := initNntpPool(); err != nil {
return err
} else {
defer pool.Close()
}
for i, group := range args.Groups {
if i > 0 && conf.Directsearch.FirstGroupOnly && searchInGroupError == nil {
fmt.Println()
Log.Info("Skipping other groups based on config settings.")
return nil
}
fmt.Println()
Log.Info("Searching in group '%s' ...", group)
searchInGroupError = nil
if searchInGroupError = searchInGroup(group); searchInGroupError != nil {
Log.Error(searchInGroupError.Error())
} else {
if len(directsearchHits) > 0 {
for _, hit := range directsearchHits {
var nzb = &nzbparser.Nzb{}
for _, files := range hit {
nzb.Files = append(nzb.Files, files)
}
nzbparser.MakeUnique(nzb)
nzbparser.ScanNzbFile(nzb)
sort.Sort(nzb.Files)
for id := range nzb.Files {
sort.Sort(nzb.Files[id].Segments)
}
processResult(nzb, name)
}
} else {
Log.Warn("No result found in group '%s'", group)
}
}
}
return nil
}
func searchInGroup(group string) error {
var searchesWG sync.WaitGroup
var searchesGuard = make(chan struct{}, conf.Directsearch.Scans)
defer close(searchesGuard)
var searchesErrorChannel = make(chan error, 1)
defer close(searchesErrorChannel)
var searchesCtx, searchesCancel = context.WithCancel(context.Background())
defer searchesCancel() // Make sure it's called to release resources even if no errors
var step = conf.Directsearch.Step
startDate = args.UnixDate - int64(conf.Directsearch.Hours*60*60)
endDate = args.UnixDate + int64(60*60*conf.Directsearch.ForwardHours)
if !args.IsTimestamp {
endDate += 60 * 60 * 24
}
var currentMessageID, lastMessageID int
var messageDate time.Time
var err error
Log.Info("Scanning from %s to %s", time.Unix(startDate, 0).Format("02.01.2006 15:04:05 MST"), time.Unix(endDate, 0).Format("02.01.2006 15:04:05 MST"))
currentMessageID, messageDate, err = getFirstMessageNumberFromGroup(group, startDate, searchesCtx)
if err != nil {
return fmt.Errorf("Error while scanning group '%s' for the first message: %v\n", group, err)
}
Log.Info("Found first message number: %v / Date: %v", currentMessageID, messageDate.Local().Format("02.01.2006 15:04:05 MST"))
lastMessageID, messageDate, err = getLastMessageNumberFromGroup(group, endDate, searchesCtx)
if err != nil {
return fmt.Errorf("Error while scanning group '%s' for the last message: %v\n", group, err)
}
if currentMessageID >= lastMessageID {
return errors.New("no messages found within search range")
}
Log.Info("Found last message number: %v / Date: %v", lastMessageID, messageDate.Local().Format("02.01.2006 15:04:05 MST"))
Log.Info("Scanning messages %v to %v", currentMessageID, lastMessageID)
directsearchCounter = 0
bar := progressbar.NewOptions(lastMessageID-currentMessageID,
progressbar.OptionSetDescription(" Scanning messages ... "),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionThrottle(time.Millisecond*100),
progressbar.OptionShowElapsedTimeOnFinish(),
)
go func(bar *progressbar.ProgressBar, ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
bar.Set(int(atomic.LoadUint64(&directsearchCounter)))
}
}
}(bar, searchesCtx)
for currentMessageID <= lastMessageID {
var lastMessage int
if currentMessageID+step > lastMessageID {
lastMessage = lastMessageID
} else {
lastMessage = currentMessageID + step
}
searchesGuard <- struct{}{} // will block if guard channel is already filled
searchesWG.Add(1)
go func(ctx context.Context, currentMessageID int, lastMessage int, group string) {
defer func() {
searchesWG.Done()
<-searchesGuard
}()
if err := searchMessages(ctx, currentMessageID, lastMessage, group); err != nil {
select {
case searchesErrorChannel <- err:
default:
}
searchesCancel()
return
}
}(searchesCtx, currentMessageID, lastMessage, group)
// update currentMessageID for next request
currentMessageID = lastMessage + 1
}
searchesWG.Wait()
if searchesCtx.Err() != nil {
fmt.Println()
return <-searchesErrorChannel
}
searchesCancel()
bar.Finish()
fmt.Println()
return nil
}
func searchMessages(ctx context.Context, firstMessage int, lastMessage int, group string) error {
select {
case <-ctx.Done():
return nil // Error somewhere, terminate
default: // required, otherwise it will block
}
conn, firstMessageID, lastMessageID, err := switchToGroup(group, ctx)
if err != nil {
return err
}
defer pool.Put(conn)
if firstMessage < firstMessageID {
firstMessage = firstMessageID
}
if lastMessage > lastMessageID {
lastMessage = lastMessageID
}
select {
case <-ctx.Done():
return nil // Error somewhere, terminate
default: // required, otherwise it will block
}
results, err := conn.Overview(firstMessage, lastMessage)
if err != nil {
return fmt.Errorf("Error retrieving message overview from the usenet server while searching in group '%s': %v\n", group, err)
}
for _, overview := range results {
select {
case <-ctx.Done():
return nil // Error somewhere, terminate
default: // required, otherwise it will block
}
currentDate := overview.Date.Unix()
if currentDate >= endDate {
return nil
}
subject := html.UnescapeString(strings.ToValidUTF8(overview.Subject, ""))
if strings.Contains(strings.ToLower(subject), strings.ToLower(args.Header)) {
if subject, err := subjectparser.Parse(subject); err == nil {
var date int64
if date = overview.Date.Unix(); date < 0 {
date = 0
}
poster := strings.ToValidUTF8(overview.From, "")
// make hashes
headerHash := GetMD5Hash(subject.Header + poster + strconv.Itoa(subject.TotalFiles))
fileHash := GetMD5Hash(headerHash + subject.Filename + strconv.Itoa(subject.File) + strconv.Itoa(subject.TotalSegments))
mutex.Lock()
if _, ok := directsearchHits[headerHash]; !ok {
directsearchHits[headerHash] = make(map[string]nzbparser.NzbFile)
}
if _, ok := directsearchHits[headerHash][fileHash]; !ok {
directsearchHits[headerHash][fileHash] = nzbparser.NzbFile{
Groups: []string{group},
Subject: subject.Subject,
Poster: poster,
Number: subject.File,
Filename: subject.Filename,
Basefilename: subject.Basefilename,
}
}
file := directsearchHits[headerHash][fileHash]
if file.Groups[len(file.Groups)-1] != group {
file.Groups = append(file.Groups, html.EscapeString(group))
}
if subject.Segment == 1 {
file.Subject = subject.Subject
}
if int(date) > file.Date {
file.Date = int(date)
}
file.Segments = append(file.Segments, nzbparser.NzbSegment{
Number: subject.Segment,
Id: html.EscapeString(strings.Trim(overview.MessageId, "<>")),
Bytes: overview.Bytes,
})
directsearchHits[headerHash][fileHash] = file
mutex.Unlock()
}
}
atomic.AddUint64(&directsearchCounter, 1)
}
return nil
}
func switchToGroup(group string, ctx context.Context) (*nntpPool.NNTPConn, int, int, error) {
conn, err := pool.Get(ctx)
if err != nil {
return nil, 0, 0, fmt.Errorf("Error connecting to the usenet server: %v", err)
}
_, firstMessageID, lastMessageID, err := conn.Group(group)
if err != nil {
return nil, 0, 0, fmt.Errorf("Error retrieving group information for group '%s' from the usenet server: %v\n", group, err)
}
return conn, firstMessageID, lastMessageID, nil
}
func GetMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}
func getFirstMessageNumberFromGroup(group string, startDate int64, ctx context.Context) (int, time.Time, error) {
var firstMessageNumber, lastMessageNumber, overviewStart, overviewEnd int
var lastStep bool
conn, err := pool.Get(ctx)
defer pool.Put(conn)
if err != nil {
return 0, time.Time{}, err
}
_, firstMessageNumber, lastMessageNumber, err = conn.Group(group)
if err != nil {
return 0, time.Time{}, err
}
low := firstMessageNumber
high := lastMessageNumber
step := 1000
bar := progressbar.NewOptions(calcSteps(firstMessageNumber, lastMessageNumber, step),
progressbar.OptionSetDescription(" Scanning for first message number ..."),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionThrottle(time.Millisecond*100),
progressbar.OptionShowElapsedTimeOnFinish(),
)
defer func() {
bar.Finish()
fmt.Println()
}()
for low <= high {
difference := high - low
if difference < step {
overviewStart = low
overviewEnd = high
lastStep = true
} else {
overviewStart = low + (difference / 2)
overviewEnd = overviewStart + step
}
results, err := conn.Overview(overviewStart, overviewEnd)
if err != nil {
return 0, time.Time{}, err
}
bar.Add(1)
for i, result := range results {
if i == 0 && result.Date.Unix() > startDate {
high = result.MessageNumber
break
}
if result.Date.Unix() >= startDate {
return result.MessageNumber, result.Date, nil
}
if i == len(results)-1 {
low = result.MessageNumber
}
}
if lastStep {
if low == firstMessageNumber {
return results[0].MessageNumber, results[0].Date, nil
} else {
return 0, time.Time{}, fmt.Errorf("start date of search range is newer than latest message of this group")
}
}
}
return 0, time.Time{}, fmt.Errorf("unknown error which should not happen...??")
}
func getLastMessageNumberFromGroup(group string, endDate int64, ctx context.Context) (int, time.Time, error) {
var firstMessageNumber, lastMessageNumber, overviewStart, overviewEnd int
var lastStep bool
conn, err := pool.Get(ctx)
defer pool.Put(conn)
if err != nil {
return 0, time.Time{}, err
}
_, firstMessageNumber, lastMessageNumber, err = conn.Group(group)
if err != nil {
return 0, time.Time{}, err
}
low := firstMessageNumber
high := lastMessageNumber
step := 1000
bar := progressbar.NewOptions(calcSteps(firstMessageNumber, lastMessageNumber, step),
progressbar.OptionSetDescription(" Scanning for last message number ... "),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionThrottle(time.Millisecond*100),
progressbar.OptionShowElapsedTimeOnFinish(),
)
defer func() {
bar.Finish()
fmt.Println()
}()
for low <= high {
difference := high - low
if difference < step {
overviewStart = low
overviewEnd = high
lastStep = true
} else {
overviewStart = low + (difference / 2)
overviewEnd = overviewStart + step
}
results, err := conn.Overview(overviewStart, overviewEnd)
if err != nil {
return 0, time.Time{}, err
}
bar.Add(1)
for i, result := range results {
if i == 0 && result.Date.Unix() > endDate {
high = result.MessageNumber
break
}
if result.Date.Unix() >= endDate {
return result.MessageNumber, result.Date, nil
}
if i == len(results)-1 {
low = result.MessageNumber
}
}
if lastStep {
if high == lastMessageNumber {
return results[len(results)-1].MessageNumber, results[len(results)-1].Date, nil
} else {
return 0, time.Time{}, fmt.Errorf("end date of search range is older than oldest message of this group")
}
}
}
return 0, time.Time{}, fmt.Errorf("unknown error which should not happen...??")
}
func calcSteps(low int, high int, step int) (steps int) {
mid := (high - low) / 2
for i := 1; i > 0; i++ {
if mid < step {
return i
}
mid = (mid - low) / 2
}
return 0
}