-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-proxy-utils.go
106 lines (85 loc) · 3.19 KB
/
reverse-proxy-utils.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
package reverse_proxy
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"github.com/bygui86/go-reverse-proxy/reverse-proxy/logging"
)
func setupSingleHostReverseProxy(targetUrlString string) (*httputil.ReverseProxy, error) {
logging.SugaredLog.Debugf("Setup new single-host reverse proxy to target %s", targetUrlString)
logging.SugaredLog.Debugf("Parse target URL %s", targetUrlString)
targetUrl, urlErr := url.Parse(targetUrlString)
if urlErr != nil {
logging.SugaredLog.Errorf("Parse downstream url %s failed: %s", targetUrl, urlErr.Error())
return nil, urlErr
}
logging.Log.Debug("Create reverse proxy")
proxy := httputil.NewSingleHostReverseProxy(targetUrl)
logging.Log.Debug("Setup proxy director")
proxy.Director = addXForwardHeaderToDefaultDirector(proxy)
logging.Log.Debug("Set proxy modify response")
proxy.ModifyResponse = modifyResponse
// INFO: if not using gorilla mux router with HTTP server, uncomment this line to directly access to reverse proxy
// http.HandleFunc(rootEndpoint, r.proxy.ServeHTTP)
return proxy, nil
}
func addXForwardHeaderToDefaultDirector(proxy *httputil.ReverseProxy) func(req *http.Request) {
logging.Log.Debug("Get default director from proxy")
defaultDirector := proxy.Director
logging.Log.Debug("Set custom director on proxy")
return func(req *http.Request) {
logging.Log.Debug("Custom proxy director")
logRequestInfo(req, "BEFORE default redirection")
// BEFORE DEFAULT REDIRECTION
// req.Host = localhost:8080
// req.Header.Get(hostHeaderKey) = -empty-
// req.URL.Host = -empty-
// req.URL.Scheme = -empty-
// WARN: this is required to perform a correct redirection (see httputil.NewSingleHostReverseProxy)
defaultDirector(req)
logRequestInfo(req, "AFTER default redirection")
// AFTER DEFAULT REDIRECTION
// req.Host = localhost:8080
// req.Header.Get(hostHeaderKey) = -empty-
// req.URL.Host = localhost:8081
// req.URL.Scheme = http
req.Header.Set(xForwardedHost, req.Host)
}
}
func logRequestInfo(req *http.Request, prefix string) {
logging.SugaredLog.Debugf("[%s] Host: req[%s], reqHeader[%s], reqUrl[%s]",
prefix, req.Host, req.Header.Get(hostHeaderKey), req.URL.Host)
logging.SugaredLog.Debugf("[%s] Scheme: [%s]", prefix, req.URL.Scheme)
}
func modifyResponse(res *http.Response) error {
body, err := duplicateResponseBody(res)
if err != nil {
return err
}
return customBehaviour(body)
}
// Because go lang is a pain in the ass if you read the body then any subsequent calls
// are unable to read the body again...
func duplicateResponseBody(res *http.Response) ([]byte, error) {
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}
closeErr := res.Body.Close()
if closeErr != nil {
logging.SugaredLog.Errorf("Close response body failed: %s", closeErr.Error())
}
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return body, nil
}
// TODO better understand usage
func dumpResponse(res *http.Response) ([]byte, error) {
return httputil.DumpResponse(res, true)
}
// INFO: add your custom behaviour here
func customBehaviour(responseBody []byte) error {
logging.SugaredLog.Infof("Custom behaviour on response body: %s", string(responseBody))
return nil
}