From 36ee2b29fb36e3000da8cd2030c9a170b53eea8a Mon Sep 17 00:00:00 2001 From: Alex X Date: Sat, 14 Oct 2023 15:51:43 +0300 Subject: [PATCH] Update TLS files handling --- internal/api/api.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index ce848613..cb12ec6b 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -85,7 +85,14 @@ func Init() { // Initialize the HTTPS server if cfg.Mod.TLSListen != "" && cfg.Mod.TLSCert != "" && cfg.Mod.TLSKey != "" { - cert, err := tls.X509KeyPair(readPEM(cfg.Mod.TLSCert), readPEM(cfg.Mod.TLSKey)) + var cert tls.Certificate + if strings.IndexByte(cfg.Mod.TLSCert, '\n') < 0 && strings.IndexByte(cfg.Mod.TLSKey, '\n') < 0 { + // check if file path + cert, err = tls.LoadX509KeyPair(cfg.Mod.TLSCert, cfg.Mod.TLSKey) + } else { + // if text file content + cert, err = tls.X509KeyPair([]byte(cfg.Mod.TLSCert), []byte(cfg.Mod.TLSKey)) + } if err != nil { log.Error().Err(err).Caller().Send() return @@ -260,12 +267,3 @@ func Error(w http.ResponseWriter, err error) { http.Error(w, err.Error(), http.StatusInsufficientStorage) } - -func readPEM(s string) []byte { - if strings.IndexByte(s, '\n') > 0 { - return []byte(s) - } - - b, _ := os.ReadFile(s) - return b -}