gh-7 Implemented automatic self-signed SSL generation on startup under ~/.config/enervent-ctrl
This commit is contained in:
parent
0311605bae
commit
2738b60386
|
@ -7,7 +7,10 @@ require (
|
|||
github.com/gorilla/handlers v1.5.1
|
||||
)
|
||||
|
||||
require golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
|
||||
|
||||
require (
|
||||
github.com/felixge/httpsnoop v1.0.1 // indirect
|
||||
github.com/goburrow/serial v0.1.0 // indirect
|
||||
github.com/rocketlaunchr/https-go v0.0.0-20200218083740-ba6c48f29f4d
|
||||
)
|
||||
|
|
|
@ -6,3 +6,7 @@ github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA
|
|||
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
|
||||
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
|
||||
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
|
||||
github.com/rocketlaunchr/https-go v0.0.0-20200218083740-ba6c48f29f4d h1:bL0c7wxznxDDQ+ebCpGN5T20ATeYDXedomXbQHwFwHA=
|
||||
github.com/rocketlaunchr/https-go v0.0.0-20200218083740-ba6c48f29f4d/go.mod h1:kDbnFcjPe/2KqPfycPSq0Ripnddx0DtCC2M1k95myWQ=
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
|
||||
|
|
|
@ -10,9 +10,11 @@ import (
|
|||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/0ranki/enervent-ctrl/enervent-ctrl-go/pingvinKL"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/rocketlaunchr/https-go"
|
||||
)
|
||||
|
||||
// Remember to dereference the symbolic links under ./static/html
|
||||
|
@ -22,7 +24,7 @@ import (
|
|||
var static embed.FS
|
||||
|
||||
var (
|
||||
version = "0.0.17"
|
||||
version = "0.0.18"
|
||||
pingvin pingvinKL.PingvinKL
|
||||
DEBUG *bool
|
||||
INTERVAL *int
|
||||
|
@ -144,12 +146,52 @@ func listen() {
|
|||
}
|
||||
}
|
||||
|
||||
func generateCertificate(certpath, cert, key string) {
|
||||
if _, err := os.Stat(certpath); err != nil {
|
||||
log.Println("Generating configuration directory", certpath)
|
||||
if err := os.MkdirAll(certpath, 0750); err != nil {
|
||||
log.Fatal("Failed to generate configuration directory:", err)
|
||||
}
|
||||
}
|
||||
opts := https.GenerateOptions{Host: "enervent-ctrl.local", RSABits: 4096, ValidFor: 10 * 365 * 24 * time.Hour}
|
||||
log.Println("Generating new self-signed SSL keypair to ", certpath)
|
||||
pub, priv, err := https.GenerateKeys(opts)
|
||||
if err != nil {
|
||||
log.Fatal("Error generating SSL certificate: ", err)
|
||||
}
|
||||
pingvin.Debug.Println("Certificate:\n", string(pub))
|
||||
pingvin.Debug.Println("Key:\n", string(priv))
|
||||
if err := os.WriteFile(key, priv, 0600); err != nil {
|
||||
log.Fatal("Error writing private key ", key, ": ", err)
|
||||
}
|
||||
log.Println("Wrote new SSL private key ", cert)
|
||||
if err := os.WriteFile(cert, pub, 0644); err != nil {
|
||||
log.Fatal("Error writing certificate ", cert, ": ", err)
|
||||
}
|
||||
log.Println("Wrote new SSL public key ", cert)
|
||||
}
|
||||
|
||||
func configure() {
|
||||
log.Println("Reading configuration")
|
||||
DEBUG = flag.Bool("debug", false, "Enable debug logging")
|
||||
INTERVAL = flag.Int("interval", 4, "Set the interval of background updates")
|
||||
ACCESS_LOG = flag.Bool("httplog", false, "Enable HTTP access logging")
|
||||
generatecert := flag.Bool("regenerate-certs", false, "Generate a new SSL certificate. A new one is generated on startup as `~/.config/enervent-ctrl/server.crt` if it doesn't exist.")
|
||||
// TODO: flag for cerificate path
|
||||
// TODO: log file flag
|
||||
flag.Parse()
|
||||
// Get the user home directory path
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatal("Could not determine user home directory")
|
||||
}
|
||||
certpath := homedir + "/.config/enervent-ctrl/"
|
||||
cert := certpath + "certificate.pem"
|
||||
key := certpath + "privatekey.pem"
|
||||
// Check that certificate file exists
|
||||
if _, err = os.Stat(cert); err != nil || *generatecert {
|
||||
generateCertificate(certpath, cert, key)
|
||||
}
|
||||
if *DEBUG {
|
||||
log.Println("Debug logging enabled")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue