-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
75 lines (66 loc) · 1.64 KB
/
proxy.go
File metadata and controls
75 lines (66 loc) · 1.64 KB
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 (
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
)
var stripResponseHeaders = []string{
"Set-Cookie",
"Server",
"X-Powered-By",
}
var proxyTransport = &http.Transport{
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
func newProxy() http.Handler {
if cfg.GatewayURL == "" {
return http.NotFoundHandler()
}
target, err := url.Parse(cfg.GatewayURL)
if err != nil {
slog.Error("invalid GATEWAY_URL", "error", err)
os.Exit(1)
}
rp := &httputil.ReverseProxy{
Transport: proxyTransport,
Rewrite: func(pr *httputil.ProxyRequest) {
pr.SetURL(target)
pr.Out.URL.Path = strings.TrimPrefix(pr.In.URL.Path, "/control")
if pr.Out.URL.Path == "" {
pr.Out.URL.Path = "/"
}
pr.Out.Host = target.Host
pr.Out.Header.Del("Cookie")
pr.Out.Header.Del("Authorization")
pr.Out.Header.Del("X-Forwarded-For")
pr.Out.Header.Del("X-Forwarded-Proto")
pr.Out.Header.Del("X-Forwarded-Host")
pr.Out.Header.Del("X-Real-IP")
pr.Out.Header.Del("Forwarded")
pr.SetXForwarded()
},
ModifyResponse: func(resp *http.Response) error {
for _, h := range stripResponseHeaders {
resp.Header.Del(h)
}
return nil
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
slog.Error("proxy upstream error", "error", err)
badGateway(w)
},
}
return rp
}
func badGateway(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte(`{"error":"Bad Gateway"}`))
}