mirror of
https://github.com/tale/headplane.git
synced 2026-08-29 16:37:10 +00:00
fix: handle ws upgrade smoothly
This commit is contained in:
+4
-8
@@ -1,5 +1,6 @@
|
|||||||
import { constants, access } from 'node:fs/promises';
|
import { constants, access } from 'node:fs/promises';
|
||||||
import { createServer } from 'node:http';
|
import { createServer } from 'node:http';
|
||||||
|
import { WebSocketServer } from 'ws';
|
||||||
import { hp_getConfig, hp_loadConfig } from '~server/context/loader';
|
import { hp_getConfig, hp_loadConfig } from '~server/context/loader';
|
||||||
import { listener } from '~server/listener';
|
import { listener } from '~server/listener';
|
||||||
import log from '~server/utils/log';
|
import log from '~server/utils/log';
|
||||||
@@ -21,18 +22,13 @@ await hp_loadConfig();
|
|||||||
const server = createServer(listener);
|
const server = createServer(listener);
|
||||||
|
|
||||||
const context = hp_getConfig();
|
const context = hp_getConfig();
|
||||||
const ws = initWebsocket(context.server.agent.authkey);
|
if (context.server.agent.authkey.length > 0) {
|
||||||
if (ws) {
|
const ws = new WebSocketServer({ server });
|
||||||
|
initWebsocket(ws, context.server.agent.authkey);
|
||||||
await hp_loadAgentCache(
|
await hp_loadAgentCache(
|
||||||
context.server.agent.ttl,
|
context.server.agent.ttl,
|
||||||
context.server.agent.cache_path,
|
context.server.agent.cache_path,
|
||||||
);
|
);
|
||||||
|
|
||||||
server.on('upgrade', (req, socket, head) => {
|
|
||||||
ws.handleUpgrade(req, socket, head, (ws) => {
|
|
||||||
ws.emit('connection', ws, req);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen(context.server.port, context.server.host, () => {
|
server.listen(context.server.port, context.server.host, () => {
|
||||||
|
|||||||
+1
-6
@@ -1,12 +1,7 @@
|
|||||||
import WebSocket, { WebSocketServer } from 'ws';
|
import WebSocket, { WebSocketServer } from 'ws';
|
||||||
import log from '~server/utils/log';
|
import log from '~server/utils/log';
|
||||||
|
|
||||||
const server = new WebSocketServer({ noServer: true });
|
export function initWebsocket(server: WebSocketServer, authKey: string) {
|
||||||
export function initWebsocket(authKey: string) {
|
|
||||||
if (authKey.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info('SRVX', 'Starting a WebSocket server for agent connections');
|
log.info('SRVX', 'Starting a WebSocket server for agent connections');
|
||||||
server.on('connection', (ws, req) => {
|
server.on('connection', (ws, req) => {
|
||||||
const tailnetID = req.headers['x-headplane-tailnet-id'];
|
const tailnetID = req.headers['x-headplane-tailnet-id'];
|
||||||
|
|||||||
Reference in New Issue
Block a user