2019-10-08 09:04:13 +01:00
|
|
|
// Package dns resolves names to dns records
|
2019-07-28 12:14:40 +01:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/network/resolver"
|
|
|
|
)
|
|
|
|
|
2019-08-20 12:48:51 +01:00
|
|
|
// Resolver is a DNS network resolve
|
2019-07-28 12:14:40 +01:00
|
|
|
type Resolver struct{}
|
|
|
|
|
|
|
|
// Resolve assumes ID is a domain name e.g micro.mu
|
2019-07-28 20:00:09 +01:00
|
|
|
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
2019-10-08 09:04:13 +01:00
|
|
|
host, port, err := net.SplitHostPort(name)
|
|
|
|
if err != nil {
|
|
|
|
host = name
|
|
|
|
port = "8085"
|
|
|
|
}
|
|
|
|
|
2019-10-13 18:36:22 +01:00
|
|
|
if len(host) == 0 {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:04:13 +01:00
|
|
|
addrs, err := net.LookupHost(host)
|
2019-07-28 12:14:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-08 09:04:13 +01:00
|
|
|
|
2019-07-28 12:14:40 +01:00
|
|
|
var records []*resolver.Record
|
2019-10-08 09:04:13 +01:00
|
|
|
|
2019-07-28 12:14:40 +01:00
|
|
|
for _, addr := range addrs {
|
2019-10-08 09:04:13 +01:00
|
|
|
// join resolved record with port
|
|
|
|
address := net.JoinHostPort(addr, port)
|
|
|
|
// append to record set
|
2019-07-28 12:14:40 +01:00
|
|
|
records = append(records, &resolver.Record{
|
|
|
|
Address: address,
|
|
|
|
})
|
|
|
|
}
|
2019-10-08 09:04:13 +01:00
|
|
|
|
2019-07-28 12:14:40 +01:00
|
|
|
return records, nil
|
|
|
|
}
|