feat: switch to a central singleton handler
This also adds support for Headscale TLS installations
This commit is contained in:
parent
43e06987ad
commit
6108de52e7
35 changed files with 339 additions and 399 deletions
|
|
@ -1,10 +1,13 @@
|
|||
import { open } from 'node:fs/promises';
|
||||
import type { HostInfo } from '~/types';
|
||||
import {
|
||||
hp_getSingleton,
|
||||
hp_getSingletonUnsafe,
|
||||
hp_setSingleton,
|
||||
} from '~server/context/global';
|
||||
import log from '~server/utils/log';
|
||||
import { TimedCache } from './cache';
|
||||
import { hp_getAgents } from './socket';
|
||||
|
||||
let cache: TimedCache<HostInfo> | undefined;
|
||||
export async function hp_loadAgentCache(defaultTTL: number, filepath: string) {
|
||||
log.debug('CACH', `Loading agent cache from ${filepath}`);
|
||||
|
||||
|
|
@ -17,18 +20,16 @@ export async function hp_loadAgentCache(defaultTTL: number, filepath: string) {
|
|||
return;
|
||||
}
|
||||
|
||||
cache = new TimedCache(defaultTTL, filepath);
|
||||
}
|
||||
|
||||
export function hp_getAgentCache() {
|
||||
return cache;
|
||||
const cache = new TimedCache<HostInfo>(defaultTTL, filepath);
|
||||
hp_setSingleton('ws_agent_data', cache);
|
||||
}
|
||||
|
||||
export async function hp_agentRequest(nodeList: string[]) {
|
||||
// Request to all connected agents (we can have multiple)
|
||||
// Luckily we can parse all the data at once through message parsing
|
||||
// and then overlapping cache entries will be overwritten by time
|
||||
const agents = hp_getAgents();
|
||||
const agents = hp_getSingleton('ws_agents');
|
||||
const cache = hp_getSingletonUnsafe('ws_agent_data');
|
||||
|
||||
// Deduplicate the list of nodes
|
||||
const NodeIDs = [...new Set(nodeList)];
|
||||
|
|
|
|||
|
|
@ -1,8 +1,14 @@
|
|||
import WebSocket, { WebSocketServer } from 'ws';
|
||||
import { hp_setSingleton } from '~server/context/global';
|
||||
import log from '~server/utils/log';
|
||||
import { hp_agentRequest } from './data';
|
||||
|
||||
export function initWebsocket(server: WebSocketServer, authKey: string) {
|
||||
log.info('SRVX', 'Starting a WebSocket server for agent connections');
|
||||
const agents = new Map<string, WebSocket>();
|
||||
hp_setSingleton('ws_agents', agents);
|
||||
hp_setSingleton('ws_fetch_data', hp_agentRequest);
|
||||
|
||||
server.on('connection', (ws, req) => {
|
||||
const tailnetID = req.headers['x-headplane-tailnet-id'];
|
||||
if (!tailnetID || typeof tailnetID !== 'string') {
|
||||
|
|
@ -50,8 +56,3 @@ export function initWebsocket(server: WebSocketServer, authKey: string) {
|
|||
|
||||
return server;
|
||||
}
|
||||
|
||||
const agents = new Map<string, WebSocket>();
|
||||
export function hp_getAgents() {
|
||||
return agents;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue