-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyslog_udp_server.ts
More file actions
44 lines (40 loc) · 1.41 KB
/
syslog_udp_server.ts
File metadata and controls
44 lines (40 loc) · 1.41 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
// syslog_udp_server.ts
import { parseArgs } from "jsr:@std/cli/parse-args";
const args = parseArgs(Deno.args);
// user-args
const PORT = Number(args.port) || 514;
const LOG_DIR = args.log || "./log";
const consolelog: Boolean = args.console==="false"?false:true;
const onlysave = args._;
// create log folder
await Deno.mkdir(LOG_DIR, { recursive: false }).catch((err)=>{});
const socket = Deno.listenDatagram({
port: PORT,
transport: "udp",
hostname: "0.0.0.0",
});
console.log(`✅ Syslog UDP server is listening on port ${PORT}...`);
function parsePri(msg: string): number | null {
const priMatch = msg.match(/^<(\d+)>/);
if (!priMatch) return null;
return parseInt(priMatch[1]);
}
for await (const [msg, addr] of socket) {
const message = new TextDecoder().decode(msg).trim();
const now = new Date();
const dateStr = now.toISOString().split("T")[0];
//const timeStr = now.toTimeString().split(" ")[0];
const logLine = `${message}\n`;
const Pri = parsePri(message)
const logFilePath = `${LOG_DIR}/${dateStr}.txt`;
if(onlysave.length === 0 || (onlysave.length !== 0 && Pri !== null && onlysave.includes(Pri) === true) || onlysave.length !== 0 && Pri === null){
await Deno.writeTextFile(logFilePath, logLine, { append: true });
if(consolelog === true){
if ("hostname" in addr) {
console.log(`📥 ${addr.hostname} ➜ ${message}`);
} else {
console.log(`📥 [unix socket] ➜ ${message}`);
}
}
}
}