62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
const http = require('http');
|
|
|
|
function post(path, data) {
|
|
return new Promise((resolve, reject) => {
|
|
const payload = JSON.stringify(data);
|
|
const req = http.request({
|
|
hostname: 'localhost', port: 8082, path,
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(payload) },
|
|
}, (res) => {
|
|
let body = '';
|
|
res.on('data', c => body += c);
|
|
res.on('end', () => { try { resolve(JSON.parse(body)); } catch { resolve(body); } });
|
|
});
|
|
req.on('error', reject);
|
|
req.write(payload);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
console.log('\n === Full Alert Test (with Telegram) ===\n');
|
|
|
|
// Delete old rule if exists
|
|
try { await post('/api/monitor/rule/telegram-test', {}); } catch {}
|
|
|
|
// Create rule with Telegram notification
|
|
console.log(' Creating rule with Telegram notification...');
|
|
const res = await post('/api/monitor/rules', {
|
|
id: 'telegram-test',
|
|
name: 'Security Alert (Telegram)',
|
|
enabled: true,
|
|
sources: ['sample-logs/security-audit.log'],
|
|
patterns: ['root.*login', 'DROP TABLE', 'GRANT.*PRIVILEGES'],
|
|
patternLogic: 'any',
|
|
timeRange: { type: 'all' },
|
|
notifications: [{
|
|
type: 'telegram',
|
|
botToken: '8116790669:AAE_llOFDFSwDhPx40vZMBa6dGv3Xxa9ZiQ',
|
|
chatId: '8599028500',
|
|
}],
|
|
maxLinesInMessage: 5,
|
|
cooldownMs: 10000,
|
|
matchCount: 0,
|
|
lastTriggered: null,
|
|
schedule: { mode: 'interval' },
|
|
});
|
|
console.log(' Rule created:', res.ok ? 'OK' : 'FAILED');
|
|
|
|
// Trigger it
|
|
console.log(' Triggering rule (this will send Telegram alert)...');
|
|
const result = await post('/api/monitor/trigger/telegram-test', {});
|
|
if (result && result.totalMatches) {
|
|
console.log(` ✅ ${result.totalMatches} matches found and Telegram alert sent!`);
|
|
console.log(' Check your Telegram for the alert message.\n');
|
|
} else {
|
|
console.log(' No matches (unexpected).\n');
|
|
}
|
|
}
|
|
|
|
main().catch(e => console.error('Error:', e.message));
|