Fix: dns use Extra records

This commit is contained in:
gVisor bot 2019-02-24 01:26:51 +08:00
parent b674db947b
commit 950204d5b9

View file

@ -64,12 +64,11 @@ func (r *Resolver) Exchange(m *D.Msg) (msg *D.Msg, err error) {
putMsgToCache(r.cache, q.String(), msg) putMsgToCache(r.cache, q.String(), msg)
if r.mapping { if r.mapping {
if ips, err := r.msgToIP(msg); err == nil { ips := r.msgToIP(msg)
for _, ip := range ips { for _, ip := range ips {
putMsgToCache(r.cache, ip.String(), msg) putMsgToCache(r.cache, ip.String(), msg)
} }
} }
}
}() }()
isIPReq := isIPRequest(q) isIPReq := isIPRequest(q)
@ -131,8 +130,7 @@ func (r *Resolver) resolveIP(m *D.Msg) (msg *D.Msg, err error) {
return nil, errors.New("GeoIP can't use") return nil, errors.New("GeoIP can't use")
} }
ips, err := r.msgToIP(res.Msg) if ips := r.msgToIP(res.Msg); len(ips) != 0 {
if err == nil {
if record, _ := mmdb.Country(ips[0]); record.Country.IsoCode == "CN" || record.Country.IsoCode == "" { if record, _ := mmdb.Country(ips[0]); record.Country.IsoCode == "CN" || record.Country.IsoCode == "" {
// release channel // release channel
go func() { <-fallbackMsg }() go func() { <-fallbackMsg }()
@ -165,18 +163,17 @@ func (r *Resolver) ResolveIP(host string) (ip net.IP, err error) {
return nil, err return nil, err
} }
var ips []net.IP ips := r.msgToIP(msg)
ips, err = r.msgToIP(msg) if len(ips) == 0 {
if err != nil { return nil, errors.New("can't found ip")
return nil, err
} }
ip = ips[0] ip = ips[0]
return return
} }
func (r *Resolver) msgToIP(msg *D.Msg) ([]net.IP, error) { func (r *Resolver) msgToIP(msg *D.Msg) []net.IP {
var ips []net.IP ips := []net.IP{}
for _, answer := range msg.Answer { for _, answer := range msg.Answer {
switch ans := answer.(type) { switch ans := answer.(type) {
@ -187,11 +184,16 @@ func (r *Resolver) msgToIP(msg *D.Msg) ([]net.IP, error) {
} }
} }
if len(ips) == 0 { for _, extra := range msg.Extra {
return nil, errors.New("Can't parse msg") switch record := extra.(type) {
case *D.AAAA:
ips = append(ips, record.AAAA)
case *D.A:
ips = append(ips, record.A)
}
} }
return ips, nil return ips
} }
func (r *Resolver) IPToHost(ip net.IP) (string, bool) { func (r *Resolver) IPToHost(ip net.IP) (string, bool) {