additional debug logging, use mapper pointer

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2023-07-24 08:58:51 +02:00 committed by Kristoffer Dalby
parent eff529f2c5
commit e0ba325b3b
9 changed files with 69 additions and 16 deletions

View file

@ -49,9 +49,14 @@ type Mapper struct {
dnsCfg *tailcfg.DNSConfig
logtail bool
randomClientPort bool
uid string
created time.Time
seq uint64
}
func NewMapper(
machine *types.Machine,
db *db.HSDatabase,
privateKey *key.MachinePrivate,
isNoise bool,
@ -61,6 +66,14 @@ func NewMapper(
logtail bool,
randomClientPort bool,
) *Mapper {
log.Debug().
Caller().
Bool("noise", isNoise).
Str("machine", machine.Hostname).
Msg("creating new mapper")
uid, _ := util.GenerateRandomStringDNSSafe(8)
return &Mapper{
db: db,
@ -72,9 +85,17 @@ func NewMapper(
dnsCfg: dnsCfg,
logtail: logtail,
randomClientPort: randomClientPort,
uid: uid,
created: time.Now(),
seq: 0,
}
}
func (m *Mapper) String() string {
return fmt.Sprintf("Mapper: { seq: %d, uid: %s, created: %s }", m.seq, m.uid, m.created)
}
// TODO: Optimise
// As this work continues, the idea is that there will be one Mapper instance
// per node, attached to the open stream between the control and client.
@ -270,7 +291,7 @@ func addNextDNSMetadata(resolvers []*dnstype.Resolver, machine types.Machine) {
}
// FullMapResponse returns a MapResponse for the given machine.
func (m Mapper) FullMapResponse(
func (m *Mapper) FullMapResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
pol *policy.ACLPolicy,
@ -306,7 +327,7 @@ func (m Mapper) FullMapResponse(
return m.marshalMapResponse(mapResponse, machine, mapRequest.Compress)
}
func (m Mapper) KeepAliveResponse(
func (m *Mapper) KeepAliveResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
) ([]byte, error) {
@ -316,7 +337,7 @@ func (m Mapper) KeepAliveResponse(
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}
func (m Mapper) DERPMapResponse(
func (m *Mapper) DERPMapResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
derpMap tailcfg.DERPMap,
@ -327,7 +348,7 @@ func (m Mapper) DERPMapResponse(
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}
func (m Mapper) PeerChangedResponse(
func (m *Mapper) PeerChangedResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
machineKeys []uint64,
@ -380,12 +401,12 @@ func (m Mapper) PeerChangedResponse(
resp := m.baseMapResponse(machine)
resp.PeersChanged = tailPeers
resp.PeerSeenChange = lastSeen
// resp.PeerSeenChange = lastSeen
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}
func (m Mapper) PeerRemovedResponse(
func (m *Mapper) PeerRemovedResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
removed []tailcfg.NodeID,
@ -396,11 +417,13 @@ func (m Mapper) PeerRemovedResponse(
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}
func (m Mapper) marshalMapResponse(
func (m *Mapper) marshalMapResponse(
resp *tailcfg.MapResponse,
machine *types.Machine,
compression string,
) ([]byte, error) {
atomic.AddUint64(&m.seq, 1)
var machineKey key.MachinePublic
err := machineKey.UnmarshalText([]byte(util.MachinePublicKeyEnsurePrefix(machine.MachineKey)))
if err != nil {
@ -445,11 +468,11 @@ func (m Mapper) marshalMapResponse(
mapResponsePath := path.Join(
mPath,
fmt.Sprintf("%d-%s-%d.json", now, m.uid, atomic.LoadUint64(&m.seq)),
fmt.Sprintf("%d-%s-%d.json", atomic.LoadUint64(&m.seq), m.uid, now),
)
log.Trace().Msgf("Writing MapResponse to %s", mapResponsePath)
err = os.WriteFile(mapResponsePath, body, perms)
err = os.WriteFile(mapResponsePath, jsonBody, perms)
if err != nil {
panic(err)
}

View file

@ -5,6 +5,7 @@ import (
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log"
)
type Notifier struct {
@ -25,6 +26,11 @@ func (n *Notifier) AddNode(machineKey string, c chan<- types.StateUpdate) {
}
n.nodes[machineKey] = c
log.Trace().
Str("machine_key", machineKey).
Int("open_chans", len(n.nodes)).
Msg("Added new channel")
}
func (n *Notifier) RemoveNode(machineKey string) {
@ -36,6 +42,11 @@ func (n *Notifier) RemoveNode(machineKey string) {
}
delete(n.nodes, machineKey)
log.Trace().
Str("machine_key", machineKey).
Int("open_chans", len(n.nodes)).
Msg("Removed channel")
}
func (n *Notifier) NotifyAll(update types.StateUpdate) {

View file

@ -65,6 +65,7 @@ func (h *Headscale) handlePoll(
logInfo, logErr := logPollFunc(mapRequest, machine, isNoise)
mapp := mapper.NewMapper(
machine,
h.db,
h.privateKey2019,
isNoise,
@ -176,7 +177,6 @@ func (h *Headscale) handlePoll(
Bool("noise", isNoise).
Str("machine", machine.Hostname).
Msg("Ignoring request, don't know how to handle it")
http.Error(writer, "", http.StatusBadRequest)
return
}
@ -239,6 +239,7 @@ func (h *Headscale) pollNetMapStream(
defer cancel()
for {
logInfo("Waiting for update on stream channel")
select {
case <-keepAliveTicker.C:
data, err := mapp.KeepAliveResponse(mapRequest, machine)
@ -256,6 +257,8 @@ func (h *Headscale) pollNetMapStream(
if flusher, ok := writer.(http.Flusher); ok {
flusher.Flush()
} else {
log.Error().Msg("Failed to create http flusher")
return
}
@ -267,6 +270,8 @@ func (h *Headscale) pollNetMapStream(
}
case update := <-updateChan:
logInfo("Received update")
var data []byte
var err error
@ -304,6 +309,8 @@ func (h *Headscale) pollNetMapStream(
if flusher, ok := writer.(http.Flusher); ok {
flusher.Flush()
} else {
log.Error().Msg("Failed to create http flusher")
return
}
@ -318,6 +325,7 @@ func (h *Headscale) pollNetMapStream(
return
}
logInfo("Update sent")
case <-ctx.Done():
logInfo("The client has closed the connection")
@ -335,6 +343,8 @@ func (h *Headscale) pollNetMapStream(
return
}
}
logInfo("Finishing map stream session")
}
func closeChanWithLog[C chan []byte | chan struct{} | chan types.StateUpdate](channel C, machine, name string) {

View file

@ -91,7 +91,7 @@ func (h *Headscale) PollNetMapHandler(
Str("handler", "PollNetMap").
Str("id", machineKeyStr).
Str("machine", machine.Hostname).
Msg("A machine is entering polling via the legacy protocol")
Msg("A machine is sending a MapRequest via legacy protocol")
h.handlePoll(writer, req.Context(), machine, mapRequest, false)
}

View file

@ -72,7 +72,7 @@ func (ns *noiseServer) NoisePollNetMapHandler(
log.Debug().
Str("handler", "NoisePollNetMap").
Str("machine", machine.Hostname).
Msg("A machine is entering polling via the Noise protocol")
Msg("A machine sending a MapRequest with Noise protocol")
ns.headscale.handlePoll(writer, req.Context(), machine, mapRequest, true)
}