-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
131 lines (108 loc) · 5.62 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package resolver
import (
"context"
"net"
"net/netip"
"github.com/miekg/dns"
"darvaza.org/resolver/pkg/errors"
"darvaza.org/resolver/pkg/exdns"
)
// A DialerFunc is a function that establishes TCP or UDP connection
type DialerFunc func(ctx context.Context, network, address string) (net.Conn, error)
// A Resolver implements the interface of net.Resolver
type Resolver interface {
// LookupIPAddr looks up host using the assigned Lookuper.
// It returns a slice of that host's IPv4 and IPv6 addresses.
LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)
// LookupIP looks up host for the given network using assigned Lookuper
// It returns a slice of that host's IP addresses of the type specified
// by network. network must be one of "ip", "ip4" or "ip6".
LookupIP(ctx context.Context, network, host string) ([]net.IP, error)
// LookupNetIP looks up host using the assigned Lookuper. It returns a
// slice of that host's IP addresses of the type specified by network.
// The network must be one of "ip", "ip4" or "ip6".
LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error)
// LookupAddr performs a reverse lookup for the given address, returning a list
// of names mapping to that address.
//
// The returned names are validated to be properly formatted presentation-format
// domain names. If the response contains invalid names, those records are
// filtered out and an error will be returned alongside the remaining results,
// if any.
LookupAddr(ctx context.Context, addr string) ([]string, error)
// LookupCNAME returns the canonical name for the given host. Callers that do not
// care about the canonical name can call LookupHost or LookupIP directly; both
// take care of resolving the canonical name as part of the lookup.
// A canonical name is the final name after following zero or more CNAME records.
// LookupCNAME does not return an error if host does not contain DNS "CNAME"
// records, as long as host resolves to address records.
// The returned canonical name is validated to be a properly formatted
// presentation-format domain name.
LookupCNAME(ctx context.Context, host string) (string, error)
// LookupHost looks up the given host using the assigned Lookuper. It returns a
// slice of that host's addresses.
LookupHost(ctx context.Context, host string) (addrs []string, err error)
// LookupMX returns the DNS MX records for the given domain name sorted by
// preference.
// The returned mail server names are validated to be properly formatted
// presentation-format domain names. If the response contains invalid names,
// those records are filtered out and an error will be returned alongside
// the remaining results, if any.
LookupMX(ctx context.Context, name string) ([]*net.MX, error)
// LookupNS returns the DNS NS records for the given domain name.
// The returned name server names are validated to be properly formatted
// presentation-format domain names. If the response contains invalid names,
// those records are filtered out and an error will be returned alongside
// the remaining results, if any.
LookupNS(ctx context.Context, name string) ([]*net.NS, error)
// LookupSRV tries to resolve an SRV query of the given service, protocol,
// and domain name. The proto is "tcp" or "udp". The returned records are
// sorted by priority and randomized by weight within a priority.
//
// LookupSRV constructs the DNS name to look up following RFC 2782. That is,
// it looks up _service._proto.name. To accommodate services publishing SRV
// records under non-standard names, if both service and proto are empty
// strings, LookupSRV looks up name directly.
//
// The returned service names are validated to be properly formatted
// presentation-format domain names. If the response contains invalid names,
// those records are filtered out and an error will be returned alongside
// the remaining results, if any.
LookupSRV(ctx context.Context, service, proto, name string) (cname string,
addrs []*net.SRV, err error)
// LookupTXT returns the DNS TXT records for the given domain name.
LookupTXT(ctx context.Context, name string) ([]string, error)
}
// Lookuper is the interface that wraps the basic iterative Lookup method.
type Lookuper interface {
Lookup(ctx context.Context, qName string, qType uint16) (*dns.Msg, error)
}
// LookuperFunc is a function that implements the [Lookuper] interface
type LookuperFunc func(context.Context, string, uint16) (*dns.Msg, error)
// Lookup implements the [Lookuper] interface
func (fn LookuperFunc) Lookup(ctx context.Context, qName string, qType uint16) (*dns.Msg, error) {
return fn(ctx, qName, qType)
}
// Exchange implements the [Exchanger] interface using a [Lookuper] function
func (fn LookuperFunc) Exchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
if msg != nil && len(msg.Question) > 0 {
q := msg.Question[0]
return fn(ctx, q.Name, q.Qtype)
}
return nil, errors.ErrBadRequest()
}
// Exchanger performs a Lookup using a pre-assembled [dns.Msg] question.
type Exchanger interface {
Exchange(ctx context.Context, q *dns.Msg) (*dns.Msg, error)
}
// ExchangerFunc is a function that implements the [Exchanger] interface
type ExchangerFunc func(context.Context, *dns.Msg) (*dns.Msg, error)
// Exchange implements the [Exchanger] interface
func (fn ExchangerFunc) Exchange(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) {
return fn(ctx, msg)
}
// Lookup implements the [Lookuper] interface using an [Exchanger] function
func (fn ExchangerFunc) Lookup(ctx context.Context, qName string, qType uint16) (*dns.Msg, error) {
msg := exdns.NewRequestFromParts(dns.Fqdn(qName), dns.ClassINET, qType)
return fn(ctx, msg)
}