Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

✨ Allow to overwrite the host #18

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,34 @@ import (
"strings"
)

type HttpHandler struct{}
var upstream *url.URL
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 {
host = h.upstream.Host
} else {
host = r.Host
}

r.Host = upstream.Host
r.URL.Scheme = upstream.Scheme
proxy := httputil.NewSingleHostReverseProxy(upstream)
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/") {

// 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":"(?:.+?)",`)
Expand Down Expand Up @@ -58,6 +72,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
Expand All @@ -67,16 +82,14 @@ func main() {
log.Fatal(err.Error())
}

// Set upstream
upstream = parsedUpstream

// Setup the reverse proxy server
httpHandler := &HttpHandler{}
httpHandler := &HTTPHandler{}
httpHandler.upstream = parsedUpstream
httpHandler.overwriteHost = overwritehost
http.Handle("/", httpHandler)
err = http.ListenAndServe(*addr, nil)

if err != nil {
log.Fatal(err.Error())
}

}