Created
February 10, 2023 16:23
-
-
Save sebasrock/5b023ae49749ff2be19bcd7689a0a981 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)
This file contains hidden or 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 ( | |
"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