2024-03-21 18:54:01 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
Migrate codebase from CommonJS to ES Modules
- Convert all require()/module.exports to import/export across 260+ files
- Add "type": "module" to package.json to enable ESM by default
- Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible
- Convert eslint.config.js to ESM with sourceType: "module"
- Replace __dirname/__filename with import.meta.dirname/import.meta.filename
- Replace require.main === module with process.argv[1] === import.meta.filename
- Remove 'use strict' directives (implicit in ESM)
- Convert dynamic require() in switch statements to static import lookup maps
(dns.js, domains.js, backupformats.js, backupsites.js, network.js)
- Extract self-referencing exports.CONSTANT patterns into standalone const
declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.)
- Lazify SERVICES object in services.js to avoid circular dependency TDZ issues
- Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests
- Add _setMockApp() to ldapserver.js for ESM-safe test mocking
- Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests
- Convert backupupload.js to use dynamic imports so --check exits before
loading the module graph (which requires BOX_ENV)
- Update check-install to use ESM import for infra_version.js
- Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations)
- All 1315 tests passing
Migration stats (AI-assisted using Cursor with Claude):
- Wall clock time: ~3-4 hours
- Assistant completions: ~80-100
- Estimated token usage: ~1-2M tokens
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
|
|
|
import debugModule from 'debug';
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import net from 'node:net';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import paths from './src/paths.js';
|
|
|
|
|
import util from 'node:util';
|
2024-03-21 18:54:01 +01:00
|
|
|
|
Migrate codebase from CommonJS to ES Modules
- Convert all require()/module.exports to import/export across 260+ files
- Add "type": "module" to package.json to enable ESM by default
- Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible
- Convert eslint.config.js to ESM with sourceType: "module"
- Replace __dirname/__filename with import.meta.dirname/import.meta.filename
- Replace require.main === module with process.argv[1] === import.meta.filename
- Remove 'use strict' directives (implicit in ESM)
- Convert dynamic require() in switch statements to static import lookup maps
(dns.js, domains.js, backupformats.js, backupsites.js, network.js)
- Extract self-referencing exports.CONSTANT patterns into standalone const
declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.)
- Lazify SERVICES object in services.js to avoid circular dependency TDZ issues
- Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests
- Add _setMockApp() to ldapserver.js for ESM-safe test mocking
- Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests
- Convert backupupload.js to use dynamic imports so --check exits before
loading the module graph (which requires BOX_ENV)
- Update check-install to use ESM import for infra_version.js
- Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations)
- All 1315 tests passing
Migration stats (AI-assisted using Cursor with Claude):
- Wall clock time: ~3-4 hours
- Assistant completions: ~80-100
- Estimated token usage: ~1-2M tokens
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
|
|
|
const debug = debugModule('syslog:server');
|
|
|
|
|
|
2024-03-30 19:17:04 +01:00
|
|
|
|
2024-03-21 18:54:01 +01:00
|
|
|
let gServer = null;
|
|
|
|
|
|
2024-09-10 12:59:42 +02:00
|
|
|
// https://docs.docker.com/engine/logging/drivers/syslog/
|
2024-10-14 13:54:27 +02:00
|
|
|
// example: <34>1 2023-09-07T14:33:22Z myhost myapp pid msgid [exampleSDID@32473 iut="3" eventSource="Application"] An example message
|
|
|
|
|
// the structured data can be "-" when missing
|
2024-09-10 13:46:13 +02:00
|
|
|
function parseRFC5424Message(rawMessage) {
|
2025-09-15 13:33:41 +02:00
|
|
|
const syslogRegex = /^<(?<priority>\d+)>(?<version>\d+) (?<timestamp>\S+) (?<hostname>\S+) (?<appName>\S+) (?<procId>\S+) (?<msgId>\S+) (?:\[(?<structuredData>.*?)\]|-)(?<message>.*)$/s; // /s means .* will match newline . (?: is non-capturing group
|
2024-09-10 12:59:42 +02:00
|
|
|
|
2024-09-10 13:46:13 +02:00
|
|
|
const match = rawMessage.match(syslogRegex);
|
2024-09-10 12:59:42 +02:00
|
|
|
if (!match) return null;
|
|
|
|
|
|
2025-09-15 13:33:41 +02:00
|
|
|
const { priority, version, timestamp, hostname, appName, procId, msgId, structuredData, message } = match.groups;
|
2024-09-10 12:59:42 +02:00
|
|
|
|
|
|
|
|
return {
|
2025-09-15 14:01:47 +02:00
|
|
|
pri: parseInt(priority, 10), // priority
|
2024-09-10 12:59:42 +02:00
|
|
|
version: parseInt(version, 10), // version
|
|
|
|
|
timestamp, // timestamp
|
|
|
|
|
hostname, // hostname
|
|
|
|
|
appName, // app name
|
|
|
|
|
procId, // process ID
|
|
|
|
|
msgId, // message ID
|
2025-09-15 13:33:41 +02:00
|
|
|
structuredData: structuredData || null, // structured data (if present)
|
2024-09-10 13:46:13 +02:00
|
|
|
message // message
|
2024-09-10 12:59:42 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 18:54:01 +01:00
|
|
|
async function start() {
|
|
|
|
|
debug('==========================================');
|
|
|
|
|
debug(' Cloudron Syslog Daemon ');
|
|
|
|
|
debug('==========================================');
|
|
|
|
|
|
|
|
|
|
gServer = net.createServer();
|
|
|
|
|
|
|
|
|
|
gServer.on('error', function (error) {
|
|
|
|
|
console.error(`server error: ${error}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gServer.on('connection', function (socket) {
|
2024-09-10 13:46:13 +02:00
|
|
|
socket.on('data', function (data) {
|
2025-09-15 14:01:47 +02:00
|
|
|
const msg = data.toString('utf8').trim(); // strip any trailing empty new lines. it's unclear why we get it in the first place
|
2024-10-14 13:54:27 +02:00
|
|
|
for (const line of msg.split('\n')) { // empirically, multiple messages can arrive in a single packet
|
|
|
|
|
const info = parseRFC5424Message(line);
|
2025-09-15 13:33:41 +02:00
|
|
|
if (!info) return debug(`Unable to parse: [${msg}]`);
|
|
|
|
|
if (!info.appName) return debug(`Ignore unknown app: [${msg}]`);
|
2024-10-14 13:54:27 +02:00
|
|
|
|
|
|
|
|
const appLogDir = path.join(paths.LOG_DIR, info.appName);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(appLogDir, { recursive: true });
|
|
|
|
|
fs.appendFileSync(`${appLogDir}/app.log`, `${info.timestamp} ${info.message.trim()}\n`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
debug(error);
|
|
|
|
|
}
|
2024-09-10 12:59:42 +02:00
|
|
|
}
|
2024-03-21 18:54:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('error', function (error) {
|
2024-09-10 12:59:42 +02:00
|
|
|
debug(`socket error: ${error}`);
|
2024-03-21 18:54:01 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await fs.promises.rm(paths.SYSLOG_SOCKET_FILE, { force: true });
|
|
|
|
|
await util.promisify(gServer.listen.bind(gServer))(paths.SYSLOG_SOCKET_FILE);
|
|
|
|
|
|
|
|
|
|
debug(`Listening on ${paths.SYSLOG_SOCKET_FILE}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function stop() {
|
|
|
|
|
await fs.promises.rm(paths.SYSLOG_SOCKET_FILE, { force: true });
|
|
|
|
|
gServer.unref(); // TODO : cleanup client connections. otherwise server.close() won't return
|
|
|
|
|
gServer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
await start();
|
|
|
|
|
|
|
|
|
|
process.on('SIGTERM', async function () {
|
|
|
|
|
debug('Received SIGTERM. Shutting down.');
|
|
|
|
|
await stop();
|
|
|
|
|
setTimeout(process.exit.bind(process), 1000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
Migrate codebase from CommonJS to ES Modules
- Convert all require()/module.exports to import/export across 260+ files
- Add "type": "module" to package.json to enable ESM by default
- Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible
- Convert eslint.config.js to ESM with sourceType: "module"
- Replace __dirname/__filename with import.meta.dirname/import.meta.filename
- Replace require.main === module with process.argv[1] === import.meta.filename
- Remove 'use strict' directives (implicit in ESM)
- Convert dynamic require() in switch statements to static import lookup maps
(dns.js, domains.js, backupformats.js, backupsites.js, network.js)
- Extract self-referencing exports.CONSTANT patterns into standalone const
declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.)
- Lazify SERVICES object in services.js to avoid circular dependency TDZ issues
- Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests
- Add _setMockApp() to ldapserver.js for ESM-safe test mocking
- Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests
- Convert backupupload.js to use dynamic imports so --check exits before
loading the module graph (which requires BOX_ENV)
- Update check-install to use ESM import for infra_version.js
- Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations)
- All 1315 tests passing
Migration stats (AI-assisted using Cursor with Claude):
- Wall clock time: ~3-4 hours
- Assistant completions: ~80-100
- Estimated token usage: ~1-2M tokens
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
|
|
|
if (process.argv[1] === import.meta.filename) {
|
2024-03-30 19:17:04 +01:00
|
|
|
main();
|
|
|
|
|
}
|
2026-02-14 15:43:24 +01:00
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
start,
|
|
|
|
|
stop
|
|
|
|
|
};
|