1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 03:25:23 +00:00
v2fly/app/dns/nameserver.go

36 lines
633 B
Go
Raw Normal View History

2017-12-19 22:55:09 +00:00
package dns
2016-05-16 06:09:28 +00:00
import (
"context"
2016-05-16 06:09:28 +00:00
"v2ray.com/core/common/net"
2016-05-16 06:09:28 +00:00
)
type NameServerInterface interface {
2018-06-26 13:04:47 +00:00
QueryIP(ctx context.Context, domain string) ([]net.IP, error)
2016-05-16 06:09:28 +00:00
}
2016-05-16 16:05:01 +00:00
2018-06-27 09:23:39 +00:00
type localNameServer struct {
2018-06-26 13:16:45 +00:00
resolver net.Resolver
2016-05-16 16:05:01 +00:00
}
2018-06-27 09:23:39 +00:00
func (s *localNameServer) QueryIP(ctx context.Context, domain string) ([]net.IP, error) {
2018-06-26 13:16:45 +00: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 09:23:39 +00:00
func NewLocalNameServer() *localNameServer {
return &localNameServer{
2018-06-26 13:16:45 +00:00
resolver: net.Resolver{
PreferGo: true,
},
}
2016-05-16 16:05:01 +00:00
}