Skip to content

Commit 2c32f3c

Browse files
authored
Implement Optional HTTP Redirection, colored terminal log (#12)
This change implements optional HTTP redirection to the TLS/HTTPS fromURL where ssl-proxy is serving TLS traffic. This makes it easy to redirect any traffic coming in at http://mysite:80 to https://mysite where ssl-proxy is serving TLS certificates. This closes #11. This feature always redirects HTTP traffic from port 80.
1 parent 2b10ebf commit 2c32f3c

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

main.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"log"
67
"net/http"
78
"net/url"
@@ -16,11 +17,12 @@ import (
1617
)
1718

1819
var (
19-
to = flag.String("to", "http://127.0.0.1:80", "the address and port for which to proxy requests to")
20-
fromURL = flag.String("from", "127.0.0.1:4430", "the tcp address and port this proxy should listen for requests on")
21-
certFile = flag.String("cert", "", "path to a tls certificate file. If not provided, ssl-proxy will generate one for you in ~/.ssl-proxy/")
22-
keyFile = flag.String("key", "", "path to a private key file. If not provided, ssl-proxy will generate one for you in ~/.ssl-proxy/")
23-
domain = flag.String("domain", "", "domain to mint letsencrypt certificates for. Usage of this parameter implies acceptance of the LetsEncrypt terms of service.")
20+
to = flag.String("to", "http://127.0.0.1:80", "the address and port for which to proxy requests to")
21+
fromURL = flag.String("from", "127.0.0.1:4430", "the tcp address and port this proxy should listen for requests on")
22+
certFile = flag.String("cert", "", "path to a tls certificate file. If not provided, ssl-proxy will generate one for you in ~/.ssl-proxy/")
23+
keyFile = flag.String("key", "", "path to a private key file. If not provided, ssl-proxy will generate one for you in ~/.ssl-proxy/")
24+
domain = flag.String("domain", "", "domain to mint letsencrypt certificates for. Usage of this parameter implies acceptance of the LetsEncrypt terms of service.")
25+
redirectHTTP = flag.Bool("redirectHTTP", false, "if true, redirects http requests from port 80 to https at your fromURL")
2426
)
2527

2628
const (
@@ -84,9 +86,24 @@ func main() {
8486
mux := http.NewServeMux()
8587
mux.Handle("/", p)
8688

87-
log.Printf("Proxying calls from https://%s (SSL/TLS) to %s", *fromURL, toURL)
89+
log.Printf(green("Proxying calls from https://%s (SSL/TLS) to %s"), *fromURL, toURL)
8890

89-
// Determine if we should serve with autogenerated LetsEncrypt certificates or not
91+
// Redirect http requests on port 80 to TLS port using https
92+
if *redirectHTTP {
93+
redirectTLS := func(w http.ResponseWriter, r *http.Request) {
94+
http.Redirect(w, r, "https://"+*fromURL+r.RequestURI, http.StatusMovedPermanently)
95+
}
96+
go func() {
97+
log.Println(fmt.Sprintf("Also redirecting https requests on port 80 to https requests on %s", *fromURL))
98+
err := http.ListenAndServe(":80", http.HandlerFunc(redirectTLS))
99+
if err != nil {
100+
log.Println("HTTP redirection server failure")
101+
log.Println(err)
102+
}
103+
}()
104+
}
105+
106+
// Determine if we should serve over TLS with autogenerated LetsEncrypt certificates or not
90107
if validDomain {
91108
// Domain is present, use autocert
92109
// TODO: validate domain (though, autocert may do this)
@@ -106,9 +123,16 @@ func main() {
106123
}
107124
s.Handler = mux
108125
log.Fatal(s.ListenAndServeTLS("", ""))
126+
} else {
127+
// Domain is not provided, serve TLS using provided/generated certificate files
128+
log.Fatal(http.ListenAndServeTLS(*fromURL, *certFile, *keyFile, mux))
109129
}
110130

111-
// Domain is not provided, serve using provided/generated certificate files
112-
log.Fatal(http.ListenAndServeTLS(*fromURL, *certFile, *keyFile, mux))
131+
}
113132

133+
// green takes an input string and returns it with the proper ANSI escape codes to render it green-colored
134+
// in a supported terminal.
135+
// TODO: if more colors used in the future, generalize or pull in an external pkg
136+
func green(in string) string {
137+
return fmt.Sprintf("\033[0;32m%s\033[0;0m", in)
114138
}

0 commit comments

Comments
 (0)