123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- /*
- *
- * Copyright 2018 gRPC authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- // Package dns implements a dns resolver to be installed as the default resolver
- // in grpc.
- package dns
- import (
- "context"
- "encoding/json"
- "fmt"
- "net"
- "os"
- "strconv"
- "strings"
- "sync"
- "time"
- grpclbstate "google.golang.org/grpc/balancer/grpclb/state"
- "google.golang.org/grpc/grpclog"
- "google.golang.org/grpc/internal/backoff"
- "google.golang.org/grpc/internal/envconfig"
- "google.golang.org/grpc/internal/grpcrand"
- "google.golang.org/grpc/internal/resolver/dns/internal"
- "google.golang.org/grpc/resolver"
- "google.golang.org/grpc/serviceconfig"
- )
- // EnableSRVLookups controls whether the DNS resolver attempts to fetch gRPCLB
- // addresses from SRV records. Must not be changed after init time.
- var EnableSRVLookups = false
- var logger = grpclog.Component("dns")
- func init() {
- resolver.Register(NewBuilder())
- internal.TimeAfterFunc = time.After
- internal.NewNetResolver = newNetResolver
- internal.AddressDialer = addressDialer
- }
- const (
- defaultPort = "443"
- defaultDNSSvrPort = "53"
- golang = "GO"
- // txtPrefix is the prefix string to be prepended to the host name for txt
- // record lookup.
- txtPrefix = "_grpc_config."
- // In DNS, service config is encoded in a TXT record via the mechanism
- // described in RFC-1464 using the attribute name grpc_config.
- txtAttribute = "grpc_config="
- )
- var addressDialer = func(address string) func(context.Context, string, string) (net.Conn, error) {
- return func(ctx context.Context, network, _ string) (net.Conn, error) {
- var dialer net.Dialer
- return dialer.DialContext(ctx, network, address)
- }
- }
- var newNetResolver = func(authority string) (internal.NetResolver, error) {
- if authority == "" {
- return net.DefaultResolver, nil
- }
- host, port, err := parseTarget(authority, defaultDNSSvrPort)
- if err != nil {
- return nil, err
- }
- authorityWithPort := net.JoinHostPort(host, port)
- return &net.Resolver{
- PreferGo: true,
- Dial: internal.AddressDialer(authorityWithPort),
- }, nil
- }
- // NewBuilder creates a dnsBuilder which is used to factory DNS resolvers.
- func NewBuilder() resolver.Builder {
- return &dnsBuilder{}
- }
- type dnsBuilder struct{}
- // Build creates and starts a DNS resolver that watches the name resolution of
- // the target.
- func (b *dnsBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
- host, port, err := parseTarget(target.Endpoint(), defaultPort)
- if err != nil {
- return nil, err
- }
- // IP address.
- if ipAddr, ok := formatIP(host); ok {
- addr := []resolver.Address{{Addr: ipAddr + ":" + port}}
- cc.UpdateState(resolver.State{Addresses: addr})
- return deadResolver{}, nil
- }
- // DNS address (non-IP).
- ctx, cancel := context.WithCancel(context.Background())
- d := &dnsResolver{
- host: host,
- port: port,
- ctx: ctx,
- cancel: cancel,
- cc: cc,
- rn: make(chan struct{}, 1),
- disableServiceConfig: opts.DisableServiceConfig,
- }
- d.resolver, err = internal.NewNetResolver(target.URL.Host)
- if err != nil {
- return nil, err
- }
- d.wg.Add(1)
- go d.watcher()
- return d, nil
- }
- // Scheme returns the naming scheme of this resolver builder, which is "dns".
- func (b *dnsBuilder) Scheme() string {
- return "dns"
- }
- // deadResolver is a resolver that does nothing.
- type deadResolver struct{}
- func (deadResolver) ResolveNow(resolver.ResolveNowOptions) {}
- func (deadResolver) Close() {}
- // dnsResolver watches for the name resolution update for a non-IP target.
- type dnsResolver struct {
- host string
- port string
- resolver internal.NetResolver
- ctx context.Context
- cancel context.CancelFunc
- cc resolver.ClientConn
- // rn channel is used by ResolveNow() to force an immediate resolution of the
- // target.
- rn chan struct{}
- // wg is used to enforce Close() to return after the watcher() goroutine has
- // finished. Otherwise, data race will be possible. [Race Example] in
- // dns_resolver_test we replace the real lookup functions with mocked ones to
- // facilitate testing. If Close() doesn't wait for watcher() goroutine
- // finishes, race detector sometimes will warns lookup (READ the lookup
- // function pointers) inside watcher() goroutine has data race with
- // replaceNetFunc (WRITE the lookup function pointers).
- wg sync.WaitGroup
- disableServiceConfig bool
- }
- // ResolveNow invoke an immediate resolution of the target that this
- // dnsResolver watches.
- func (d *dnsResolver) ResolveNow(resolver.ResolveNowOptions) {
- select {
- case d.rn <- struct{}{}:
- default:
- }
- }
- // Close closes the dnsResolver.
- func (d *dnsResolver) Close() {
- d.cancel()
- d.wg.Wait()
- }
- func (d *dnsResolver) watcher() {
- defer d.wg.Done()
- backoffIndex := 1
- for {
- state, err := d.lookup()
- if err != nil {
- // Report error to the underlying grpc.ClientConn.
- d.cc.ReportError(err)
- } else {
- err = d.cc.UpdateState(*state)
- }
- var waitTime time.Duration
- if err == nil {
- // Success resolving, wait for the next ResolveNow. However, also wait 30
- // seconds at the very least to prevent constantly re-resolving.
- backoffIndex = 1
- waitTime = internal.MinResolutionRate
- select {
- case <-d.ctx.Done():
- return
- case <-d.rn:
- }
- } else {
- // Poll on an error found in DNS Resolver or an error received from
- // ClientConn.
- waitTime = backoff.DefaultExponential.Backoff(backoffIndex)
- backoffIndex++
- }
- select {
- case <-d.ctx.Done():
- return
- case <-internal.TimeAfterFunc(waitTime):
- }
- }
- }
- func (d *dnsResolver) lookupSRV() ([]resolver.Address, error) {
- if !EnableSRVLookups {
- return nil, nil
- }
- var newAddrs []resolver.Address
- _, srvs, err := d.resolver.LookupSRV(d.ctx, "grpclb", "tcp", d.host)
- if err != nil {
- err = handleDNSError(err, "SRV") // may become nil
- return nil, err
- }
- for _, s := range srvs {
- lbAddrs, err := d.resolver.LookupHost(d.ctx, s.Target)
- if err != nil {
- err = handleDNSError(err, "A") // may become nil
- if err == nil {
- // If there are other SRV records, look them up and ignore this
- // one that does not exist.
- continue
- }
- return nil, err
- }
- for _, a := range lbAddrs {
- ip, ok := formatIP(a)
- if !ok {
- return nil, fmt.Errorf("dns: error parsing A record IP address %v", a)
- }
- addr := ip + ":" + strconv.Itoa(int(s.Port))
- newAddrs = append(newAddrs, resolver.Address{Addr: addr, ServerName: s.Target})
- }
- }
- return newAddrs, nil
- }
- func handleDNSError(err error, lookupType string) error {
- dnsErr, ok := err.(*net.DNSError)
- if ok && !dnsErr.IsTimeout && !dnsErr.IsTemporary {
- // Timeouts and temporary errors should be communicated to gRPC to
- // attempt another DNS query (with backoff). Other errors should be
- // suppressed (they may represent the absence of a TXT record).
- return nil
- }
- if err != nil {
- err = fmt.Errorf("dns: %v record lookup error: %v", lookupType, err)
- logger.Info(err)
- }
- return err
- }
- func (d *dnsResolver) lookupTXT() *serviceconfig.ParseResult {
- ss, err := d.resolver.LookupTXT(d.ctx, txtPrefix+d.host)
- if err != nil {
- if envconfig.TXTErrIgnore {
- return nil
- }
- if err = handleDNSError(err, "TXT"); err != nil {
- return &serviceconfig.ParseResult{Err: err}
- }
- return nil
- }
- var res string
- for _, s := range ss {
- res += s
- }
- // TXT record must have "grpc_config=" attribute in order to be used as
- // service config.
- if !strings.HasPrefix(res, txtAttribute) {
- logger.Warningf("dns: TXT record %v missing %v attribute", res, txtAttribute)
- // This is not an error; it is the equivalent of not having a service
- // config.
- return nil
- }
- sc := canaryingSC(strings.TrimPrefix(res, txtAttribute))
- return d.cc.ParseServiceConfig(sc)
- }
- func (d *dnsResolver) lookupHost() ([]resolver.Address, error) {
- addrs, err := d.resolver.LookupHost(d.ctx, d.host)
- if err != nil {
- err = handleDNSError(err, "A")
- return nil, err
- }
- newAddrs := make([]resolver.Address, 0, len(addrs))
- for _, a := range addrs {
- ip, ok := formatIP(a)
- if !ok {
- return nil, fmt.Errorf("dns: error parsing A record IP address %v", a)
- }
- addr := ip + ":" + d.port
- newAddrs = append(newAddrs, resolver.Address{Addr: addr})
- }
- return newAddrs, nil
- }
- func (d *dnsResolver) lookup() (*resolver.State, error) {
- srv, srvErr := d.lookupSRV()
- addrs, hostErr := d.lookupHost()
- if hostErr != nil && (srvErr != nil || len(srv) == 0) {
- return nil, hostErr
- }
- state := resolver.State{Addresses: addrs}
- if len(srv) > 0 {
- state = grpclbstate.Set(state, &grpclbstate.State{BalancerAddresses: srv})
- }
- if !d.disableServiceConfig {
- state.ServiceConfig = d.lookupTXT()
- }
- return &state, nil
- }
- // formatIP returns ok = false if addr is not a valid textual representation of
- // an IP address. If addr is an IPv4 address, return the addr and ok = true.
- // If addr is an IPv6 address, return the addr enclosed in square brackets and
- // ok = true.
- func formatIP(addr string) (addrIP string, ok bool) {
- ip := net.ParseIP(addr)
- if ip == nil {
- return "", false
- }
- if ip.To4() != nil {
- return addr, true
- }
- return "[" + addr + "]", true
- }
- // parseTarget takes the user input target string and default port, returns
- // formatted host and port info. If target doesn't specify a port, set the port
- // to be the defaultPort. If target is in IPv6 format and host-name is enclosed
- // in square brackets, brackets are stripped when setting the host.
- // examples:
- // target: "www.google.com" defaultPort: "443" returns host: "www.google.com", port: "443"
- // target: "ipv4-host:80" defaultPort: "443" returns host: "ipv4-host", port: "80"
- // target: "[ipv6-host]" defaultPort: "443" returns host: "ipv6-host", port: "443"
- // target: ":80" defaultPort: "443" returns host: "localhost", port: "80"
- func parseTarget(target, defaultPort string) (host, port string, err error) {
- if target == "" {
- return "", "", internal.ErrMissingAddr
- }
- if ip := net.ParseIP(target); ip != nil {
- // target is an IPv4 or IPv6(without brackets) address
- return target, defaultPort, nil
- }
- if host, port, err = net.SplitHostPort(target); err == nil {
- if port == "" {
- // If the port field is empty (target ends with colon), e.g. "[::1]:",
- // this is an error.
- return "", "", internal.ErrEndsWithColon
- }
- // target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port
- if host == "" {
- // Keep consistent with net.Dial(): If the host is empty, as in ":80",
- // the local system is assumed.
- host = "localhost"
- }
- return host, port, nil
- }
- if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil {
- // target doesn't have port
- return host, port, nil
- }
- return "", "", fmt.Errorf("invalid target address %v, error info: %v", target, err)
- }
- type rawChoice struct {
- ClientLanguage *[]string `json:"clientLanguage,omitempty"`
- Percentage *int `json:"percentage,omitempty"`
- ClientHostName *[]string `json:"clientHostName,omitempty"`
- ServiceConfig *json.RawMessage `json:"serviceConfig,omitempty"`
- }
- func containsString(a *[]string, b string) bool {
- if a == nil {
- return true
- }
- for _, c := range *a {
- if c == b {
- return true
- }
- }
- return false
- }
- func chosenByPercentage(a *int) bool {
- if a == nil {
- return true
- }
- return grpcrand.Intn(100)+1 <= *a
- }
- func canaryingSC(js string) string {
- if js == "" {
- return ""
- }
- var rcs []rawChoice
- err := json.Unmarshal([]byte(js), &rcs)
- if err != nil {
- logger.Warningf("dns: error parsing service config json: %v", err)
- return ""
- }
- cliHostname, err := os.Hostname()
- if err != nil {
- logger.Warningf("dns: error getting client hostname: %v", err)
- return ""
- }
- var sc string
- for _, c := range rcs {
- if !containsString(c.ClientLanguage, golang) ||
- !chosenByPercentage(c.Percentage) ||
- !containsString(c.ClientHostName, cliHostname) ||
- c.ServiceConfig == nil {
- continue
- }
- sc = string(*c.ServiceConfig)
- break
- }
- return sc
- }
|