fix dns Resolve
Signed-off-by: arraykeys@gmail.com <arraykeys@gmail.com>
This commit is contained in:
@ -855,10 +855,13 @@ func (a *DomainResolver) MustResolve(address string) (ip string) {
|
|||||||
func (a *DomainResolver) Resolve(address string) (ip string, err error) {
|
func (a *DomainResolver) Resolve(address string) (ip string, err error) {
|
||||||
domain := address
|
domain := address
|
||||||
port := ""
|
port := ""
|
||||||
|
fromCache := "false"
|
||||||
defer func() {
|
defer func() {
|
||||||
if port != "" {
|
if port != "" {
|
||||||
ip = net.JoinHostPort(ip, port)
|
ip = net.JoinHostPort(ip, port)
|
||||||
}
|
}
|
||||||
|
log.Printf("dns:%s->%s,cache:%s", address, ip, fromCache)
|
||||||
|
//a.PrintData()
|
||||||
}()
|
}()
|
||||||
if strings.Contains(domain, ":") {
|
if strings.Contains(domain, ":") {
|
||||||
domain, port, err = net.SplitHostPort(domain)
|
domain, port, err = net.SplitHostPort(domain)
|
||||||
@ -868,25 +871,28 @@ func (a *DomainResolver) Resolve(address string) (ip string, err error) {
|
|||||||
}
|
}
|
||||||
if net.ParseIP(domain) != nil {
|
if net.ParseIP(domain) != nil {
|
||||||
ip = domain
|
ip = domain
|
||||||
|
fromCache = "ip ignore"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
item, ok := a.data.Get(domain)
|
item, ok := a.data.Get(domain)
|
||||||
if ok {
|
if ok {
|
||||||
|
//log.Println("find ", domain)
|
||||||
if (*item.(*DomainResolverItem)).expiredAt > time.Now().Unix() {
|
if (*item.(*DomainResolverItem)).expiredAt > time.Now().Unix() {
|
||||||
ip = (*item.(*DomainResolverItem)).ip
|
ip = (*item.(*DomainResolverItem)).ip
|
||||||
|
fromCache = "true"
|
||||||
|
//log.Println("from cache ", domain)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
item = &DomainResolverItem{
|
item = &DomainResolverItem{
|
||||||
domain: domain,
|
domain: domain,
|
||||||
}
|
}
|
||||||
a.data.Set(domain, item)
|
|
||||||
}
|
}
|
||||||
c := new(dns.Client)
|
c := new(dns.Client)
|
||||||
c.DialTimeout = time.Millisecond * 1500
|
c.DialTimeout = time.Millisecond * 5000
|
||||||
c.ReadTimeout = time.Millisecond * 1500
|
c.ReadTimeout = time.Millisecond * 5000
|
||||||
c.WriteTimeout = time.Millisecond * 1500
|
c.WriteTimeout = time.Millisecond * 5000
|
||||||
m := new(dns.Msg)
|
m := new(dns.Msg)
|
||||||
m.SetQuestion(dns.Fqdn(domain), dns.TypeA)
|
m.SetQuestion(dns.Fqdn(domain), dns.TypeA)
|
||||||
m.RecursionDesired = true
|
m.RecursionDesired = true
|
||||||
@ -906,10 +912,11 @@ func (a *DomainResolver) Resolve(address string) (ip string, err error) {
|
|||||||
_item := item.(*DomainResolverItem)
|
_item := item.(*DomainResolverItem)
|
||||||
(*_item).expiredAt = time.Now().Unix() + int64(a.ttl)
|
(*_item).expiredAt = time.Now().Unix() + int64(a.ttl)
|
||||||
(*_item).ip = ip
|
(*_item).ip = ip
|
||||||
}
|
a.data.Set(domain, item)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func (a *DomainResolver) PrintData() {
|
func (a *DomainResolver) PrintData() {
|
||||||
|
|||||||
Reference in New Issue
Block a user