Add dns net.LookupHost resolver!

This commit is contained in:
Asim Aslam 2019-10-08 09:04:13 +01:00
parent 89d71417f5
commit ad823d5177
2 changed files with 45 additions and 7 deletions

View File

@ -1,8 +1,7 @@
// Package dns resolves names to dns srv records // Package dns resolves names to dns records
package dns package dns
import ( import (
"fmt"
"net" "net"
"github.com/micro/go-micro/network/resolver" "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 // Resolve assumes ID is a domain name e.g micro.mu
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) { 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 { if err != nil {
return nil, err return nil, err
} }
var records []*resolver.Record var records []*resolver.Record
for _, addr := range addrs { for _, addr := range addrs {
address := addr.Target // join resolved record with port
if addr.Port > 0 { address := net.JoinHostPort(addr, port)
address = fmt.Sprintf("%s:%d", addr.Target, addr.Port) // append to record set
}
records = append(records, &resolver.Record{ records = append(records, &resolver.Record{
Address: address, Address: address,
}) })
} }
return records, nil return records, nil
} }

View File

@ -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
}