Move the network resolver out (#1944)
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
// Package dns resolves names to dns records
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/micro/go-micro/v3/network/resolver"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Resolver is a DNS network resolve
|
||||
type Resolver struct {
|
||||
// The resolver address to use
|
||||
Address string
|
||||
}
|
||||
|
||||
// Resolve assumes ID is a domain name e.g micro.mu
|
||||
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
||||
host, port, err := net.SplitHostPort(name)
|
||||
if err != nil {
|
||||
host = name
|
||||
port = "8085"
|
||||
}
|
||||
|
||||
if len(host) == 0 {
|
||||
host = "localhost"
|
||||
}
|
||||
|
||||
if len(r.Address) == 0 {
|
||||
r.Address = "1.0.0.1:53"
|
||||
}
|
||||
|
||||
//nolint:prealloc
|
||||
var records []*resolver.Record
|
||||
|
||||
// parsed an actual ip
|
||||
if v := net.ParseIP(host); v != nil {
|
||||
records = append(records, &resolver.Record{
|
||||
Address: net.JoinHostPort(host, port),
|
||||
})
|
||||
return records, nil
|
||||
}
|
||||
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion(dns.Fqdn(host), dns.TypeA)
|
||||
rec, err := dns.ExchangeContext(context.Background(), m, r.Address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, answer := range rec.Answer {
|
||||
h := answer.Header()
|
||||
// check record type matches
|
||||
if h.Rrtype != dns.TypeA {
|
||||
continue
|
||||
}
|
||||
|
||||
arec, _ := answer.(*dns.A)
|
||||
addr := arec.A.String()
|
||||
|
||||
// join resolved record with port
|
||||
address := net.JoinHostPort(addr, port)
|
||||
// append to record set
|
||||
records = append(records, &resolver.Record{
|
||||
Address: address,
|
||||
})
|
||||
}
|
||||
|
||||
// no records returned so just best effort it
|
||||
if len(records) == 0 {
|
||||
records = append(records, &resolver.Record{
|
||||
Address: net.JoinHostPort(host, port),
|
||||
})
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
// Package dns srv resolves names to dns srv records
|
||||
package dnssrv
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/micro/go-micro/v3/network/resolver"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
records := make([]*resolver.Record, 0, len(addrs))
|
||||
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
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
// Package http resolves names to network addresses using a http request
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/micro/go-micro/v3/network/resolver"
|
||||
)
|
||||
|
||||
// Resolver is a HTTP network resolver
|
||||
type Resolver struct {
|
||||
// If not set, defaults to http
|
||||
Proto string
|
||||
|
||||
// Path sets the path to lookup. Defaults to /network
|
||||
Path string
|
||||
|
||||
// Host url to use for the query
|
||||
Host string
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Nodes []*resolver.Record `json:"nodes,omitempty"`
|
||||
}
|
||||
|
||||
// Resolve assumes ID is a domain which can be converted to a http://name/network request
|
||||
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
||||
proto := "http"
|
||||
host := "localhost:8080"
|
||||
path := "/network/nodes"
|
||||
|
||||
if len(r.Proto) > 0 {
|
||||
proto = r.Proto
|
||||
}
|
||||
|
||||
if len(r.Path) > 0 {
|
||||
path = r.Path
|
||||
}
|
||||
|
||||
if len(r.Host) > 0 {
|
||||
host = r.Host
|
||||
}
|
||||
|
||||
uri := &url.URL{
|
||||
Scheme: proto,
|
||||
Path: path,
|
||||
Host: host,
|
||||
}
|
||||
q := uri.Query()
|
||||
q.Set("name", name)
|
||||
uri.RawQuery = q.Encode()
|
||||
|
||||
rsp, err := http.Get(uri.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
if rsp.StatusCode != 200 {
|
||||
return nil, errors.New("non 200 response")
|
||||
}
|
||||
b, err := ioutil.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// encoding format is assumed to be json
|
||||
var response *Response
|
||||
|
||||
if err := json.Unmarshal(b, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response.Nodes, nil
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
// Package noop is a noop resolver
|
||||
package noop
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/v3/network/resolver"
|
||||
)
|
||||
|
||||
type Resolver struct{}
|
||||
|
||||
// Resolve returns the list of nodes
|
||||
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
||||
return []*resolver.Record{}, nil
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
// Package registry resolves names using the go-micro registry
|
||||
package registry
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/v3/network/resolver"
|
||||
"github.com/micro/go-micro/v3/registry"
|
||||
"github.com/micro/go-micro/v3/registry/mdns"
|
||||
)
|
||||
|
||||
// Resolver is a registry network resolver
|
||||
type Resolver struct {
|
||||
// Registry is the registry to use otherwise we use the defaul
|
||||
Registry registry.Registry
|
||||
}
|
||||
|
||||
// Resolve assumes ID is a domain name e.g micro.mu
|
||||
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
||||
reg := r.Registry
|
||||
if reg == nil {
|
||||
reg = mdns.NewRegistry()
|
||||
}
|
||||
|
||||
services, err := reg.GetService(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var records []*resolver.Record
|
||||
|
||||
for _, service := range services {
|
||||
for _, node := range service.Nodes {
|
||||
records = append(records, &resolver.Record{
|
||||
Address: node.Address,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
// Package resolver resolves network names to addresses
|
||||
package resolver
|
||||
|
||||
// Resolver is network resolver. It's used to find network nodes
|
||||
// via the name to connect to. This is done based on Network.Name().
|
||||
// Before we can be part of any network, we have to connect to it.
|
||||
type Resolver interface {
|
||||
// Resolve returns a list of addresses for a name
|
||||
Resolve(name string) ([]*Record, error)
|
||||
}
|
||||
|
||||
// A resolved record
|
||||
type Record struct {
|
||||
Address string `json:"address"`
|
||||
Priority int64 `json:"priority"`
|
||||
}
|
@@ -1,33 +0,0 @@
|
||||
// Package static is a static resolver
|
||||
package static
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/v3/network/resolver"
|
||||
)
|
||||
|
||||
// Resolver returns a static list of nodes. In the event the node list
|
||||
// is not present it will return the name of the network passed in.
|
||||
type Resolver struct {
|
||||
// A static list of nodes
|
||||
Nodes []string
|
||||
}
|
||||
|
||||
// Resolve returns the list of nodes
|
||||
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
||||
// if there are no nodes just return the name
|
||||
if len(r.Nodes) == 0 {
|
||||
return []*resolver.Record{
|
||||
{Address: name},
|
||||
}, nil
|
||||
}
|
||||
|
||||
records := make([]*resolver.Record, 0, len(r.Nodes))
|
||||
|
||||
for _, node := range r.Nodes {
|
||||
records = append(records, &resolver.Record{
|
||||
Address: node,
|
||||
})
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
Reference in New Issue
Block a user