From ad823d5177805a707ec3e2d3d8923b5a9b58ec6b Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 8 Oct 2019 09:04:13 +0100 Subject: [PATCH] Add dns net.LookupHost resolver! --- network/resolver/dns/dns.go | 21 ++++++++++++++------- network/resolver/dnssrv/dnssrv.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 network/resolver/dnssrv/dnssrv.go diff --git a/network/resolver/dns/dns.go b/network/resolver/dns/dns.go index 3cfa2746..fe022d93 100644 --- a/network/resolver/dns/dns.go +++ b/network/resolver/dns/dns.go @@ -1,8 +1,7 @@ -// Package dns resolves names to dns srv records +// Package dns resolves names to dns records package dns import ( - "fmt" "net" "github.com/micro/go-micro/network/resolver" @@ -13,19 +12,27 @@ type Resolver struct{} // Resolve assumes ID is a domain name e.g micro.mu func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) { - _, addrs, err := net.LookupSRV("network", "udp", name) + host, port, err := net.SplitHostPort(name) + if err != nil { + host = name + port = "8085" + } + + addrs, err := net.LookupHost(host) if err != nil { return nil, err } + var records []*resolver.Record + for _, addr := range addrs { - address := addr.Target - if addr.Port > 0 { - address = fmt.Sprintf("%s:%d", addr.Target, addr.Port) - } + // join resolved record with port + address := net.JoinHostPort(addr, port) + // append to record set records = append(records, &resolver.Record{ Address: address, }) } + return records, nil } diff --git a/network/resolver/dnssrv/dnssrv.go b/network/resolver/dnssrv/dnssrv.go new file mode 100644 index 00000000..75a11795 --- /dev/null +++ b/network/resolver/dnssrv/dnssrv.go @@ -0,0 +1,31 @@ +// Package dns srv resolves names to dns srv records +package dnssrv + +import ( + "fmt" + "net" + + "github.com/micro/go-micro/network/resolver" +) + +// Resolver is a DNS network resolve +type Resolver struct{} + +// Resolve assumes ID is a domain name e.g micro.mu +func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) { + _, addrs, err := net.LookupSRV("network", "udp", name) + if err != nil { + return nil, err + } + var records []*resolver.Record + for _, addr := range addrs { + address := addr.Target + if addr.Port > 0 { + address = fmt.Sprintf("%s:%d", addr.Target, addr.Port) + } + records = append(records, &resolver.Record{ + Address: address, + }) + } + return records, nil +}