mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
update resolver package
This commit is contained in:
parent
31ebe517e5
commit
de42aa6073
@ -1,97 +0,0 @@
|
|||||||
// Package dns_resolver is a simple dns resolver
|
|
||||||
// based on miekg/dns
|
|
||||||
package dns_resolver
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"math/rand"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DnsResolver represents a dns resolver
|
|
||||||
type DnsResolver struct {
|
|
||||||
Servers []string
|
|
||||||
RetryTimes int
|
|
||||||
r *rand.Rand
|
|
||||||
}
|
|
||||||
|
|
||||||
// New initializes DnsResolver.
|
|
||||||
func New(servers []string) *DnsResolver {
|
|
||||||
for i := range servers {
|
|
||||||
servers[i] = net.JoinHostPort(servers[i], "53")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &DnsResolver{servers, len(servers) * 2, rand.New(rand.NewSource(time.Now().UnixNano()))}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFromResolvConf initializes DnsResolver from resolv.conf like file.
|
|
||||||
func NewFromResolvConf(path string) (*DnsResolver, error) {
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
||||||
return &DnsResolver{}, errors.New("no such file or directory: " + path)
|
|
||||||
}
|
|
||||||
config, err := dns.ClientConfigFromFile(path)
|
|
||||||
servers := []string{}
|
|
||||||
for _, ipAddress := range config.Servers {
|
|
||||||
servers = append(servers, net.JoinHostPort(ipAddress, "53"))
|
|
||||||
}
|
|
||||||
return &DnsResolver{servers, len(servers) * 2, rand.New(rand.NewSource(time.Now().UnixNano()))}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// LookupHost returns IP addresses of provied host.
|
|
||||||
// In case of timeout retries query RetryTimes times.
|
|
||||||
func (r *DnsResolver) LookupHost(host string, dnsType uint16) ([]net.IP, error) {
|
|
||||||
return r.lookupHost(host, dnsType, r.RetryTimes)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *DnsResolver) lookupHost(host string, dnsType uint16, triesLeft int) ([]net.IP, error) {
|
|
||||||
m1 := new(dns.Msg)
|
|
||||||
m1.Id = dns.Id()
|
|
||||||
m1.RecursionDesired = true
|
|
||||||
m1.Question = make([]dns.Question, 1)
|
|
||||||
|
|
||||||
switch dnsType {
|
|
||||||
case dns.TypeA:
|
|
||||||
m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeA, dns.ClassINET}
|
|
||||||
case dns.TypeAAAA:
|
|
||||||
m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeAAAA, dns.ClassINET}
|
|
||||||
}
|
|
||||||
|
|
||||||
in, err := dns.Exchange(m1, r.Servers[r.r.Intn(len(r.Servers))])
|
|
||||||
|
|
||||||
result := []net.IP{}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if strings.HasSuffix(err.Error(), "i/o timeout") && triesLeft > 0 {
|
|
||||||
triesLeft--
|
|
||||||
return r.lookupHost(host, dnsType, triesLeft)
|
|
||||||
}
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if in != nil && in.Rcode != dns.RcodeSuccess {
|
|
||||||
return result, errors.New(dns.RcodeToString[in.Rcode])
|
|
||||||
}
|
|
||||||
|
|
||||||
if dnsType == dns.TypeA {
|
|
||||||
for _, record := range in.Answer {
|
|
||||||
if t, ok := record.(*dns.A); ok {
|
|
||||||
result = append(result, t.A)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if dnsType == dns.TypeAAAA {
|
|
||||||
for _, record := range in.Answer {
|
|
||||||
if t, ok := record.(*dns.AAAA); ok {
|
|
||||||
result = append(result, t.AAAA)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, err
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package dns_resolver
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
|
||||||
servers := []string{"8.8.8.8", "8.8.4.4"}
|
|
||||||
expectedServers := []string{"8.8.8.8:53", "8.8.4.4:53"}
|
|
||||||
resolver := New(servers)
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(resolver.Servers, expectedServers) {
|
|
||||||
t.Error("resolver.Servers: ", resolver.Servers, "should be equal to", expectedServers)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLookupHost_ValidServer(t *testing.T) {
|
|
||||||
resolver := New([]string{"8.8.8.8", "8.8.4.4"})
|
|
||||||
result, err := resolver.LookupHost("google-public-dns-a.google.com", dns.TypeA)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
t.Error("Should succeed dns lookup")
|
|
||||||
}
|
|
||||||
|
|
||||||
if result[0].String() != "8.8.8.8" {
|
|
||||||
t.Error("google-public-dns-a.google.com should be resolved to 8.8.8.8")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLookupHostIPv6_ValidServer(t *testing.T) {
|
|
||||||
resolver := New([]string{"2001:4860:4860::8888", "2001:4860:4860::8844"})
|
|
||||||
result, err := resolver.LookupHost("google-public-dns-a.google.com", dns.TypeAAAA)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
t.Error("Should succeed dns lookup")
|
|
||||||
}
|
|
||||||
|
|
||||||
if result[0].String() != "2001:4860:4860::8888" {
|
|
||||||
t.Error("result shoudl be: 2001:4860:4860::8888")
|
|
||||||
}
|
|
||||||
}
|
|
5
utils.go
5
utils.go
@ -12,7 +12,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/TimothyYe/godns/dns_resolver"
|
dnsResolver "github.com/TimothyYe/godns/resolver"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
gomail "gopkg.in/gomail.v2"
|
gomail "gopkg.in/gomail.v2"
|
||||||
@ -377,7 +378,7 @@ func ResolveDNS(hostname, resolver, ipType string) string {
|
|||||||
}
|
}
|
||||||
return dnsAdress[0]
|
return dnsAdress[0]
|
||||||
}
|
}
|
||||||
res := dns_resolver.New([]string{resolver})
|
res := dnsResolver.New([]string{resolver})
|
||||||
// In case of i/o timeout
|
// In case of i/o timeout
|
||||||
res.RetryTimes = 5
|
res.RetryTimes = 5
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user