I would like that if request localhost:8080?url=google.com
it serves as a reverse proxy to Google. I have seen other posts with some degree of similarity but constraints like maintaining the port.
Here is my go at it:
package main
import(
"fmt"
"log"
"net/url"
"net/http"
"net/http/httputil"
)
type baseHandle struct{}
type handler struct {
proxy *httputil.ReverseProxy
}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.proxy.ServeHTTP(w, r)
}
func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url_arg := r.URL.Query()["url"]
if len(url_arg) < 0 {
w.Write([]byte("403: Host forbidden"))
return
}
url_parsed, err := url.Parse(url_arg[0])
if err != nil {
w.Write([]byte("403: Host forbidden"))
return
}
director := func(req *http.Request) {
req.URL.Scheme = "http://"
req.URL.Host = url_parsed.Host
}
reverseProxy := &httputil.ReverseProxy{Director: director}
handler := handler{proxy: reverseProxy}
http.Handle("/", handler)
http.ListenAndServe(fmt.Sprintf(":%d", 80), nil)
return
}
func main(){
h := &baseHandle{}
http.Handle("/", h)
server := &http.Server{
Addr: ":8080",
Handler: h,
}
log.Fatal(server.ListenAndServe())
}
I feel I'm not that away from accomplishing it cause I see the network bringing stuff on the browser but I must be missing something.
Just to give you a perspective of the reason behind this is that our company VPN blocks all non-https traffic and our team is highly reliable on EMR and their applications (Zeppelin, Jupyter, Livy, etc.).
Creating ELB is out of the table cause EMR clusters are created and deleted really frequently but if I could make this reverse proxy dynamic to all hosts and ports just based on the initial request it could allow us to connect to all the applications rooting all traffic through this server.
question from:
https://stackoverflow.com/questions/65837686/reverse-proxy-with-dynamic-host-and-port 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…