headscale/hscontrol/mapper/tail.go

175 lines
3.9 KiB
Go
Raw Normal View History

package mapper
import (
"fmt"
"net/netip"
"strconv"
"time"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/samber/lo"
"tailscale.com/tailcfg"
)
func tailNodes(
2023-09-24 07:42:05 -04:00
nodes types.Nodes,
capVer tailcfg.CapabilityVersion,
pol *policy.ACLPolicy,
dnsConfig *tailcfg.DNSConfig,
baseDomain string,
randomClientPort bool,
) ([]*tailcfg.Node, error) {
2023-09-24 07:42:05 -04:00
tNodes := make([]*tailcfg.Node, len(nodes))
2023-09-24 07:42:05 -04:00
for index, node := range nodes {
node, err := tailNode(
2023-09-24 07:42:05 -04:00
node,
capVer,
pol,
dnsConfig,
baseDomain,
randomClientPort,
)
if err != nil {
return nil, err
}
2023-09-24 07:42:05 -04:00
tNodes[index] = node
}
2023-09-24 07:42:05 -04:00
return tNodes, nil
}
2023-09-24 07:42:05 -04:00
// tailNode converts a Node into a Tailscale Node. includeRoutes is false for shared nodes
// as per the expected behaviour in the official SaaS.
func tailNode(
2023-09-24 07:42:05 -04:00
node *types.Node,
capVer tailcfg.CapabilityVersion,
pol *policy.ACLPolicy,
dnsConfig *tailcfg.DNSConfig,
baseDomain string,
randomClientPort bool,
) (*tailcfg.Node, error) {
2023-09-24 07:42:05 -04:00
nodeKey, err := node.NodePublicKey()
if err != nil {
return nil, err
}
2023-09-24 07:42:05 -04:00
machineKey, err := node.MachinePublicKey()
if err != nil {
return nil, err
}
2023-09-24 07:42:05 -04:00
discoKey, err := node.DiscoPublicKey()
if err != nil {
return nil, err
}
2023-09-24 07:42:05 -04:00
addrs := node.IPAddresses.Prefixes()
allowedIPs := append(
[]netip.Prefix{},
addrs...) // we append the node own IP, as it is required by the clients
primaryPrefixes := []netip.Prefix{}
2023-09-24 07:42:05 -04:00
for _, route := range node.Routes {
if route.Enabled {
if route.IsPrimary {
allowedIPs = append(allowedIPs, netip.Prefix(route.Prefix))
primaryPrefixes = append(primaryPrefixes, netip.Prefix(route.Prefix))
} else if route.IsExitRoute() {
allowedIPs = append(allowedIPs, netip.Prefix(route.Prefix))
}
}
}
var derp string
2023-09-24 07:42:05 -04:00
if node.HostInfo.NetInfo != nil {
derp = fmt.Sprintf("127.3.3.40:%d", node.HostInfo.NetInfo.PreferredDERP)
} else {
derp = "127.3.3.40:0" // Zero means disconnected or unknown.
}
var keyExpiry time.Time
2023-09-24 07:42:05 -04:00
if node.Expiry != nil {
keyExpiry = *node.Expiry
} else {
keyExpiry = time.Time{}
}
2023-09-24 07:42:05 -04:00
hostname, err := node.GetFQDN(dnsConfig, baseDomain)
if err != nil {
return nil, err
}
2023-09-24 07:42:05 -04:00
hostInfo := node.GetHostInfo()
2023-09-24 07:42:05 -04:00
online := node.IsOnline()
2023-09-24 07:42:05 -04:00
tags, _ := pol.TagsOfNode(node)
tags = lo.Uniq(append(tags, node.ForcedTags...))
2023-09-24 07:42:05 -04:00
tNode := tailcfg.Node{
ID: tailcfg.NodeID(node.ID), // this is the actual ID
StableID: tailcfg.StableNodeID(
2023-09-24 07:42:05 -04:00
strconv.FormatUint(node.ID, util.Base10),
), // in headscale, unlike tailcontrol server, IDs are permanent
Name: hostname,
2023-09-24 07:42:05 -04:00
User: tailcfg.UserID(node.UserID),
Key: nodeKey,
KeyExpiry: keyExpiry,
Machine: machineKey,
DiscoKey: discoKey,
Addresses: addrs,
AllowedIPs: allowedIPs,
2023-09-24 07:42:05 -04:00
Endpoints: node.Endpoints,
DERP: derp,
Hostinfo: hostInfo.View(),
2023-09-24 07:42:05 -04:00
Created: node.CreatedAt,
Tags: tags,
PrimaryRoutes: primaryPrefixes,
2023-09-24 07:42:05 -04:00
LastSeen: node.LastSeen,
Online: &online,
2023-09-24 07:42:05 -04:00
MachineAuthorized: !node.IsExpired(),
}
// - 74: 2023-09-18: Client understands NodeCapMap
if capVer >= 74 {
tNode.CapMap = tailcfg.NodeCapMap{
tailcfg.CapabilityFileSharing: []tailcfg.RawMessage{},
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
}
if randomClientPort {
tNode.CapMap[tailcfg.NodeAttrRandomizeClientPort] = []tailcfg.RawMessage{}
}
} else {
tNode.Capabilities = []tailcfg.NodeCapability{
tailcfg.CapabilityFileSharing,
tailcfg.CapabilityAdmin,
tailcfg.CapabilitySSH,
}
if randomClientPort {
tNode.Capabilities = append(tNode.Capabilities, tailcfg.NodeAttrRandomizeClientPort)
}
}
// - 72: 2023-08-23: TS-2023-006 UPnP issue fixed; UPnP can now be used again
if capVer < 72 {
tNode.Capabilities = append(tNode.Capabilities, tailcfg.NodeAttrDisableUPnP)
}
2023-09-24 07:42:05 -04:00
return &tNode, nil
}