2023-05-10 03:24:05 -04:00
|
|
|
package hscontrol
|
2022-08-14 10:13:17 -04:00
|
|
|
|
|
|
|
import (
|
2023-02-02 03:06:24 -05:00
|
|
|
"time"
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-08-14 10:13:17 -04:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *Headscale) generateMapResponse(
|
|
|
|
mapRequest tailcfg.MapRequest,
|
|
|
|
machine *Machine,
|
|
|
|
) (*tailcfg.MapResponse, error) {
|
|
|
|
log.Trace().
|
|
|
|
Str("func", "generateMapResponse").
|
|
|
|
Str("machine", mapRequest.Hostinfo.Hostname).
|
|
|
|
Msg("Creating Map response")
|
2023-05-11 03:09:18 -04:00
|
|
|
node, err := h.db.toNode(*machine, h.aclPolicy, h.cfg.BaseDomain, h.cfg.DNSConfig)
|
2022-08-14 10:13:17 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Str("func", "generateMapResponse").
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot convert to node")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
peers, err := h.db.getValidPeers(h.aclPolicy, h.aclRules, machine)
|
2022-08-14 10:13:17 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Str("func", "generateMapResponse").
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot fetch peers")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
profiles := h.db.getMapResponseUserProfiles(*machine, peers)
|
2022-08-14 10:13:17 -04:00
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
nodePeers, err := h.db.toNodes(peers, h.aclPolicy, h.cfg.BaseDomain, h.cfg.DNSConfig)
|
2022-08-14 10:13:17 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Str("func", "generateMapResponse").
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to convert peers to Tailscale nodes")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsConfig := getMapResponseDNSConfig(
|
|
|
|
h.cfg.DNSConfig,
|
|
|
|
h.cfg.BaseDomain,
|
|
|
|
*machine,
|
|
|
|
peers,
|
|
|
|
)
|
|
|
|
|
2023-02-02 03:06:24 -05:00
|
|
|
now := time.Now()
|
|
|
|
|
2022-08-14 10:13:17 -04:00
|
|
|
resp := tailcfg.MapResponse{
|
2023-02-02 03:18:33 -05:00
|
|
|
KeepAlive: false,
|
|
|
|
Node: node,
|
|
|
|
|
|
|
|
// TODO: Only send if updated
|
|
|
|
DERPMap: h.DERPMap,
|
|
|
|
|
|
|
|
// TODO: Only send if updated
|
|
|
|
Peers: nodePeers,
|
|
|
|
|
|
|
|
// TODO(kradalby): Implement:
|
|
|
|
// https://github.com/tailscale/tailscale/blob/main/tailcfg/tailcfg.go#L1351-L1374
|
|
|
|
// PeersChanged
|
|
|
|
// PeersRemoved
|
|
|
|
// PeersChangedPatch
|
|
|
|
// PeerSeenChange
|
|
|
|
// OnlineChange
|
|
|
|
|
|
|
|
// TODO: Only send if updated
|
|
|
|
DNSConfig: dnsConfig,
|
|
|
|
|
|
|
|
// TODO: Only send if updated
|
|
|
|
Domain: h.cfg.BaseDomain,
|
|
|
|
|
2023-02-02 03:18:50 -05:00
|
|
|
// Do not instruct clients to collect services, we do not
|
|
|
|
// support or do anything with them
|
|
|
|
CollectServices: "false",
|
|
|
|
|
2023-02-02 03:18:33 -05:00
|
|
|
// TODO: Only send if updated
|
2022-08-14 10:13:17 -04:00
|
|
|
PacketFilter: h.aclRules,
|
2023-02-02 03:18:33 -05:00
|
|
|
|
2022-08-14 10:13:17 -04:00
|
|
|
UserProfiles: profiles,
|
2023-02-02 03:18:33 -05:00
|
|
|
|
|
|
|
// TODO: Only send if updated
|
|
|
|
SSHPolicy: h.sshPolicy,
|
|
|
|
|
|
|
|
ControlTime: &now,
|
|
|
|
|
2022-08-14 10:13:17 -04:00
|
|
|
Debug: &tailcfg.Debug{
|
|
|
|
DisableLogTail: !h.cfg.LogTail.Enabled,
|
|
|
|
RandomizeClientPort: h.cfg.RandomizeClientPort,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("func", "generateMapResponse").
|
|
|
|
Str("machine", mapRequest.Hostinfo.Hostname).
|
|
|
|
// Interface("payload", resp).
|
2023-05-11 03:09:18 -04:00
|
|
|
Msgf("Generated map response: %s", util.TailMapResponseToString(resp))
|
2022-08-14 10:13:17 -04:00
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|