From 11f0b097259a79befecf566b38d76e01bc0fd5c9 Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Sun, 29 Mar 2020 21:05:34 -0500 Subject: [PATCH] Add HTTPS support --- factorio-site.go | 19 ++++++++++++++++++- go.mod | 5 ++++- go.sum | 9 +++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/factorio-site.go b/factorio-site.go index cb2ca27..1f8066b 100644 --- a/factorio-site.go +++ b/factorio-site.go @@ -1,6 +1,7 @@ package main import ( + "crypto/tls" "flag" "fmt" "html/template" @@ -8,6 +9,7 @@ import ( "net/http" "github.com/james4k/rcon" + "golang.org/x/crypto/acme/autocert" ) type serverData struct { @@ -69,7 +71,22 @@ func main() { t.Execute(w, data) }) + certManager := autocert.Manager{ + Prompt: autocert.AcceptTOS, + HostPolicy: autocert.HostWhitelist("factorio.blackolivepineapple.pizza"), // TODO: add config + Cache: autocert.DirCache("certs"), + } + + server := &http.Server{ + Addr: ":https", + TLSConfig: &tls.Config{ + GetCertificate: certManager.GetCertificate, + }, + } + + go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // Handler for LetsEncrypt + fmt.Println("Serving...") - http.ListenAndServe(":http", nil) + server.ListenAndServeTLS("", "") // Key/cert come from server.TLSConfig } diff --git a/go.mod b/go.mod index c1890a4..ea2ff3a 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/chandlerswift/factorio-site go 1.14 -require github.com/james4k/rcon v0.0.0-20120923215419-8fbb8268b60a +require ( + github.com/james4k/rcon v0.0.0-20120923215419-8fbb8268b60a + golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 +) diff --git a/go.sum b/go.sum index a8fa9d6..9a448f9 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,11 @@ github.com/james4k/rcon v0.0.0-20120923215419-8fbb8268b60a h1:JxcWget6X/VfBMKxPIc28Jel37LGREut2fpV+ObkwJ0= github.com/james4k/rcon v0.0.0-20120923215419-8fbb8268b60a/go.mod h1:1qNVsDcmNQDsAXYfUuF/Z0rtK5eT8x9D6Pi7S3PjXAg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=