-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-recorder.go
231 lines (200 loc) · 6.7 KB
/
request-recorder.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"runtime"
"strings"
"time"
"gopkg.in/elazarl/goproxy.v1"
"gopkg.in/elazarl/goproxy.v1/transport"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
)
var proxyHeader = "X-proxy-req-id"
func main() {
proxyVerbose := flag.Bool("proxy-verbose", false, "should every proxy request be logged to stdout")
keyParam := flag.String("key", "X-tid", "which header attribute should be used as request key")
proxyAddr := flag.String("proxy", "8080", "proxy listen address")
serverAddr := flag.String("server", "8081", "server listen address")
redisAddr := flag.String("redis-addr", "localhost:6379", "redis address")
redisTTL := flag.Int("redis-ttl", 2*60*60, "redis key ttl in seconds")
flag.Parse()
repository := NewRedisRepository(*redisAddr, *redisTTL)
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = *proxyVerbose
tr := transport.Transport{Proxy: transport.ProxyFromEnvironment}
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
ctx.RoundTripper = goproxy.RoundTripperFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (resp *http.Response, err error) {
ctx.UserData, resp, err = tr.DetailedRoundTrip(req)
return
})
id := fmt.Sprintf("%v-%v", time.Now().UnixNano(), ctx.Session)
uri := req.URL.Host + req.URL.Path
keyValue := req.Header.Get(*keyParam)
Info.Printf("saving request uri:[%s] key:[%s] id:[%s]", uri, keyValue, id)
repository.Add(createRepositoryID(keyValue, uri, strings.ToUpper(req.Method)), id)
req.Header.Add(proxyHeader, id)
b, body := readBody(req.Body)
req.Body = body
content := HttpContent{ID: id, Timestamp: time.Now().UnixNano() / int64(time.Millisecond), URI: req.RequestURI, Method: req.Method, Body: b, Headers: toHeaders(req.Header)}
repository.SetHTTPContent("req-"+id, content)
return req, nil
})
proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if resp == nil {
return resp
}
b, body := readBody(resp.Body)
resp.Body = body
id := resp.Request.Header.Get(proxyHeader)
Info.Printf("saving response id:[%s] code:[%v]", id, resp.StatusCode)
content := HttpContent{ID: id, Timestamp: time.Now().UnixNano() / int64(time.Millisecond), Code: resp.StatusCode, Body: b, Headers: toHeaders(resp.Header)}
repository.SetHTTPContent("resp-"+id, content)
return resp
})
go func(addr string, repository Repository) {
e := echo.New()
e.SetHTTPErrorHandler(errorHandler)
e.Get("/version", version)
e.Get("/requests", requests(repository))
e.Get("/metadata/:id/:type", metadata(repository))
e.Get("/body/:id/:type", body(repository))
Info.Printf("listening server on %v", addr)
server := standard.New(fmt.Sprintf(":%v", addr))
server.SetHandler(e)
server.ListenAndServe()
}(*serverAddr, repository)
Info.Printf("listening proxy on %v", *proxyAddr)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", *proxyAddr), proxy))
}
func createRepositoryID(key, uri, method string) string {
return "id-" + method + "-" + key + "-" + uri
}
func version(c echo.Context) error {
return c.String(http.StatusOK, "v1.0, "+runtime.Version())
}
func requests(repository Repository) echo.HandlerFunc {
return func(c echo.Context) error {
key := c.QueryParam("key")
uri := c.QueryParam("uri")
method := strings.ToUpper(c.QueryParam("method"))
if key == "" || uri == "" || method == "" {
return fmt.Errorf("'key', 'uri' or 'method' is empty")
}
Info.Printf("[%s] get requests for method:[%s] key:[%s] uri:[%s]", "requests", method, key, uri)
ids, err := repository.List(createRepositoryID(key, uri, method))
if err != nil {
return err
}
return c.JSON(http.StatusOK, ids)
}
}
func metadata(repository Repository) echo.HandlerFunc {
return func(c echo.Context) error {
param := c.Param("type")
if "req" != param && "resp" != param {
return fmt.Errorf("type:[%v] not allowed", param)
}
id := c.Param("id")
if id == "" {
return fmt.Errorf("empty id not allowed")
}
Info.Printf("[%s] get meta info for request id:[%s]", "metadata", id)
content, err := repository.GetHTTPContent(param + "-" + id)
if err != nil {
return err
}
return c.JSON(http.StatusOK, supressBody(content))
}
}
func supressBody(content HttpContent) HttpContent {
content.Body = nil
return content
}
func body(repository Repository) echo.HandlerFunc {
return func(c echo.Context) error {
param := c.Param("type")
if "req" != param && "resp" != param {
return fmt.Errorf("type:[%v] not allowed", param)
}
id := c.Param("id")
if id == "" {
return fmt.Errorf("empty id not allowed")
}
Info.Printf("[%s] get body for request id:[%s]", "body", id)
content, err := repository.GetHTTPContent(param + "-" + id)
if err != nil {
return err
}
for k, v := range content.Headers {
c.Response().Header().Add(k, v)
}
if content.Code == 0 {
c.Response().WriteHeader(http.StatusOK)
} else {
c.Response().WriteHeader(content.Code)
}
c.Response().Write(content.Body)
return nil
}
}
func errorHandler(err error, c echo.Context) {
contentType := "application/vnd.rrp.error.v1+json"
switch err := err.(type) {
case *echo.HTTPError:
jsonResponse(c, err.Code, contentType, struct {
Code string
Message string
}{Code: fmt.Sprintf("HTTP%v", err.Code), Message: err.Error()})
default:
jsonResponse(c, http.StatusInternalServerError, contentType, struct {
Code string
Message string
}{Code: "RRP000", Message: err.Error()})
}
}
func jsonResponse(c echo.Context, code int, contentType string, i interface{}) error {
c.Response().Header().Set("Content-Type", contentType)
c.Response().WriteHeader(code)
result, _ := json.Marshal(i)
c.Set("response-body", result)
c.Response().Write([]byte(result))
return nil
}
func readBody(body io.ReadCloser) ([]byte, io.ReadCloser) {
if body == nil {
return nil, nil
}
defer body.Close()
reqBody, err := ioutil.ReadAll(body)
if err != nil {
panic(err)
}
return reqBody, WrapperReader{bytes.NewBuffer(reqBody)}
}
func toHeaders(headers http.Header) map[string]string {
result := make(map[string]string)
for key, value := range headers {
result[key] = value[0]
}
return result
}
type WrapperReader struct {
*bytes.Buffer
}
func (WrapperReader) Close() error { return nil }
type HttpContent struct {
ID string `json:"id,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
URI string `json:"uri,omitempty"`
Method string `json:"method,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Code int `json:"code,omitempty"`
Body []byte `json:"body,omitempty"`
}