commit 8f888cddac2ca88a7dccd73ee236dda1c88caa26 Author: MrMelon54 Date: Mon Oct 23 12:31:27 2023 +0100 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1c6eea --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +host-rewriter diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f64538c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/MrMelon54/host-rewriter + +go 1.21.3 diff --git a/host-rewriter.go b/host-rewriter.go new file mode 100644 index 0000000..5e9e4eb --- /dev/null +++ b/host-rewriter.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "log" + "net/http" + "net/http/httputil" + "net/url" + "time" +) + +var listenFlag, targetFlag, hostFlag string + +func main() { + flag.StringVar(&listenFlag, "listen", ":8080", "Listen address") + flag.StringVar(&targetFlag, "target", "", "Set target URL to forward to") + flag.StringVar(&hostFlag, "host", "", "Set the host value") + flag.Parse() + + target, err := url.Parse(targetFlag) + if err != nil { + log.Fatal("Failed to parse target URL:", err) + } + + proxy := &httputil.ReverseProxy{ + Rewrite: func(r *httputil.ProxyRequest) { + r.SetURL(target) + r.Out.Host = hostFlag + }, + } + s := http.Server{ + Addr: listenFlag, + Handler: proxy, + ReadTimeout: 150 * time.Second, + ReadHeaderTimeout: 150 * time.Second, + WriteTimeout: 150 * time.Second, + IdleTimeout: 150 * time.Second, + MaxHeaderBytes: 4096000, + } + + log.Printf("Host Rewriter listening on %s\n", listenFlag) + log.Printf("Rewriting host to '%s' and forwarding to '%s'\n", hostFlag, targetFlag) + if err := s.ListenAndServe(); err != nil { + log.Fatal(err) + } +}