-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
127 lines (114 loc) · 2.61 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
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
package main
import (
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/CortexFoundation/CortexTheseus/torrentfs"
"github.com/anacrolix/torrent/metainfo"
cli "gopkg.in/urfave/cli.v1"
glog "log"
"io/ioutil"
"os"
"os/signal"
"syscall"
"strings"
"time"
)
type Config struct {
Dir string
TaskList string
LogLevel int
NSeed int
NActive int
Dht bool
}
var gitCommit = "" // Git SHA1 commit hash of the release (set via linker flags)
func main() {
var conf Config
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "verbosity",
Value: 3,
Usage: "verbose level",
Destination: &conf.LogLevel,
},
cli.IntFlag{
Name: "nseed",
Value: 120,
Usage: "seed num",
Destination: &conf.NSeed,
},
cli.IntFlag{
Name: "nactive",
Value: 120,
Usage: "active num",
Destination: &conf.NActive,
},
cli.StringFlag{
Name: "dir",
Value: "data",
Usage: "datadir",
Destination: &conf.Dir,
},
cli.StringFlag{
Name: "task",
Value: "task",
Usage: "task list",
Destination: &conf.TaskList,
},
cli.BoolFlag{
Name: "dht",
Usage: "dht",
Destination: &conf.Dht,
},
}
app.Action = func(c *cli.Context) error {
mainExitCode(&conf)
return nil
}
err := app.Run(os.Args)
if err != nil {
glog.Fatal(err)
}
}
func mainExitCode(conf *Config) int {
log.Root().SetHandler(
log.LvlFilterHandler(log.Lvl(conf.LogLevel),
log.StreamHandler(os.Stdout, log.TerminalFormat(true))),
)
cfg := torrentfs.Config{
RpcURI: "",
DefaultTrackers: torrentfs.DefaultConfig.DefaultTrackers,
BoostNodes: torrentfs.DefaultConfig.BoostNodes,
SyncMode: torrentfs.DefaultConfig.SyncMode,
DisableUTP: torrentfs.DefaultConfig.DisableUTP,
MaxSeedingNum: conf.NSeed,
MaxActiveNum: conf.NActive,
}
cfg.DataDir = conf.Dir
cfg.DisableDHT = !conf.Dht
cfg.DisableUTP = true
tm := torrentfs.NewTorrentManager(&cfg)
tm.Start()
log.Info("Torrent fs start with config", "config", cfg)
if contents, err := ioutil.ReadFile(conf.TaskList); err == nil {
tasks := strings.Split(string(contents), "\n")
for _, task := range tasks {
if len(task) != 40 {
continue
}
log.Info("Task added", "task", task)
tm.NewTorrent(torrentfs.FlowControlMeta{
InfoHash: metainfo.NewHashFromHex(task),
BytesRequested: 10000000,
})
time.Sleep(10 * time.Millisecond)
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
for {
<-c
tm.Close()
}
return 0
}