Skip to content

Commit

Permalink
feat: HTTP -> HTTPS redirects on all site configs (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy authored Jan 18, 2024
1 parent 51fe92f commit 588ead0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ access.log

/*.conf
Casketfile
Casketfile.*
!casketfile/
casket/go.mod
casket/go.sum
Expand Down
10 changes: 8 additions & 2 deletions caskethttp/httpserver/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func activateHTTPS(cctx casket.Context) error {
operatorPresent := !casket.Started()

if !casket.Quiet && operatorPresent {
fmt.Print("Activating privacy features... ")
fmt.Println("Activating privacy features... ")
}

ctx := cctx.(*httpContext)
Expand Down Expand Up @@ -147,7 +147,8 @@ func makePlaintextRedirects(allConfigs []*SiteConfig) []*SiteConfig {
httpPort := strconv.Itoa(certmagic.HTTPPort)
httpsPort := strconv.Itoa(certmagic.HTTPSPort)
for i, cfg := range allConfigs {
if cfg.TLS.Managed &&
if cfg.TLS.Enabled &&
!cfg.TLS.NoRedirect &&
!hostHasOtherPort(allConfigs, i, httpPort) &&
(cfg.Addr.Port == httpsPort || !hostHasOtherPort(allConfigs, i, httpsPort)) {
allConfigs = append(allConfigs, redirPlaintextHost(cfg))
Expand Down Expand Up @@ -193,6 +194,11 @@ func redirPlaintextHost(cfg *SiteConfig) *SiteConfig {
redirPort = ""
}

operatorPresent := !casket.Started()
if !casket.Quiet && operatorPresent {
fmt.Println("[INFO] Creating automatic HTTP->HTTPS redirect for", cfg.Addr.Host)
}

redirMiddleware := func(next Handler) Handler {
return HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
// Construct the URL to which to redirect. Note that the Host in a
Expand Down
12 changes: 6 additions & 6 deletions caskethttp/httpserver/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"strconv"
"testing"

"github.com/tmpim/certmagic"
"github.com/tmpim/casket/caskettls"
"github.com/tmpim/certmagic"
)

func TestRedirPlaintextHost(t *testing.T) {
Expand Down Expand Up @@ -153,18 +153,18 @@ func TestHostHasOtherPort(t *testing.T) {
func TestMakePlaintextRedirects(t *testing.T) {
configs := []*SiteConfig{
// Happy path = standard redirect from 80 to 443
{Addr: Address{Host: "example.com"}, TLS: &caskettls.Config{Managed: true}},
{Addr: Address{Host: "example.com"}, TLS: &caskettls.Config{Managed: true, Enabled: true}},

// Host on port 80 already defined; don't change it (no redirect)
{Addr: Address{Host: "sub1.example.com", Port: "80", Scheme: "http"}, TLS: new(caskettls.Config)},
{Addr: Address{Host: "sub1.example.com"}, TLS: &caskettls.Config{Managed: true}},
{Addr: Address{Host: "sub1.example.com"}, TLS: &caskettls.Config{Managed: true, Enabled: true}},

// Redirect from port 80 to port 5000 in this case
{Addr: Address{Host: "sub2.example.com", Port: "5000"}, TLS: &caskettls.Config{Managed: true}},
{Addr: Address{Host: "sub2.example.com", Port: "5000"}, TLS: &caskettls.Config{Managed: true, Enabled: true}},

// Can redirect from 80 to either 443 or 5001, but choose 443
{Addr: Address{Host: "sub3.example.com", Port: "443"}, TLS: &caskettls.Config{Managed: true}},
{Addr: Address{Host: "sub3.example.com", Port: "5001", Scheme: "https"}, TLS: &caskettls.Config{Managed: true}},
{Addr: Address{Host: "sub3.example.com", Port: "443"}, TLS: &caskettls.Config{Managed: true, Enabled: true}},
{Addr: Address{Host: "sub3.example.com", Port: "5001", Scheme: "https"}, TLS: &caskettls.Config{Managed: true, Enabled: true}},
}

result := makePlaintextRedirects(configs)
Expand Down
4 changes: 4 additions & 0 deletions caskettls/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ type Config struct {
// Manager is how certificates are managed
Manager *certmagic.Config

// NoRedirect will disable the automatic HTTP->HTTPS redirect, regardless
// of whether the site is managed or not.
NoRedirect bool

// SelfSigned means that this hostname is
// served with a self-signed certificate
// that we generated in memory for convenience
Expand Down
2 changes: 2 additions & 0 deletions caskettls/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ func setupTLS(c *casket.Controller) error {
}
parts[0] = "*"
config.Hostname = strings.Join(parts, ".")
case "no_redirect":
config.NoRedirect = true
default:
return c.Errf("Unknown subdirective '%s'", c.Val())
}
Expand Down

0 comments on commit 588ead0

Please sign in to comment.