-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
75 lines (65 loc) · 1.73 KB
/
proxy.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
package main
import (
"bytes"
"compress/gzip"
"fmt"
"github.com/andybalholm/brotli"
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"time"
)
func NewProxyHandler(
URL *url.URL,
logChan chan Log,
stdout bool,
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = URL.Host
r.URL.Host = URL.Host
r.URL.Scheme = URL.Scheme
var reqBody bytes.Buffer
r.Body = io.NopCloser(io.TeeReader(r.Body, &reqBody))
res := httptest.NewRecorder()
start := time.Now()
httputil.NewSingleHostReverseProxy(URL).ServeHTTP(res, r)
elapsed := time.Since(start).Truncate(time.Millisecond) / time.Millisecond
for k, v := range res.Header() {
w.Header()[k] = v
}
w.WriteHeader(res.Code)
w.Write(res.Body.Bytes())
r.Body = io.NopCloser(bytes.NewReader(reqBody.Bytes()))
if res.Header().Get("Content-Encoding") == "gzip" {
reader, _ := gzip.NewReader(res.Body)
data, _ := io.ReadAll(reader)
reader.Close()
res.Body = bytes.NewBuffer(data)
} else if res.Header().Get("Content-Encoding") == "br" {
reader := brotli.NewReader(res.Body)
data, _ := io.ReadAll(reader)
res.Body = bytes.NewBuffer(data)
}
dumpReq, _ := httputil.DumpRequest(r, true)
dumpRes, _ := httputil.DumpResponse(res.Result(), true)
if stdout {
fmt.Println("------------------------")
fmt.Println(string(dumpReq) + "\n")
fmt.Println(string(dumpRes) + "\n\n")
}
logChan <- Log{
Method: r.Method,
URL: *r.URL,
FullURL: r.URL.String(),
Status: res.Result().Status,
StatusCode: res.Result().StatusCode,
Req: string(dumpReq),
Res: string(dumpRes),
Elapsed: elapsed,
StartedAt: start,
DoneAt: time.Now(),
}
})
}