Skip to content

Instantly share code, notes, and snippets.

@aliharis
Created January 25, 2024 19:59
Show Gist options
  • Save aliharis/3e359c0cd87cc6f3e7f6d23e7e374456 to your computer and use it in GitHub Desktop.
Save aliharis/3e359c0cd87cc6f3e7f6d23e7e374456 to your computer and use it in GitHub Desktop.
Reverse proxy in Go
package main
import (
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
// Parse the target URL
targetURL, err := url.Parse("https://target-url.com")
if err != nil {
panic(err)
}
// Create a reverse proxy
proxy := httputil.NewSingleHostReverseProxy(targetURL)
// Modify the request before sending it
proxy.Director = func(req *http.Request) {
req.URL.Scheme = targetURL.Scheme
req.URL.Host = targetURL.Host
req.Host = targetURL.Host // Set the Host header to the target's host
}
// Start the server and listen on port 80 (or any port of your choice)
http.ListenAndServe(":80", proxy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment