-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollect.go
More file actions
705 lines (617 loc) · 19.2 KB
/
collect.go
File metadata and controls
705 lines (617 loc) · 19.2 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
nostr "github.com/nbd-wtf/go-nostr"
)
// eventLine represents a relay list event for serialized JSONL writes
type eventLine struct {
id string
line string
}
// progressTracker tracks collection progress across goroutines
type progressTracker struct {
eventsReceived atomic.Int64
eventsWritten atomic.Int64
batchesTotal int
batchesDone atomic.Int64
relaysTotal int
}
func collectCmd(args []string) {
fs := flag.NewFlagSet("collect", flag.ExitOnError)
dataDir := commonFlags(fs)
pubkey := fs.String("pubkey", "", "your 64-hex pubkey to read kind-3 follows from")
relaysCSV := fs.String("relays", "wss://relay.damus.io,wss://nos.lol,wss://nostr.wine,wss://relay.snort.social,wss://wot.brainstorm.social,wss://profiles.nostr1.com", "comma-separated relay URLs to query for kind-10002")
followRelay := fs.String("follow-relay", "", "optional specific relay to query kind 3 (defaults to first in relays)")
batchSize := fs.Int("batch-size", 50, "number of authors per 10002 REQ batch")
timeoutSec := fs.Int("timeout", 12, "seconds to wait for REQ per relay/batch")
parallel := fs.Int("parallel", 4, "number of relays to query in parallel for 10002")
if err := fs.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, "failed to parse flags: %v\n", err)
os.Exit(1)
}
if *pubkey == "" || !isHex64(strings.ToLower(*pubkey)) {
fmt.Fprintln(os.Stderr, "--pubkey (64-hex) is required and must be valid hex")
os.Exit(1)
}
dataDirectory := *dataDir
if err := os.MkdirAll(dataDirectory, 0o755); err != nil {
fmt.Fprintf(os.Stderr, "failed to create data directory: %v\n", err)
os.Exit(1)
}
jsonlPath := filepath.Join(dataDirectory, "all_relay_lists.jsonl")
followsPath := filepath.Join(dataDirectory, "follows_list.txt")
userRelayListPath := filepath.Join(dataDirectory, "user_relay_list.txt")
userPubkeyPath := filepath.Join(dataDirectory, "user_pubkey.txt")
followSetsDir := filepath.Join(dataDirectory, "follow_sets")
relays := splitCSV(*relaysCSV)
if len(relays) == 0 {
fmt.Fprintln(os.Stderr, "no relays provided")
os.Exit(1)
}
followRelayURL := *followRelay
if followRelayURL == "" {
followRelayURL = relays[0]
}
ctx := context.Background()
timeout := time.Duration(*timeoutSec) * time.Second
// Step 1: Fetch user's own relay list (kind 10002)
fmt.Println("\n==> Step 1: Fetching your relay list (kind 10002)")
fmt.Printf(" Connecting to %s...\n", followRelayURL)
userRelays, err := fetchUserRelayList(ctx, followRelayURL, *pubkey, timeout)
if err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to get your relay list from %s: %v\n", followRelayURL, err)
// Continue anyway - not critical
} else if len(userRelays) > 0 {
if err := writeLines(userRelayListPath, userRelays); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to write user relay list: %v\n", err)
} else {
fmt.Printf(" ✓ Found %d relays in your relay list\n", len(userRelays))
}
} else {
fmt.Println(" ⚠ No relay list found for your pubkey")
}
// Step 2: Fetch follows (kind 3)
fmt.Println("\n==> Step 2: Fetching your follow list (kind 3)")
fmt.Printf(" Connecting to %s...\n", followRelayURL)
follows, err := fetchFollows(ctx, followRelayURL, *pubkey, timeout)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get follows from %s: %v\n", followRelayURL, err)
os.Exit(1)
}
fmt.Printf(" ✓ Found %d follows from kind 3\n", len(follows))
// Step 2b: Fetch follow sets (kind 30000)
fmt.Println("\n==> Step 2b: Fetching your follow sets (kind 30000)")
fmt.Printf(" Connecting to %s...\n", followRelayURL)
// Create follow_sets directory
if err := os.MkdirAll(followSetsDir, 0o755); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to create follow_sets directory: %v\n", err)
} else {
followSets, err := fetchAndSaveFollowSets(ctx, followRelayURL, *pubkey, timeout, followSetsDir)
if err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to get follow sets from %s: %v\n", followRelayURL, err)
} else {
fmt.Printf(" ✓ Saved %d follow sets to %s\n", len(followSets), followSetsDir)
// Merge all follow sets into follows list
for _, setPubkeys := range followSets {
follows = append(follows, setPubkeys...)
}
follows = deduplicateAndSort(follows)
}
}
if len(follows) == 0 {
fmt.Println(" No follows found; nothing to do")
if err := writeLines(followsPath, nil); err != nil {
fmt.Fprintf(os.Stderr, "failed to write follows file: %v\n", err)
}
os.Exit(0)
}
if err := writeLines(followsPath, follows); err != nil {
fmt.Fprintf(os.Stderr, "failed to write follows file: %v\n", err)
os.Exit(1)
}
fmt.Printf(" ✓ Total unique follows: %d\n", len(follows))
// Save user pubkey for later use
if err := writeLines(userPubkeyPath, []string{strings.ToLower(*pubkey)}); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to write user pubkey file: %v\n", err)
}
// Step 3: Fetch kind 10002 relay-list events for follows in batches across relays
fmt.Println("\n==> Step 3: Fetching kind 10002 relay lists for follows")
// Prepare output file for JSONL writes
jsonlFile, err := os.Create(jsonlPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create JSONL file: %v\n", err)
os.Exit(1)
}
defer jsonlFile.Close()
jsonlWriter := bufio.NewWriter(jsonlFile)
defer jsonlWriter.Flush()
// Create batches and initialize progress tracking
batches := chunkAuthors(follows, *batchSize)
progress := &progressTracker{
batchesTotal: len(batches),
relaysTotal: len(relays),
}
fmt.Printf(" Querying %d relays with %d batches of ~%d authors each\n",
len(relays), len(batches), *batchSize)
fmt.Printf(" Parallel workers: %d\n", *parallel)
fmt.Println()
// Channel to serialize JSONL writes and deduplicate by event ID
eventChan := make(chan eventLine, 1024)
writerDone := make(chan struct{})
seenEvents := make(map[string]struct{})
var seenMutex sync.Mutex
// Start writer goroutine
go func() {
for event := range eventChan {
progress.eventsReceived.Add(1)
seenMutex.Lock()
if _, exists := seenEvents[event.id]; !exists {
seenEvents[event.id] = struct{}{}
fmt.Fprintln(jsonlWriter, event.line)
progress.eventsWritten.Add(1)
}
seenMutex.Unlock()
}
jsonlWriter.Flush()
close(writerDone)
}()
// Start progress reporter
progressDone := make(chan struct{})
go func() {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
select {
case <-progressDone:
return
case <-ticker.C:
received := progress.eventsReceived.Load()
written := progress.eventsWritten.Load()
batchesDone := progress.batchesDone.Load()
totalBatches := int64(progress.batchesTotal * progress.relaysTotal)
pct := float64(batchesDone) / float64(totalBatches) * 100
fmt.Printf(" Progress: %d/%d batches (%.1f%%) | Events: %d received, %d unique\n",
batchesDone, totalBatches, pct, received, written)
}
}
}()
// Process relays with semaphore for parallelism control
// Each relay gets one connection that handles all batches
semaphore := make(chan struct{}, *parallel)
var wg sync.WaitGroup
for _, relayURL := range relays {
semaphore <- struct{}{}
wg.Add(1)
go func(url string) {
defer wg.Done()
defer func() { <-semaphore }()
if err := fetchAllBatches(ctx, url, batches, timeout, eventChan, progress); err != nil {
// Log errors but continue with other relays
fmt.Fprintf(os.Stderr, " ⚠ Error from %s: %v\n", url, err)
}
}(relayURL)
}
wg.Wait()
close(eventChan)
<-writerDone
close(progressDone)
// Final summary
fmt.Println()
fmt.Println("==> Collection complete")
fmt.Printf(" ✓ Total events received: %d\n", progress.eventsReceived.Load())
fmt.Printf(" ✓ Unique events written: %d\n", progress.eventsWritten.Load())
fmt.Printf(" ✓ JSONL file: %s\n", jsonlPath)
fmt.Printf(" ✓ Follows file: %s\n", followsPath)
fmt.Printf(" ✓ User relay list: %s\n", userRelayListPath)
fmt.Printf(" ✓ User pubkey: %s\n", userPubkeyPath)
}
func splitCSV(s string) []string {
parts := strings.Split(s, ",")
var out []string
for _, p := range parts {
p = strings.TrimSpace(p)
if p != "" {
out = append(out, p)
}
}
return out
}
// fetchUserRelayList retrieves the user's own relay list (kind 10002) from a relay
func fetchUserRelayList(ctx context.Context, relayURL, pubkey string, timeout time.Duration) ([]string, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
relay, err := nostr.RelayConnect(ctx, relayURL)
if err != nil {
return nil, fmt.Errorf("relay connect: %w", err)
}
defer relay.Close()
filters := nostr.Filters{
nostr.Filter{
Kinds: []int{10002},
Authors: []string{strings.ToLower(pubkey)},
Limit: 1,
},
}
subscription, err := relay.Subscribe(ctx, filters)
if err != nil {
return nil, fmt.Errorf("subscribe: %w", err)
}
defer subscription.Unsub()
var relays []string
for {
select {
case <-ctx.Done():
return deduplicateAndSort(relays), nil
case <-subscription.EndOfStoredEvents:
// Relay finished sending stored events
return deduplicateAndSort(relays), nil
case event := <-subscription.Events:
if event == nil {
continue
}
if event.Kind != 10002 {
continue
}
// Extract relay URLs from r-tags
for _, tag := range event.Tags {
if len(tag) >= 2 && tag[0] == "r" {
relayURL := strings.TrimSpace(tag[1])
// Only include valid relay URLs (no query params, etc)
if isValidRelayURL(relayURL) {
relays = append(relays, relayURL)
}
}
}
}
}
}
// fetchFollows retrieves the follow list (kind 3) for a given pubkey from a relay
func fetchFollows(ctx context.Context, relayURL, pubkey string, timeout time.Duration) ([]string, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
relay, err := nostr.RelayConnect(ctx, relayURL)
if err != nil {
return nil, fmt.Errorf("relay connect: %w", err)
}
defer relay.Close()
filters := nostr.Filters{
nostr.Filter{
Kinds: []int{3},
Authors: []string{strings.ToLower(pubkey)},
},
}
subscription, err := relay.Subscribe(ctx, filters)
if err != nil {
return nil, fmt.Errorf("subscribe: %w", err)
}
defer subscription.Unsub()
var follows []string
for {
select {
case <-ctx.Done():
return deduplicateAndSort(follows), nil
case <-subscription.EndOfStoredEvents:
// Relay finished sending stored events
return deduplicateAndSort(follows), nil
case event := <-subscription.Events:
if event == nil {
continue
}
if event.Kind != 3 {
continue
}
// Extract p-tags (pubkeys being followed)
for _, tag := range event.Tags {
if len(tag) >= 2 && tag[0] == "p" {
pubkeyHex := strings.ToLower(tag[1])
if isHex64(pubkeyHex) {
follows = append(follows, pubkeyHex)
}
}
}
}
}
}
// followSet represents a kind 30000 follow set with its identifier and pubkeys
type followSet struct {
dTag string
title string
pubkeys []string
}
// fetchAndSaveFollowSets retrieves follow sets (kind 30000) and saves each to a separate file
func fetchAndSaveFollowSets(ctx context.Context, relayURL, pubkey string, timeout time.Duration, outputDir string) (map[string][]string, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
relay, err := nostr.RelayConnect(ctx, relayURL)
if err != nil {
return nil, fmt.Errorf("relay connect: %w", err)
}
defer relay.Close()
filters := nostr.Filters{
nostr.Filter{
Kinds: []int{30000},
Authors: []string{strings.ToLower(pubkey)},
},
}
subscription, err := relay.Subscribe(ctx, filters)
if err != nil {
return nil, fmt.Errorf("subscribe: %w", err)
}
defer subscription.Unsub()
sets := make(map[string]*followSet)
for {
select {
case <-ctx.Done():
return saveFollowSets(sets, outputDir)
case <-subscription.EndOfStoredEvents:
return saveFollowSets(sets, outputDir)
case event := <-subscription.Events:
if event == nil {
continue
}
if event.Kind != 30000 {
continue
}
// Extract d-tag identifier
dTag := "unnamed"
title := ""
for _, tag := range event.Tags {
if len(tag) >= 2 && tag[0] == "d" {
dTag = sanitizeFilename(tag[1])
if dTag == "" {
dTag = "unnamed"
}
} else if len(tag) >= 2 && tag[0] == "title" {
title = tag[1]
}
}
// Initialize set if not exists
if sets[dTag] == nil {
sets[dTag] = &followSet{
dTag: dTag,
title: title,
pubkeys: []string{},
}
}
// Extract p-tags (pubkeys in follow sets)
for _, tag := range event.Tags {
if len(tag) >= 2 && tag[0] == "p" {
pubkeyHex := strings.ToLower(tag[1])
if isHex64(pubkeyHex) {
sets[dTag].pubkeys = append(sets[dTag].pubkeys, pubkeyHex)
}
}
}
}
}
}
// saveFollowSets writes each follow set to a separate file
func saveFollowSets(sets map[string]*followSet, outputDir string) (map[string][]string, error) {
result := make(map[string][]string)
usedFilenames := make(map[string]bool)
for dTag, set := range sets {
// Deduplicate and sort pubkeys
set.pubkeys = deduplicateAndSort(set.pubkeys)
if len(set.pubkeys) == 0 {
continue
}
// Create filename from d-tag with collision detection
baseFilename := fmt.Sprintf("follow_set_%s.txt", dTag)
filename := baseFilename
counter := 1
// Handle filename collisions
for usedFilenames[filename] {
filename = fmt.Sprintf("follow_set_%s_%d.txt", dTag, counter)
counter++
if counter > 100 {
return nil, fmt.Errorf("too many filename collisions for d-tag: %s", dTag)
}
}
usedFilenames[filename] = true
filePath := filepath.Join(outputDir, filename)
// Security check: ensure filePath is within outputDir
absPath, err := filepath.Abs(filePath)
if err != nil {
return nil, fmt.Errorf("failed to resolve path for %s: %w", filename, err)
}
absDir, err := filepath.Abs(outputDir)
if err != nil {
return nil, fmt.Errorf("failed to resolve output directory: %w", err)
}
if !strings.HasPrefix(absPath, absDir) {
return nil, fmt.Errorf("security: attempted path traversal with d-tag: %s", set.dTag)
}
// Prepare file content with header
lines := []string{}
if set.title != "" {
lines = append(lines, fmt.Sprintf("# %s", set.title))
}
lines = append(lines, fmt.Sprintf("# d-tag: %s", set.dTag))
lines = append(lines, fmt.Sprintf("# pubkeys: %d", len(set.pubkeys)))
lines = append(lines, "#")
lines = append(lines, set.pubkeys...)
if err := writeLines(filePath, lines); err != nil {
return nil, fmt.Errorf("failed to write %s: %w", filename, err)
}
fmt.Printf(" - %s (%d pubkeys)\n", filename, len(set.pubkeys))
result[dTag] = set.pubkeys
}
return result, nil
}
// sanitizeFilename removes or replaces characters that are unsafe for filenames
func sanitizeFilename(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return ""
}
// Convert to lowercase for consistency
s = strings.ToLower(s)
// Remove any path separators to prevent directory traversal
s = strings.ReplaceAll(s, "..", "")
s = strings.ReplaceAll(s, "./", "")
s = strings.ReplaceAll(s, ".\\", "")
// Replace unsafe characters with underscores
replacer := strings.NewReplacer(
"/", "_",
"\\", "_",
":", "_",
"*", "_",
"?", "_",
"\"", "_",
"<", "_",
">", "_",
"|", "_",
" ", "_",
"\t", "_",
"\n", "_",
"\r", "_",
)
s = replacer.Replace(s)
// Strip non-ASCII and control characters
var result strings.Builder
for _, r := range s {
if r >= 32 && r <= 126 {
result.WriteRune(r)
}
}
s = result.String()
// Remove leading/trailing dots, dashes, underscores
s = strings.Trim(s, ".-_")
// Collapse multiple underscores
for strings.Contains(s, "__") {
s = strings.ReplaceAll(s, "__", "_")
}
// Check for reserved names (Windows)
reserved := map[string]bool{
"con": true, "prn": true, "aux": true, "nul": true,
"com1": true, "com2": true, "com3": true, "com4": true,
"com5": true, "com6": true, "com7": true, "com8": true,
"com9": true, "lpt1": true, "lpt2": true, "lpt3": true,
"lpt4": true, "lpt5": true, "lpt6": true, "lpt7": true,
"lpt8": true, "lpt9": true,
}
if reserved[s] {
s = "_" + s
}
// Limit length (leave room for collision suffix)
if len(s) > 40 {
s = s[:40]
}
// Ensure non-empty result
if s == "" {
s = "unnamed"
}
return s
}
// fetchAllBatches opens one connection to a relay and processes all batches sequentially
func fetchAllBatches(ctx context.Context, relayURL string, batches [][]string, timeout time.Duration,
out chan<- eventLine, progress *progressTracker) error {
// Connect once to the relay
connectCtx, connectCancel := context.WithTimeout(ctx, timeout)
defer connectCancel()
relay, err := nostr.RelayConnect(connectCtx, relayURL)
if err != nil {
return fmt.Errorf("relay connect: %w", err)
}
defer relay.Close()
// Process each batch with a new subscription on the same connection
for batchIdx, authors := range batches {
if err := fetchBatch(ctx, relay, relayURL, authors, batchIdx, timeout, out); err != nil {
// Log error but continue with next batch
fmt.Fprintf(os.Stderr, " ⚠ Error from %s batch %d: %v\n", relayURL, batchIdx+1, err)
}
progress.batchesDone.Add(1)
}
return nil
}
// fetchBatch retrieves kind 10002 events for a batch of authors using an existing relay connection
func fetchBatch(ctx context.Context, relay *nostr.Relay, relayURL string, authors []string, batchIdx int,
timeout time.Duration, out chan<- eventLine) error {
// Validate and normalize authors to ensure all are 64-char hex
validAuthors := make([]string, 0, len(authors))
for _, author := range authors {
author = strings.ToLower(strings.TrimSpace(author))
if isHex64(author) {
validAuthors = append(validAuthors, author)
}
}
if len(validAuthors) == 0 {
return nil
}
// Create a timeout context for this batch
batchCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
filters := nostr.Filters{
nostr.Filter{
Kinds: []int{10002},
Authors: validAuthors,
},
}
subscription, err := relay.Subscribe(batchCtx, filters)
if err != nil {
return fmt.Errorf("subscribe: %w", err)
}
defer subscription.Unsub()
for {
select {
case <-batchCtx.Done():
return nil
case <-subscription.EndOfStoredEvents:
// Relay finished sending stored events, exit early
return nil
case event := <-subscription.Events:
if event == nil {
continue
}
if event.Kind != 10002 {
continue
}
line := event.String()
out <- eventLine{
id: strings.ToLower(event.ID),
line: line,
}
}
}
}
// deduplicateAndSort removes duplicates and sorts a slice of strings
func deduplicateAndSort(items []string) []string {
if len(items) == 0 {
return nil
}
sort.Strings(items)
unique := make([]string, 0, len(items))
last := ""
for _, item := range items {
if item != last {
unique = append(unique, item)
last = item
}
}
return unique
}
// chunkAuthors splits a slice of authors into batches of the specified size
func chunkAuthors(authors []string, batchSize int) [][]string {
if batchSize <= 0 {
return [][]string{authors}
}
var batches [][]string
for i := 0; i < len(authors); i += batchSize {
end := i + batchSize
if end > len(authors) {
end = len(authors)
}
batches = append(batches, authors[i:end])
}
return batches
}