github.com/yaling888/clash@v1.53.0/common/cert/storage.go (about)

     1  package cert
     2  
     3  import (
     4  	"crypto/tls"
     5  	"sync"
     6  
     7  	"github.com/yaling888/clash/component/trie"
     8  )
     9  
    10  // DomainTrieCertsStorage cache wildcard certificates
    11  type DomainTrieCertsStorage struct {
    12  	certsCache *trie.DomainTrie[*tls.Certificate]
    13  	lock       sync.RWMutex
    14  }
    15  
    16  // Get gets the certificate from the storage
    17  func (c *DomainTrieCertsStorage) Get(key string) (*tls.Certificate, bool) {
    18  	c.lock.RLock()
    19  	defer c.lock.RUnlock()
    20  
    21  	ca := c.certsCache.Search(key)
    22  	if ca == nil {
    23  		return nil, false
    24  	}
    25  	return ca.Data, true
    26  }
    27  
    28  // Set saves the certificate to the storage
    29  func (c *DomainTrieCertsStorage) Set(key string, cert *tls.Certificate) {
    30  	c.lock.Lock()
    31  	_ = c.certsCache.Insert(key, cert)
    32  	c.lock.Unlock()
    33  }
    34  
    35  func NewDomainTrieCertsStorage() *DomainTrieCertsStorage {
    36  	return &DomainTrieCertsStorage{
    37  		certsCache: trie.New[*tls.Certificate](),
    38  	}
    39  }