-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
confluence.go
103 lines (96 loc) · 2.86 KB
/
confluence.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
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type confluenceHandler struct {
confluenceHost string
confluenceScheme string
confluenceTransport http.RoundTripper
}
func (ch *confluenceHandler) data(w http.ResponseWriter, incomingRequest *http.Request, ih string, path string, gatewayDomains []string) {
(&httputil.ReverseProxy{
Director: func(r *http.Request) {
r.URL.Host = ch.confluenceHost
r.URL.Scheme = ch.confluenceScheme
r.URL.Path = "/data"
r.URL.RawQuery = url.Values{"ih": {ih}, "path": {path}}.Encode()
},
ModifyResponse: func(r *http.Response) error {
// Looks like it's sufficient to set a good Content-Type.
if false {
// Confluence sets filename=, we also want to ensure inline. It might be sufficient for
// this project to just set inline and let the browser infer filename because we're
// routing torrent file paths using the URL path component.
r.Header.Set("Content-Disposition", "inline")
}
// Copied from anacrolix/webtorrent for streaming in Chrome:
if r.Header.Get("Content-Type") == "video/x-matroska" {
r.Header.Set("Content-Type", "video/webm")
}
if requestHasBtlinkQueryFlag(incomingRequest, "src only") {
r.Header.Set(
"Content-Security-Policy",
strings.Join(
append(
[]string{"default-src", "'self'"},
func() (hostSources []string) {
for _, domain := range gatewayDomains {
hostSources = append(hostSources, domain, "*."+domain)
}
return
}()...),
" "))
}
return nil
},
Transport: ch.confluenceTransport,
}).ServeHTTP(w, incomingRequest)
}
func (ch *confluenceHandler) newRequest(ctx context.Context, method string, ref *url.URL, body io.Reader) *http.Request {
base := url.URL{
Scheme: ch.confluenceScheme,
Host: ch.confluenceHost,
}
u := base.ResolveReference(ref)
log.Printf("%q", u.String())
req, err := http.NewRequestWithContext(ctx, method, u.String(), body)
if err != nil {
panic(err)
}
return req
}
func (ch *confluenceHandler) do(req *http.Request) (resp *http.Response, err error) {
// TODO: Reuse
hc := http.Client{
Transport: ch.confluenceTransport,
}
resp, err = hc.Do(req)
return
}
func (ch *confluenceHandler) get(ctx context.Context, path string, q url.Values) (resp *http.Response, err error) {
req := ch.newRequest(ctx, http.MethodGet, &url.URL{
Path: path,
RawQuery: q.Encode(),
}, nil)
return ch.do(req)
}
func (ch *confluenceHandler) dhtGet(ctx context.Context, target, salt string) (b []byte, err error) {
resp, err := ch.get(ctx, "/bep44", url.Values{"target": {target}, "salt": {salt}})
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("unexpected response status code: %v", resp.StatusCode)
return
}
b, err = io.ReadAll(resp.Body)
return
}