2015-12-06 05:00:10 -05:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2015-12-10 17:55:39 -05:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2015-12-06 05:00:10 -05:00
|
|
|
)
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
const (
|
|
|
|
APP_ID = app.ID(2)
|
|
|
|
)
|
2015-12-06 05:00:10 -05:00
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
// A DnsCache is an internal cache of DNS resolutions.
|
|
|
|
type DnsCache interface {
|
|
|
|
Get(domain string) net.IP
|
|
|
|
Add(domain string, ip net.IP)
|
2015-12-06 05:00:10 -05:00
|
|
|
}
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
type dnsCacheWithContext interface {
|
|
|
|
Get(context app.Context, domain string) net.IP
|
|
|
|
Add(contaxt app.Context, domain string, ip net.IP)
|
2015-12-06 05:00:10 -05:00
|
|
|
}
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
type contextedDnsCache struct {
|
|
|
|
context app.Context
|
|
|
|
dnsCache dnsCacheWithContext
|
2015-12-06 05:00:10 -05:00
|
|
|
}
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
func (this *contextedDnsCache) Get(domain string) net.IP {
|
|
|
|
return this.dnsCache.Get(this.context, domain)
|
2015-12-06 05:00:10 -05:00
|
|
|
}
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
func (this *contextedDnsCache) Add(domain string, ip net.IP) {
|
|
|
|
this.dnsCache.Add(this.context, domain, ip)
|
2015-12-06 05:00:10 -05:00
|
|
|
}
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
func init() {
|
|
|
|
app.RegisterApp(APP_ID, func(context app.Context, obj interface{}) interface{} {
|
|
|
|
dcContext := obj.(dnsCacheWithContext)
|
|
|
|
return &contextedDnsCache{
|
|
|
|
context: context,
|
|
|
|
dnsCache: dcContext,
|
|
|
|
}
|
|
|
|
})
|
2015-12-06 05:00:10 -05:00
|
|
|
}
|