2020-06-24 13:46:51 +03:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2021-02-21 18:51:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
2020-06-24 13:46:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewRouter returns an initialized dns router
|
|
|
|
func NewRouter(opts ...router.Option) router.Router {
|
2021-02-21 18:51:17 +03:00
|
|
|
options := router.NewOptions(opts...)
|
2020-08-21 11:23:01 +03:00
|
|
|
return &dns{options}
|
2020-06-24 13:46:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type dns struct {
|
|
|
|
options router.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dns) Init(opts ...router.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&d.options)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dns) Options() router.Options {
|
|
|
|
return d.options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dns) Table() router.Table {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
func (d *dns) Close() error {
|
2020-06-24 13:46:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-21 18:51:17 +03:00
|
|
|
func (d *dns) Lookup(opts ...router.QueryOption) ([]router.Route, error) {
|
|
|
|
options := router.NewQuery(opts...)
|
2020-06-24 13:46:51 +03:00
|
|
|
// check to see if we have the port provided in the service, e.g. go-micro-srv-foo:8000
|
2021-02-21 18:51:17 +03:00
|
|
|
host, port, err := net.SplitHostPort(options.Service)
|
2020-06-24 13:46:51 +03:00
|
|
|
if err == nil {
|
|
|
|
// lookup the service using A records
|
|
|
|
ips, err := net.LookupHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p, _ := strconv.Atoi(port)
|
|
|
|
|
|
|
|
// convert the ip addresses to routes
|
|
|
|
result := make([]router.Route, len(ips))
|
|
|
|
for i, ip := range ips {
|
|
|
|
result[i] = router.Route{
|
2021-02-21 18:51:17 +03:00
|
|
|
Service: options.Service,
|
2020-06-24 13:46:51 +03:00
|
|
|
Address: fmt.Sprintf("%s:%d", ip, uint16(p)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// we didn't get the port so we'll lookup the service using SRV records. If we can't lookup the
|
|
|
|
// service using the SRV record, we return the error.
|
2021-02-21 18:51:17 +03:00
|
|
|
_, nodes, err := net.LookupSRV(options.Service, "tcp", d.options.Network)
|
2020-06-24 13:46:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert the nodes (net services) to routes
|
|
|
|
result := make([]router.Route, len(nodes))
|
|
|
|
for i, n := range nodes {
|
|
|
|
result[i] = router.Route{
|
2021-02-21 18:51:17 +03:00
|
|
|
Service: options.Service,
|
2020-06-24 13:46:51 +03:00
|
|
|
Address: fmt.Sprintf("%s:%d", n.Target, n.Port),
|
2020-08-21 11:23:01 +03:00
|
|
|
Network: d.options.Network,
|
2020-06-24 13:46:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2020-08-21 11:23:01 +03:00
|
|
|
|
|
|
|
func (d *dns) Watch(opts ...router.WatchOption) (router.Watcher, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-02-21 18:51:17 +03:00
|
|
|
func (d *dns) Name() string {
|
|
|
|
return d.options.Name
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
func (d *dns) String() string {
|
|
|
|
return "dns"
|
|
|
|
}
|