forked from inconshreveable/ngrok
/
http.go
269 lines (236 loc) · 6.38 KB
/
http.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
// interactive web user interface
package web
import (
"encoding/base64"
"encoding/json"
"encoding/xml"
"html/template"
"net/http"
"net/http/httputil"
"net/url"
"ngrok/client/assets"
"ngrok/client/mvc"
"ngrok/log"
"ngrok/proto"
"ngrok/util"
"strings"
"unicode/utf8"
)
type SerializedTxn struct {
Id string
Duration int64
Start int64
ConnCtx mvc.ConnectionContext
*proto.HttpTxn `json:"-"`
Req SerializedRequest
Resp SerializedResponse
}
type SerializedBody struct {
RawContentType string
ContentType string
Text string
Length int
Error string
ErrorOffset int
Form url.Values
}
type SerializedRequest struct {
Raw string
MethodPath string
Params url.Values
Header http.Header
Body SerializedBody
Binary bool
}
type SerializedResponse struct {
Raw string
Status string
Header http.Header
Body SerializedBody
Binary bool
}
type WebHttpView struct {
log.Logger
webview *WebView
ctl mvc.Controller
httpProto *proto.Http
state chan SerializedUiState
HttpRequests *util.Ring
idToTxn map[string]*SerializedTxn
}
type SerializedUiState struct {
Tunnels []mvc.Tunnel
}
type SerializedPayload struct {
Txns []interface{}
UiState SerializedUiState
}
func newWebHttpView(ctl mvc.Controller, wv *WebView, proto *proto.Http) *WebHttpView {
whv := &WebHttpView{
Logger: log.NewPrefixLogger("view", "web", "http"),
webview: wv,
ctl: ctl,
httpProto: proto,
idToTxn: make(map[string]*SerializedTxn),
HttpRequests: util.NewRing(20),
}
ctl.Go(whv.updateHttp)
whv.register()
return whv
}
type XMLDoc struct {
data []byte `xml:",innerxml"`
}
func makeBody(h http.Header, body []byte) SerializedBody {
b := SerializedBody{
Length: len(body),
Text: base64.StdEncoding.EncodeToString(body),
ErrorOffset: -1,
}
// some errors like XML errors only give a line number
// and not an exact offset
offsetForLine := func(line int) int {
lines := strings.SplitAfterN(b.Text, "\n", line)
return b.Length - len(lines[len(lines)-1])
}
var err error
b.RawContentType = h.Get("Content-Type")
if b.RawContentType != "" {
b.ContentType = strings.TrimSpace(strings.Split(b.RawContentType, ";")[0])
switch b.ContentType {
case "application/xml", "text/xml":
err = xml.Unmarshal(body, new(XMLDoc))
if err != nil {
if syntaxError, ok := err.(*xml.SyntaxError); ok {
// xml syntax errors only give us a line number, so we
// count to find an offset
b.ErrorOffset = offsetForLine(syntaxError.Line)
}
}
case "application/json":
err = json.Unmarshal(body, new(json.RawMessage))
if err != nil {
if syntaxError, ok := err.(*json.SyntaxError); ok {
b.ErrorOffset = int(syntaxError.Offset)
}
}
case "application/x-www-form-urlencoded":
b.Form, err = url.ParseQuery(string(body))
}
}
if err != nil {
b.Error = err.Error()
}
return b
}
func (whv *WebHttpView) updateHttp() {
// open channels for incoming http state changes
// and broadcasts
txnUpdates := whv.httpProto.Txns.Reg()
for txn := range txnUpdates {
// XXX: it's not safe for proto.Http and this code
// to be accessing txn and txn.(req/resp) without synchronization
htxn := txn.(*proto.HttpTxn)
// we haven't processed this transaction yet if we haven't set the
// user data
if htxn.UserCtx == nil {
rawReq, err := proto.DumpRequestOut(htxn.Req.Request, true)
if err != nil {
whv.Error("Failed to dump request: %v", err)
continue
}
body := makeBody(htxn.Req.Header, htxn.Req.BodyBytes)
whtxn := &SerializedTxn{
Id: util.RandId(8),
HttpTxn: htxn,
Req: SerializedRequest{
MethodPath: htxn.Req.Method + " " + htxn.Req.URL.Path,
Raw: base64.StdEncoding.EncodeToString(rawReq),
Params: htxn.Req.URL.Query(),
Header: htxn.Req.Header,
Body: body,
Binary: !utf8.Valid(rawReq),
},
Start: htxn.Start.Unix(),
ConnCtx: htxn.ConnUserCtx.(mvc.ConnectionContext),
}
htxn.UserCtx = whtxn
// XXX: unsafe map access from multiple go routines
whv.idToTxn[whtxn.Id] = whtxn
// XXX: use return value to delete from map so we don't leak memory
whv.HttpRequests.Add(whtxn)
} else {
rawResp, err := httputil.DumpResponse(htxn.Resp.Response, true)
if err != nil {
whv.Error("Failed to dump response: %v", err)
continue
}
txn := htxn.UserCtx.(*SerializedTxn)
body := makeBody(htxn.Resp.Header, htxn.Resp.BodyBytes)
txn.Duration = htxn.Duration.Nanoseconds()
txn.Resp = SerializedResponse{
Status: htxn.Resp.Status,
Raw: base64.StdEncoding.EncodeToString(rawResp),
Header: htxn.Resp.Header,
Body: body,
Binary: !utf8.Valid(rawResp),
}
payload, err := json.Marshal(txn)
if err != nil {
whv.Error("Failed to serialized txn payload for websocket: %v", err)
}
whv.webview.wsMessages.In() <- payload
}
}
}
func (whv *WebHttpView) register() {
http.HandleFunc("/http/in/replay", func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
err := util.MakePanicTrace(r)
whv.Error("Replay failed: %v", err)
http.Error(w, err, 500)
}
}()
r.ParseForm()
txnid := r.Form.Get("txnid")
if txn, ok := whv.idToTxn[txnid]; ok {
reqBytes, err := base64.StdEncoding.DecodeString(txn.Req.Raw)
if err != nil {
panic(err)
}
whv.ctl.PlayRequest(txn.ConnCtx.Tunnel, reqBytes)
w.Write([]byte(http.StatusText(200)))
} else {
http.Error(w, http.StatusText(400), 400)
}
})
http.HandleFunc("/http/in", func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
err := util.MakePanicTrace(r)
whv.Error("HTTP web view failed: %v", err)
http.Error(w, err, 500)
}
}()
pageTmpl, err := assets.Asset("assets/client/page.html")
if err != nil {
panic(err)
}
tmpl := template.Must(template.New("page.html").Delims("{%", "%}").Parse(string(pageTmpl)))
payloadData := SerializedPayload{
Txns: whv.HttpRequests.Slice(),
UiState: SerializedUiState{Tunnels: whv.ctl.State().GetTunnels()},
}
payload, err := json.Marshal(payloadData)
if err != nil {
panic(err)
}
// write the response
if err := tmpl.Execute(w, string(payload)); err != nil {
panic(err)
}
})
}
func (whv *WebHttpView) Shutdown() {
}