install.fairie/system/usr/local/etc/clamav/executable_clamav-email
Brian Zalewski 2731e1121b Latest
2024-05-28 03:45:14 +00:00

128 lines
No EOL
3.4 KiB
Text

#!/usr/bin/env zx
import os from 'os';
import dotenv from 'dotenv';
import fs from 'fs';
import sgMail from '@sendgrid/mail';
// Load environment variables from .env file
dotenv.config();
// Email configuration from environment variables
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
const EMAIL_FROM = process.env.EMAIL_FROM;
const EMAIL_TO = process.env.EMAIL_TO;
const EMAIL_SUBJECT = 'ClamAV Virus Scan Alert';
// Configure SendGrid
sgMail.setApiKey(SENDGRID_API_KEY);
// Logger function
function log(message) {
fs.appendFileSync(`/var/log/clamav/detected.log`, `${new Date().toISOString()} - ${message}\n`);
}
// Function to send email
async function sendEmail(subject, body) {
const msg = {
to: EMAIL_TO,
from: EMAIL_FROM,
subject: subject,
html: body,
};
try {
await sgMail.send(msg);
log('Email sent successfully');
} catch (error) {
log(`Failed to send email: ${error.message}`);
throw new Error('Failed to send email');
}
}
(async () => {
try {
// Extract scan details from environment variables
const filePath = process.env.CLAMAV_FILE_PATH || 'Unknown file';
const virusName = process.env.CLAMAV_VIRUS_NAME || 'Unknown virus';
const scanDate = new Date().toISOString();
const hostname = os.hostname();
const username = process.env.USER || process.env.USERNAME || 'Unknown user';
// Create email body
const emailBody = `
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f6f6f6;
}
.container {
width: 100%;
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header {
background-color: #0073b7;
color: #ffffff;
padding: 10px 20px;
border-radius: 8px 8px 0 0;
}
.header h2 {
margin: 0;
}
.content {
padding: 20px;
}
.content p {
margin: 10px 0;
}
.footer {
padding: 10px 20px;
text-align: center;
font-size: 12px;
color: #777777;
border-top: 1px solid #dddddd;
margin-top: 20px;
}
pre {
background-color: #f6f6f6;
padding: 10px;
border-radius: 4px;
overflow: auto;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h2>ClamAV Virus Scan Alert</h2>
</div>
<div class="content">
<p><strong>Date:</strong> ${scanDate}</p>
<p><strong>File Path:</strong> ${filePath}</p>
<p><strong>Virus Name:</strong> ${virusName}</p>
<p><strong>Hostname:</strong> ${hostname}</p>
<p><strong>User:</strong> ${username}</p>
</div>
<div class="footer">
<p>Generated by ClamAV Virus Scan System</p>
</div>
</div>
</body>
</html>
`;
// Send email
await sendEmail(EMAIL_SUBJECT, emailBody);
} catch (error) {
log(`Script execution failed: ${error.message}`);
}
})();