2019-10-08 11:04:13 +03:00
|
|
|
// Package dns resolves names to dns records
|
2019-07-28 14:14:40 +03:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2019-11-12 18:46:30 +03:00
|
|
|
"context"
|
2019-07-28 14:14:40 +03:00
|
|
|
"net"
|
2021-02-13 15:35:56 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-07-28 14:14:40 +03:00
|
|
|
|
2020-08-20 15:23:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/resolver"
|
2019-07-28 14:14:40 +03:00
|
|
|
)
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// Resolver is a DNS network resolve
|
2019-11-12 18:46:30 +03:00
|
|
|
type Resolver struct {
|
|
|
|
// The resolver address to use
|
2021-02-13 15:35:56 +03:00
|
|
|
Address string
|
|
|
|
goresolver *net.Resolver
|
|
|
|
sync.RWMutex
|
2019-11-12 18:46:30 +03:00
|
|
|
}
|
2019-07-28 14:14:40 +03:00
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Resolve tries to resolve endpoint address
|
2019-07-28 22:00:09 +03:00
|
|
|
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
2019-10-08 11:04:13 +03:00
|
|
|
host, port, err := net.SplitHostPort(name)
|
|
|
|
if err != nil {
|
|
|
|
host = name
|
|
|
|
port = "8085"
|
|
|
|
}
|
|
|
|
|
2019-10-13 20:36:22 +03:00
|
|
|
if len(host) == 0 {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:46:30 +03:00
|
|
|
if len(r.Address) == 0 {
|
2021-02-13 15:35:56 +03:00
|
|
|
r.Address = "1.1.1.1:53"
|
2019-11-12 18:46:30 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 02:28:39 +03:00
|
|
|
// parsed an actual ip
|
|
|
|
if v := net.ParseIP(host); v != nil {
|
2021-02-13 15:35:56 +03:00
|
|
|
rec := &resolver.Record{Address: net.JoinHostPort(host, port)}
|
|
|
|
return []*resolver.Record{rec}, nil
|
2019-12-08 02:28:39 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
r.RLock()
|
|
|
|
goresolver := r.goresolver
|
|
|
|
r.RUnlock()
|
|
|
|
|
|
|
|
if goresolver == nil {
|
|
|
|
r.Lock()
|
|
|
|
r.goresolver = &net.Resolver{
|
|
|
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
d := net.Dialer{
|
|
|
|
Timeout: time.Millisecond * time.Duration(100),
|
|
|
|
}
|
|
|
|
return d.DialContext(ctx, "udp", r.Address)
|
|
|
|
},
|
2019-11-12 18:46:30 +03:00
|
|
|
}
|
2021-02-13 15:35:56 +03:00
|
|
|
r.Unlock()
|
|
|
|
}
|
2019-11-12 18:46:30 +03:00
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
addrs, err := goresolver.LookupIP(context.TODO(), "ip", host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-08 11:04:13 +03:00
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
if len(addrs) == 0 {
|
|
|
|
rec := &resolver.Record{Address: net.JoinHostPort(host, port)}
|
|
|
|
return []*resolver.Record{rec}, nil
|
2019-07-28 14:14:40 +03:00
|
|
|
}
|
2019-10-08 11:04:13 +03:00
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
records := make([]*resolver.Record, 0, len(addrs))
|
|
|
|
for _, addr := range addrs {
|
2019-12-08 02:28:39 +03:00
|
|
|
records = append(records, &resolver.Record{
|
2021-02-13 15:35:56 +03:00
|
|
|
Address: net.JoinHostPort(addr.String(), port),
|
2019-12-08 02:28:39 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-28 14:14:40 +03:00
|
|
|
return records, nil
|
|
|
|
}
|