2022-08-14 16:13:17 +02:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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")
|
2022-11-24 16:00:40 +00:00
|
|
|
node, err := h.toNode(*machine, h.cfg.BaseDomain, h.cfg.DNSConfig)
|
2022-08-14 16:13:17 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Str("func", "generateMapResponse").
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot convert to node")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peers, err := h.getValidPeers(machine)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Str("func", "generateMapResponse").
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot fetch peers")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-22 17:36:19 +01:00
|
|
|
profiles := h.getMapResponseUserProfiles(*machine, peers)
|
2022-08-14 16:13:17 +02:00
|
|
|
|
2022-11-24 16:00:40 +00:00
|
|
|
nodePeers, err := h.toNodes(peers, h.cfg.BaseDomain, h.cfg.DNSConfig)
|
2022-08-14 16:13:17 +02: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,
|
|
|
|
)
|
|
|
|
|
|
|
|
resp := tailcfg.MapResponse{
|
|
|
|
KeepAlive: false,
|
|
|
|
Node: node,
|
|
|
|
Peers: nodePeers,
|
|
|
|
DNSConfig: dnsConfig,
|
|
|
|
Domain: h.cfg.BaseDomain,
|
|
|
|
PacketFilter: h.aclRules,
|
2022-09-30 20:44:23 +02:00
|
|
|
SSHPolicy: h.sshPolicy,
|
2022-08-14 16:13:17 +02:00
|
|
|
DERPMap: h.DERPMap,
|
|
|
|
UserProfiles: profiles,
|
|
|
|
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).
|
|
|
|
Msgf("Generated map response: %s", tailMapResponseToString(resp))
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|