Skip to content

Commit

Permalink
When automatic redirect is enabled, use domain if available. (#30)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
suyashkumar authored Apr 5, 2020
1 parent 89a1353 commit 66c9c0d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 66c9c0d

Please sign in to comment.