-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.ts
More file actions
158 lines (128 loc) · 4.39 KB
/
Copy pathserver.ts
File metadata and controls
158 lines (128 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import config from './config';
import { checkDocker, checkDockerRunning, initContainerStateMap } from './handlers/docker';
import { getCurrentStats, initStatsCollection, saveStats } from './handlers/stats';
import logger, { drawHeader } from './logger';
import { handleHttpRequest } from './router';
import { getAllowedIpCheck } from './security/hmac';
import { checkRateLimit } from './security/rateLimit';
import { validateContainerId } from './validation';
import type { WsData } from './ws/server';
import { buildWsData, openConnections, wsClose, wsMessage, wsOpen } from './ws/server';
function isPrivateIp(ip: string): boolean {
return (
ip === '127.0.0.1' ||
ip === '::1' ||
ip === 'localhost' ||
ip.startsWith('10.') ||
ip.startsWith('192.168.') ||
/^172\.(1[6-9]|2\d|3[01])\./.test(ip)
);
}
function resolveEffectiveIp(req: Request, server: ReturnType<typeof Bun.serve>): string {
const rawIp = server.requestIP(req);
const socketIp = rawIp?.address.replace(/^::ffff:/, '') ?? 'unknown';
if (Bun.env.BEHIND_PROXY === 'true') {
if (isPrivateIp(socketIp)) {
return req.headers.get('x-forwarded-for')?.split(',')[0].trim() || socketIp;
}
logger.warn(`BEHIND_PROXY=true but ${socketIp} is not a trusted proxy`);
}
return socketIp;
}
function attemptUpgrade(req: Request, server: ReturnType<typeof Bun.serve>): boolean | Response {
if (req.method !== 'GET') return false;
const url = new URL(req.url);
const parts = url.pathname.split('/').filter(Boolean);
const route = parts[0];
const containerId = parts[1];
const validRoutes = ['container', 'containerstatus', 'containerevents'];
if (!validRoutes.includes(route) || !containerId) return false;
if (parts.length !== 2) return false;
if (!validateContainerId(containerId)) return false;
const effectiveIp = resolveEffectiveIp(req, server);
const ipErr = getAllowedIpCheck(effectiveIp);
if (ipErr) return ipErr;
const rlErr = checkRateLimit(effectiveIp, 60);
if (rlErr) return rlErr;
return server.upgrade(req, {
data: buildWsData(route as 'container' | 'containerstatus' | 'containerevents', containerId),
});
}
process.on('uncaughtException', (err) => {
logger.error('uncaught exception', err);
});
process.on('unhandledRejection', (reason) => {
logger.error('unhandled rejection', reason as Error);
});
drawHeader(config.version, config.port);
try {
await checkDocker();
await checkDockerRunning();
await initContainerStateMap();
} catch (err) {
logger.error('docker is not ready, so container actions are paused for now', err as Error);
}
initStatsCollection();
const tls =
config.tlsCertPath && config.tlsKeyPath
? {
cert: Bun.file(config.tlsCertPath),
key: Bun.file(config.tlsKeyPath),
}
: undefined;
if (config.tlsCertPath && !config.tlsKeyPath) {
logger.warn('TLS certificate configured without TLS key; TLS disabled');
}
export const server = Bun.serve<WsData>({
port: config.port,
hostname: '0.0.0.0',
fetch(req, server) {
const upgradeResult = attemptUpgrade(req, server);
if (upgradeResult === true) return;
if (upgradeResult instanceof Response) return upgradeResult;
return handleHttpRequest(req, server);
},
websocket: {
open(ws) {
wsOpen(ws);
},
message(ws, msg) {
wsMessage(ws, msg);
},
close(ws, code, why) {
wsClose(ws, code, why);
},
drain() {
/* bun requires this */
},
},
tls,
});
logger.ok(`ready on port ${config.port}`);
if (process.env.DAEMON_WORKER_MODE === '1')
(self as unknown as Worker).postMessage({ type: 'ready', port: config.port });
setInterval(async () => {
try {
const stats = await getCurrentStats();
saveStats(stats);
} catch (err) {
logger.error('could not collect host stats', err);
}
}, config.statsInterval);
async function shutdown(signal: string): Promise<void> {
logger.info(`${signal} received, shutting down`);
server.stop(false);
for (const ws of openConnections) ws.close(1001, 'server shutting down');
try {
const stats = await getCurrentStats();
saveStats(stats);
} catch {
/* don't let a stats error block shutdown */
}
await new Promise<void>((resolve) => setTimeout(resolve, 10_000));
logger.info('shutdown finished');
process.exit(0);
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGHUP', () => shutdown('SIGHUP'));