-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel.go
110 lines (94 loc) · 2.56 KB
/
tunnel.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
package xo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/256dpi/serve"
)
const envelopeContentType = "application/x-sentry-envelope"
var newlineBytes = []byte("\n")
// VerifyDSN will require the submitted DNS to match one of the provided DSNs.
func VerifyDSN(list ...string) func(*http.Request, string, *url.URL) bool {
return func(_ *http.Request, reqDSN string, _ *url.URL) bool {
for _, dsn := range list {
if reqDSN == dsn {
return true
}
}
return false
}
}
// Tunnel returns a handler that forwards received Sentry envelopes to the
// endpoint specified by the DSN received in the envelopes. The optional verify
// callback may set to verify received DSNs. This handler can be used together
// with the sentry tunnel feature to relay browser errors via a custom endpoint.
func Tunnel(limit int64, verify func(*http.Request, string, *url.URL) bool, reporter func(error)) http.Handler {
// prepare client
var client http.Client
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// enforce limit
if limit != 0 {
serve.LimitBody(w, r, limit)
}
// read body
body, err := io.ReadAll(r.Body)
if err == serve.ErrBodyLimitExceeded {
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
} else if err != nil {
reporter(W(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
// split out header
parts := bytes.SplitN(body, newlineBytes, 2)
// parse header data
var headerData struct {
DSN string `json:"dsn"`
}
err = json.Unmarshal(parts[0], &headerData)
if err != nil {
reporter(W(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
// parse dsn
dsn, err := url.Parse(headerData.DSN)
if err != nil {
reporter(W(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
// verify dsn if available
if verify != nil && !verify(r, headerData.DSN, dsn) {
w.WriteHeader(http.StatusBadRequest)
return
}
// prepare target
target := fmt.Sprintf("%s://%s/api/%s/envelope/", dsn.Scheme, dsn.Host, strings.Trim(dsn.Path, "/"))
// post envelope
res, err := client.Post(target, envelopeContentType, bytes.NewReader(body))
if err != nil {
reporter(W(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer res.Body.Close()
// check status
if res.StatusCode != http.StatusOK {
w.WriteHeader(http.StatusBadGateway)
return
}
// copy result
_, err = io.Copy(w, res.Body)
if err != nil {
reporter(W(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
})
}