|
1 | 1 | package gohpts |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/tls" |
4 | 5 | "fmt" |
5 | 6 | "io" |
6 | 7 | "net" |
@@ -42,6 +43,18 @@ func delHopHeaders(header http.Header) { |
42 | 43 | } |
43 | 44 | } |
44 | 45 |
|
| 46 | +// delConnectionHeaders removes hop-by-hop headers listed in the "Connection" header |
| 47 | +// https://datatracker.ietf.org/doc/html/rfc7230#section-6.1 |
| 48 | +func delConnectionHeaders(h http.Header) { |
| 49 | + for _, f := range h["Connection"] { |
| 50 | + for _, sf := range strings.Split(f, ",") { |
| 51 | + if sf = strings.TrimSpace(sf); sf != "" { |
| 52 | + h.Del(sf) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
45 | 58 | func appendHostToXForwardHeader(header http.Header, host string) { |
46 | 59 | if prior, ok := header["X-Forwarded-For"]; ok { |
47 | 60 | host = strings.Join(prior, ", ") + ", " + host |
@@ -78,6 +91,8 @@ func (app *app) handleForward(w http.ResponseWriter, r *http.Request) { |
78 | 91 | w.WriteHeader(http.StatusInternalServerError) |
79 | 92 | return |
80 | 93 | } |
| 94 | + req.RequestURI = "" |
| 95 | + delConnectionHeaders(r.Header) |
81 | 96 | delHopHeaders(r.Header) |
82 | 97 | copyHeader(req.Header, r.Header) |
83 | 98 | if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil { |
@@ -111,6 +126,7 @@ func (app *app) handleForward(w http.ResponseWriter, r *http.Request) { |
111 | 126 | } |
112 | 127 | defer resp.Body.Close() |
113 | 128 |
|
| 129 | + delConnectionHeaders(resp.Header) |
114 | 130 | delHopHeaders(resp.Header) |
115 | 131 | copyHeader(w.Header(), resp.Header) |
116 | 132 | w.WriteHeader(resp.StatusCode) |
@@ -254,6 +270,9 @@ func New(conf *Config) *app { |
254 | 270 | MaxHeaderBytes: 1 << 20, |
255 | 271 | } |
256 | 272 | hc := &http.Client{ |
| 273 | + Transport: &http.Transport{ |
| 274 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 275 | + }, |
257 | 276 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
258 | 277 | return http.ErrUseLastResponse |
259 | 278 | }, |
|
0 commit comments