-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway.go
531 lines (500 loc) · 14.4 KB
/
gateway.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package main
import (
"bytes"
"context"
_ "embed"
"encoding/gob"
"encoding/hex"
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/anacrolix/generics"
"github.com/davecgh/go-spew/spew"
"github.com/huandu/xstrings"
"github.com/anacrolix/dht/v2/bep44"
"github.com/anacrolix/dht/v2/krpc"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/metainfo"
"github.com/dgraph-io/ristretto"
"github.com/dustin/go-humanize"
"github.com/multiformats/go-base36"
"golang.org/x/exp/slices"
"golang.org/x/sync/singleflight"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
type dhtItemCacheValue struct {
target bep44.Target
updated time.Time
updating bool
payload krpc.Bep46Payload
}
type gatewayHandler struct {
dirPageTemplate *template.Template
confluence confluenceHandler
dhtItemCache *ristretto.Cache
dhtItemCacheGetDedup singleflight.Group
dhtGetDedup singleflight.Group
metainfoCache *ristretto.Cache
gatewayDomains []string
}
func reverse(ss []string) {
for i := 0; i < len(ss)/2; i++ {
j := len(ss) - i - 1
ss[i], ss[j] = ss[j], ss[i]
}
}
type gatewayRootPageData struct {
JustUploaded *uploadedPageData
}
type uploadedPageData struct {
Infohash string
Magnet template.URL
GatewayUrl *url.URL
Debug string
}
func (h *gatewayHandler) serveRoot(w http.ResponseWriter, r *http.Request) {
pageData := gatewayRootPageData{}
if r.Method != http.MethodGet {
pageData.JustUploaded = h.doUpload(w, r)
}
err := htmlTemplates.ExecuteTemplate(w, "gateway-root.html", pageData)
if err != nil {
panic(err)
}
}
func (h *gatewayHandler) doUpload(w http.ResponseWriter, r *http.Request) *uploadedPageData {
if false {
err := r.ParseMultipartForm(420)
if err != nil {
err = fmt.Errorf("parsing multipart upload form: %w", err)
log.Print(err)
http.Error(w, "error parsing multipart form", http.StatusBadRequest)
return nil
}
spew.Fdump(w, r.MultipartForm)
return nil
}
confluenceRequest := h.confluence.newRequest(r.Context(), r.Method, &url.URL{Path: "/upload"}, r.Body)
for _, h := range []string{"Content-Type", "Content-Length"} {
confluenceRequest.Header[h] = r.Header[h]
}
confluenceResponse, err := h.confluence.do(confluenceRequest)
if err != nil {
panic(err)
}
defer confluenceResponse.Body.Close()
if confluenceResponse.StatusCode != http.StatusOK {
io.Copy(os.Stderr, confluenceResponse.Body)
panic(confluenceResponse.StatusCode)
}
mi, err := metainfo.Load(confluenceResponse.Body)
if err != nil {
panic(err)
}
ih := mi.HashInfoBytes()
info, err := mi.UnmarshalInfo()
if err != nil {
panic(err)
}
var debug bytes.Buffer
if false {
spew.Fdump(&debug, info)
}
magnet := mi.Magnet(&ih, &info)
addGatewayWebseedToMagnet(&magnet, infohashHost(ih.HexString(), r.Host), h.gatewayWebseedScheme(r), r.URL)
templateData := &uploadedPageData{
ih.HexString(),
template.URL(magnet.String()),
&url.URL{
Scheme: r.URL.Scheme,
Host: infohashHost(ih.HexString(), r.Host),
},
debug.String(),
}
mi.InfoBytes = nil
if false {
spew.Fdump(&debug, mi)
}
// Confluence immediately imports upload data.
if false {
// If the data isn't accessible, the gateway will get stuck trying to load the autoindex if the torrent has one.
templateData.GatewayUrl.RawQuery = "btlink-no-autoindex"
}
return templateData
}
func infohashHost(ihHex, gateway string) string {
return ihHex + "-ih." + gateway
}
func (h *gatewayHandler) resolveDomainDnsLink(w http.ResponseWriter, r *http.Request) bool {
name := "_dnslink." + r.Host
txts, err := net.LookupTXT(name)
if err != nil {
err = fmt.Errorf("resolving txt for %v: %w", name, err)
log.Printf("error %s", err)
return false
}
if len(txts) == 0 {
return false
}
return h.serveBtLinkDomain(w, r, strings.Split(txts[0], "."), generics.None[string]())
}
func (h *gatewayHandler) serveOldBtLinkRedirection(w http.ResponseWriter, r *http.Request) bool {
log.Printf("considering %q for btlink handling", r.Host)
newHost := redirectOldBtRoot(r.Host)
if !newHost.Ok {
return false
}
newUrl := r.URL.ResolveReference(&url.URL{
Host: newHost.Value,
})
http.Redirect(w, r, newUrl.String(), http.StatusTemporaryRedirect)
return true
}
func (h *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) bool {
if h.serveBtLink(w, r) {
return true
}
if h.serveOldBtLinkRedirection(w, r) {
return true
}
if h.resolveDomainDnsLink(w, r) {
return true
}
return false
}
func (h *gatewayHandler) serveBtLink(w http.ResponseWriter, r *http.Request) bool {
log.Printf("considering %q for btlink handling", r.Host)
ss := strings.Split(r.Host, ".")
reverse(ss)
gatewayDomainParts := make([]string, 0, len(ss))
// Strip potential gateway labels
for len(ss) != 0 && ss[0] != rootDomain {
gatewayDomainParts = append(gatewayDomainParts, ss[0])
ss = ss[1:]
}
// The root domain was not seen
if len(ss) == 0 {
return false
}
log.Printf("handling btlink request for %q", requestUrl(r))
gatewayDomainParts = append(gatewayDomainParts, ss[0])
// Strip the root domain
ss = ss[1:]
reverse(gatewayDomainParts)
return h.serveBtLinkDomain(w, r, ss, generics.Some(strings.Join(gatewayDomainParts, ".")))
}
func (h *gatewayHandler) serveBtLinkDomain(w http.ResponseWriter, r *http.Request, ss []string, gatewayDomain generics.Option[string]) bool {
if len(ss) == 0 {
h.serveRoot(w, r)
return true
}
labelParts := strings.Split(ss[0], "-")
ss = ss[1:]
reverse(labelParts)
switch labelParts[0] {
case "ih":
labelParts = labelParts[1:]
reverse(labelParts)
if len(labelParts) != 1 {
http.Error(w, "bad host", http.StatusBadRequest)
return true
}
h.serveTorrentPath(w, r, strings.Join(labelParts, "-"), gatewayDomain)
return true
case "pk":
labelParts = labelParts[1:]
if len(labelParts) == 0 {
http.Error(w, "bad host", http.StatusBadRequest)
return true
}
pk, err := base36.DecodeString(labelParts[0])
if err != nil {
http.Error(w, fmt.Errorf("error decoding public key from base36: %w", err).Error(), http.StatusBadRequest)
return true
}
labelParts = labelParts[1:]
reverse(labelParts)
reverse(ss)
if len(labelParts) != 0 {
ss = append(ss, strings.Join(labelParts, "-"))
}
salt := strings.Join(ss, ".")
log.Printf("salt for %q: %q", r.Host, salt)
target := bep44.MakeMutableTarget(*(*[32]byte)(pk), []byte(salt))
log.Printf("looking up infohash for %q at %x", r.Host, target)
bep46, err := h.getMutableInfohash(target, string(salt))
if err != nil {
log.Printf("error resolving %q: %v", r.Host, err)
http.Error(w, err.Error(), http.StatusNotFound)
return true
}
log.Printf("resolved %q to %x", r.Host, bep46.Ih)
h.serveTorrentPath(w, r, hex.EncodeToString(bep46.Ih[:]), gatewayDomain)
return true
}
http.Error(w, "bad host", http.StatusBadRequest)
return true
}
func redirectOldBtRoot(host string) (newHost generics.Option[string]) {
ss := strings.Split(host, ".")
reverse(ss)
var gatewayParts []string
// Strip potential gateway labels
for len(ss) != 0 && ss[0] != oldRootDomain {
gatewayParts = append(gatewayParts, ss[0])
ss = ss[1:]
}
// The root domain was not seen
if len(ss) == 0 {
return
}
if len(gatewayParts) == 0 || gatewayParts[len(gatewayParts)-1] != rootDomain {
gatewayParts = append(gatewayParts, rootDomain)
}
reverse(gatewayParts)
gateway := strings.Join(gatewayParts, ".")
// Strip the root domain
ss = ss[1:]
if len(ss) == 0 {
return generics.Some(gateway)
}
switch ss[0] {
case "ih":
ss = ss[1:]
reverse(ss)
return generics.Some(infohashHost(strings.Join(ss, "."), gateway))
case "pk":
ss = ss[1:]
reverse(ss)
return generics.Some(strings.Join(ss, ".") + "-pk." + gateway)
}
return
}
func (h *gatewayHandler) getTorrentMetaInfo(w http.ResponseWriter, r *http.Request, ihHex string) (mi metainfo.MetaInfo, ok bool) {
cacheVal, ok := h.metainfoCache.Get(ihHex)
if ok {
mi = cacheVal.(metainfo.MetaInfo)
return
}
resp, err := h.confluence.get(r.Context(), "/metainfo", url.Values{"ih": {ihHex}})
if err != nil {
log.Printf("error getting meta-info from confluence [ih: %q]: %v", ihHex, err)
http.Error(w, "error getting torrent meta-info", http.StatusBadGateway)
return
}
defer resp.Body.Close()
err = bencode.NewDecoder(resp.Body).Decode(&mi)
if err != nil {
log.Printf("error decoding info: %v", err)
http.Error(w, "error decoding torrent meta-info", http.StatusBadGateway)
return
}
ok = true
cost := estimateRecursiveMemoryUse(mi)
log.Printf("store info for %v in cache with estimated cost %v", ihHex, cost)
h.metainfoCache.Set(ihHex, mi, int64(cost))
return
}
func estimateRecursiveMemoryUse(val interface{}) int {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(val)
if err != nil {
panic(err)
}
return buf.Len()
}
type dirPageItem struct {
Href string
Name string
Size string
}
var torrentFilesCollator = collate.New(language.AmericanEnglish, collate.Numeric, collate.IgnoreCase)
func (h *gatewayHandler) gatewayWebseedScheme(r *http.Request) string {
if r.TLS != nil {
return "https"
}
return "http"
}
func requestHasBtlinkQueryFlag(r *http.Request, flag string) bool {
return r.URL.Query().Has(xstrings.ToKebabCase("btlink " + flag))
}
func (h *gatewayHandler) serveTorrentDir(w http.ResponseWriter, r *http.Request, ihHex string, gatewayDomain generics.Option[string]) {
mi, ok := h.getTorrentMetaInfo(w, r, ihHex)
if !ok {
return
}
info, err := mi.UnmarshalInfo()
if err != nil {
panic(err)
}
var subFiles []dirPageItem
autoIndex := !requestHasBtlinkQueryFlag(r, "no autoindex")
baseDisplayPath := r.URL.Path[1:]
upvertedFiles := info.UpvertedFiles()
subDirs := make(map[string]int64, len(upvertedFiles))
for _, f := range upvertedFiles {
dp := f.DisplayPath(&info)
if !strings.HasPrefix(dp, baseDisplayPath) {
continue
}
relPath := dp[len(baseDisplayPath):]
if autoIndex {
// Serve this file as the directory.
if relPath == "index.html" {
h.serveTorrentFile(w, r, ihHex, dp, gatewayDomain)
return
}
}
nextSep := strings.Index(relPath, "/")
if nextSep == -1 {
subFiles = append(subFiles, dirPageItem{
Href: relPath,
Name: relPath,
Size: humanize.Bytes(uint64(f.Length)),
})
} else {
relPath = relPath[:nextSep+1]
subDirs[relPath] += f.Length
}
}
if len(subDirs) == 0 && len(subFiles) == 0 {
http.NotFound(w, r)
return
}
children := make([]dirPageItem, 0, 1+len(subDirs)+len(subFiles))
if baseDisplayPath != "" {
children = append(children, dirPageItem{"../", "../", ""})
}
for relPath, size := range subDirs {
children = append(children, dirPageItem{
Href: relPath,
Name: relPath,
Size: humanize.Bytes(uint64(size)),
})
}
children = append(children, subFiles...)
// Dirs come from an unstable source (map), but their names are unique.
slices.SortStableFunc(children, func(l, r dirPageItem) bool {
lDir := strings.HasSuffix(l.Name, "/")
rDir := strings.HasSuffix(r.Name, "/")
if lDir != rDir {
return lDir
}
i := torrentFilesCollator.CompareString(l.Name, r.Name)
if i != 0 {
return i < 0
}
return false
})
dirPath := r.URL.Path
infoHash := metainfo.NewHashFromHex(ihHex)
w.Header().Set("Content-Type", "text/html")
magnet := mi.Magnet(&infoHash, &info)
spew.Dump(*r.URL)
spew.Dump(h.gatewayWebseedScheme(r), magnet)
addGatewayWebseedToMagnet(&magnet, r.Host, h.gatewayWebseedScheme(r), r.URL)
h.dirPageTemplate.Execute(w, dirPageData{
Path: dirPath,
Children: children,
MagnetURI: template.URL(magnet.String()),
})
}
func addGatewayWebseedToMagnet(m *metainfo.Magnet, gatewayHost, scheme string, baseUrl *url.URL) {
ref := url.URL{
Host: gatewayHost,
Path: "/",
}
if baseUrl.Scheme == "" {
ref.Scheme = scheme
}
m.Params.Add("ws", baseUrl.ResolveReference(&ref).String())
}
type dirPageData struct {
Path string
Children []dirPageItem
MagnetURI template.URL
}
func (h *gatewayHandler) serveTorrentPath(w http.ResponseWriter, r *http.Request, ihHex string, gatewayDomain generics.Option[string]) {
if strings.HasSuffix(r.URL.Path, "/") {
h.serveTorrentDir(w, r, ihHex, gatewayDomain)
return
}
h.serveTorrentFile(w, r, ihHex, r.URL.Path[1:], gatewayDomain)
}
func (h *gatewayHandler) serveTorrentFile(w http.ResponseWriter, r *http.Request, ihHex, filePath string, gatewayDomain generics.Option[string]) {
gatewayDomains := h.gatewayDomains
if gatewayDomain.Ok {
gatewayDomains = append([]string{gatewayDomain.Value}, gatewayDomains...)
}
h.confluence.data(w, r, ihHex, filePath, gatewayDomains)
}
func (h *gatewayHandler) getMutableInfohash(target bep44.Target, salt string) (_ krpc.Bep46Payload, err error) {
ret, err, _ := h.dhtItemCacheGetDedup.Do(string(target[:]), func() (interface{}, error) {
v, ok := h.dhtItemCache.Get(target[:])
if ok {
v := v.(*dhtItemCacheValue)
stale := time.Since(v.updated) >= time.Minute
if !v.updating && stale {
log.Printf("initiating async refresh of cached dht item [target=%x]", target)
v.updating = true
go func() {
bep46, err := h.getMutableInfohashFromDht(target, salt)
v.updating = false
if err != nil {
log.Printf("error updating dht item cache [target=%x]: %v", target, err)
return
}
h.cacheDhtItem(target, bep46)
}()
}
log.Printf("served dht item from cache [target=%x, stale=%v]", target, stale)
return v.payload, nil
}
bep46, err := h.getMutableInfohashFromDht(target, salt)
if err == nil {
h.cacheDhtItem(target, bep46)
}
return bep46, err
})
if err != nil {
return
}
return ret.(krpc.Bep46Payload), err
}
func (h *gatewayHandler) cacheDhtItem(target bep44.Target, bep46 krpc.Bep46Payload) {
stored := h.dhtItemCache.Set(target[:], &dhtItemCacheValue{
updated: time.Now(),
updating: false,
payload: bep46,
target: target,
}, 1)
log.Printf("caching dht item [target=%x, stored=%v]", target, stored)
}
func (h *gatewayHandler) getMutableInfohashFromDht(target bep44.Target, salt string) (_ krpc.Bep46Payload, err error) {
ret, err, _ := h.dhtGetDedup.Do(string(target[:]), func() (_ interface{}, err error) {
b, err := h.confluence.dhtGet(context.Background(), hex.EncodeToString(target[:]), salt)
if err != nil {
err = fmt.Errorf("getting from dht via confluence: %w", err)
return
}
var bep46 krpc.Bep46Payload
err = bencode.Unmarshal(b, &bep46)
if err != nil {
err = fmt.Errorf("unmarshalling bep46 payload from confluence response: %w", err)
return
}
return bep46, err
})
if err != nil {
return
}
return ret.(krpc.Bep46Payload), err
}