forked from boramalper/magnetico
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
102 lines (88 loc) · 2.35 KB
/
main.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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"tgragnato.it/magnetico/dht"
"tgragnato.it/magnetico/dht/mainline"
"tgragnato.it/magnetico/metadata"
"tgragnato.it/magnetico/opflags"
"tgragnato.it/magnetico/persistence"
"tgragnato.it/magnetico/stats"
"tgragnato.it/magnetico/web"
)
func main() {
// opFlags is the "operational flags"
opFlags := opflags.OpFlags{}
if err := opFlags.Parse(); err != nil {
log.Fatalln(err.Error())
}
// Handle Ctrl-C gracefully.
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt)
// Reload credentials when you receive SIGHUP
sighupChan := make(chan os.Signal, 1)
signal.Notify(sighupChan, syscall.SIGHUP)
go func() {
for range sighupChan {
if opFlags.Credentials == nil {
// Ignoring SIGHUP since no credential file was supplied
continue
}
opFlags.Credentials = make(map[string][]byte)
if err := opFlags.LoadCred(); err != nil {
log.Fatalf("couldn't load credentials %s\n", err.Error())
}
}
}()
database, err := persistence.MakeDatabase(opFlags.DatabaseURL)
if err != nil {
log.Fatalf("Could not open the database %s. %s\n", opFlags.DatabaseURL, err.Error())
}
defer func() {
if err = database.Close(); err != nil {
log.Printf("Could not close database! %s\n", err.Error())
}
}()
if opFlags.RunWeb {
go web.StartWeb(opFlags.Addr, opFlags.Credentials, database)
}
if !opFlags.RunDaemon {
<-interruptChan
return
}
mainline.DefaultThrottleRate = int(opFlags.MaxRPS)
trawlingManager := dht.NewManager(
opFlags.IndexerAddrs,
opFlags.IndexerMaxNeighbors,
opFlags.BootstrappingNodes,
opFlags.FilterNodesIpNets,
)
metadataSink := metadata.NewSink(
time.Duration(opFlags.LeechDeadline)*time.Second,
int(opFlags.LeechMaxN),
opFlags.FilterNodesIpNets,
)
// The Event Loop
for stopped := false; !stopped; {
select {
case result := <-trawlingManager.Output():
infoHash := result.InfoHash()
exists, err := database.DoesTorrentExist(infoHash[:])
if err != nil {
go stats.GetInstance().IncDBError(false)
} else if !exists {
metadataSink.Sink(result)
}
case md := <-metadataSink.Drain():
if err := database.AddNewTorrent(md.InfoHash, md.Name, md.Files); err != nil {
go stats.GetInstance().IncDBError(true)
}
case <-interruptChan:
trawlingManager.Terminate()
stopped = true
}
}
}