2019-07-28 22:00:09 +03:00
|
|
|
// Package dns resolves names to dns srv records
|
2019-07-28 14:14:40 +03:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/network/resolver"
|
|
|
|
)
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// Resolver is a DNS network resolve
|
2019-07-28 14:14:40 +03:00
|
|
|
type Resolver struct{}
|
|
|
|
|
|
|
|
// Resolve assumes ID is a domain name e.g micro.mu
|
2019-07-28 22:00:09 +03:00
|
|
|
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
|
|
|
_, addrs, err := net.LookupSRV("network", "udp", name)
|
2019-07-28 14:14:40 +03:00
|
|
|
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
|
|
|
|
}
|