mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-07-01 00:42:02 +08:00
Allow concurrent DNS queries
This commit is contained in:
parent
27af360726
commit
54774ceca6
@ -75,58 +75,58 @@ func ApplyECH(c *Config, config *tls.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type record struct {
|
type ECHConfigCache struct {
|
||||||
echConfig []byte
|
echConfig []byte
|
||||||
expire time.Time
|
expire time.Time
|
||||||
|
updateLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dnsCache sync.Map
|
GlobalECHConfigCache map[string]*ECHConfigCache
|
||||||
// global Lock? I'm not sure if this needs finer grained locks.
|
GlobalECHConfigCacheAccess sync.Mutex
|
||||||
// If we do this, we will need to nest another layer of struct
|
|
||||||
updating sync.Mutex
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// QueryRecord returns the ECH config for given domain.
|
// QueryRecord returns the ECH config for given domain.
|
||||||
// If the record is not in cache or expired, it will query the DNS server and update the cache.
|
// If the record is not in cache or expired, it will query the DNS server and update the cache.
|
||||||
func QueryRecord(domain string, server string) ([]byte, error) {
|
func QueryRecord(domain string, server string) ([]byte, error) {
|
||||||
val, found := dnsCache.Load(domain)
|
// Global cache init
|
||||||
rec, _ := val.(record)
|
GlobalECHConfigCacheAccess.Lock()
|
||||||
if found && rec.expire.After(time.Now()) {
|
if GlobalECHConfigCache == nil {
|
||||||
|
GlobalECHConfigCache = make(map[string]*ECHConfigCache)
|
||||||
|
}
|
||||||
|
|
||||||
|
echConfigCache := GlobalECHConfigCache[domain]
|
||||||
|
if echConfigCache != nil && echConfigCache.expire.After(time.Now()) {
|
||||||
errors.LogDebug(context.Background(), "Cache hit for domain: ", domain)
|
errors.LogDebug(context.Background(), "Cache hit for domain: ", domain)
|
||||||
return rec.echConfig, nil
|
GlobalECHConfigCacheAccess.Unlock()
|
||||||
|
return echConfigCache.echConfig, nil
|
||||||
}
|
}
|
||||||
|
if echConfigCache == nil {
|
||||||
updating.Lock()
|
echConfigCache = &ECHConfigCache{}
|
||||||
defer updating.Unlock()
|
GlobalECHConfigCache[domain] = echConfigCache
|
||||||
// Try to get cache again after lock, in case another goroutine has updated it
|
|
||||||
// This might happen when the core tring is just stared and multiple goroutines are trying to query the same domain
|
|
||||||
val, found = dnsCache.Load(domain)
|
|
||||||
rec, _ = val.(record)
|
|
||||||
if found && rec.expire.After(time.Now()) {
|
|
||||||
errors.LogDebug(context.Background(), "ECH Config cache hit for domain: ", domain, " after trying to get update lock")
|
|
||||||
return rec.echConfig, nil
|
|
||||||
}
|
}
|
||||||
|
GlobalECHConfigCacheAccess.Unlock()
|
||||||
|
|
||||||
|
echConfigCache.updateLock.Lock()
|
||||||
|
defer echConfigCache.updateLock.Unlock()
|
||||||
|
// Double check cache after acquiring lock
|
||||||
|
if echConfigCache.expire.After(time.Now()) {
|
||||||
|
errors.LogDebug(context.Background(), "Cache hit for domain after double check: ", domain)
|
||||||
|
return echConfigCache.echConfig, nil
|
||||||
|
}
|
||||||
// Query ECH config from DNS server
|
// Query ECH config from DNS server
|
||||||
errors.LogDebug(context.Background(), "Trying to query ECH config for domain: ", domain, " with ECH server: ", server)
|
errors.LogDebug(context.Background(), "Trying to query ECH config for domain: ", domain, " with ECH server: ", server)
|
||||||
echConfig, ttl, err := dnsQuery(server, domain)
|
echConfig, ttl, err := dnsQuery(server, domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set minimum TTL to 600 seconds
|
// Set minimum TTL to 600 seconds
|
||||||
if ttl < 600 {
|
if ttl < 600 {
|
||||||
ttl = 600
|
ttl = 600
|
||||||
}
|
}
|
||||||
|
echConfigCache.echConfig = echConfig
|
||||||
// Update cache
|
echConfigCache.expire = time.Now().Add(time.Second * time.Duration(ttl))
|
||||||
newRecored := record{
|
return echConfigCache.echConfig, nil
|
||||||
echConfig: echConfig,
|
|
||||||
expire: time.Now().Add(time.Second * time.Duration(ttl)),
|
|
||||||
}
|
|
||||||
dnsCache.Store(domain, newRecored)
|
|
||||||
return echConfig, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// dnsQuery is the real func for sending type65 query for given domain to given DNS server.
|
// dnsQuery is the real func for sending type65 query for given domain to given DNS server.
|
||||||
@ -327,7 +327,7 @@ func GenerateECHKeySet(configID uint8, domain string, kem uint16) (reality.EchCo
|
|||||||
return config, nil, err
|
return config, nil, err
|
||||||
}
|
}
|
||||||
privKey, _ := curve.NewPrivateKey(priv)
|
privKey, _ := curve.NewPrivateKey(priv)
|
||||||
config.PublicKey = privKey.PublicKey().Bytes();
|
config.PublicKey = privKey.PublicKey().Bytes()
|
||||||
return config, priv, nil
|
return config, priv, nil
|
||||||
// }
|
// }
|
||||||
// TODO: add mlkem768 (former kyber768 draft00). The golang mlkem private key is 64 bytes seed?
|
// TODO: add mlkem768 (former kyber768 draft00). The golang mlkem private key is 64 bytes seed?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user