From 66c9c0dac8484381585f052990220fe1542afc36 Mon Sep 17 00:00:00 2001 From: Suyash Kumar Date: Sat, 4 Apr 2020 21:47:42 -0700 Subject: [PATCH] When automatic redirect is enabled, use domain if available. (#30) When the -redirectHTTP flag is enabled, the value for domain should be used for the redirect if specified. Otherwise, the value of the from flag should be used (which might be a public facing IP, or perhaps a localhost address for local work). This closes #27. --- main.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index bd127bd..447a777 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,8 @@ import ( "net/http" "net/url" "os" - "time" - "strings" + "time" "github.com/suyashkumar/ssl-proxy/gen" "github.com/suyashkumar/ssl-proxy/reverseproxy" @@ -90,11 +89,18 @@ func main() { // Redirect http requests on port 80 to TLS port using https if *redirectHTTP { + // Redirect to fromURL by default, unless a domain is specified--in that case, redirect using the public facing + // domain + redirectURL := *fromURL + if validDomain { + redirectURL = *domain + } redirectTLS := func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "https://"+*fromURL+r.RequestURI, http.StatusMovedPermanently) + http.Redirect(w, r, "https://"+redirectURL+r.RequestURI, http.StatusMovedPermanently) } go func() { - log.Println(fmt.Sprintf("Also redirecting https requests on port 80 to https requests on %s", *fromURL)) + log.Println( + fmt.Sprintf("Also redirecting https requests on port 80 to https requests on %s", redirectURL)) err := http.ListenAndServe(":80", http.HandlerFunc(redirectTLS)) if err != nil { log.Println("HTTP redirection server failure")