2021-11-30 23:53:34 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tsdial
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-02 19:10:35 +00:00
|
|
|
"errors"
|
2021-11-30 23:53:34 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2021-11-30 23:53:34 +00:00
|
|
|
"tailscale.com/types/netmap"
|
|
|
|
"tailscale.com/util/dnsname"
|
|
|
|
)
|
|
|
|
|
2021-12-02 19:10:35 +00:00
|
|
|
// dnsMap maps MagicDNS names (both base + FQDN) to their first IP.
|
2021-11-30 23:53:34 +00:00
|
|
|
// It must not be mutated once created.
|
|
|
|
//
|
|
|
|
// Example keys are "foo.domain.tld.beta.tailscale.net" and "foo",
|
2022-03-25 21:27:22 +00:00
|
|
|
// both without trailing dots, and both always lowercase.
|
2021-12-02 19:10:35 +00:00
|
|
|
type dnsMap map[string]netaddr.IP
|
2021-11-30 23:53:34 +00:00
|
|
|
|
2022-03-25 21:27:22 +00:00
|
|
|
// canonMapKey canonicalizes its input s to be a dnsMap map key.
|
|
|
|
func canonMapKey(s string) string {
|
|
|
|
return strings.ToLower(strings.TrimSuffix(s, "."))
|
|
|
|
}
|
|
|
|
|
2021-12-02 19:10:35 +00:00
|
|
|
func dnsMapFromNetworkMap(nm *netmap.NetworkMap) dnsMap {
|
|
|
|
if nm == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make(dnsMap)
|
2021-11-30 23:53:34 +00:00
|
|
|
suffix := nm.MagicDNSSuffix()
|
|
|
|
have4 := false
|
|
|
|
if nm.Name != "" && len(nm.Addresses) > 0 {
|
2022-07-25 03:08:42 +00:00
|
|
|
ip := nm.Addresses[0].Addr()
|
2022-03-25 21:27:22 +00:00
|
|
|
ret[canonMapKey(nm.Name)] = ip
|
2021-11-30 23:53:34 +00:00
|
|
|
if dnsname.HasSuffix(nm.Name, suffix) {
|
2022-03-25 21:27:22 +00:00
|
|
|
ret[canonMapKey(dnsname.TrimSuffix(nm.Name, suffix))] = ip
|
2021-11-30 23:53:34 +00:00
|
|
|
}
|
|
|
|
for _, a := range nm.Addresses {
|
2022-07-25 03:08:42 +00:00
|
|
|
if a.Addr().Is4() {
|
2021-11-30 23:53:34 +00:00
|
|
|
have4 = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, p := range nm.Peers {
|
|
|
|
if p.Name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, a := range p.Addresses {
|
2022-07-25 03:08:42 +00:00
|
|
|
ip := a.Addr()
|
2021-11-30 23:53:34 +00:00
|
|
|
if ip.Is4() && !have4 {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-25 21:27:22 +00:00
|
|
|
ret[canonMapKey(p.Name)] = ip
|
2021-11-30 23:53:34 +00:00
|
|
|
if dnsname.HasSuffix(p.Name, suffix) {
|
2022-03-25 21:27:22 +00:00
|
|
|
ret[canonMapKey(dnsname.TrimSuffix(p.Name, suffix))] = ip
|
2021-11-30 23:53:34 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, rec := range nm.DNS.ExtraRecords {
|
|
|
|
if rec.Type != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip, err := netaddr.ParseIP(rec.Value)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-25 21:27:22 +00:00
|
|
|
ret[canonMapKey(rec.Name)] = ip
|
2021-11-30 23:53:34 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-12-02 19:10:35 +00:00
|
|
|
// errUnresolved is a sentinel error returned by dnsMap.resolveMemory.
|
|
|
|
var errUnresolved = errors.New("address well formed but not resolved")
|
|
|
|
|
|
|
|
func splitHostPort(addr string) (host string, port uint16, err error) {
|
|
|
|
host, portStr, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
port16, err := strconv.ParseUint(portStr, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, fmt.Errorf("invalid port in address %q", addr)
|
|
|
|
}
|
|
|
|
return host, uint16(port16), nil
|
|
|
|
}
|
|
|
|
|
2021-11-30 23:53:34 +00:00
|
|
|
// Resolve resolves addr into an IP:port using first the MagicDNS contents
|
|
|
|
// of m, else using the system resolver.
|
2021-12-02 19:10:35 +00:00
|
|
|
//
|
|
|
|
// The error is [exactly] errUnresolved if the addr is a name that isn't known
|
|
|
|
// in the map.
|
|
|
|
func (m dnsMap) resolveMemory(ctx context.Context, network, addr string) (_ netaddr.IPPort, err error) {
|
|
|
|
host, port, err := splitHostPort(addr)
|
2021-11-30 23:53:34 +00:00
|
|
|
if err != nil {
|
2021-12-02 19:10:35 +00:00
|
|
|
// addr malformed or invalid port.
|
2021-11-30 23:53:34 +00:00
|
|
|
return netaddr.IPPort{}, err
|
|
|
|
}
|
2021-12-02 19:10:35 +00:00
|
|
|
if ip, err := netaddr.ParseIP(host); err == nil {
|
|
|
|
// addr was literal ip:port.
|
|
|
|
return netaddr.IPPortFrom(ip, port), nil
|
2021-11-30 23:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Host is not an IP, so assume it's a DNS name.
|
|
|
|
|
|
|
|
// Try MagicDNS first, otherwise a real DNS lookup.
|
2022-03-25 21:27:22 +00:00
|
|
|
ip := m[canonMapKey(host)]
|
2022-07-25 03:08:42 +00:00
|
|
|
if ip.IsValid() {
|
2021-12-02 19:10:35 +00:00
|
|
|
return netaddr.IPPortFrom(ip, port), nil
|
2021-11-30 23:53:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 19:10:35 +00:00
|
|
|
return netaddr.IPPort{}, errUnresolved
|
2021-11-30 23:53:34 +00:00
|
|
|
}
|