-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
131 lines (115 loc) · 3.99 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
128
129
130
131
package main
// SPDX-FileCopyrightText: 2019 NetSys Lab
// SPDX-License-Identifier: GPL-3.0-only
import (
"io/ioutil"
"github.com/anacrolix/tagflag"
"github.com/netsys-lab/dht"
"github.com/scionproto/scion/go/lib/snet"
log "github.com/sirupsen/logrus"
"github.com/netsys-lab/bittorrent-over-scion/config"
"github.com/netsys-lab/bittorrent-over-scion/server"
"github.com/netsys-lab/bittorrent-over-scion/torrentfile"
)
var flags = struct {
InPath string `help:"Path to torrent file that should be processed"`
OutPath string `help:"Path where BitTorrent writes the downloaded file"`
Peer string `help:"Remote SCION address"`
Seed bool `help:"Start BitTorrent in Seeder mode"`
File string `help:"Load the file to which the torrent of InPath refers. Only required if seed=true"`
Local string `help:"Local SCION address of the seeder"`
NumPaths int `help:"Optional: Limit the number of paths the seeder uses to upload to each leecher. Per default 0, meaning the seeder aims to distribute paths in a fair manner to all leechers"`
DialBackStartPort int `help:"Optional: Start port of the connections the seeder uses to dial back to the leecher."`
LogLevel string `help:"Optional: Change log level"`
EnableDht bool `help:"Optional: Run a dht network to announce peers"`
DhtPort int `help:"Optional: Configure the port to run the dht network"`
DhtBootstrapAddr string `help:"Optional: SCION address of the dht network"`
PrintMetrics bool `help:"Optional: Display per-path metrics at the end of the download. Only for seed=false"`
ExportMetricsTo string `help:"Optional: Export per-path metrics to a particular target, at the moment a csv file (e.g. /tmp/metrics.csv)"`
}{
Seed: false,
NumPaths: 0,
DialBackStartPort: 45000,
LogLevel: "INFO",
PrintMetrics: false,
ExportMetricsTo: "http://19-ffaa:1:c3f,141.44.25.148:80/btmetrics",
}
func setLogging(loglevel string) {
switch loglevel {
case "TRACE":
log.SetLevel(log.TraceLevel)
break
case "DEBUG":
log.SetLevel(log.DebugLevel)
break
case "INFO":
log.SetLevel(log.InfoLevel)
break
case "WARN":
log.SetLevel(log.WarnLevel)
break
case "ERROR":
log.SetLevel(log.ErrorLevel)
break
case "FATAL":
log.SetLevel(log.FatalLevel)
break
}
}
func main() {
tagflag.Parse(&flags)
setLogging(flags.LogLevel)
log.Infof("Input %s, Output %s, Peer %s, seed %t, file %s", flags.InPath, flags.OutPath, flags.Peer, flags.Seed, flags.File)
peerDiscoveryConfig := config.DefaultPeerDisoveryConfig()
peerDiscoveryConfig.EnableDht = flags.EnableDht
dhtAddr, err := snet.ParseUDPAddr(flags.DhtBootstrapAddr)
if err == nil {
peerDiscoveryConfig.DhtNodes = []dht.Addr{dht.NewAddr(*dhtAddr)}
}
if flags.DhtPort > 0 {
peerDiscoveryConfig.DhtPort = uint16(flags.DhtPort)
}
tf, err := torrentfile.Open(flags.InPath)
if err != nil {
log.Fatal(err)
}
tf.PrintMetrics = flags.PrintMetrics
if flags.Seed {
log.Info("Loading file to RAM...")
tf.Content, err = ioutil.ReadFile(flags.File)
if err != nil {
log.Fatal(err)
}
log.Info("Loaded file to RAM")
// peer := fmt.Sprintf("%s:%d", flags.Peer, port)
conf := server.ServerConfig{
LAddr: flags.Local,
TorrentFile: &tf,
PathSelectionResponsibility: "server",
NumPaths: flags.NumPaths,
DialBackPort: flags.DialBackStartPort,
DiscoveryConfig: &peerDiscoveryConfig,
ExportMetricsTarget: flags.ExportMetricsTo,
}
server, err := server.NewServer(&conf)
if err != nil {
log.Fatal(err)
}
log.Info("Created Server")
err = server.ListenHandshake()
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
} else {
t, err := tf.DownloadToFile(flags.OutPath, flags.Peer, flags.Local, "server", &peerDiscoveryConfig)
if err != nil {
log.Fatal(err)
}
if t.DhtNode != nil {
t.DhtNode.Close()
}
}
}