2023-06-21 09:29:52 +00:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
2024-02-08 16:28:19 +00:00
|
|
|
"context"
|
2023-12-09 17:09:24 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-06-21 09:29:52 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-06-29 10:20:22 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-06-21 09:29:52 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2023-07-24 06:58:51 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-11-19 21:37:04 +00:00
|
|
|
"tailscale.com/types/key"
|
2023-06-21 09:29:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Notifier struct {
|
2024-02-08 16:28:19 +00:00
|
|
|
l sync.RWMutex
|
|
|
|
nodes map[string]chan<- types.StateUpdate
|
|
|
|
connected map[key.MachinePublic]bool
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNotifier() *Notifier {
|
2024-02-08 16:28:19 +00:00
|
|
|
return &Notifier{
|
|
|
|
nodes: make(map[string]chan<- types.StateUpdate),
|
|
|
|
connected: make(map[key.MachinePublic]bool),
|
|
|
|
}
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
func (n *Notifier) AddNode(machineKey key.MachinePublic, c chan<- types.StateUpdate) {
|
|
|
|
log.Trace().Caller().Str("key", machineKey.ShortString()).Msg("acquiring lock to add node")
|
2024-02-08 16:28:19 +00:00
|
|
|
defer log.Trace().
|
|
|
|
Caller().
|
|
|
|
Str("key", machineKey.ShortString()).
|
|
|
|
Msg("releasing lock to add node")
|
2023-09-11 11:08:44 +00:00
|
|
|
|
2023-06-21 09:29:52 +00:00
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
n.nodes[machineKey.String()] = c
|
2024-02-08 16:28:19 +00:00
|
|
|
n.connected[machineKey] = true
|
2023-07-24 06:58:51 +00:00
|
|
|
|
|
|
|
log.Trace().
|
2023-11-19 21:37:04 +00:00
|
|
|
Str("machine_key", machineKey.ShortString()).
|
2023-07-24 06:58:51 +00:00
|
|
|
Int("open_chans", len(n.nodes)).
|
|
|
|
Msg("Added new channel")
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
func (n *Notifier) RemoveNode(machineKey key.MachinePublic) {
|
|
|
|
log.Trace().Caller().Str("key", machineKey.ShortString()).Msg("acquiring lock to remove node")
|
2024-02-08 16:28:19 +00:00
|
|
|
defer log.Trace().
|
|
|
|
Caller().
|
|
|
|
Str("key", machineKey.ShortString()).
|
|
|
|
Msg("releasing lock to remove node")
|
2023-09-11 11:08:44 +00:00
|
|
|
|
2023-06-21 09:29:52 +00:00
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if len(n.nodes) == 0 {
|
2023-06-21 09:29:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
delete(n.nodes, machineKey.String())
|
2024-02-08 16:28:19 +00:00
|
|
|
n.connected[machineKey] = false
|
2023-07-24 06:58:51 +00:00
|
|
|
|
|
|
|
log.Trace().
|
2023-11-19 21:37:04 +00:00
|
|
|
Str("machine_key", machineKey.ShortString()).
|
2023-07-24 06:58:51 +00:00
|
|
|
Int("open_chans", len(n.nodes)).
|
|
|
|
Msg("Removed channel")
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
// IsConnected reports if a node is connected to headscale and has a
|
|
|
|
// poll session open.
|
|
|
|
func (n *Notifier) IsConnected(machineKey key.MachinePublic) bool {
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
return n.connected[machineKey]
|
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// TODO(kradalby): This returns a pointer and can be dangerous.
|
|
|
|
func (n *Notifier) ConnectedMap() map[key.MachinePublic]bool {
|
|
|
|
return n.connected
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (n *Notifier) NotifyAll(ctx context.Context, update types.StateUpdate) {
|
|
|
|
n.NotifyWithIgnore(ctx, update)
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (n *Notifier) NotifyWithIgnore(
|
|
|
|
ctx context.Context,
|
|
|
|
update types.StateUpdate,
|
|
|
|
ignore ...string,
|
|
|
|
) {
|
2023-09-11 11:08:44 +00:00
|
|
|
log.Trace().Caller().Interface("type", update.Type).Msg("acquiring lock to notify")
|
|
|
|
defer log.Trace().
|
|
|
|
Caller().
|
|
|
|
Interface("type", update.Type).
|
2024-02-08 16:28:19 +00:00
|
|
|
Msg("releasing lock, finished notifying")
|
2023-09-11 11:08:44 +00:00
|
|
|
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
2023-06-21 09:29:52 +00:00
|
|
|
|
|
|
|
for key, c := range n.nodes {
|
|
|
|
if util.IsStringInSlice(ignore, key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Error().
|
|
|
|
Err(ctx.Err()).
|
|
|
|
Str("mkey", key).
|
|
|
|
Any("origin", ctx.Value("origin")).
|
|
|
|
Any("hostname", ctx.Value("hostname")).
|
|
|
|
Msgf("update not sent, context cancelled")
|
|
|
|
|
|
|
|
return
|
|
|
|
case c <- update:
|
|
|
|
log.Trace().
|
|
|
|
Str("mkey", key).
|
|
|
|
Any("origin", ctx.Value("origin")).
|
|
|
|
Any("hostname", ctx.Value("hostname")).
|
|
|
|
Msgf("update successfully sent on chan")
|
|
|
|
}
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (n *Notifier) NotifyByMachineKey(
|
|
|
|
ctx context.Context,
|
|
|
|
update types.StateUpdate,
|
|
|
|
mKey key.MachinePublic,
|
|
|
|
) {
|
2024-01-05 09:41:56 +00:00
|
|
|
log.Trace().Caller().Interface("type", update.Type).Msg("acquiring lock to notify")
|
|
|
|
defer log.Trace().
|
|
|
|
Caller().
|
|
|
|
Interface("type", update.Type).
|
2024-02-08 16:28:19 +00:00
|
|
|
Msg("releasing lock, finished notifying")
|
2024-01-05 09:41:56 +00:00
|
|
|
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
|
|
|
|
|
|
|
if c, ok := n.nodes[mKey.String()]; ok {
|
2024-02-08 16:28:19 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Error().
|
|
|
|
Err(ctx.Err()).
|
|
|
|
Str("mkey", mKey.String()).
|
|
|
|
Any("origin", ctx.Value("origin")).
|
|
|
|
Any("hostname", ctx.Value("hostname")).
|
|
|
|
Msgf("update not sent, context cancelled")
|
|
|
|
|
|
|
|
return
|
|
|
|
case c <- update:
|
|
|
|
log.Trace().
|
|
|
|
Str("mkey", mKey.String()).
|
|
|
|
Any("origin", ctx.Value("origin")).
|
|
|
|
Any("hostname", ctx.Value("hostname")).
|
|
|
|
Msgf("update successfully sent on chan")
|
|
|
|
}
|
2024-01-05 09:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
func (n *Notifier) String() string {
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
|
|
|
|
|
|
|
str := []string{"Notifier, in map:\n"}
|
|
|
|
|
|
|
|
for k, v := range n.nodes {
|
|
|
|
str = append(str, fmt.Sprintf("\t%s: %v\n", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(str, "")
|
|
|
|
}
|