Skip to content

Instantly share code, notes, and snippets.

@c0d3kid
Forked from JalfResi/revprox.go
Last active January 16, 2024 16:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save c0d3kid/862276a6291e4aeaf2638e5f7e93a954 to your computer and use it in GitHub Desktop.
Save c0d3kid/862276a6291e4aeaf2638e5f7e93a954 to your computer and use it in GitHub Desktop.
Simple reverse proxy in Go (forked from original to use a struct instead of a closure with ssl)
package main
import (
"crypto/tls"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
type Proxy struct {
target *url.URL
proxy *httputil.ReverseProxy
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
r.Host = p.target.Host
//w.Header().Set("X-Ben", "radi")
p.proxy.ServeHTTP(w, r)
}
func main() {
// Replace 'target' with the URL of the server you want to proxy to
target, err := url.Parse("https://example.com")
if err != nil {
panic(err)
}
// Create a new ReverseProxy instance
proxy := httputil.NewSingleHostReverseProxy(target)
// Configure the reverse proxy to use HTTPS
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Create a new Proxy instance
p := &Proxy{target: target, proxy: proxy}
// Start the HTTP server and register the Proxy instance as the handler
err = http.ListenAndServe(":3000", p)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment