2019-10-08 11:04:13 +03:00
|
|
|
// Package dns srv resolves names to dns srv records
|
|
|
|
package dnssrv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/network/resolver"
|
2019-10-08 11:04:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2019-12-09 06:18:45 +03:00
|
|
|
records := make([]*resolver.Record, 0, len(addrs))
|
2019-10-08 11:04:13 +03:00
|
|
|
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
|
|
|
|
}
|