mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-24 05:02:14 +08:00
Fix: DNS server returns the correct TTL
This commit is contained in:
parent
bd6c6a9ad1
commit
36b5d1f18f
15
common/cache/cache.go
vendored
15
common/cache/cache.go
vendored
@ -44,6 +44,21 @@ func (c *cache) Get(key interface{}) interface{} {
|
|||||||
return elm.Payload
|
return elm.Payload
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetWithExpire element in Cache with Expire Time
|
||||||
|
func (c *cache) GetWithExpire(key interface{}) (item interface{}, expired time.Time) {
|
||||||
|
item, exist := c.mapping.Load(key)
|
||||||
|
if !exist {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
elm := item.(*element)
|
||||||
|
// expired
|
||||||
|
if time.Since(elm.Expired) > 0 {
|
||||||
|
c.mapping.Delete(key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return elm.Payload, elm.Expired
|
||||||
|
}
|
||||||
|
|
||||||
func (c *cache) cleanup() {
|
func (c *cache) cleanup() {
|
||||||
c.mapping.Range(func(k, v interface{}) bool {
|
c.mapping.Range(func(k, v interface{}) bool {
|
||||||
key := k.(string)
|
key := k.(string)
|
||||||
|
@ -52,9 +52,16 @@ func (r *Resolver) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
q := m.Question[0]
|
q := m.Question[0]
|
||||||
cache := r.cache.Get(q.String())
|
cache, expireTime := r.cache.GetWithExpire(q.String())
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
return cache.(*D.Msg).Copy(), nil
|
msg = cache.(*D.Msg).Copy()
|
||||||
|
if len(msg.Answer) > 0 {
|
||||||
|
ttl := uint32(expireTime.Sub(time.Now()).Seconds())
|
||||||
|
for _, answer := range msg.Answer {
|
||||||
|
answer.Header().Ttl = ttl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user