forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_other.go
62 lines (50 loc) · 1.02 KB
/
config_other.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// +build !windows
package tls
import (
"bytes"
"crypto/x509"
"sync"
)
type certPoolCache struct {
sync.Mutex
once sync.Once
pool *x509.CertPool
extraCerts [][]byte
}
func (c *certPoolCache) hasCert(cert []byte) bool {
for _, xCert := range c.extraCerts {
if bytes.Equal(xCert, cert) {
return true
}
}
return false
}
func (c *certPoolCache) get(extraCerts []*Certificate) *x509.CertPool {
c.once.Do(func() {
pool, err := x509.SystemCertPool()
if err != nil {
newError("failed to get system cert pool.").Base(err).WriteToLog()
return
}
c.pool = pool
})
if c.pool == nil {
return nil
}
if len(extraCerts) == 0 {
return c.pool
}
c.Lock()
defer c.Unlock()
for _, cert := range extraCerts {
if !c.hasCert(cert.Certificate) {
c.pool.AppendCertsFromPEM(cert.Certificate)
c.extraCerts = append(c.extraCerts, cert.Certificate)
}
}
return c.pool
}
var combineCertPool certPoolCache
func (c *Config) getCertPool() *x509.CertPool {
return combineCertPool.get(c.Certificate)
}