2024-03-21 18:54:01 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2024-03-30 19:17:04 +01:00
|
|
|
exports = module.exports = {
|
|
|
|
|
start,
|
|
|
|
|
stop
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 18:54:01 +01:00
|
|
|
const debug = require('debug')('syslog:server'),
|
|
|
|
|
fs = require('fs'),
|
|
|
|
|
net = require('net'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
paths = require('./src/paths.js'),
|
|
|
|
|
util = require('util');
|
|
|
|
|
|
|
|
|
|
let gServer = null;
|
|
|
|
|
|
2024-09-10 12:59:42 +02:00
|
|
|
// https://docs.docker.com/engine/logging/drivers/syslog/
|
|
|
|
|
// example: <34>1 2023-09-07T14:33:22Z myhost myapp 1234 5678 [exampleSDID@32473 iut="3" eventSource="Application"] An example message
|
|
|
|
|
function parseRFC5424Message(message) {
|
|
|
|
|
const syslogRegex = /^<(\d+)>(\d+) (\S+) (\S+) (\S+) (\S+) (\S+) (?:\[(.*?)\])?(.*)$/;
|
|
|
|
|
|
|
|
|
|
const match = message.match(syslogRegex);
|
|
|
|
|
if (!match) return null;
|
|
|
|
|
|
|
|
|
|
const [, pri, version, timestamp, hostname, appName, procId, msgId, structuredData, msg] = match;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
pri: parseInt(pri, 10), // priority
|
|
|
|
|
version: parseInt(version, 10), // version
|
|
|
|
|
timestamp, // timestamp
|
|
|
|
|
hostname, // hostname
|
|
|
|
|
appName, // app name
|
|
|
|
|
procId, // process ID
|
|
|
|
|
msgId, // message ID
|
|
|
|
|
structuredData: structuredData ? structuredData : null, // structured data (if present)
|
|
|
|
|
msg: msg ? msg.trim() : null // message
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
socket.on('data', function (msg) {
|
2024-05-15 13:17:02 +02:00
|
|
|
const lines = msg.toString().split('\n'); // may be multiline data
|
2024-03-21 18:54:01 +01:00
|
|
|
|
2024-09-10 12:59:42 +02:00
|
|
|
for (const msg of lines) {
|
|
|
|
|
const info = parseRFC5424Message(msg);
|
2024-05-15 13:17:02 +02:00
|
|
|
if (!info || !info.appName) return debug('Ignore unknown app log:', msg);
|
2024-03-21 18:54:01 +01:00
|
|
|
|
2024-05-15 13:17:02 +02:00
|
|
|
// remove line breaks to avoid holes in the log file
|
|
|
|
|
// we do not ignore empty log lines, to allow gaps for potential ease of readability
|
2024-05-21 16:48:29 +02:00
|
|
|
const message = info.message.replace(/[\n\r]+/g, '');
|
2024-05-15 13:17:02 +02:00
|
|
|
|
|
|
|
|
const appLogDir = path.join(paths.LOG_DIR, info.appName);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(appLogDir, { recursive: true });
|
2024-09-10 12:59:42 +02:00
|
|
|
fs.appendFileSync(`${appLogDir}/app.log`, `${info.timestamp} ${message}\n}`);
|
2024-05-15 13:17:02 +02:00
|
|
|
} catch (error) {
|
2024-09-10 12:59:42 +02:00
|
|
|
debug(error);
|
2024-05-15 13:17:02 +02:00
|
|
|
}
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 19:17:04 +01:00
|
|
|
if (require.main === module) {
|
|
|
|
|
main();
|
|
|
|
|
}
|