-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
291 lines (247 loc) · 7.98 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package proxy
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"net/url"
"strings"
"github.com/FrauElster/proxy/internal"
"github.com/PuerkitoBio/goquery"
)
type Target struct {
BaseUrl string
Prefix string
// PreRequest can be used to manipulate the http.Request
PreRequest func(*http.Request) *http.Request
// PostRequest can be used to manipulate the http.Response
// if the request failed, *http.Response will be nil and the returned value will be ignored
PostRequest func(*http.Response) *http.Response
}
type ProxyOption func(*Proxy)
// WithSsl enables SSL for the proxy server
// ListenAndServe will use http.ListenAndServeTLS instead of http.ListenAndServe
func WithSsl(cert tls.Certificate) ProxyOption {
return func(p *Proxy) { p.cert = &cert }
}
// WithTransport sets the transport used by the proxy server
func WithTransport(transport http.RoundTripper) ProxyOption {
return func(p *Proxy) { p.transport = transport }
}
func WithPort(port int) ProxyOption {
return func(p *Proxy) { p.port = port }
}
type Proxy struct {
targets map[string]Target
transport http.RoundTripper
server *http.Server
port int
addr *url.URL
cert *tls.Certificate
}
func NewProxy(targets []Target, opts ...ProxyOption) (*Proxy, error) {
targetMap := make(map[string]Target)
for i, target := range targets {
if !strings.HasPrefix(target.Prefix, "/") {
target.Prefix = "/" + target.Prefix
}
_, err := url.Parse(target.BaseUrl)
if err != nil {
return nil, fmt.Errorf("error parsing target URL %s: %w", target.BaseUrl, err)
}
targetMap[target.Prefix] = targets[i]
}
p := &Proxy{
targets: targetMap,
transport: http.DefaultTransport,
}
for _, opt := range opts {
opt(p)
}
p.addr = &url.URL{Scheme: "http", Host: fmt.Sprintf("0.0.0.0:%d", p.port)}
if p.cert != nil {
p.addr.Scheme = "https"
}
return p, nil
}
// ListenAndServe starts the proxy server
// It blocks until the server is shut down
// If the proxy server was started with WithSsl, it will use http.ListenAndServeTLS instead of http.ListenAndServe
func (p *Proxy) ListenAndServe() (err error) {
// start listener (so we can get the actual port, even if it was chosen by the OS)
listener, err := net.Listen("tcp", p.addr.Host)
if err != nil {
return fmt.Errorf("error starting listener: %w", err)
}
defer listener.Close()
p.addr.Host = listener.Addr().String()
// build server
mux := http.NewServeMux()
for path, target := range p.targets {
target := target
mux.HandleFunc(path, p.forwardRequest(&target))
}
p.server = &http.Server{
Addr: p.addr.Host,
Handler: mux,
}
// start server
if p.cert == nil {
return p.server.Serve(listener)
}
// start TLS server
p.server.TLSConfig = &tls.Config{Certificates: []tls.Certificate{*p.cert}}
return p.server.ServeTLS(listener, "", "")
}
func (p *Proxy) Shutdown(ctx context.Context) error {
return p.server.Shutdown(ctx)
}
func (p *Proxy) Addr() string {
return p.addr.String()
}
func (p *Proxy) forwardRequest(target *Target) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
newReq, err := buildRequest(r, *target)
if err != nil {
slog.Warn("Error constructing new request", "err", err)
http.Error(w, "Error constructing new request", http.StatusBadGateway)
return
}
// Send the new request
if target.PreRequest != nil {
newReq = target.PreRequest(newReq)
}
client := &http.Client{Transport: p.transport}
resp, err := client.Do(newReq)
if target.PostRequest != nil {
resp = target.PostRequest(resp)
}
if err != nil {
slog.Warn("Error forwarding request", "err", err)
http.Error(w, "Error forwarding request", http.StatusBadGateway)
return
}
// If it's an OPTIONS request (a preflight CORS request), respond with OK
if r.Method == http.MethodOptions {
// Add CORS headers
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.WriteHeader(http.StatusOK)
return
}
err = p.copyResponse(resp, w, *target)
if err != nil {
slog.Warn("Error copying response", "err", err)
http.Error(w, "Error copying response", http.StatusBadGateway)
return
}
}
}
func (p *Proxy) copyResponse(resp *http.Response, w http.ResponseWriter, target Target) error {
// Copy the headers from the target server to the original response writer
copyHeaders(resp, w)
// we have to decompress the response before we can copy the body
encoding := resp.Header.Get("Content-Encoding")
if encoding != "" {
err := internal.DecompressResponse(resp)
if err != nil {
return fmt.Errorf("error decompressing response body: %w", err)
}
}
defer resp.Body.Close()
// Copy the body from the target server to the original response writer
newBody, err := p.copyBody(resp, target)
if err != nil {
return fmt.Errorf("error copying response body: %w", err)
}
// compress the response again
if encoding != "" {
newBody, err = internal.CompressBody(newBody, internal.SupportedCompression(encoding))
if err != nil {
return fmt.Errorf("error compressing response body: %w", err)
}
w.Header().Set("Content-Encoding", encoding)
}
w.WriteHeader(resp.StatusCode)
w.Write([]byte(newBody))
return nil
}
func copyHeaders(resp *http.Response, w http.ResponseWriter) {
for name, values := range resp.Header {
for _, value := range values {
w.Header().Add(name, value)
}
}
// Add CORS headers
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
}
func (p *Proxy) copyBody(resp *http.Response, target Target) ([]byte, error) {
// if not HTML just copy the body
if !strings.Contains(resp.Header.Get("Content-Type"), "text/html") {
return io.ReadAll(resp.Body)
}
// parse HTML
originalBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading (decompressed) response body")
}
document, err := goquery.NewDocumentFromReader(bytes.NewReader(originalBody))
if err != nil {
return nil, fmt.Errorf("error parsing HTML content")
}
// Replace all links and script tags with the proxy URL
document.Find("a[href], img[src], link[href], script[src]").Each(func(index int, element *goquery.Selection) {
for _, attr := range []string{"href", "src"} {
if val, exists := element.Attr(attr); exists {
isDynamic := strings.HasPrefix(val, "/")
isOnOriginalHost := strings.HasPrefix(val, target.BaseUrl)
url := p.addr
url.Path = internal.JoinUrl(target.Prefix, strings.TrimPrefix(val, target.BaseUrl))
if isDynamic || isOnOriginalHost {
element.SetAttr(attr, url.String())
}
}
}
})
// parse back to HTML
newBody, err := document.Html()
if err != nil {
return nil, fmt.Errorf("error getting modified HTML content")
}
return []byte(newBody), nil
}
func buildRequest(originalReq *http.Request, target Target) (*http.Request, error) {
// Create a new URL from the base URL of the target server and the path from the original request
targetAsUrl, err := url.Parse(target.BaseUrl)
if err != nil {
return nil, fmt.Errorf("error parsing target URL")
}
newURL := *originalReq.URL
newURL.Scheme = targetAsUrl.Scheme
newURL.Host = targetAsUrl.Host
newURL.Path = strings.TrimPrefix(newURL.Path, target.Prefix)
// Create a new request with the original method, the new URL, and the original body
bodyBytes, err := io.ReadAll(originalReq.Body)
if err != nil {
return nil, fmt.Errorf("error reading request body")
}
newReq, err := http.NewRequest(originalReq.Method, newURL.String(), io.NopCloser(bytes.NewReader(bodyBytes)))
if err != nil {
return nil, fmt.Errorf("error creating new request")
}
// Copy the original headers to the new request
for name, values := range originalReq.Header {
for _, value := range values {
newReq.Header.Add(name, value)
}
}
newReq.Close = true
return newReq, nil
}