Add TLS support

This commit is contained in:
sfigato 2023-03-08 22:41:06 +01:00
parent 30abc92074
commit 12b8cab6f6
Signed by: blallo
GPG Key ID: C530464EEDCF489A
4 changed files with 104 additions and 6 deletions

View File

@ -20,7 +20,9 @@ import (
var (
debug = flag.Bool("debug", false, "Enable debug logging")
addr = flag.String("addr", ":8080", "Addres to bind to, in the 'ipaddress:port' format")
addr = flag.String("addr", ":8080", "Address to bind to, in the 'ipaddress:port' format")
tlsCert = flag.String("tls-cert", "", "Path to certificate file for TLS connection")
tlsKey = flag.String("tls-key", "", "Path to key file for TLS connection")
)
func main() {
@ -145,7 +147,20 @@ func main() {
}
}()
go setupHandler(radio, logger, *addr)
handler, err := setupHandler(radio, logger, *addr)
if err != nil {
logger.Err(map[string]any{
"msg": "Cannot create handler",
"err": err.Error(),
})
os.Exit(2)
}
if isValidTLSConf(logger) {
go http.ListenAndServeTLS(*addr, *tlsCert, *tlsKey, handler)
} else {
go http.ListenAndServe(*addr, handler)
}
if err := radio.Run(ctx); err != nil {
logger.Err(map[string]any{
@ -168,7 +183,21 @@ func withTimeout[T any](ctx context.Context, respCh <-chan T) (zero T, err error
}
}
func setupHandler(radio *broadcast.Radio, logger logz.Logger, addr string) error {
func isValidTLSConf(logger logz.Logger) bool {
if *tlsCert == "" && *tlsKey == "" {
return false
}
if (*tlsCert != "" && *tlsKey == "") || (*tlsCert == "" && *tlsKey != "") {
logger.Err(map[string]any{
"msg": "You must specify both the path to the certificate and to the key to use TLS",
})
return false
}
return true
}
func setupHandler(radio *broadcast.Radio, logger logz.Logger, addr string) (http.Handler, error) {
handler := &radioHandler{
radio: radio,
logger: logger,
@ -186,7 +215,7 @@ func setupHandler(radio *broadcast.Radio, logger logz.Logger, addr string) error
assetsHandler, err := newAssetsHandler(logger)
if err != nil {
return err
return nil, err
}
router := chi.NewRouter()
@ -200,7 +229,7 @@ func setupHandler(radio *broadcast.Radio, logger logz.Logger, addr string) error
router.Handle("/liveness", wsHandler)
router.Get("/test_ui", testUI.TestUI)
return http.ListenAndServe(addr, router)
return router, nil
}
func getBaseAddr(addr string) string {

View File

@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIIDyDCCArACEyOJW7h8A9xaVRDy29+WVBLIVw0wDQYJKoZIhvcNAQELBQAwgaAx
CzAJBgNVBAYTAklUMQwwCgYDVQQIDANaQU0xEzARBgNVBAcMCk1pbGFubyBTVUQx
JDAiBgNVBAoMG1pvbmEgQXV0b25vbWEgTWV0cm9wb2xpdGFuYTERMA8GA1UECwwI
VW5pdEBaQU0xEjAQBgNVBAMMCWJyb2FkY2FzdDEhMB8GCSqGSIb3DQEJARYSdW5p
dEBhdXRpc3RpY2kub3JnMB4XDTIzMDMwODIxMzg1MVoXDTI0MDMwNzIxMzg1MVow
gaAxCzAJBgNVBAYTAklUMQwwCgYDVQQIDANaQU0xEzARBgNVBAcMCk1pbGFubyBT
VUQxJDAiBgNVBAoMG1pvbmEgQXV0b25vbWEgTWV0cm9wb2xpdGFuYTERMA8GA1UE
CwwIVW5pdEBaQU0xEjAQBgNVBAMMCWJyb2FkY2FzdDEhMB8GCSqGSIb3DQEJARYS
dW5pdEBhdXRpc3RpY2kub3JnMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAwT3qaRiiAonq0bF4dCLqZYCnsFOj4lsCBuM9ZCb2XHAJXu0hcGI95pZbPvMd
X44UIt3y96W0eAZfZFf3CiE/Tm7FPgmbAY6vvoKT9RV3uw7sfJVRYuf5DA7ibjhI
vHKF5ouFFMNlqnR077QI0U0xnfSG6zj6bmLa7i1tfBGxaBncv5AJ1QI8KYaqqgai
wlyDUHjzL9HHpcZMz3Xnhgw3coNHt4hvSACi8pvid8TYY98vQggZtusPpQmmKfhi
yfUV5JpKhk2CRGRsyT/iTxPfGGuNkHT8nxDkadga5znU1+u5YLcG5ip7DsRDcEJk
OzixtVEYpn429dVx9nG/tgvy/wIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQAmh4tQ
Ypik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