2023-05-31 07:59:37 +00:00
|
|
|
package mapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func tailNodes(
|
2023-09-24 11:42:05 +00:00
|
|
|
nodes types.Nodes,
|
2023-09-28 19:33:53 +00:00
|
|
|
capVer tailcfg.CapabilityVersion,
|
2024-11-26 14:16:06 +00:00
|
|
|
polMan policy.PolicyManager,
|
2024-02-23 09:59:24 +00:00
|
|
|
cfg *types.Config,
|
2023-05-31 07:59:37 +00:00
|
|
|
) ([]*tailcfg.Node, error) {
|
2023-09-24 11:42:05 +00:00
|
|
|
tNodes := make([]*tailcfg.Node, len(nodes))
|
2023-05-31 07:59:37 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
for index, node := range nodes {
|
2023-05-31 07:59:37 +00:00
|
|
|
node, err := tailNode(
|
2023-09-24 11:42:05 +00:00
|
|
|
node,
|
2023-09-28 19:33:53 +00:00
|
|
|
capVer,
|
2024-11-26 14:16:06 +00:00
|
|
|
polMan,
|
2024-02-23 09:59:24 +00:00
|
|
|
cfg,
|
2023-05-31 07:59:37 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
tNodes[index] = node
|
2023-05-31 07:59:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
return tNodes, nil
|
2023-05-31 07:59:37 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 09:41:05 +00:00
|
|
|
// tailNode converts a Node into a Tailscale Node.
|
2023-05-31 07:59:37 +00:00
|
|
|
func tailNode(
|
2023-09-24 11:42:05 +00:00
|
|
|
node *types.Node,
|
2023-09-28 19:33:53 +00:00
|
|
|
capVer tailcfg.CapabilityVersion,
|
2024-11-26 14:16:06 +00:00
|
|
|
polMan policy.PolicyManager,
|
2024-02-23 09:59:24 +00:00
|
|
|
cfg *types.Config,
|
2023-05-31 07:59:37 +00:00
|
|
|
) (*tailcfg.Node, error) {
|
2024-04-17 05:03:06 +00:00
|
|
|
addrs := node.Prefixes()
|
2023-05-31 07:59:37 +00:00
|
|
|
|
|
|
|
allowedIPs := append(
|
|
|
|
[]netip.Prefix{},
|
|
|
|
addrs...) // we append the node own IP, as it is required by the clients
|
|
|
|
|
|
|
|
primaryPrefixes := []netip.Prefix{}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
for _, route := range node.Routes {
|
2023-05-31 07:59:37 +00:00
|
|
|
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
|
2024-02-08 16:28:19 +00:00
|
|
|
if node.Hostinfo != nil && node.Hostinfo.NetInfo != nil {
|
2023-11-21 17:20:06 +00:00
|
|
|
derp = fmt.Sprintf("127.3.3.40:%d", node.Hostinfo.NetInfo.PreferredDERP)
|
2023-05-31 07:59:37 +00:00
|
|
|
} else {
|
|
|
|
derp = "127.3.3.40:0" // Zero means disconnected or unknown.
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyExpiry time.Time
|
2023-09-24 11:42:05 +00:00
|
|
|
if node.Expiry != nil {
|
|
|
|
keyExpiry = *node.Expiry
|
2023-05-31 07:59:37 +00:00
|
|
|
} else {
|
|
|
|
keyExpiry = time.Time{}
|
|
|
|
}
|
|
|
|
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 12:50:17 +00:00
|
|
|
hostname, err := node.GetFQDN(cfg.BaseDomain)
|
2023-05-31 07:59:37 +00:00
|
|
|
if err != nil {
|
2023-12-09 17:09:24 +00:00
|
|
|
return nil, fmt.Errorf("tailNode, failed to create FQDN: %s", err)
|
2023-05-31 07:59:37 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 14:16:06 +00:00
|
|
|
tags := polMan.Tags(node)
|
2023-09-24 11:42:05 +00:00
|
|
|
tags = lo.Uniq(append(tags, node.ForcedTags...))
|
2023-05-31 07:59:37 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
tNode := tailcfg.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: tailcfg.NodeID(node.ID), // this is the actual ID
|
|
|
|
StableID: node.ID.StableID(),
|
|
|
|
Name: hostname,
|
|
|
|
Cap: capVer,
|
2023-05-31 07:59:37 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
User: tailcfg.UserID(node.UserID),
|
2023-05-31 07:59:37 +00:00
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
Key: node.NodeKey,
|
2024-09-03 07:22:17 +00:00
|
|
|
KeyExpiry: keyExpiry.UTC(),
|
2023-05-31 07:59:37 +00:00
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
Machine: node.MachineKey,
|
|
|
|
DiscoKey: node.DiscoKey,
|
2023-05-31 07:59:37 +00:00
|
|
|
Addresses: addrs,
|
|
|
|
AllowedIPs: allowedIPs,
|
2023-11-21 17:20:06 +00:00
|
|
|
Endpoints: node.Endpoints,
|
2023-05-31 07:59:37 +00:00
|
|
|
DERP: derp,
|
2023-11-21 17:20:06 +00:00
|
|
|
Hostinfo: node.Hostinfo.View(),
|
2024-09-03 07:22:17 +00:00
|
|
|
Created: node.CreatedAt.UTC(),
|
2023-05-31 07:59:37 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
Online: node.IsOnline,
|
|
|
|
|
2023-05-31 07:59:37 +00:00
|
|
|
Tags: tags,
|
|
|
|
|
|
|
|
PrimaryRoutes: primaryPrefixes,
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
MachineAuthorized: !node.IsExpired(),
|
2023-12-09 17:09:24 +00:00
|
|
|
Expired: node.IsExpired(),
|
2023-09-28 19:33:53 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:34:20 +00:00
|
|
|
tNode.CapMap = tailcfg.NodeCapMap{
|
|
|
|
tailcfg.CapabilityFileSharing: []tailcfg.RawMessage{},
|
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
2023-09-28 19:33:53 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:34:20 +00:00
|
|
|
if cfg.RandomizeClientPort {
|
|
|
|
tNode.CapMap[tailcfg.NodeAttrRandomizeClientPort] = []tailcfg.RawMessage{}
|
2023-05-31 07:59:37 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
if node.IsOnline == nil || !*node.IsOnline {
|
|
|
|
// LastSeen is only set when node is
|
|
|
|
// not connected to the control server.
|
|
|
|
tNode.LastSeen = node.LastSeen
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
return &tNode, nil
|
2023-05-31 07:59:37 +00:00
|
|
|
}
|