chore: reuse cert pool

This commit is contained in:
Skyxim 2023-03-29 14:09:42 +08:00
parent d305e0ddfc
commit 5b73942960

View File

@ -15,7 +15,7 @@ import (
) )
var trustCerts []*x509.Certificate var trustCerts []*x509.Certificate
var certPool *x509.CertPool
var mutex sync.RWMutex var mutex sync.RWMutex
var errNotMacth error = errors.New("certificate fingerprints do not match") var errNotMacth error = errors.New("certificate fingerprints do not match")
@ -40,10 +40,17 @@ func ResetCertificate() {
} }
func getCertPool() *x509.CertPool { func getCertPool() *x509.CertPool {
certPool, err := x509.SystemCertPool() if certPool == nil {
if err == nil { mutex.Lock()
for _, cert := range trustCerts { defer mutex.Unlock()
certPool.AddCert(cert) if certPool != nil {
return certPool
}
certPool, err := x509.SystemCertPool()
if err == nil {
for _, cert := range trustCerts {
certPool.AddCert(cert)
}
} }
} }
return certPool return certPool