-
Notifications
You must be signed in to change notification settings - Fork 8
/
storrent.go
394 lines (362 loc) · 8.61 KB
/
storrent.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// STorrent is a BitTorrent implementation that is optimised for streaming
// media.
package main
import (
"context"
crand "crypto/rand"
"errors"
"flag"
"fmt"
"io"
"log"
"math/rand/v2"
"net"
"net/netip"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"syscall"
"time"
"github.com/jech/portmap"
"github.com/jech/storrent/config"
"github.com/jech/storrent/crypto"
"github.com/jech/storrent/dht"
"github.com/jech/storrent/fuse"
"github.com/jech/storrent/http"
"github.com/jech/storrent/peer"
"github.com/jech/storrent/physmem"
"github.com/jech/storrent/rundht"
"github.com/jech/storrent/tor"
)
func main() {
var proxyURL, mountpoint string
var cpuprofile, memprofile, mutexprofile, dhtMode string
var doPortmap string
mem, err := physmem.Total()
if err != nil {
log.Printf("Couldn't determine physical memory: %v", err)
mem = 2 * 1024 * 1024 * 1024
}
fmt.Fprintf(os.Stderr, "STorrent 0.0 by Juliusz Chroboczek\n")
flag.IntVar(&config.ProtocolPort, "port", 23222,
"TCP and UDP `port` used for BitTorrent and DHT traffic")
flag.StringVar(&config.HTTPAddr, "http", "[::1]:8088",
"Web server `address`")
flag.Int64Var(&config.MemoryMark, "mem", mem/2,
"Target memory usage in `bytes`")
flag.StringVar(&cpuprofile, "cpuprofile", "",
"CPU profile `filename`")
flag.StringVar(&memprofile, "memprofile", "",
"Memory profile `filename`")
flag.StringVar(&mutexprofile, "mutexprofile", "",
"Mutex profile `filename`")
flag.StringVar(&proxyURL, "proxy", "",
"`URL` of a proxy to use for BitTorrent and tracker traffic.\n"+
"For tor, use \"socks5://127.0.0.1:9050\" and disable the DHT")
flag.StringVar(&mountpoint, "mountpoint", "",
"FUSE `mountpoint`")
if dht.Available() {
flag.StringVar(&dhtMode, "dht", "normal", "DHT mode")
}
flag.BoolVar(&config.DefaultUseTrackers, "use-trackers", false,
"Use trackers (if available)")
flag.BoolVar(&config.DefaultUseWebseeds, "use-webseeds", false,
"Use webseeds (if available)")
flag.Float64Var(&config.PrefetchRate, "prefetch-rate", 768*1024,
"Prefetch `rate` in bytes per second")
flag.BoolVar(&config.PreferEncryption, "prefer-encryption", true,
"Prefer encrytion")
flag.BoolVar(&config.ForceEncryption, "force-encryption", false,
"Force encryption")
flag.StringVar(&doPortmap, "portmap", "auto", "NAT port mappping `protocol` (natpmp, upnp, auto, or off)")
flag.BoolVar(&config.MultipathTCP, "mptcp", false,
"Use MP-TCP for peer-to-peer connections")
flag.BoolVar(&config.Debug, "debug", false,
"Log all BitTorrent messages")
flag.Parse()
if dht.Available() {
m, err := config.ParseDhtMode(dhtMode)
if err != nil {
log.Fatalf("%v", err)
}
config.DefaultDhtMode = m
}
err = config.SetDefaultProxy(proxyURL)
if err != nil {
log.Printf("SetDefaultProxy: %v", err)
return
}
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Printf("Create(cpuprofile): %v", err)
return
}
pprof.StartCPUProfile(f)
defer func() {
pprof.StopCPUProfile()
f.Close()
}()
}
if memprofile != "" {
defer func() {
f, err := os.Create(memprofile)
if err != nil {
log.Printf("Create(memprofile): %v", err)
return
}
pprof.WriteHeapProfile(f)
f.Close()
}()
}
if mutexprofile != "" {
runtime.SetMutexProfileFraction(1)
defer func() {
f, err := os.Create(mutexprofile)
if err != nil {
log.Printf("Create(mutexprofile): %v", err)
return
}
pprof.Lookup("mutex").WriteTo(f, 0)
f.Close()
}()
}
config.SetExternalIPv4Port(config.ProtocolPort, true)
config.SetExternalIPv4Port(config.ProtocolPort, false)
peer.UploadEstimator.Init(3 * time.Second)
peer.UploadEstimator.Start()
peer.DownloadEstimator.Init(3 * time.Second)
peer.DownloadEstimator.Start()
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, syscall.SIGINT)
if mountpoint != "" {
err := fuse.Serve(mountpoint)
if err != nil {
log.Fatalf("Couldn't mount directory: %v", err)
}
defer func(mountpoint string) {
err := fuse.Close(mountpoint)
if err != nil {
log.Printf("Couldn't unmount directory: %v", err)
}
}(mountpoint)
}
ctx, cancelCtx := context.WithCancel(context.Background())
portmapdone := make(chan struct{})
defer func(portmapdone <-chan struct{}) {
cancelCtx()
log.Printf("Shutting down...")
timer := time.NewTimer(4 * time.Second)
select {
case <-portmapdone:
timer.Stop()
case <-timer.C:
}
}(portmapdone)
pmkind := 0
switch doPortmap {
case "off":
pmkind = 0
case "natpmp":
pmkind = portmap.NATPMP
case "upnp":
pmkind = portmap.UPNP
case "auto":
pmkind = portmap.All
default:
log.Printf("Unknown portmapping kind %v", doPortmap)
}
if pmkind != 0 {
go func() {
err := portmap.Map(ctx, "STorrent",
uint16(config.ProtocolPort), pmkind,
func(proto string, status portmap.Status, err error) {
if err != nil {
log.Printf(
"Port mapping: %v", err,
)
} else if status.Lifetime > 0 {
log.Printf(
"Mapped %v %v->%v (%v)",
proto,
status.Internal,
status.External,
status.Lifetime,
)
} else {
log.Printf(
"Unmapped %v %v",
proto,
status.Internal,
)
}
e := status.External
if e == 0 {
e = status.Internal
}
config.SetExternalIPv4Port(
int(e), proto == "tcp",
)
},
)
if err != nil {
log.Println(err)
}
close(portmapdone)
}()
} else {
close(portmapdone)
}
var dhtaddrs []netip.AddrPort
var id []byte
if config.DHTBootstrap != "" {
id, dhtaddrs, err = rundht.Read(config.DHTBootstrap)
if err != nil {
log.Printf("Couldn't read %v: %v",
config.DHTBootstrap, err)
}
}
defer func() {
if config.DHTBootstrap != "" {
dir := filepath.Dir(config.DHTBootstrap)
os.MkdirAll(dir, 0700) // ignore errors
err := rundht.Write(config.DHTBootstrap, id)
if err != nil {
log.Printf("Couldn't write %v: %v",
config.DHTBootstrap, err)
}
}
}()
config.DhtID = make([]byte, 20)
if id != nil {
copy(config.DhtID, id)
} else {
_, err := crand.Read(config.DhtID)
if err != nil {
log.Printf("Random: %v", err)
return
}
}
dhtevent, err := rundht.Run(ctx, config.DhtID, config.ProtocolPort)
if err != nil {
log.Printf("DHT: %v", err)
return
}
go rundht.Handle(dhtevent)
go rundht.Bootstrap(ctx, dhtaddrs)
go func(args []string) {
for _, arg := range args {
proxy := config.DefaultProxy()
t, err := tor.ReadMagnet(proxy, arg)
if err != nil {
log.Printf("Read magnet: %v", err)
terminate <- nil
return
}
if t == nil {
t, err = tor.GetTorrent(ctx, proxy, arg)
if err != nil {
var perr tor.ParseURLError
if !errors.As(err, &perr) {
log.Printf("GetTorrent(%v): %v",
arg, err)
terminate <- nil
return
}
}
}
if t == nil {
torfile, err := os.Open(arg)
if err != nil {
log.Printf("Open(%v): %v", arg, err)
terminate <- nil
return
}
t, err = tor.ReadTorrent(proxy, torfile)
if err != nil {
log.Printf("%v: %v\n", arg, err)
terminate <- nil
torfile.Close()
return
}
torfile.Close()
}
tor.AddTorrent(ctx, t)
if t.InfoComplete() {
t.Log.Printf("Added torrent %v (%v)\n",
t.Name, t.Hash)
} else {
t.Log.Printf("Added torrent %v", t.Hash)
}
}
}(flag.Args())
var lc net.ListenConfig
if config.MultipathTCP {
lc.SetMultipathTCP(true)
}
listener, err :=
lc.Listen(context.Background(),
"tcp", fmt.Sprintf(":%v", config.ProtocolPort),
)
if err != nil {
log.Printf("Listen: %v", err)
return
}
go listen(listener)
err = http.Serve(config.HTTPAddr)
if err != nil {
log.Printf("Serve: %v", err)
return
}
log.Printf("Listening on http://%v", config.HTTPAddr)
go func() {
min := 250 * time.Millisecond
max := 16 * time.Second
interval := max
for {
rc := tor.Expire()
if rc < 0 {
interval = interval / 2
if interval < min {
interval = min
}
} else if rc > 0 {
interval = interval * 2
if interval > max {
interval = max
}
}
time.Sleep(roughly(interval))
}
}()
<-terminate
}
func listen(listener net.Listener) {
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Accept: %v", err)
time.Sleep(roughly(2 * time.Second))
continue
}
go func(conn net.Conn) {
err = tor.Server(conn, crypto.DefaultOptions(
config.PreferEncryption,
config.ForceEncryption,
))
if err != nil && err != io.EOF {
log.Printf("Server: %v", err)
}
}(conn)
}
}
func roughly(d time.Duration) time.Duration {
r := d / 4
if r > 2*time.Second {
r = 2 * time.Second
}
m := rand.N(r)
return d + m - r/2
}