You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/app/dns/dns.go

63 lines
1.2 KiB

9 years ago
package dns
import (
"net"
"time"
"github.com/v2ray/v2ray-core/app"
9 years ago
"github.com/v2ray/v2ray-core/common/collect"
"github.com/v2ray/v2ray-core/common/serial"
9 years ago
)
type entry struct {
domain string
ip net.IP
validUntil time.Time
}
func newEntry(domain string, ip net.IP) *entry {
this := &entry{
domain: domain,
ip: ip,
}
this.Extend()
return this
}
func (this *entry) IsValid() bool {
return this.validUntil.After(time.Now())
}
func (this *entry) Extend() {
this.validUntil = time.Now().Add(time.Hour)
}
type DnsCache struct {
9 years ago
cache *collect.ValidityMap
config *CacheConfig
9 years ago
}
func NewCache(config *CacheConfig) *DnsCache {
9 years ago
cache := &DnsCache{
9 years ago
cache: collect.NewValidityMap(3600),
config: config,
9 years ago
}
return cache
}
func (this *DnsCache) Add(context app.Context, domain string, ip net.IP) {
callerTag := context.CallerTag()
if !this.config.IsTrustedSource(serial.StringLiteral(callerTag)) {
return
}
9 years ago
this.cache.Set(serial.StringLiteral(domain), newEntry(domain, ip))
9 years ago
}
func (this *DnsCache) Get(context app.Context, domain string) net.IP {
9 years ago
if value := this.cache.Get(serial.StringLiteral(domain)); value != nil {
return value.(*entry).ip
9 years ago
}
return nil
}