From f95c4e4974b270817648879e1dd6d8f9e9ea5993 Mon Sep 17 00:00:00 2001 From: Alexander Karl Date: Mon, 13 Jun 2022 15:59:42 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20Allow=20to=20overwrite=20the=20?= =?UTF-8?q?host?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index a7f0ddc..667fb54 100644 --- a/main.go +++ b/main.go @@ -13,14 +13,20 @@ import ( "strings" ) -type HttpHandler struct{} -var upstream *url.URL +type HttpHandler struct { + overwriteHost *bool +} func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - r.Host = upstream.Host - r.URL.Scheme = upstream.Scheme - proxy := httputil.NewSingleHostReverseProxy(upstream) + var host string + if *h.overwriteHost{ + host = h.upstream.Host + } else { + host = r.Host + } + + r.Host = host proxy.ModifyResponse = func(r *http.Response) error { if strings.Contains(r.Request.URL.Path, "/api/") { @@ -58,6 +64,7 @@ func main() { // Parse flags addr := flag.String("addr", ":8080", "proxy listen address") up := flag.String("upstream", "", "upstream http address") + overwritehost := flag.Bool("overwritehost", false, "overwrite host header") flag.Parse() // Parse upstream url @@ -72,6 +79,7 @@ func main() { // Setup the reverse proxy server httpHandler := &HttpHandler{} + httpHandler.overwriteHost = overwritehost http.Handle("/", httpHandler) err = http.ListenAndServe(*addr, nil) From 2736b04c0c72a33af72ca1ef4dca28912b9677d6 Mon Sep 17 00:00:00 2001 From: Alexander Karl Date: Mon, 13 Jun 2022 16:00:24 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Use=20struct=20to=20ha?= =?UTF-8?q?ndover=20upstream=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 667fb54..1e7f16e 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( type HttpHandler struct { overwriteHost *bool + upstream *url.URL } func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -27,6 +28,8 @@ func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } r.Host = host + r.URL.Scheme = h.upstream.Scheme + proxy := httputil.NewSingleHostReverseProxy(h.upstream) proxy.ModifyResponse = func(r *http.Response) error { if strings.Contains(r.Request.URL.Path, "/api/") { @@ -74,11 +77,9 @@ func main() { log.Fatal(err.Error()) } - // Set upstream - upstream = parsedUpstream - // Setup the reverse proxy server httpHandler := &HttpHandler{} + httpHandler.upstream = parsedUpstream httpHandler.overwriteHost = overwritehost http.Handle("/", httpHandler) err = http.ListenAndServe(*addr, nil) @@ -86,5 +87,4 @@ func main() { if err != nil { log.Fatal(err.Error()) } - } From 74db235146e5f3a1ff3c457b901c2bcda9935521 Mon Sep 17 00:00:00 2001 From: Alexander Karl Date: Mon, 13 Jun 2022 16:10:29 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=92=9A=20Add=20error-handling=20for?= =?UTF-8?q?=20IOReader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 1e7f16e..090119d 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,12 @@ func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.Request.URL.Path, "/api/") { // Read the response body - b, _ := ioutil.ReadAll(r.Body) + b, err := ioutil.ReadAll(r.Body) + + if err != nil { + log.Printf("Error reading response body: %s", err.Error()) + return err + } // Compile the regex var re = regexp.MustCompile(`"created":"(?:.+?)",`) From 47eabb96666650867fa31cac39536301bf3c41b4 Mon Sep 17 00:00:00 2001 From: Alexander Karl Date: Mon, 13 Jun 2022 16:11:19 +0200 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=92=9A=20Change=20HttpHandler=20to=20?= =?UTF-8?q?HTTPHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 090119d..0eb3ed6 100644 --- a/main.go +++ b/main.go @@ -13,12 +13,12 @@ import ( "strings" ) -type HttpHandler struct { +type HTTPHandler struct { overwriteHost *bool upstream *url.URL } -func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { var host string if *h.overwriteHost{ @@ -83,7 +83,7 @@ func main() { } // Setup the reverse proxy server - httpHandler := &HttpHandler{} + httpHandler := &HTTPHandler{} httpHandler.upstream = parsedUpstream httpHandler.overwriteHost = overwritehost http.Handle("/", httpHandler) From e19c158682d5857aea5c58f9605be81a976987eb Mon Sep 17 00:00:00 2001 From: Alexander Karl Date: Mon, 13 Jun 2022 16:11:30 +0200 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=8E=A8=20Format=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 0eb3ed6..16571ca 100644 --- a/main.go +++ b/main.go @@ -15,13 +15,13 @@ import ( type HTTPHandler struct { overwriteHost *bool - upstream *url.URL + upstream *url.URL } func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { var host string - if *h.overwriteHost{ + if *h.overwriteHost { host = h.upstream.Host } else { host = r.Host