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