forked from juanfont/headscale
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move reusable method to common api file
Showing
2 changed files
with
80 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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") | ||
node, err := machine.toNode(h.cfg.BaseDomain, h.cfg.DNSConfig, true) | ||
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 | ||
} | ||
|
||
profiles := getMapResponseUserProfiles(*machine, peers) | ||
|
||
nodePeers, err := peers.toNodes(h.cfg.BaseDomain, h.cfg.DNSConfig, true) | ||
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, | ||
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 | ||
} |