2017-12-19 17:55:09 -05:00
|
|
|
package dns
|
2016-05-16 02:09:28 -04:00
|
|
|
|
|
|
|
import (
|
2017-01-26 14:46:44 -05:00
|
|
|
"context"
|
2016-05-16 02:09:28 -04:00
|
|
|
|
2017-08-29 06:56:57 -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
|
|
|
}
|