-
Notifications
You must be signed in to change notification settings - Fork 123
/
main.go
227 lines (200 loc) · 6.9 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
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
package main
import (
"flag"
"log"
"math"
"math/rand"
"os"
"os/signal"
"path"
"runtime/debug"
"runtime/pprof"
"time"
"github.com/jackpal/Taipei-Torrent/torrent"
"github.com/jackpal/Taipei-Torrent/tracker"
"golang.org/x/net/proxy"
)
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.")
useDeadlockDetector = flag.Bool("useDeadlockDetector", false, "Panic and print stack dumps when the program is stuck.")
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.")
initialCheck = flag.Bool("initialCheck", true, "Do an initial hash check on files when adding torrents.")
useSFTP = flag.String("useSFTP", "", "SFTP connection string, to store torrents over SFTP. e.g. 'username:password@192.168.1.25:22/path/'")
useRamCache = flag.Int("useRamCache", 0, "Size in MiB of cache in ram, to reduce traffic on torrent storage.")
useHdCache = flag.Int("useHdCache", 0, "Size in MiB of cache in OS temp directory, to reduce traffic on torrent storage.")
execOnSeeding = flag.String("execOnSeeding", "", "Command to execute when torrent has fully downloaded and has begun seeding.")
quickResume = flag.Bool("quickResume", false, "Save torrenting data to resume faster. '-initialCheck' should be set to false, to prevent hash check on resume.")
maxActive = flag.Int("maxActive", 16, "How many torrents should be active at a time. Torrents added beyond this value are queued.")
memoryPerTorrent = flag.Int("memoryPerTorrent", -1, "Maximum memory (in MiB) per torrent used for Active Pieces. 0 means minimum. -1 (default) means unlimited.")
)
func parseTorrentFlags() (flags *torrent.TorrentFlags, err error) {
dialer, err := dialerFromFlags()
if err != nil {
return
}
flags = &torrent.TorrentFlags{
Dial: dialer,
Port: portFromFlags(),
FileDir: *fileDir,
SeedRatio: *seedRatio,
UseDeadlockDetector: *useDeadlockDetector,
UseLPD: *useLPD,
UseDHT: *useDHT,
UseUPnP: *useUPnP,
UseNATPMP: *useNATPMP,
TrackerlessMode: *trackerlessMode,
// IP address of gateway
Gateway: *gateway,
InitialCheck: *initialCheck,
FileSystemProvider: fsproviderFromFlags(),
Cacher: cacheproviderFromFlags(),
ExecOnSeeding: *execOnSeeding,
QuickResume: *quickResume,
MaxActive: *maxActive,
MemoryPerTorrent: *memoryPerTorrent,
}
return
}
func portFromFlags() int {
if *port != 0 {
return *port
}
rr := rand.New(rand.NewSource(time.Now().UnixNano()))
return rr.Intn(48000) + 1025
}
func cacheproviderFromFlags() torrent.CacheProvider {
if (*useRamCache) > 0 && (*useHdCache) > 0 {
log.Panicln("Only one cache at a time, please.")
}
if (*useRamCache) > 0 {
return torrent.NewRamCacheProvider(*useRamCache)
}
if (*useHdCache) > 0 {
return torrent.NewHdCacheProvider(*useHdCache)
}
return nil
}
func fsproviderFromFlags() torrent.FsProvider {
if len(*useSFTP) > 0 {
return torrent.NewSftpFsProvider(*useSFTP)
}
return torrent.OsFsProvider{}
}
func dialerFromFlags() (proxy.Dialer, error) {
if len(*proxyAddress) > 0 {
return proxy.SOCKS5("tcp", string(*proxyAddress), nil, &proxy.Direct)
}
return proxy.FromEnvironment(), nil
}
func main() {
flag.Usage = usage
flag.Parse()
if *createTorrent != "" {
err := torrent.WriteMetaInfoBytes(*createTorrent, *createTracker, 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 {
log.Println("Too few arguments. Torrent file or torrent URL required.")
usage()
}
torrentFlags, err := parseTorrentFlags()
if err != nil {
log.Fatal("Could not parse flags:", err)
}
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)
}
if (*memoryPerTorrent) >= 0 { //User is worried about memory use.
debug.SetGCPercent(20) //Set the GC to clear memory more often.
}
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, err := dialerFromFlags()
if err != nil {
return
}
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
}