From 7dff34e6e3e06c67f29c62cc12c1e7594322e6d0 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 18 Nov 2020 14:05:29 -0700 Subject: [PATCH] add missing http01proxy file --- internal/http01proxy/proxy.go | 83 +++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 internal/http01proxy/proxy.go diff --git a/internal/http01proxy/proxy.go b/internal/http01proxy/proxy.go new file mode 100644 index 0000000..bd1f8a4 --- /dev/null +++ b/internal/http01proxy/proxy.go @@ -0,0 +1,83 @@ +package http01proxy + +import ( + "net/http" + "net/http/httputil" + "net/url" + "strings" + "time" +) + +// ListenAndServe will start the HTTP-01 proxy +func ListenAndServe(target string, timeout time.Duration) error { + target = strings.TrimSuffix(target, "/") + + // TODO accept listener? + targetURL, err := url.Parse(target) + if nil != err { + panic(err) + } + + //proxyHandler := httputil.NewSingleHostReverseProxy(targetURL) + proxyHandler := &httputil.ReverseProxy{ + Director: func(req *http.Request) { + req.Header.Del("X-Forwarded-For") + req.Header.Del("X-Forwarded-Proto") + req.Header.Del("X-Forwarded-Port") + + targetQuery := targetURL.RawQuery + req.URL.Scheme = targetURL.Scheme + req.URL.Host = targetURL.Host + req.Host = targetURL.Host + //req.Header.Set("Host", targetURL.Host) + req.URL.Path, req.URL.RawPath = joinURLPath(targetURL, req.URL) + + if targetQuery == "" || req.URL.RawQuery == "" { + req.URL.RawQuery = targetQuery + req.URL.RawQuery + } else { + req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery + } + + if _, ok := req.Header["User-Agent"]; !ok { + // explicitly disable User-Agent so it's not set to default value + req.Header.Set("User-Agent", "") + } + }, + } + + return http.ListenAndServe(":80", proxyHandler) +} + +// Taken from https://golang.org/src/net/http/httputil/reverseproxy.go +func joinURLPath(a, b *url.URL) (path, rawpath string) { + if a.RawPath == "" && b.RawPath == "" { + return singleJoiningSlash(a.Path, b.Path), "" + } + // Same as singleJoiningSlash, but uses EscapedPath to determine + // whether a slash should be added + apath := a.EscapedPath() + bpath := b.EscapedPath() + + aslash := strings.HasSuffix(apath, "/") + bslash := strings.HasPrefix(bpath, "/") + + switch { + case aslash && bslash: + return a.Path + b.Path[1:], apath + bpath[1:] + case !aslash && !bslash: + return a.Path + "/" + b.Path, apath + "/" + bpath + } + return a.Path + b.Path, apath + bpath +} + +func singleJoiningSlash(a, b string) string { + aslash := strings.HasSuffix(a, "/") + bslash := strings.HasPrefix(b, "/") + switch { + case aslash && bslash: + return a + b[1:] + case !aslash && !bslash: + return a + "/" + b + } + return a + b +}