2020-06-21 12:32:08 +02:00
|
|
|
// Codehere is mostly taken from github.com/tailscale/tailscale
|
|
|
|
// Copyright (c) 2020 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 headscale
|
|
|
|
|
|
|
|
import (
|
2021-10-30 14:29:03 +00:00
|
|
|
"context"
|
2020-06-21 12:32:08 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-10-30 14:29:03 +00:00
|
|
|
"net"
|
2021-08-13 10:33:19 +01:00
|
|
|
"strings"
|
2020-06-21 12:32:08 +02:00
|
|
|
|
2021-11-26 23:30:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-08-02 20:06:26 +01:00
|
|
|
"inet.af/netaddr"
|
2021-08-13 10:33:19 +01:00
|
|
|
"tailscale.com/tailcfg"
|
2021-11-26 23:30:42 +00:00
|
|
|
"tailscale.com/types/key"
|
2020-06-21 12:32:08 +02:00
|
|
|
)
|
|
|
|
|
2021-11-15 19:18:14 +00:00
|
|
|
const (
|
|
|
|
errCannotDecryptReponse = Error("cannot decrypt response")
|
|
|
|
errCouldNotAllocateIP = Error("could not find any suitable IP")
|
2021-11-26 23:30:42 +00:00
|
|
|
|
|
|
|
// These constants are copied from the upstream tailscale.com/types/key
|
|
|
|
// library, because they are not exported.
|
|
|
|
// https://github.com/tailscale/tailscale/tree/main/types/key
|
|
|
|
|
|
|
|
// nodePublicHexPrefix is the prefix used to identify a
|
|
|
|
// hex-encoded node public key.
|
|
|
|
//
|
|
|
|
// This prefix is used in the control protocol, so cannot be
|
|
|
|
// changed.
|
|
|
|
nodePublicHexPrefix = "nodekey:"
|
|
|
|
|
|
|
|
// machinePublicHexPrefix is the prefix used to identify a
|
|
|
|
// hex-encoded machine public key.
|
|
|
|
//
|
|
|
|
// This prefix is used in the control protocol, so cannot be
|
|
|
|
// changed.
|
|
|
|
machinePublicHexPrefix = "mkey:"
|
|
|
|
|
|
|
|
// discoPublicHexPrefix is the prefix used to identify a
|
|
|
|
// hex-encoded disco public key.
|
|
|
|
//
|
|
|
|
// This prefix is used in the control protocol, so cannot be
|
|
|
|
// changed.
|
|
|
|
discoPublicHexPrefix = "discokey:"
|
2021-11-28 09:17:18 +00:00
|
|
|
|
|
|
|
// privateKey prefix.
|
|
|
|
privateHexPrefix = "privkey:"
|
2021-11-15 19:18:14 +00:00
|
|
|
)
|
|
|
|
|
2021-11-26 23:30:42 +00:00
|
|
|
func MachinePublicKeyStripPrefix(machineKey key.MachinePublic) string {
|
|
|
|
return strings.TrimPrefix(machineKey.String(), machinePublicHexPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NodePublicKeyStripPrefix(nodeKey key.NodePublic) string {
|
|
|
|
return strings.TrimPrefix(nodeKey.String(), nodePublicHexPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiscoPublicKeyStripPrefix(discoKey key.DiscoPublic) string {
|
|
|
|
return strings.TrimPrefix(discoKey.String(), discoPublicHexPrefix)
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:25:12 +00:00
|
|
|
func MachinePublicKeyEnsurePrefix(machineKey string) string {
|
|
|
|
if !strings.HasPrefix(machineKey, machinePublicHexPrefix) {
|
|
|
|
return machinePublicHexPrefix + machineKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return machineKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func NodePublicKeyEnsurePrefix(nodeKey string) string {
|
|
|
|
if !strings.HasPrefix(nodeKey, nodePublicHexPrefix) {
|
|
|
|
return nodePublicHexPrefix + nodeKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiscoPublicKeyEnsurePrefix(discoKey string) string {
|
|
|
|
if !strings.HasPrefix(discoKey, discoPublicHexPrefix) {
|
|
|
|
return discoPublicHexPrefix + discoKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return discoKey
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:17:18 +00:00
|
|
|
func PrivateKeyEnsurePrefix(privateKey string) string {
|
|
|
|
if !strings.HasPrefix(privateKey, privateHexPrefix) {
|
|
|
|
return privateHexPrefix + privateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return privateKey
|
|
|
|
}
|
|
|
|
|
2021-05-06 01:01:45 +02:00
|
|
|
// Error is used to compare errors as per https://dave.cheney.net/2016/04/07/constant-errors
|
2021-05-05 23:00:04 +02:00
|
|
|
type Error string
|
|
|
|
|
|
|
|
func (e Error) Error() string { return string(e) }
|
|
|
|
|
2021-11-13 08:36:45 +00:00
|
|
|
func decode(
|
|
|
|
msg []byte,
|
2021-11-14 20:32:03 +01:00
|
|
|
output interface{},
|
2021-11-26 23:30:42 +00:00
|
|
|
pubKey *key.MachinePublic,
|
|
|
|
privKey *key.MachinePrivate,
|
2021-11-13 08:36:45 +00:00
|
|
|
) error {
|
2021-11-26 23:30:42 +00:00
|
|
|
log.Trace().Int("length", len(msg)).Msg("Trying to decrypt")
|
|
|
|
|
|
|
|
decrypted, ok := privKey.OpenFrom(*pubKey, msg)
|
|
|
|
if !ok {
|
|
|
|
return errCannotDecryptReponse
|
2020-06-21 12:32:08 +02:00
|
|
|
}
|
2021-11-26 23:30:42 +00:00
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
if err := json.Unmarshal(decrypted, output); err != nil {
|
2021-11-15 19:18:14 +00:00
|
|
|
return err
|
2020-06-21 12:32:08 +02:00
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2020-06-21 12:32:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:30:42 +00:00
|
|
|
func encode(
|
|
|
|
v interface{},
|
|
|
|
pubKey *key.MachinePublic,
|
|
|
|
privKey *key.MachinePrivate,
|
|
|
|
) ([]byte, error) {
|
2020-06-21 12:32:08 +02:00
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-11-26 23:30:42 +00:00
|
|
|
return privKey.SealTo(*pubKey, b), nil
|
2020-06-21 12:32:08 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 14:16:59 +01:00
|
|
|
func (h *Headscale) getAvailableIPs() (ips MachineAddresses, err error) {
|
|
|
|
ipPrefixes := h.cfg.IPPrefixes
|
|
|
|
for _, ipPrefix := range ipPrefixes {
|
|
|
|
var ip *netaddr.IP
|
|
|
|
ip, err = h.getAvailableIP(ipPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ips = append(ips, *ip)
|
|
|
|
}
|
2021-08-02 21:57:45 +01:00
|
|
|
|
2022-01-16 14:16:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:26:28 +01:00
|
|
|
func GetIPPrefixEndpoints(na netaddr.IPPrefix) (network, broadcast netaddr.IP) {
|
|
|
|
ipRange := na.Range()
|
|
|
|
network = ipRange.From()
|
|
|
|
broadcast = ipRange.To()
|
2022-01-30 08:35:10 +00:00
|
|
|
|
2022-01-29 15:26:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:16:59 +01:00
|
|
|
// TODO: Is this concurrency safe?
|
|
|
|
// What would happen if multiple hosts were to register at the same time?
|
|
|
|
// Would we attempt to assign the same addresses to multiple nodes?
|
|
|
|
func (h *Headscale) getAvailableIP(ipPrefix netaddr.IPPrefix) (*netaddr.IP, error) {
|
2021-08-02 21:57:45 +01:00
|
|
|
usedIps, err := h.getUsedIPs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:26:28 +01:00
|
|
|
ipPrefixNetworkAddress, ipPrefixBroadcastAddress := GetIPPrefixEndpoints(ipPrefix)
|
2021-10-31 16:05:08 +01:00
|
|
|
|
2021-08-02 21:57:45 +01:00
|
|
|
// Get the first IP in our prefix
|
2021-10-31 16:05:08 +01:00
|
|
|
ip := ipPrefixNetworkAddress.Next()
|
2021-08-02 21:57:45 +01:00
|
|
|
|
2020-06-21 12:32:08 +02:00
|
|
|
for {
|
2021-08-02 21:57:45 +01:00
|
|
|
if !ipPrefix.Contains(ip) {
|
2021-11-15 19:18:14 +00:00
|
|
|
return nil, errCouldNotAllocateIP
|
2020-06-21 12:32:08 +02:00
|
|
|
}
|
2021-08-02 21:57:45 +01:00
|
|
|
|
2021-10-31 16:05:08 +01:00
|
|
|
switch {
|
|
|
|
case ip.Compare(ipPrefixBroadcastAddress) == 0:
|
|
|
|
fallthrough
|
|
|
|
case containsIPs(usedIps, ip):
|
|
|
|
fallthrough
|
|
|
|
case ip.IsZero() || ip.IsLoopback():
|
2021-08-03 10:06:42 +01:00
|
|
|
ip = ip.Next()
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-08-02 21:57:45 +01:00
|
|
|
continue
|
|
|
|
|
2021-10-31 16:05:08 +01:00
|
|
|
default:
|
2021-08-02 21:57:45 +01:00
|
|
|
return &ip, nil
|
2020-06-21 12:32:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:57:45 +01:00
|
|
|
func (h *Headscale) getUsedIPs() ([]netaddr.IP, error) {
|
2022-01-16 14:16:59 +01:00
|
|
|
// FIXME: This really deserves a better data model,
|
|
|
|
// but this was quick to get running and it should be enough
|
|
|
|
// to begin experimenting with a dual stack tailnet.
|
|
|
|
var addressesSlices []string
|
|
|
|
h.db.Model(&Machine{}).Pluck("ip_addresses", &addressesSlices)
|
|
|
|
|
|
|
|
ips := make([]netaddr.IP, 0, len(h.cfg.IPPrefixes)*len(addressesSlices))
|
|
|
|
for _, slice := range addressesSlices {
|
|
|
|
var a MachineAddresses
|
|
|
|
err := a.Scan(slice)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read ip from database: %w", err)
|
2021-08-03 07:42:11 +01:00
|
|
|
}
|
2022-01-16 14:16:59 +01:00
|
|
|
ips = append(ips, a...)
|
2021-08-02 21:57:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsIPs(ips []netaddr.IP, ip netaddr.IP) bool {
|
|
|
|
for _, v := range ips {
|
|
|
|
if v == ip {
|
|
|
|
return true
|
2020-06-21 12:32:08 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-21 22:11:27 +01:00
|
|
|
|
2021-08-02 21:57:45 +01:00
|
|
|
return false
|
2020-06-21 12:32:08 +02:00
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
|
|
|
|
func tailNodesToString(nodes []*tailcfg.Node) string {
|
|
|
|
temp := make([]string, len(nodes))
|
|
|
|
|
|
|
|
for index, node := range nodes {
|
|
|
|
temp[index] = node.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("[ %s ](%d)", strings.Join(temp, ", "), len(temp))
|
|
|
|
}
|
|
|
|
|
|
|
|
func tailMapResponseToString(resp tailcfg.MapResponse) string {
|
2021-11-13 08:36:45 +00:00
|
|
|
return fmt.Sprintf(
|
|
|
|
"{ Node: %s, Peers: %s }",
|
|
|
|
resp.Node.Name,
|
|
|
|
tailNodesToString(resp.Peers),
|
|
|
|
)
|
2021-08-13 10:33:19 +01:00
|
|
|
}
|
2021-10-30 14:29:03 +00:00
|
|
|
|
|
|
|
func GrpcSocketDialer(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
var d net.Dialer
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-30 14:29:03 +00:00
|
|
|
return d.DialContext(ctx, "unix", addr)
|
|
|
|
}
|
2021-11-04 22:17:44 +00:00
|
|
|
|
|
|
|
func ipPrefixToString(prefixes []netaddr.IPPrefix) []string {
|
|
|
|
result := make([]string, len(prefixes))
|
|
|
|
|
|
|
|
for index, prefix := range prefixes {
|
|
|
|
result[index] = prefix.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:24:24 +00:00
|
|
|
func stringToIPPrefix(prefixes []string) ([]netaddr.IPPrefix, error) {
|
2021-11-04 22:17:44 +00:00
|
|
|
result := make([]netaddr.IPPrefix, len(prefixes))
|
|
|
|
|
|
|
|
for index, prefixStr := range prefixes {
|
|
|
|
prefix, err := netaddr.ParseIPPrefix(prefixStr)
|
|
|
|
if err != nil {
|
|
|
|
return []netaddr.IPPrefix{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result[index] = prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:24:24 +00:00
|
|
|
func containsIPPrefix(prefixes []netaddr.IPPrefix, prefix netaddr.IPPrefix) bool {
|
2021-11-04 22:17:44 +00:00
|
|
|
for _, p := range prefixes {
|
|
|
|
if prefix == p {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|