2022-09-01 01:09:44 +00:00
|
|
|
package tailnet
|
|
|
|
|
|
|
|
import (
|
2022-10-17 23:36:23 +00:00
|
|
|
"context"
|
2022-09-01 01:09:44 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-01-25 21:27:36 +00:00
|
|
|
"fmt"
|
2023-07-26 22:54:21 +00:00
|
|
|
"html/template"
|
2022-09-01 01:09:44 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
2023-01-25 21:27:36 +00:00
|
|
|
"net/http"
|
2022-09-01 01:09:44 +00:00
|
|
|
"net/netip"
|
|
|
|
"sync"
|
2022-10-17 13:43:30 +00:00
|
|
|
"time"
|
2022-09-01 01:09:44 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-01-26 03:23:14 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2023-01-26 00:29:51 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2022-09-01 01:09:44 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/key"
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
"cdr.dev/slog"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/util/slice"
|
2022-09-01 01:09:44 +00:00
|
|
|
)
|
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
// Coordinator exchanges nodes with agents to establish connections.
|
|
|
|
// ┌──────────────────┐ ┌────────────────────┐ ┌───────────────────┐ ┌──────────────────┐
|
|
|
|
// │tailnet.Coordinate├──►│tailnet.AcceptClient│◄─►│tailnet.AcceptAgent│◄──┤tailnet.Coordinate│
|
|
|
|
// └──────────────────┘ └────────────────────┘ └───────────────────┘ └──────────────────┘
|
|
|
|
// Coordinators have different guarantees for HA support.
|
|
|
|
type Coordinator interface {
|
2023-01-25 21:27:36 +00:00
|
|
|
// ServeHTTPDebug serves a debug webpage that shows the internal state of
|
|
|
|
// the coordinator.
|
|
|
|
ServeHTTPDebug(w http.ResponseWriter, r *http.Request)
|
2022-10-17 13:43:30 +00:00
|
|
|
// Node returns an in-memory node by ID.
|
|
|
|
Node(id uuid.UUID) *Node
|
|
|
|
// ServeClient accepts a WebSocket connection that wants to connect to an agent
|
|
|
|
// with the specified ID.
|
|
|
|
ServeClient(conn net.Conn, id uuid.UUID, agent uuid.UUID) error
|
|
|
|
// ServeAgent accepts a WebSocket connection to an agent that listens to
|
|
|
|
// incoming connections and publishes node updates.
|
2023-01-25 21:27:36 +00:00
|
|
|
// Name is just used for debug information. It can be left blank.
|
|
|
|
ServeAgent(conn net.Conn, id uuid.UUID, name string) error
|
2022-10-17 13:43:30 +00:00
|
|
|
// Close closes the coordinator.
|
|
|
|
Close() error
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
ServeMultiAgent(id uuid.UUID) MultiAgentConn
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 01:09:44 +00:00
|
|
|
// Node represents a node in the network.
|
|
|
|
type Node struct {
|
2022-09-28 16:04:10 +00:00
|
|
|
// ID is used to identify the connection.
|
|
|
|
ID tailcfg.NodeID `json:"id"`
|
2022-10-17 13:43:30 +00:00
|
|
|
// AsOf is the time the node was created.
|
|
|
|
AsOf time.Time `json:"as_of"`
|
2022-09-28 16:04:10 +00:00
|
|
|
// Key is the Wireguard public key of the node.
|
|
|
|
Key key.NodePublic `json:"key"`
|
2023-07-12 22:37:31 +00:00
|
|
|
// DiscoKey is used for discovery messages over DERP to establish
|
|
|
|
// peer-to-peer connections.
|
2022-09-28 16:04:10 +00:00
|
|
|
DiscoKey key.DiscoPublic `json:"disco"`
|
2023-07-12 22:37:31 +00:00
|
|
|
// PreferredDERP is the DERP server that peered connections should meet at
|
|
|
|
// to establish.
|
2022-09-28 16:04:10 +00:00
|
|
|
PreferredDERP int `json:"preferred_derp"`
|
|
|
|
// DERPLatency is the latency in seconds to each DERP server.
|
|
|
|
DERPLatency map[string]float64 `json:"derp_latency"`
|
2023-03-01 22:18:14 +00:00
|
|
|
// DERPForcedWebsocket contains a mapping of DERP regions to
|
|
|
|
// error messages that caused the connection to be forced to
|
|
|
|
// use WebSockets. We don't use WebSockets by default because
|
|
|
|
// they are less performant.
|
|
|
|
DERPForcedWebsocket map[int]string `json:"derp_forced_websockets"`
|
2022-09-28 16:04:10 +00:00
|
|
|
// Addresses are the IP address ranges this connection exposes.
|
|
|
|
Addresses []netip.Prefix `json:"addresses"`
|
2023-07-12 22:37:31 +00:00
|
|
|
// AllowedIPs specify what addresses can dial the connection. We allow all
|
|
|
|
// by default.
|
2022-09-28 16:04:10 +00:00
|
|
|
AllowedIPs []netip.Prefix `json:"allowed_ips"`
|
|
|
|
// Endpoints are ip:port combinations that can be used to establish
|
|
|
|
// peer-to-peer connections.
|
|
|
|
Endpoints []string `json:"endpoints"`
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeCoordinator matches the RW structure of a coordinator to exchange node messages.
|
|
|
|
func ServeCoordinator(conn net.Conn, updateNodes func(node []*Node) error) (func(node *Node), <-chan error) {
|
2022-09-01 16:41:47 +00:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
sendErr := func(err error) {
|
|
|
|
select {
|
|
|
|
case errChan <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 01:09:44 +00:00
|
|
|
go func() {
|
|
|
|
decoder := json.NewDecoder(conn)
|
|
|
|
for {
|
|
|
|
var nodes []*Node
|
|
|
|
err := decoder.Decode(&nodes)
|
|
|
|
if err != nil {
|
2022-09-01 16:41:47 +00:00
|
|
|
sendErr(xerrors.Errorf("read: %w", err))
|
2022-09-01 01:09:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = updateNodes(nodes)
|
|
|
|
if err != nil {
|
2022-09-01 16:41:47 +00:00
|
|
|
sendErr(xerrors.Errorf("update nodes: %w", err))
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func(node *Node) {
|
|
|
|
data, err := json.Marshal(node)
|
|
|
|
if err != nil {
|
2022-09-01 16:41:47 +00:00
|
|
|
sendErr(xerrors.Errorf("marshal node: %w", err))
|
2022-09-01 01:09:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = conn.Write(data)
|
|
|
|
if err != nil {
|
2022-09-01 16:41:47 +00:00
|
|
|
sendErr(xerrors.Errorf("write: %w", err))
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
}, errChan
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
const LoggerName = "coord"
|
2023-04-27 09:59:01 +00:00
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
// NewCoordinator constructs a new in-memory connection coordinator. This
|
|
|
|
// coordinator is incompatible with multiple Coder replicas as all node data is
|
|
|
|
// in-memory.
|
2023-04-27 09:59:01 +00:00
|
|
|
func NewCoordinator(logger slog.Logger) Coordinator {
|
2022-10-17 13:43:30 +00:00
|
|
|
return &coordinator{
|
2023-05-02 16:58:21 +00:00
|
|
|
core: newCore(logger),
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
// coordinator exchanges nodes with agents to establish connections entirely in-memory.
|
|
|
|
// The Enterprise implementation provides this for high-availability.
|
2022-09-01 01:09:44 +00:00
|
|
|
// ┌──────────────────┐ ┌────────────────────┐ ┌───────────────────┐ ┌──────────────────┐
|
|
|
|
// │tailnet.Coordinate├──►│tailnet.AcceptClient│◄─►│tailnet.AcceptAgent│◄──┤tailnet.Coordinate│
|
|
|
|
// └──────────────────┘ └────────────────────┘ └───────────────────┘ └──────────────────┘
|
2023-07-12 22:37:31 +00:00
|
|
|
// This coordinator is incompatible with multiple Coder replicas as all node
|
|
|
|
// data is in-memory.
|
2022-10-17 13:43:30 +00:00
|
|
|
type coordinator struct {
|
2023-05-02 16:58:21 +00:00
|
|
|
core *core
|
|
|
|
}
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
func (c *coordinator) ServeMultiAgent(id uuid.UUID) MultiAgentConn {
|
|
|
|
m := (&MultiAgent{
|
|
|
|
ID: id,
|
|
|
|
AgentIsLegacyFunc: c.core.agentIsLegacy,
|
|
|
|
OnSubscribe: c.core.clientSubscribeToAgent,
|
|
|
|
OnUnsubscribe: c.core.clientUnsubscribeFromAgent,
|
|
|
|
OnNodeUpdate: c.core.clientNodeUpdate,
|
2023-09-21 19:30:48 +00:00
|
|
|
OnRemove: func(enq Queue) { c.core.clientDisconnected(enq.UniqueID()) },
|
2023-07-12 22:37:31 +00:00
|
|
|
}).Init()
|
|
|
|
c.core.addClient(id, m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) addClient(id uuid.UUID, ma Queue) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
c.clients[id] = ma
|
|
|
|
c.clientsToAgents[id] = map[uuid.UUID]Queue{}
|
|
|
|
c.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
// core is an in-memory structure of Node and TrackedConn mappings. Its methods may be called from multiple goroutines;
|
|
|
|
// it is protected by a mutex to ensure data stay consistent.
|
|
|
|
type core struct {
|
2023-04-27 09:59:01 +00:00
|
|
|
logger slog.Logger
|
2023-01-25 21:27:36 +00:00
|
|
|
mutex sync.RWMutex
|
2022-10-17 13:43:30 +00:00
|
|
|
closed bool
|
2022-09-01 01:09:44 +00:00
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
// nodes maps agent and connection IDs their respective node.
|
2022-09-01 01:09:44 +00:00
|
|
|
nodes map[uuid.UUID]*Node
|
2022-10-17 13:43:30 +00:00
|
|
|
// agentSockets maps agent IDs to their open websocket.
|
2023-07-12 22:37:31 +00:00
|
|
|
agentSockets map[uuid.UUID]Queue
|
2022-10-17 13:43:30 +00:00
|
|
|
// agentToConnectionSockets maps agent IDs to connection IDs of conns that
|
|
|
|
// are subscribed to updates for that agent.
|
2023-07-12 22:37:31 +00:00
|
|
|
agentToConnectionSockets map[uuid.UUID]map[uuid.UUID]Queue
|
|
|
|
|
|
|
|
// clients holds a map of all clients connected to the coordinator. This is
|
|
|
|
// necessary because a client may not be subscribed into any agents.
|
|
|
|
clients map[uuid.UUID]Queue
|
|
|
|
// clientsToAgents is an index of clients to all of their subscribed agents.
|
|
|
|
clientsToAgents map[uuid.UUID]map[uuid.UUID]Queue
|
2023-01-26 03:23:14 +00:00
|
|
|
|
|
|
|
// agentNameCache holds a cache of agent names. If one of them disappears,
|
|
|
|
// it's helpful to have a name cached for debugging.
|
|
|
|
agentNameCache *lru.Cache[uuid.UUID, string]
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
// legacyAgents holda a mapping of all agents detected as legacy, meaning
|
|
|
|
// they only listen on codersdk.WorkspaceAgentIP. They aren't compatible
|
|
|
|
// with the new ServerTailnet, so they must be connected through
|
|
|
|
// wsconncache.
|
|
|
|
legacyAgents map[uuid.UUID]struct{}
|
|
|
|
}
|
|
|
|
|
2023-09-21 19:30:48 +00:00
|
|
|
type QueueKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
QueueKindClient QueueKind = 1 + iota
|
|
|
|
QueueKindAgent
|
|
|
|
)
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
type Queue interface {
|
|
|
|
UniqueID() uuid.UUID
|
2023-09-21 19:30:48 +00:00
|
|
|
Kind() QueueKind
|
2023-07-12 22:37:31 +00:00
|
|
|
Enqueue(n []*Node) error
|
|
|
|
Name() string
|
|
|
|
Stats() (start, lastWrite int64)
|
|
|
|
Overwrites() int64
|
|
|
|
// CoordinatorClose is used by the coordinator when closing a Queue. It
|
|
|
|
// should skip removing itself from the coordinator.
|
|
|
|
CoordinatorClose() error
|
2023-09-21 19:30:48 +00:00
|
|
|
Done() <-chan struct{}
|
2023-07-12 22:37:31 +00:00
|
|
|
Close() error
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
func newCore(logger slog.Logger) *core {
|
|
|
|
nameCache, err := lru.New[uuid.UUID, string](512)
|
|
|
|
if err != nil {
|
|
|
|
panic("make lru cache: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &core{
|
|
|
|
logger: logger,
|
|
|
|
closed: false,
|
2023-07-12 22:37:31 +00:00
|
|
|
nodes: map[uuid.UUID]*Node{},
|
|
|
|
agentSockets: map[uuid.UUID]Queue{},
|
|
|
|
agentToConnectionSockets: map[uuid.UUID]map[uuid.UUID]Queue{},
|
2023-05-02 16:58:21 +00:00
|
|
|
agentNameCache: nameCache,
|
2023-07-12 22:37:31 +00:00
|
|
|
legacyAgents: map[uuid.UUID]struct{}{},
|
|
|
|
clients: map[uuid.UUID]Queue{},
|
|
|
|
clientsToAgents: map[uuid.UUID]map[uuid.UUID]Queue{},
|
2023-05-02 16:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrWouldBlock = xerrors.New("would block")
|
|
|
|
|
2022-09-01 01:09:44 +00:00
|
|
|
// Node returns an in-memory node by ID.
|
2022-10-17 13:43:30 +00:00
|
|
|
// If the node does not exist, nil is returned.
|
|
|
|
func (c *coordinator) Node(id uuid.UUID) *Node {
|
2023-05-02 16:58:21 +00:00
|
|
|
return c.core.node(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) node(id uuid.UUID) *Node {
|
2022-09-01 01:09:44 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2022-10-17 13:43:30 +00:00
|
|
|
return c.nodes[id]
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 23:22:34 +00:00
|
|
|
func (c *coordinator) NodeCount() int {
|
2023-05-02 16:58:21 +00:00
|
|
|
return c.core.nodeCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) nodeCount() int {
|
2023-01-23 23:22:34 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
return len(c.nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *coordinator) AgentCount() int {
|
2023-05-02 16:58:21 +00:00
|
|
|
return c.core.agentCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) agentCount() int {
|
2023-01-23 23:22:34 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
return len(c.agentSockets)
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
// ServeClient accepts a WebSocket connection that wants to connect to an agent
|
|
|
|
// with the specified ID.
|
2023-07-12 22:37:31 +00:00
|
|
|
func (c *coordinator) ServeClient(conn net.Conn, id, agentID uuid.UUID) error {
|
2023-05-02 16:58:21 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-07-12 22:37:31 +00:00
|
|
|
logger := c.core.clientLogger(id, agentID)
|
2023-05-02 16:58:21 +00:00
|
|
|
logger.Debug(ctx, "coordinating client")
|
2023-07-12 22:37:31 +00:00
|
|
|
|
2023-09-21 19:30:48 +00:00
|
|
|
tc := NewTrackedConn(ctx, cancel, conn, id, logger, id.String(), 0, QueueKindClient)
|
2023-07-12 22:37:31 +00:00
|
|
|
defer tc.Close()
|
|
|
|
|
|
|
|
c.core.addClient(id, tc)
|
|
|
|
defer c.core.clientDisconnected(id)
|
|
|
|
|
|
|
|
agentNode, err := c.core.clientSubscribeToAgent(tc, agentID)
|
2023-05-02 16:58:21 +00:00
|
|
|
if err != nil {
|
2023-07-12 22:37:31 +00:00
|
|
|
return xerrors.Errorf("subscribe agent: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if agentNode != nil {
|
|
|
|
err := tc.Enqueue([]*Node{agentNode})
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug(ctx, "enqueue initial node", slog.Error(err))
|
|
|
|
}
|
2023-05-02 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// On this goroutine, we read updates from the client and publish them. We start a second goroutine
|
|
|
|
// to write updates back to the client.
|
|
|
|
go tc.SendUpdates()
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(conn)
|
|
|
|
for {
|
2023-07-12 22:37:31 +00:00
|
|
|
err := c.handleNextClientMessage(id, decoder)
|
2023-05-02 16:58:21 +00:00
|
|
|
if err != nil {
|
2023-06-20 10:30:45 +00:00
|
|
|
logger.Debug(ctx, "unable to read client update, connection may be closed", slog.Error(err))
|
2023-05-02 16:58:21 +00:00
|
|
|
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, context.Canceled) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return xerrors.Errorf("handle next client message: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) clientLogger(id, agent uuid.UUID) slog.Logger {
|
|
|
|
return c.logger.With(slog.F("client_id", id), slog.F("agent_id", agent))
|
|
|
|
}
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
func (c *core) initOrSetAgentConnectionSocketLocked(agentID uuid.UUID, enq Queue) {
|
|
|
|
connectionSockets, ok := c.agentToConnectionSockets[agentID]
|
2022-09-01 01:09:44 +00:00
|
|
|
if !ok {
|
2023-07-12 22:37:31 +00:00
|
|
|
connectionSockets = map[uuid.UUID]Queue{}
|
|
|
|
c.agentToConnectionSockets[agentID] = connectionSockets
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
2023-07-12 22:37:31 +00:00
|
|
|
connectionSockets[enq.UniqueID()] = enq
|
|
|
|
|
|
|
|
c.clientsToAgents[enq.UniqueID()][agentID] = c.agentSockets[agentID]
|
2023-05-02 16:58:21 +00:00
|
|
|
}
|
2023-01-25 21:27:36 +00:00
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
func (c *core) clientDisconnected(id uuid.UUID) {
|
|
|
|
logger := c.clientLogger(id, uuid.Nil)
|
2023-05-02 16:58:21 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
// Clean all traces of this connection from the map.
|
|
|
|
delete(c.nodes, id)
|
|
|
|
logger.Debug(context.Background(), "deleted client node")
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
for agentID := range c.clientsToAgents[id] {
|
|
|
|
connectionSockets, ok := c.agentToConnectionSockets[agentID]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(connectionSockets, id)
|
|
|
|
logger.Debug(context.Background(), "deleted client connectionSocket from map", slog.F("agent_id", agentID))
|
|
|
|
|
|
|
|
if len(connectionSockets) == 0 {
|
|
|
|
delete(c.agentToConnectionSockets, agentID)
|
|
|
|
logger.Debug(context.Background(), "deleted last client connectionSocket from map", slog.F("agent_id", agentID))
|
|
|
|
}
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
delete(c.clients, id)
|
|
|
|
delete(c.clientsToAgents, id)
|
|
|
|
logger.Debug(context.Background(), "deleted client agents")
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
func (c *coordinator) handleNextClientMessage(id uuid.UUID, decoder *json.Decoder) error {
|
|
|
|
logger := c.core.clientLogger(id, uuid.Nil)
|
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
var node Node
|
|
|
|
err := decoder.Decode(&node)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("read json: %w", err)
|
|
|
|
}
|
2023-07-12 22:37:31 +00:00
|
|
|
|
2023-04-27 09:59:01 +00:00
|
|
|
logger.Debug(context.Background(), "got client node update", slog.F("node", node))
|
2023-07-12 22:37:31 +00:00
|
|
|
return c.core.clientNodeUpdate(id, &node)
|
2023-05-02 16:58:21 +00:00
|
|
|
}
|
2022-10-17 13:43:30 +00:00
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
func (c *core) clientNodeUpdate(id uuid.UUID, node *Node) error {
|
2022-10-17 13:43:30 +00:00
|
|
|
c.mutex.Lock()
|
2023-05-02 16:58:21 +00:00
|
|
|
defer c.mutex.Unlock()
|
2023-07-12 22:37:31 +00:00
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
// Update the node of this client in our in-memory map. If an agent entirely
|
|
|
|
// shuts down and reconnects, it needs to be aware of all clients attempting
|
|
|
|
// to establish connections.
|
2023-05-02 16:58:21 +00:00
|
|
|
c.nodes[id] = node
|
2022-10-17 13:43:30 +00:00
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
return c.clientNodeUpdateLocked(id, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) clientNodeUpdateLocked(id uuid.UUID, node *Node) error {
|
|
|
|
logger := c.clientLogger(id, uuid.Nil)
|
|
|
|
|
|
|
|
agents := []uuid.UUID{}
|
|
|
|
for agentID, agentSocket := range c.clientsToAgents[id] {
|
|
|
|
if agentSocket == nil {
|
|
|
|
logger.Debug(context.Background(), "enqueue node to agent; socket is nil", slog.F("agent_id", agentID))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := agentSocket.Enqueue([]*Node{node})
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug(context.Background(), "unable to Enqueue node to agent", slog.Error(err), slog.F("agent_id", agentID))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
agents = append(agents, agentID)
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
logger.Debug(context.Background(), "enqueued node to agents", slog.F("agent_ids", agents))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) clientSubscribeToAgent(enq Queue, agentID uuid.UUID) (*Node, error) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
logger := c.clientLogger(enq.UniqueID(), agentID)
|
|
|
|
|
|
|
|
c.initOrSetAgentConnectionSocketLocked(agentID, enq)
|
|
|
|
|
|
|
|
node, ok := c.nodes[enq.UniqueID()]
|
|
|
|
if ok {
|
|
|
|
// If we have the client node, send it to the agent. If not, it will be
|
|
|
|
// sent async.
|
|
|
|
agentSocket, ok := c.agentSockets[agentID]
|
|
|
|
if !ok {
|
|
|
|
logger.Debug(context.Background(), "subscribe to agent; socket is nil")
|
|
|
|
} else {
|
|
|
|
err := agentSocket.Enqueue([]*Node{node})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("enqueue client to agent: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Debug(context.Background(), "multiagent node doesn't exist")
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
agentNode, ok := c.nodes[agentID]
|
|
|
|
if !ok {
|
|
|
|
// This is ok, once the agent connects the node will be sent over.
|
|
|
|
logger.Debug(context.Background(), "agent node doesn't exist", slog.F("agent_id", agentID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the subscribed agent back to the multi agent.
|
|
|
|
return agentNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) clientUnsubscribeFromAgent(enq Queue, agentID uuid.UUID) error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
delete(c.clientsToAgents[enq.UniqueID()], agentID)
|
|
|
|
delete(c.agentToConnectionSockets[agentID], enq.UniqueID())
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) agentLogger(id uuid.UUID) slog.Logger {
|
|
|
|
return c.logger.With(slog.F("agent_id", id))
|
|
|
|
}
|
2022-10-17 13:43:30 +00:00
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
// ServeAgent accepts a WebSocket connection to an agent that
|
|
|
|
// listens to incoming connections and publishes node updates.
|
|
|
|
func (c *coordinator) ServeAgent(conn net.Conn, id uuid.UUID, name string) error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
logger := c.core.agentLogger(id)
|
|
|
|
logger.Debug(context.Background(), "coordinating agent")
|
|
|
|
// This uniquely identifies a connection that belongs to this goroutine.
|
|
|
|
unique := uuid.New()
|
|
|
|
tc, err := c.core.initAndTrackAgent(ctx, cancel, conn, id, unique, name)
|
2022-10-17 13:43:30 +00:00
|
|
|
if err != nil {
|
2023-05-02 16:58:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// On this goroutine, we read updates from the agent and publish them. We start a second goroutine
|
|
|
|
// to write updates back to the agent.
|
|
|
|
go tc.SendUpdates()
|
|
|
|
|
|
|
|
defer c.core.agentDisconnected(id, unique)
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(conn)
|
|
|
|
for {
|
|
|
|
err := c.handleNextAgentMessage(id, decoder)
|
|
|
|
if err != nil {
|
2023-06-20 10:30:45 +00:00
|
|
|
logger.Debug(ctx, "unable to read agent update, connection may be closed", slog.Error(err))
|
2023-05-02 16:58:21 +00:00
|
|
|
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, context.Canceled) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return xerrors.Errorf("handle next agent message: %w", err)
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-02 16:58:21 +00:00
|
|
|
}
|
2022-10-17 13:43:30 +00:00
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
func (c *core) agentDisconnected(id, unique uuid.UUID) {
|
|
|
|
logger := c.agentLogger(id)
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
// Only delete the connection if it's ours. It could have been
|
|
|
|
// overwritten.
|
2023-07-12 22:37:31 +00:00
|
|
|
if idConn, ok := c.agentSockets[id]; ok && idConn.UniqueID() == unique {
|
2023-05-02 16:58:21 +00:00
|
|
|
delete(c.agentSockets, id)
|
|
|
|
delete(c.nodes, id)
|
|
|
|
logger.Debug(context.Background(), "deleted agent socket and node")
|
|
|
|
}
|
2023-07-12 22:37:31 +00:00
|
|
|
for clientID := range c.agentToConnectionSockets[id] {
|
|
|
|
c.clientsToAgents[clientID][id] = nil
|
|
|
|
}
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
// initAndTrackAgent creates a TrackedConn for the agent, and sends any initial nodes updates if we have any. It is
|
|
|
|
// one function that does two things because it is critical that we hold the mutex for both things, lest we miss some
|
|
|
|
// updates.
|
|
|
|
func (c *core) initAndTrackAgent(ctx context.Context, cancel func(), conn net.Conn, id, unique uuid.UUID, name string) (*TrackedConn, error) {
|
2023-04-27 09:59:01 +00:00
|
|
|
logger := c.logger.With(slog.F("agent_id", id))
|
2022-09-01 01:09:44 +00:00
|
|
|
c.mutex.Lock()
|
2023-05-02 16:58:21 +00:00
|
|
|
defer c.mutex.Unlock()
|
2022-10-17 13:43:30 +00:00
|
|
|
if c.closed {
|
2023-05-02 16:58:21 +00:00
|
|
|
return nil, xerrors.New("coordinator is closed")
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
overwrites := int64(0)
|
|
|
|
// If an old agent socket is connected, we Close it to avoid any leaks. This
|
|
|
|
// shouldn't ever occur because we expect one agent to be running, but it's
|
|
|
|
// possible for a race condition to happen when an agent is disconnected and
|
|
|
|
// attempts to reconnect before the server realizes the old connection is
|
|
|
|
// dead.
|
|
|
|
oldAgentSocket, ok := c.agentSockets[id]
|
|
|
|
if ok {
|
2023-07-12 22:37:31 +00:00
|
|
|
overwrites = oldAgentSocket.Overwrites() + 1
|
2023-05-02 16:58:21 +00:00
|
|
|
_ = oldAgentSocket.Close()
|
|
|
|
}
|
2023-09-21 19:30:48 +00:00
|
|
|
tc := NewTrackedConn(ctx, cancel, conn, unique, logger, name, overwrites, QueueKindAgent)
|
2023-01-26 03:23:14 +00:00
|
|
|
c.agentNameCache.Add(id, name)
|
|
|
|
|
2022-09-01 01:09:44 +00:00
|
|
|
sockets, ok := c.agentToConnectionSockets[id]
|
|
|
|
if ok {
|
|
|
|
// Publish all nodes that want to connect to the
|
|
|
|
// desired agent ID.
|
|
|
|
nodes := make([]*Node, 0, len(sockets))
|
|
|
|
for targetID := range sockets {
|
|
|
|
node, ok := c.nodes[targetID]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
2023-05-02 16:58:21 +00:00
|
|
|
err := tc.Enqueue(nodes)
|
|
|
|
// this should never error since we're still the only goroutine that
|
|
|
|
// knows about the TrackedConn. If we hit an error something really
|
|
|
|
// wrong is happening
|
2022-09-01 01:09:44 +00:00
|
|
|
if err != nil {
|
2023-05-02 16:58:21 +00:00
|
|
|
logger.Critical(ctx, "unable to queue initial nodes", slog.Error(err))
|
|
|
|
return nil, err
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
2023-05-02 16:58:21 +00:00
|
|
|
logger.Debug(ctx, "wrote initial client(s) to agent", slog.F("nodes", nodes))
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
c.agentSockets[id] = tc
|
2023-07-12 22:37:31 +00:00
|
|
|
for clientID := range c.agentToConnectionSockets[id] {
|
|
|
|
c.clientsToAgents[clientID][id] = tc
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
logger.Debug(ctx, "added agent socket")
|
|
|
|
return tc, nil
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *coordinator) handleNextAgentMessage(id uuid.UUID, decoder *json.Decoder) error {
|
2023-05-02 16:58:21 +00:00
|
|
|
logger := c.core.agentLogger(id)
|
2022-10-17 13:43:30 +00:00
|
|
|
var node Node
|
|
|
|
err := decoder.Decode(&node)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("read json: %w", err)
|
|
|
|
}
|
2023-04-27 09:59:01 +00:00
|
|
|
logger.Debug(context.Background(), "decoded agent node", slog.F("node", node))
|
2023-05-02 16:58:21 +00:00
|
|
|
return c.core.agentNodeUpdate(id, &node)
|
|
|
|
}
|
2022-10-17 13:43:30 +00:00
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
// This is copied from codersdk because importing it here would cause an import
|
|
|
|
// cycle. This is just temporary until wsconncache is phased out.
|
|
|
|
var legacyAgentIP = netip.MustParseAddr("fd7a:115c:a1e0:49d6:b259:b7ac:b1b2:48f4")
|
|
|
|
|
|
|
|
// This is temporary until we no longer need to detect for agent backwards
|
|
|
|
// compatibility.
|
|
|
|
// See: https://github.com/coder/coder/issues/8218
|
|
|
|
func (c *core) agentIsLegacy(agentID uuid.UUID) bool {
|
|
|
|
c.mutex.RLock()
|
|
|
|
_, ok := c.legacyAgents[agentID]
|
|
|
|
c.mutex.RUnlock()
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:58:21 +00:00
|
|
|
func (c *core) agentNodeUpdate(id uuid.UUID, node *Node) error {
|
|
|
|
logger := c.agentLogger(id)
|
2022-10-17 13:43:30 +00:00
|
|
|
c.mutex.Lock()
|
2023-05-02 16:58:21 +00:00
|
|
|
defer c.mutex.Unlock()
|
|
|
|
c.nodes[id] = node
|
2023-07-12 22:37:31 +00:00
|
|
|
|
|
|
|
// Keep a cache of all legacy agents.
|
|
|
|
if len(node.Addresses) > 0 && node.Addresses[0].Addr() == legacyAgentIP {
|
|
|
|
c.legacyAgents[id] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
connectionSockets, ok := c.agentToConnectionSockets[id]
|
|
|
|
if !ok {
|
2023-04-27 09:59:01 +00:00
|
|
|
logger.Debug(context.Background(), "no client sockets; unable to send node")
|
2022-10-17 13:43:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish the new node to every listening socket.
|
2023-04-27 09:59:01 +00:00
|
|
|
for clientID, connectionSocket := range connectionSockets {
|
2023-05-02 16:58:21 +00:00
|
|
|
err := connectionSocket.Enqueue([]*Node{node})
|
|
|
|
if err == nil {
|
|
|
|
logger.Debug(context.Background(), "enqueued agent node to client",
|
|
|
|
slog.F("client_id", clientID))
|
|
|
|
} else {
|
|
|
|
// queue is backed up for some reason. This is bad, but we don't want to drop
|
|
|
|
// updates to other clients over it. Log and move on.
|
|
|
|
logger.Error(context.Background(), "failed to Enqueue",
|
2023-04-27 09:59:01 +00:00
|
|
|
slog.F("client_id", clientID), slog.Error(err))
|
2023-05-02 16:58:21 +00:00
|
|
|
}
|
2022-10-17 13:43:30 +00:00
|
|
|
}
|
2023-07-12 22:37:31 +00:00
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all of the open connections in the coordinator and stops the
|
|
|
|
// coordinator from accepting new connections.
|
|
|
|
func (c *coordinator) Close() error {
|
2023-05-02 16:58:21 +00:00
|
|
|
return c.core.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) close() error {
|
2022-10-17 13:43:30 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
if c.closed {
|
2022-10-17 16:47:45 +00:00
|
|
|
c.mutex.Unlock()
|
2022-10-17 13:43:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.closed = true
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
wg.Add(len(c.agentSockets))
|
|
|
|
for _, socket := range c.agentSockets {
|
|
|
|
socket := socket
|
|
|
|
go func() {
|
2023-07-12 22:37:31 +00:00
|
|
|
_ = socket.CoordinatorClose()
|
2022-10-17 13:43:30 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-07-12 22:37:31 +00:00
|
|
|
wg.Add(len(c.clients))
|
|
|
|
for _, client := range c.clients {
|
|
|
|
client := client
|
|
|
|
go func() {
|
|
|
|
_ = client.CoordinatorClose()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
2022-10-17 13:43:30 +00:00
|
|
|
|
2022-10-17 16:47:45 +00:00
|
|
|
c.mutex.Unlock()
|
|
|
|
|
2022-10-17 13:43:30 +00:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
2022-09-01 01:09:44 +00:00
|
|
|
}
|
2023-01-25 21:27:36 +00:00
|
|
|
|
2023-01-26 22:32:38 +00:00
|
|
|
func (c *coordinator) ServeHTTPDebug(w http.ResponseWriter, r *http.Request) {
|
2023-05-02 16:58:21 +00:00
|
|
|
c.core.serveHTTPDebug(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *core) serveHTTPDebug(w http.ResponseWriter, r *http.Request) {
|
2023-01-25 21:27:36 +00:00
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
CoordinatorHTTPDebug(
|
|
|
|
HTTPDebugFromLocal(false, c.agentSockets, c.agentToConnectionSockets, c.nodes, c.agentNameCache),
|
|
|
|
)(w, r)
|
2023-01-26 22:32:38 +00:00
|
|
|
}
|
2023-01-26 00:29:51 +00:00
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
func HTTPDebugFromLocal(
|
2023-07-26 22:54:21 +00:00
|
|
|
ha bool,
|
2023-07-12 22:37:31 +00:00
|
|
|
agentSocketsMap map[uuid.UUID]Queue,
|
|
|
|
agentToConnectionSocketsMap map[uuid.UUID]map[uuid.UUID]Queue,
|
2023-07-26 22:54:21 +00:00
|
|
|
nodesMap map[uuid.UUID]*Node,
|
2023-01-26 22:32:38 +00:00
|
|
|
agentNameCache *lru.Cache[uuid.UUID, string],
|
2023-07-28 22:59:31 +00:00
|
|
|
) HTMLDebug {
|
|
|
|
now := time.Now()
|
|
|
|
data := HTMLDebug{HA: ha}
|
|
|
|
for id, conn := range agentSocketsMap {
|
|
|
|
start, lastWrite := conn.Stats()
|
|
|
|
agent := &HTMLAgent{
|
|
|
|
Name: conn.Name(),
|
|
|
|
ID: id,
|
|
|
|
CreatedAge: now.Sub(time.Unix(start, 0)).Round(time.Second),
|
|
|
|
LastWriteAge: now.Sub(time.Unix(lastWrite, 0)).Round(time.Second),
|
|
|
|
Overwrites: int(conn.Overwrites()),
|
2023-01-25 21:27:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
for id, conn := range agentToConnectionSocketsMap[id] {
|
2023-07-26 22:54:21 +00:00
|
|
|
start, lastWrite := conn.Stats()
|
2023-07-28 22:59:31 +00:00
|
|
|
agent.Connections = append(agent.Connections, &HTMLClient{
|
2023-07-26 22:54:21 +00:00
|
|
|
Name: conn.Name(),
|
|
|
|
ID: id,
|
|
|
|
CreatedAge: now.Sub(time.Unix(start, 0)).Round(time.Second),
|
|
|
|
LastWriteAge: now.Sub(time.Unix(lastWrite, 0)).Round(time.Second),
|
|
|
|
})
|
2023-01-26 00:29:51 +00:00
|
|
|
}
|
2023-08-09 19:50:26 +00:00
|
|
|
slices.SortFunc(agent.Connections, func(a, b *HTMLClient) int {
|
|
|
|
return slice.Ascending(a.Name, b.Name)
|
2023-07-26 22:54:21 +00:00
|
|
|
})
|
2023-01-25 21:27:36 +00:00
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
data.Agents = append(data.Agents, agent)
|
|
|
|
}
|
2023-08-09 19:50:26 +00:00
|
|
|
slices.SortFunc(data.Agents, func(a, b *HTMLAgent) int {
|
|
|
|
return slice.Ascending(a.Name, b.Name)
|
2023-07-28 22:59:31 +00:00
|
|
|
})
|
2023-01-26 00:29:51 +00:00
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
for agentID, conns := range agentToConnectionSocketsMap {
|
|
|
|
if len(conns) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-26 22:54:21 +00:00
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
if _, ok := agentSocketsMap[agentID]; ok {
|
|
|
|
continue
|
2023-07-26 22:54:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
agentName, ok := agentNameCache.Get(agentID)
|
|
|
|
if !ok {
|
|
|
|
agentName = "unknown"
|
|
|
|
}
|
|
|
|
agent := &HTMLAgent{
|
|
|
|
Name: agentName,
|
|
|
|
ID: agentID,
|
|
|
|
}
|
|
|
|
for id, conn := range conns {
|
|
|
|
start, lastWrite := conn.Stats()
|
|
|
|
agent.Connections = append(agent.Connections, &HTMLClient{
|
|
|
|
Name: conn.Name(),
|
|
|
|
ID: id,
|
|
|
|
CreatedAge: now.Sub(time.Unix(start, 0)).Round(time.Second),
|
|
|
|
LastWriteAge: now.Sub(time.Unix(lastWrite, 0)).Round(time.Second),
|
2023-01-26 22:32:38 +00:00
|
|
|
})
|
2023-07-26 22:54:21 +00:00
|
|
|
}
|
2023-08-09 19:50:26 +00:00
|
|
|
slices.SortFunc(agent.Connections, func(a, b *HTMLClient) int {
|
|
|
|
return slice.Ascending(a.Name, b.Name)
|
2023-07-26 22:54:21 +00:00
|
|
|
})
|
2023-01-26 03:23:14 +00:00
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
data.MissingAgents = append(data.MissingAgents, agent)
|
|
|
|
}
|
2023-08-09 19:50:26 +00:00
|
|
|
slices.SortFunc(data.MissingAgents, func(a, b *HTMLAgent) int {
|
|
|
|
return slice.Ascending(a.Name, b.Name)
|
2023-07-28 22:59:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for id, node := range nodesMap {
|
|
|
|
name, _ := agentNameCache.Get(id)
|
|
|
|
data.Nodes = append(data.Nodes, &HTMLNode{
|
|
|
|
ID: id,
|
|
|
|
Name: name,
|
|
|
|
Node: node,
|
|
|
|
})
|
|
|
|
}
|
2023-08-09 19:50:26 +00:00
|
|
|
slices.SortFunc(data.Nodes, func(a, b *HTMLNode) int {
|
|
|
|
return slice.Ascending(a.Name+a.ID.String(), b.Name+b.ID.String())
|
2023-07-28 22:59:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func CoordinatorHTTPDebug(data HTMLDebug) func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
|
|
|
tmpl, err := template.New("coordinator_debug").Funcs(template.FuncMap{
|
|
|
|
"marshal": func(v any) template.JS {
|
|
|
|
a, err := json.MarshalIndent(v, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
//nolint:gosec
|
|
|
|
return template.JS(fmt.Sprintf(`{"err": %q}`, err))
|
|
|
|
}
|
|
|
|
//nolint:gosec
|
|
|
|
return template.JS(a)
|
|
|
|
},
|
|
|
|
}).Parse(coordinatorDebugTmpl)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-26 22:54:21 +00:00
|
|
|
err = tmpl.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
return
|
2023-01-25 21:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 22:54:21 +00:00
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
type HTMLDebug struct {
|
2023-07-26 22:54:21 +00:00
|
|
|
HA bool
|
2023-07-28 22:59:31 +00:00
|
|
|
Agents []*HTMLAgent
|
|
|
|
MissingAgents []*HTMLAgent
|
|
|
|
Nodes []*HTMLNode
|
2023-07-26 22:54:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
type HTMLAgent struct {
|
2023-07-26 22:54:21 +00:00
|
|
|
Name string
|
|
|
|
ID uuid.UUID
|
|
|
|
CreatedAge time.Duration
|
|
|
|
LastWriteAge time.Duration
|
|
|
|
Overwrites int
|
2023-07-28 22:59:31 +00:00
|
|
|
Connections []*HTMLClient
|
2023-07-26 22:54:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
type HTMLClient struct {
|
2023-07-26 22:54:21 +00:00
|
|
|
Name string
|
|
|
|
ID uuid.UUID
|
|
|
|
CreatedAge time.Duration
|
|
|
|
LastWriteAge time.Duration
|
|
|
|
}
|
|
|
|
|
2023-07-28 22:59:31 +00:00
|
|
|
type HTMLNode struct {
|
2023-07-26 22:54:21 +00:00
|
|
|
ID uuid.UUID
|
|
|
|
Name string
|
2023-07-28 22:59:31 +00:00
|
|
|
Node any
|
2023-07-26 22:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var coordinatorDebugTmpl = `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
{{- if .HA }}
|
|
|
|
<h1>high-availability wireguard coordinator debug</h1>
|
|
|
|
<h4 style="margin-top:-25px">warning: this only provides info from the node that served the request, if there are multiple replicas this data may be incomplete</h4>
|
|
|
|
{{- else }}
|
|
|
|
<h1>in-memory wireguard coordinator debug</h1>
|
|
|
|
{{- end }}
|
|
|
|
|
|
|
|
<h2 id=agents> <a href=#agents>#</a> agents: total {{ len .Agents }} </h2>
|
|
|
|
<ul>
|
|
|
|
{{- range .Agents }}
|
|
|
|
<li style="margin-top:4px">
|
|
|
|
<b>{{ .Name }}</b> (<code>{{ .ID }}</code>): created {{ .CreatedAge }} ago, write {{ .LastWriteAge }} ago, overwrites {{ .Overwrites }}
|
|
|
|
<h3 style="margin:0px;font-size:16px;font-weight:400"> connections: total {{ len .Connections}} </h3>
|
|
|
|
<ul>
|
|
|
|
{{- range .Connections }}
|
|
|
|
<li><b>{{ .Name }}</b> (<code>{{ .ID }}</code>): created {{ .CreatedAge }} ago, write {{ .LastWriteAge }} ago </li>
|
|
|
|
{{- end }}
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
{{- end }}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<h2 id=missing-agents><a href=#missing-agents>#</a> missing agents: total {{ len .MissingAgents }}</h2>
|
|
|
|
<ul>
|
|
|
|
{{- range .MissingAgents}}
|
|
|
|
<li style="margin-top:4px"><b>{{ .Name }}</b> (<code>{{ .ID }}</code>): created ? ago, write ? ago, overwrites ? </li>
|
|
|
|
<h3 style="margin:0px;font-size:16px;font-weight:400"> connections: total {{ len .Connections }} </h3>
|
|
|
|
<ul>
|
|
|
|
{{- range .Connections }}
|
|
|
|
<li><b>{{ .Name }}</b> (<code>{{ .ID }}</code>): created {{ .CreatedAge }} ago, write {{ .LastWriteAge }} ago </li>
|
|
|
|
{{- end }}
|
|
|
|
</ul>
|
|
|
|
{{- end }}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<h2 id=nodes><a href=#nodes>#</a> nodes: total {{ len .Nodes }}</h2>
|
|
|
|
<ul>
|
|
|
|
{{- range .Nodes }}
|
|
|
|
<li style="margin-top:4px"><b>{{ .Name }}</b> (<code>{{ .ID }}</code>):
|
|
|
|
<span style="white-space: pre;"><code>{{ marshal .Node }}</code></span>
|
|
|
|
</li>
|
|
|
|
{{- end }}
|
|
|
|
</ul>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|