2021-02-14 16:16:01 +03:00
|
|
|
// Package dnssrv resolves names to dns srv records
|
2021-10-02 19:55:07 +03:00
|
|
|
package dnssrv // import "go.unistack.org/micro/v3/resolver/dnssrv"
|
2019-10-08 09:04:13 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/resolver"
|
2019-10-08 09:04:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Resolver is a DNS network resolve
|
2021-02-14 11:28:50 +03:00
|
|
|
type Resolver struct {
|
|
|
|
Address string
|
|
|
|
}
|
2019-10-08 09:04:13 +01:00
|
|
|
|
|
|
|
// 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 11:18:45 +08:00
|
|
|
records := make([]*resolver.Record, 0, len(addrs))
|
2019-10-08 09:04:13 +01: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
|
|
|
|
}
|