forked from jackpal/Taipei-Torrent
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
164 lines (145 loc) · 4.34 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
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
package main
import (
"flag"
"github.com/hailiang/gosocks"
"github.com/jackpal/Taipei-Torrent/torrent"
"github.com/jackpal/Taipei-Torrent/tracker"
"log"
"math"
"os"
"os/signal"
"path"
"runtime/pprof"
)
var (
cpuprofile = flag.String("cpuprofile", "", "If not empty, collects CPU profile samples and writes the profile to the given file before the program exits")
memprofile = flag.String("memprofile", "", "If not empty, writes memory heap allocations to the given file before the program exits")
createTorrent = flag.String("createTorrent", "", "If not empty, creates a torrent file from the given root. Writes to stdout")
createTracker = flag.String("createTracker", "", "Creates a tracker serving the given torrent file on the given address. Example --createTracker=:8080 to serve on port 8080.")
port = flag.Int("port", 7777, "Port to listen on. 0 means pick random port. Note that 6881 is blacklisted by some trackers.")
fileDir = flag.String("fileDir", ".", "path to directory where files are stored")
seedRatio = flag.Float64("seedRatio", math.Inf(0), "Seed until ratio >= this value before quitting.")
useLPD = flag.Bool("useLPD", false, "Use Local Peer Discovery")
useUPnP = flag.Bool("useUPnP", false, "Use UPnP to open port in firewall.")
useNATPMP = flag.Bool("useNATPMP", false, "Use NAT-PMP to open port in firewall.")
gateway = flag.String("gateway", "", "IP Address of gateway.")
useDHT = flag.Bool("useDHT", false, "Use DHT to get peers.")
trackerlessMode = flag.Bool("trackerlessMode", false, "Do not get peers from the tracker. Good for testing DHT mode.")
proxyAddress = flag.String("proxyAddress", "", "Address of a SOCKS5 proxy to use.")
)
func parseTorrentFlags() *torrent.TorrentFlags {
return &torrent.TorrentFlags{
Dial: dialerFromFlags(),
Port: *port,
FileDir: *fileDir,
SeedRatio: *seedRatio,
UseLPD: *useLPD,
UseDHT: *useDHT,
UseUPnP: *useUPnP,
UseNATPMP: *useNATPMP,
TrackerlessMode: *trackerlessMode,
// IP address of gateway
Gateway: *gateway,
}
}
func dialerFromFlags() torrent.Dialer {
if len(*proxyAddress) > 0 {
return socks.DialSocksProxy(socks.SOCKS5, string(*proxyAddress))
}
return nil
}
func main() {
flag.Usage = usage
flag.Parse()
if *createTorrent != "" {
err := torrent.WriteMetaInfoBytes(*createTorrent, os.Stdout)
if err != nil {
log.Fatal("Could not create torrent file:", err)
}
return
}
if *createTracker != "" {
err := startTracker(*createTracker, flag.Args())
if err != nil {
log.Fatal("Tracker returned error:", err)
}
return
}
args := flag.Args()
narg := flag.NArg()
if narg != 1 {
if narg < 1 {
log.Println("Too few arguments. Torrent file or torrent URL required.")
} else {
log.Printf("Too many arguments. (Expected 1): %v", args)
}
usage()
}
torrentFlags := parseTorrentFlags()
if *cpuprofile != "" {
cpuf, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(cpuf)
defer pprof.StopCPUProfile()
}
if *memprofile != "" {
defer func(file string) {
memf, err := os.Create(file)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(memf)
}(*memprofile)
}
log.Println("Starting.")
err := torrent.RunTorrents(torrentFlags, args)
if err != nil {
log.Fatal("Could not run torrents", args, err)
}
}
func usage() {
log.Printf("usage: torrent.Torrent [options] (torrent-file | torrent-url)")
flag.PrintDefaults()
os.Exit(2)
}
func startTracker(addr string, torrentFiles []string) (err error) {
t := tracker.NewTracker()
// TODO(jackpal) Allow caller to choose port number
t.Addr = addr
dial := dialerFromFlags()
for _, torrentFile := range torrentFiles {
var metaInfo *torrent.MetaInfo
metaInfo, err = torrent.GetMetaInfo(dial, torrentFile)
if err != nil {
return
}
name := metaInfo.Info.Name
if name == "" {
name = path.Base(torrentFile)
}
err = t.Register(metaInfo.InfoHash, name)
if err != nil {
return
}
}
go func() {
quitChan := listenSigInt()
select {
case <-quitChan:
log.Printf("got control-C")
t.Quit()
}
}()
err = t.ListenAndServe()
if err != nil {
return
}
return
}
func listenSigInt() chan os.Signal {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
return c
}