From 8f888cddac2ca88a7dccd73ee236dda1c88caa26 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Mon, 23 Oct 2023 12:31:27 +0100 Subject: [PATCH] First commit --- .gitignore | 2 ++ go.mod | 3 +++ host-rewriter.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 host-rewriter.go 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) + } +}