From 8af2fb6cec0324ae4c2b0a2fdc103087f2e1c65f Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 23 Nov 2020 12:33:25 -0700 Subject: [PATCH] cleanup debugging --- cmd/telebit/telebit.go | 2 +- internal/http01/http01.go | 1 + internal/http01proxy/proxy.go | 35 ++++++++++++++++++++++------------- internal/mgmt/route.go | 13 ++++++++----- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/cmd/telebit/telebit.go b/cmd/telebit/telebit.go index cf7113d..a843a38 100644 --- a/cmd/telebit/telebit.go +++ b/cmd/telebit/telebit.go @@ -497,7 +497,7 @@ func parseFlagsAndENVs() { } // Proxy for HTTP-01 requests - // TODO needs to be limited to .well-known/acme-challenges + // TODO needs to be limited to .well-known/acme-challenge if len(*proxyHTTP01) > 0 { go func() { fmt.Printf("Proxying HTTP-01 on port 80 to %s\n", *proxyHTTP01) diff --git a/internal/http01/http01.go b/internal/http01/http01.go index c185421..23c7e02 100644 --- a/internal/http01/http01.go +++ b/internal/http01/http01.go @@ -127,6 +127,7 @@ func (s *Solver) doRequest(method, uri string, msg interface{}) error { req.Header.Set("Authorization", "Bearer "+apiToken) } + //fmt.Printf("curl -X %s %s \\\n -H 'Authorization: Bearer %s' \\\n -d '%s'\n\n", method, endpoint.String(), apiToken, string(data)) resp, err := s.config.HTTPClient.Do(req) if err != nil { return err diff --git a/internal/http01proxy/proxy.go b/internal/http01proxy/proxy.go index 6134ee2..16036c2 100644 --- a/internal/http01proxy/proxy.go +++ b/internal/http01proxy/proxy.go @@ -1,7 +1,6 @@ package http01proxy import ( - "log" "net/http" "net/http/httputil" "net/url" @@ -29,21 +28,31 @@ func ListenAndServe(target string, timeout time.Duration) error { // We want the incoming host header to remain unchanged, // which is the domain name that is being challenged - log.Printf("[debug] Incoming Host: %q", req.Host) - log.Printf("[debug] Incoming URL.Host: %q", req.URL.Host) - log.Printf("[debug] Incoming Header.Host: %q", req.Header.Get("Host")) + // This is the ORIGINAL req.Header.Host + //log.Printf("[debug] Incoming Host: %q", req.Host) + // This will always be an empty string "" + //log.Printf("[debug] Incoming URL.Host: %q", req.URL.Host) + // This will always be an empty string "" + //log.Printf("[debug] Incoming Header.Host: %q", req.Header.Get("Host")) + + // This will become the HTTP Host header + //req.Host targetQuery := targetURL.RawQuery - req.URL.Scheme = targetURL.Scheme - // But we want the proxy target to be updated to the new target - //req.Host = targetURL.Host - req.URL.Host = targetURL.Host - req.URL.Path, req.URL.RawPath = joinURLPath(targetURL, req.URL) - //log.Printf("[debug] Target Host: %q", req.Host) - log.Printf("[debug] Target URL.Host: %q", req.URL.Host) - log.Printf("[debug] Target URL.Path: %q", req.URL.Path) - log.Printf("[debug] Target URL.RawPath: %q", req.URL.Path) + // This will change the scheme (http/s) used to connect to the target + req.URL.Scheme = targetURL.Scheme + //log.Printf("[debug] Target URL.Scheme: %q", req.URL.Scheme) + + // This will change the network host target + // but will NOT change the HTTP Host header + req.URL.Host = targetURL.Host + //log.Printf("[debug] Target URL.Host: %q", req.URL.Host) + + // This will add the target prefix to the original url + req.URL.Path, req.URL.RawPath = joinURLPath(targetURL, req.URL) + //log.Printf("[debug] Target URL.Path: %q", req.URL.Path) + //log.Printf("[debug] Target URL.RawPath: %q", req.URL.Path) if targetQuery == "" || req.URL.RawQuery == "" { req.URL.RawQuery = targetQuery + req.URL.RawQuery diff --git a/internal/mgmt/route.go b/internal/mgmt/route.go index 7efaf79..53db559 100644 --- a/internal/mgmt/route.go +++ b/internal/mgmt/route.go @@ -44,12 +44,15 @@ func RouteStatic(r chi.Router) chi.Router { func getACMEChallenges(w http.ResponseWriter, r *http.Request) { //token := chi.URLParam(r, "token") host := r.Host - xHost := r.Header.Get("X-Forwarded-Host") - log.Printf("[debug] Host: %q\n[debug] X-Host: %q", host, xHost) - if len(xHost) > 0 { + /* // TODO TrustProxy option? - host = xHost - } + xHost := r.Header.Get("X-Forwarded-Host") + //log.Printf("[debug] Host: %q\n[debug] X-Host: %q", host, xHost) + if len(xHost) > 0 { + host = xHost + } + */ + if strings.ContainsAny(host, "/:|\\") { host = "" }