Created
January 25, 2024 19:59
-
-
Save aliharis/3e359c0cd87cc6f3e7f6d23e7e374456 to your computer and use it in GitHub Desktop.
Reverse proxy in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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