-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathapiServer.js
More file actions
489 lines (403 loc) · 15.9 KB
/
apiServer.js
File metadata and controls
489 lines (403 loc) · 15.9 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
const configManager = require('./ZelBack/src/services/utils/configManager');
if (typeof AbortController === 'undefined') {
// polyfill for nodeJS 14.18.1 - without having to use experimental features
// eslint-disable-next-line global-require
const abortControler = require('node-abort-controller');
globalThis.AbortController = abortControler.AbortController;
}
process.env.NODE_CONFIG_DIR = `${__dirname}/ZelBack/config/`;
const fs = require('node:fs');
const http = require('node:http');
const https = require('node:https');
const path = require('node:path');
const axios = require('axios').default;
const config = require('config');
const serviceManager = require('./ZelBack/src/services/serviceManager');
const fluxServer = require('./ZelBack/src/lib/fluxServer');
const log = require('./ZelBack/src/lib/log');
const serviceHelper = require('./ZelBack/src/services/serviceHelper');
const upnpService = require('./ZelBack/src/services/upnpService');
const requestHistoryStore = require('./ZelBack/src/services/utils/requestHistory');
const globalState = require('./ZelBack/src/services/utils/globalState');
const fluxNetworkHelper = require('./ZelBack/src/services/fluxNetworkHelper');
const fluxCommunicationMessagesSender = require('./ZelBack/src/services/fluxCommunicationMessagesSender');
const dockerService = require('./ZelBack/src/services/dockerService');
const dbHelper = require('./ZelBack/src/services/dbHelper');
const apiPort = globalThis.userconfig.initial.apiport || config.server.apiport;
const apiPortHttps = +apiPort + 1;
let requestHistory = null;
let axiosDefaultsSet = false;
/**
* The Cacheable. So we only instantiate it once (and for testing)
*/
let cacheable = null;
function getrequestHistory() {
return requestHistory;
}
/**
* Gets the cacheable CacheableLookup() for testing
*/
function getCacheable() {
return cacheable;
}
/**
* Gets the cacheable CacheableLookup() for testing
*/
function resetCacheable() {
cacheable = null;
}
/**
* Adds extra servers to DNS, if they are not being used already. This is just
* within the NodeJS process, not systemwide.
*
* Sets these globally for both http and https (axios) It will use the OS servers
* by default, and if they fail, move on to our added servers, if a server fails, requests
* go to an active server immediately, for a period.
* @param {Map?} userCache An optional cache, we use this as a reference for testing
* @returns {Promise<void>}
*/
async function createDnsCache(userCache) {
try {
if (cacheable) return;
const cache = userCache || new Map();
// we have to dynamic import here as cacheable-lookup only supports ESM.
const { default: CacheableLookup } = await import('cacheable-lookup');
cacheable = new CacheableLookup({ maxTtl: 360, cache });
cacheable.install(http.globalAgent);
cacheable.install(https.globalAgent);
const cloudflareDns = '1.1.1.1';
const googleDns = '8.8.8.8';
const quad9Dns = '9.9.9.9';
const backupServers = [cloudflareDns, googleDns, quad9Dns];
const existingServers = cacheable.servers;
// it dedupes any servers
cacheable.servers = [...existingServers, ...backupServers];
} catch (error) {
log.error(error);
}
}
function setAxiosDefaults(socketIoServers) {
if (axiosDefaultsSet) return;
axiosDefaultsSet = true;
log.info('setting axios defaults');
axios.defaults.timeout = 20_000;
if (!globalThis.userconfig.initial.debug) return;
log.info('User defined debug set, setting up socket.io for debug.');
requestHistory = new requestHistoryStore.RequestHistory({ maxAge: 60_000 * 60 });
const rooms = [];
const requestRoom = 'outboundHttp';
socketIoServers.forEach((server) => {
const debugRoom = server.getRoom(requestRoom, { namespace: 'debug' });
rooms.push(debugRoom);
const debugAdapter = server.getAdapter('debug');
debugAdapter.on('join-room', (room, id) => {
if (room !== requestRoom) return;
const socket = server.getSocketById('debug', id);
socket.emit('addHistory', requestHistory.allHistory);
});
});
requestHistory.on('requestAdded', (request) => {
rooms.forEach((room) => room.emit('addRequest', request));
});
requestHistory.on('requestRemoved', (request) => {
rooms.forEach((room) => room.emit('removeRequest', request));
});
axios.interceptors.request.use(
(conf) => {
const {
baseURL, url, method, timeout,
} = conf;
const fullUrl = baseURL ? `${baseURL}${url}` : url;
const requestData = {
url: fullUrl, verb: method.toUpperCase(), timeout, timestamp: Date.now(),
};
requestHistory.storeRequest(requestData);
return conf;
},
(error) => Promise.reject(error),
);
}
/**
* Utility function to log error before exiting. As the logging is async, if
* we don't wait a while, the process exits bofore the logging takes place
*
* @param {string} msg
* @param {{delay?: number, exitCode?: number}} options
*/
async function logErrorAndExit(msg, options = {}) {
const delayMs = options.delay || 1_000;
const exitCode = options.exitCode || 0;
if (msg) log.error(msg);
const delayS = Math.round((delayMs / 1000) * 100) / 100;
log.info(`Waiting: ${delayS}s, before exiting with code: ${exitCode}`);
await serviceHelper.delay(delayMs);
process.exit(exitCode);
}
async function loadUpnpIfRequired() {
try {
let verifyUpnp = false;
let setupUpnp = false;
if (globalThis.userconfig.initial.apiport) {
verifyUpnp = await upnpService.verifyUPNPsupport(apiPort);
if (verifyUpnp) {
setupUpnp = await upnpService.setupUPNP(apiPort);
}
}
if ((globalThis.userconfig.initial.apiport && globalThis.userconfig.initial.apiport !== config.server.apiport) || globalThis.userconfig.initial.routerIP) {
if (verifyUpnp !== true) {
await logErrorAndExit(
`Flux port ${globalThis.userconfig.initial.apiport} specified but UPnP failed to verify support. Shutting down.`,
{ exitCode: 1, delay: 120_000 },
);
}
if (setupUpnp !== true) {
await logErrorAndExit(
`Flux port ${globalThis.userconfig.initial.apiport} specified but UPnP failed to map to api or home port. Shutting down.`,
{ exitCode: 1, delay: 120_000 },
);
}
}
} catch (error) {
log.error(error);
}
}
async function configReload() {
// Config watching is now handled by configManager
await configManager.startWatching(log, async (newConfig) => {
if (newConfig?.initial?.apiport) {
await loadUpnpIfRequired();
}
});
}
/**
* Main entrypoint
*
* @returns {Promise<String>}
*/
async function initiate() {
if (!config.server.allowedPorts.includes(+apiPort)) {
await logErrorAndExit(`Flux port ${apiPort} is not supported. Shutting down.`);
}
process.on('uncaughtException', (err) => {
const dnsErrors = ['ENOTFOUND', 'EAI_AGAIN', 'ESERVFAIL'];
if (dnsErrors.includes(err.code) && err.hostname) {
log.error('Uncaught DNS Lookup Error!!, swallowing.');
log.error(err);
return;
}
logErrorAndExit(err, { exitCode: 1 });
});
await createDnsCache();
await loadUpnpIfRequired();
setImmediate(configReload);
const appRoot = process.cwd();
// ToDo: move this to async
const certExists = fs.existsSync(path.join(appRoot, 'certs/v1.key'));
if (!certExists) {
const cwd = path.join(appRoot, 'helpers');
const scriptPath = path.join(cwd, 'createSSLcert.sh');
await serviceHelper.runCommand(scriptPath, { cwd });
}
// ToDo: move these to async
const key = fs.readFileSync(path.join(appRoot, 'certs/v1.key'), 'utf8');
const cert = fs.readFileSync(path.join(appRoot, 'certs/v1.crt'), 'utf8');
const httpServer = new fluxServer.FluxServer();
const httpsServer = new fluxServer.FluxServer({
mode: 'https', key, cert, expressApp: httpServer.app,
});
const httpError = await httpServer.listen(apiPort).catch((err) => err);
if (httpError) {
// if shutting down clean, nodemon won't restart
logErrorAndExit(`Flux api server unable to start. ${httpError}`);
return '';
}
const httpsError = await httpsServer.listen(apiPortHttps).catch((err) => err);
if (httpsError) {
// if shutting down clean, nodemon won't restart
logErrorAndExit(`Flux api server unable to start. ${httpsError}`);
return '';
}
log.info(`Flux listening on port ${apiPort}!`);
log.info(`Flux https listening on port ${apiPortHttps}!`);
setAxiosDefaults([httpServer.socketIo, httpsServer.socketIo]);
serviceManager.startFluxFunctions();
return apiPort;
}
/**
* Check if the system is shutting down or rebooting.
* Uses multiple detection methods for reliability.
* @returns {Promise<boolean>} True if system appears to be shutting down/rebooting
*/
async function isSystemShuttingDown() {
// Method 1: Check for systemd scheduled shutdown file (most reliable for scheduled shutdowns)
try {
if (fs.existsSync('/run/systemd/shutdown/scheduled')) {
log.info('System shutdown detected via /run/systemd/shutdown/scheduled');
return true;
}
} catch (e) {
// Ignore errors
}
// Method 2: Check systemd's current state
const { stdout: systemState } = await serviceHelper.runCommand('systemctl', {
params: ['is-system-running'],
timeout: 5000,
logError: false,
});
if (systemState && systemState.trim() === 'stopping') {
log.info('System shutdown detected via systemctl is-system-running (stopping)');
return true;
}
// Method 3: Check for active shutdown/reboot jobs in systemd
const { stdout: jobs } = await serviceHelper.runCommand('systemctl', {
params: ['list-jobs', '--no-pager'],
timeout: 5000,
logError: false,
});
if (jobs && (jobs.includes('shutdown.target') || jobs.includes('reboot.target') || jobs.includes('poweroff.target') || jobs.includes('halt.target'))) {
log.info('System shutdown detected via systemctl list-jobs');
return true;
}
// Method 4: Check for running shutdown/reboot processes
const { stdout: shutdownPid } = await serviceHelper.runCommand('pgrep', {
params: ['-x', 'shutdown'],
timeout: 5000,
logError: false,
});
if (shutdownPid && shutdownPid.trim()) {
log.info('System shutdown detected via running shutdown process');
return true;
}
// Method 5: Check runlevel (0 = halt, 6 = reboot)
const { stdout: runlevel } = await serviceHelper.runCommand('runlevel', {
timeout: 5000,
logError: false,
});
if (runlevel) {
const trimmedRunlevel = runlevel.trim();
if (trimmedRunlevel.endsWith(' 0') || trimmedRunlevel.endsWith(' 6')) {
log.info(`System shutdown detected via runlevel: ${trimmedRunlevel}`);
return true;
}
}
// Method 6: Check for /run/nologin (created during shutdown, but NOT /etc/nologin which can be manual)
try {
if (fs.existsSync('/run/nologin')) {
log.info('System shutdown detected via /run/nologin file');
return true;
}
} catch (e) {
// Ignore errors
}
return false;
}
/**
* Handle SIGTERM signal for graceful shutdown.
* Only broadcasts fluxnodesigterm message if the system is actually rebooting/shutting down,
* not when the service is just being restarted by systemd/pm2.
*/
async function handleSigterm() {
log.info('SIGTERM received, checking if system is shutting down...');
// Small delay to allow systemd to update its state before we check
await serviceHelper.delay(100);
const systemShuttingDown = await isSystemShuttingDown();
if (!systemShuttingDown) {
log.info('System is not shutting down (service restart detected), skipping shutdown broadcast');
process.exit(0);
}
log.info('System shutdown/reboot detected, initiating graceful shutdown with peer notification...');
try {
const { runningAppsCache } = globalState;
if (runningAppsCache.size > 0) {
log.info(`Node was running ${runningAppsCache.size} apps, broadcasting shutdown notification to peers...`);
const ip = await fluxNetworkHelper.getMyFluxIPandPort();
if (ip) {
const sigtermMessage = {
type: 'fluxnodesigterm',
version: 1,
ip,
broadcastedAt: Date.now(),
};
log.info(`Broadcasting fluxnodesigterm message: ${JSON.stringify(sigtermMessage)}`);
await fluxCommunicationMessagesSender.broadcastMessageToOutgoing(sigtermMessage);
await serviceHelper.delay(500);
await fluxCommunicationMessagesSender.broadcastMessageToIncoming(sigtermMessage);
// Update local DB to expire app location records in ~7 minutes,
// same manipulation that peers apply when receiving the sigterm.
// This keeps the local DB consistent with the network so that on
// reboot the TTL check correctly detects expired locations.
try {
const db = dbHelper.databaseConnection();
const database = db.db(config.database.appsglobal.database);
const globalAppsLocations = config.database.appsglobal.collections.appsLocations;
const newBroadcastedAt = new Date(sigtermMessage.broadcastedAt - (7500 - 420) * 1000);
const newExpireAt = new Date(sigtermMessage.broadcastedAt + (420 * 1000));
const update = { $set: { broadcastedAt: newBroadcastedAt, expireAt: newExpireAt } };
await dbHelper.updateInDatabase(database, globalAppsLocations, { ip }, update);
log.info('Local app location records updated to expire in ~7 minutes');
} catch (dbError) {
log.warn(`Failed to update local app location expiration: ${dbError.message}`);
}
log.info('Shutdown notification broadcasted successfully');
} else {
log.warn('Could not get IP address, skipping shutdown broadcast');
}
} else {
log.info('No running apps cached, skipping shutdown broadcast');
}
} catch (error) {
log.error(`Error during SIGTERM handling: ${error.message}`);
}
// Gracefully stop all running Flux app containers
try {
let containers = await dockerService.dockerListContainers(false);
containers = containers || [];
containers = containers.filter((c) => c.Names[0].slice(1, 4) === 'zel' || c.Names[0].slice(1, 5) === 'flux');
if (containers.length > 0) {
log.info(`Gracefully stopping ${containers.length} Flux app containers...`);
// Fire all stop requests in parallel. Each sends SIGTERM and falls back
// to force-kill after 9 seconds. Promise.allSettled waits for every
// container to finish, so total shutdown time is ~9s (not N * 9s).
const stopPromises = containers.map((container) => {
const containerName = container.Names[0].slice(1);
return dockerService.appDockerStop(containerName, 9)
.then(() => {
log.info(`Container ${containerName} stopped`);
})
.catch(async (stopErr) => {
log.warn(`Graceful stop failed for ${containerName}: ${stopErr.message}, force killing...`);
try {
await dockerService.appDockerKill(containerName);
log.info(`Container ${containerName} force killed`);
} catch (killErr) {
log.warn(`Failed to kill container ${containerName}: ${killErr.message}`);
}
});
});
await Promise.allSettled(stopPromises);
log.info(`Shutdown stop completed for ${containers.length} Flux app containers`);
} else {
log.info('No running Flux app containers to stop');
}
} catch (error) {
log.error(`Error stopping containers during shutdown: ${error.message}`);
}
// Give some time for the broadcast to complete
await serviceHelper.delay(1000);
log.info('Graceful shutdown complete, exiting...');
process.exit(0);
}
// Register SIGTERM handler for graceful shutdown on system reboot/shutdown
process.on('SIGTERM', handleSigterm);
if (require.main === module) {
initiate();
}
module.exports = {
createDnsCache,
getCacheable,
getrequestHistory,
handleSigterm,
initiate,
isSystemShuttingDown,
resetCacheable,
};