1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 18:36:11 -05:00
v2fly/app/dns/nameserver.go

36 lines
624 B
Go
Raw Normal View History

2017-12-19 17:55:09 -05:00
package dns
2016-05-16 02:09:28 -04:00
import (
"context"
2016-05-16 02:09:28 -04:00
"v2ray.com/core/common/net"
2016-05-16 02:09:28 -04:00
)
type NameServer interface {
2018-06-26 09:04:47 -04:00
QueryIP(ctx context.Context, domain string) ([]net.IP, error)
2016-05-16 02:09:28 -04:00
}
2016-05-16 12:05:01 -04:00
2018-06-27 05:23:39 -04:00
type localNameServer struct {
2018-06-26 09:16:45 -04:00
resolver net.Resolver
2016-05-16 12:05:01 -04:00
}
2018-06-27 05:23:39 -04:00
func (s *localNameServer) QueryIP(ctx context.Context, domain string) ([]net.IP, error) {
2018-06-26 09:16:45 -04:00
ipAddr, err := s.resolver.LookupIPAddr(ctx, domain)
if err != nil {
return nil, err
}
var ips []net.IP
for _, addr := range ipAddr {
ips = append(ips, addr.IP)
}
return ips, nil
}
2018-06-27 05:23:39 -04:00
func NewLocalNameServer() *localNameServer {
return &localNameServer{
2018-06-26 09:16:45 -04:00
resolver: net.Resolver{
PreferGo: true,
},
}
2016-05-16 12:05:01 -04:00
}