1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-12-04 14:46:29 -05:00

🐛 Fix 'concurrent map writes' panic and don't check for cert every time

This commit is contained in:
makeworld 2020-11-18 19:29:22 -05:00
parent c72a225a43
commit e026eb25e0

View File

@ -5,6 +5,7 @@ import (
"io/ioutil"
"net"
"net/url"
"sync"
"github.com/makeworld-the-better-one/go-gemini"
"github.com/mitchellh/go-homedir"
@ -12,10 +13,14 @@ import (
)
var certCache = make(map[string][][]byte)
var certCacheMu = &sync.RWMutex{}
func clientCert(host string) ([]byte, []byte) {
if cert := certCache[host]; cert != nil {
return cert[0], cert[1]
certCacheMu.RLock()
pair, ok := certCache[host]
certCacheMu.RUnlock()
if ok {
return pair[0], pair[1]
}
// Expand paths starting with ~/
@ -28,22 +33,30 @@ func clientCert(host string) ([]byte, []byte) {
keyPath = viper.GetString("auth.keys." + host)
}
if certPath == "" && keyPath == "" {
certCacheMu.Lock()
certCache[host] = [][]byte{nil, nil}
certCacheMu.Unlock()
return nil, nil
}
cert, err := ioutil.ReadFile(certPath)
if err != nil {
certCacheMu.Lock()
certCache[host] = [][]byte{nil, nil}
certCacheMu.Unlock()
return nil, nil
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
certCacheMu.Lock()
certCache[host] = [][]byte{nil, nil}
certCacheMu.Unlock()
return nil, nil
}
certCacheMu.Lock()
certCache[host] = [][]byte{cert, key}
certCacheMu.Unlock()
return cert, key
}