docs: finalize installation instructions and infrastructure for overhauled worker
This commit is contained in:
@@ -1,35 +1,65 @@
|
||||
import { Redis } from 'ioredis';
|
||||
import { spawn } from 'child_process';
|
||||
import dotenv from 'dotenv';
|
||||
require('dotenv').config();
|
||||
const chokidar = require('chokidar');
|
||||
const axios = require('axios');
|
||||
const { exec } = require('child_process');
|
||||
|
||||
dotenv.config();
|
||||
const NEXTJS_WEBHOOK_URL = process.env.NEXTJS_WEBHOOK_URL;
|
||||
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
|
||||
const vmailPath = '/var/lib/docker/volumes/mailcowdockerized_vmail-vol-1/_data/';
|
||||
|
||||
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
|
||||
const CONTAINER_NAME = process.env.MAILCOW_CONTAINER_NAME || 'mailcowdockerized-dovecot-mailcow-1';
|
||||
console.log("🚀 AyrisTech Güvenli Dekoder (V10.3) Başlatıldı...");
|
||||
|
||||
const redis = new Redis(REDIS_URL);
|
||||
const watcher = chokidar.watch(vmailPath, {
|
||||
ignored: /(^|[\/\\])\../,
|
||||
persistent: true,
|
||||
ignoreInitial: true,
|
||||
depth: 5
|
||||
});
|
||||
|
||||
console.log("🚀 AyrisTech Worker Bağlanıyor...");
|
||||
watcher.on('add', (filePath) => {
|
||||
if (filePath.includes('/new/')) {
|
||||
const parts = filePath.split('/');
|
||||
const fullEmail = `${parts[8]}@${parts[7]}`;
|
||||
|
||||
// Dovecot loglarını dinliyoruz
|
||||
const logProcess = spawn('docker', ['logs', '-f', CONTAINER_NAME]);
|
||||
console.log(`📩 Yeni şifreli mail: ${fullEmail}. Çözülüyor...`);
|
||||
|
||||
logProcess.stdout.on('data', async (data) => {
|
||||
const line = data.toString();
|
||||
// Komutu daha garantici bir hale getirdik
|
||||
const cmd = `docker exec mailcowdockerized-dovecot-mailcow-1 doveadm fetch -u "${fullEmail}" "hdr.from hdr.subject body.snippet" ALL`;
|
||||
|
||||
// Mailcow'un mail teslimat logunu yakala
|
||||
if (line.includes('saved mail to INBOX')) {
|
||||
const match = line.match(/user=<([^>]+)>/);
|
||||
if (match) {
|
||||
const email = match[1];
|
||||
console.log(`📩 Mail Geldi: ${email}`);
|
||||
setTimeout(() => {
|
||||
exec(cmd, async (error, stdout, stderr) => {
|
||||
if (error || stderr) {
|
||||
console.error("❌ Doveadm Hatası:", error ? error.message : stderr);
|
||||
return;
|
||||
}
|
||||
|
||||
// Olayı Redis'e fırlat (Pub/Sub)
|
||||
// Bu sayede Next.js veya diğer servisler bu haberi alabilir
|
||||
await redis.publish('NEW_MAIL_EVENT', JSON.stringify({
|
||||
to: email,
|
||||
time: new Date().toISOString()
|
||||
}));
|
||||
}
|
||||
if (!stdout.trim()) {
|
||||
console.log("⚠️ Mail içeriği henüz hazır değil (Stdout boş).");
|
||||
return;
|
||||
}
|
||||
|
||||
let mailData = { to: fullEmail, from: 'Bilinmiyor', subject: 'Konu Yok', snippet: '' };
|
||||
|
||||
// Satır satır parse etme
|
||||
const lines = stdout.split('\n');
|
||||
lines.forEach(line => {
|
||||
const l = line.trim();
|
||||
if (l.toLowerCase().startsWith('hdr.from:')) mailData.from = l.split(':').slice(1).join(':').trim();
|
||||
if (l.toLowerCase().startsWith('hdr.subject:')) mailData.subject = l.split(':').slice(1).join(':').trim();
|
||||
if (l.toLowerCase().startsWith('body.snippet:')) mailData.snippet = l.split(':').slice(1).join(':').trim();
|
||||
});
|
||||
|
||||
console.log(`✨ İçerik Çözüldü: ${mailData.subject}`);
|
||||
|
||||
try {
|
||||
await axios.post(NEXTJS_WEBHOOK_URL, mailData, {
|
||||
headers: { 'x-ayristech-secret': WEBHOOK_SECRET }
|
||||
});
|
||||
console.log(`✅ Webhook Next.js'e başarıyla ulaştı.`);
|
||||
} catch (err) {
|
||||
console.error("❌ Webhook Hatası:", err.message);
|
||||
}
|
||||
});
|
||||
}, 2500); // Mailin DB'ye tam işlenmesi için süreyi biraz artırdık
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user