-
Notifications
You must be signed in to change notification settings - Fork 3
/
mdncheck.js
159 lines (147 loc) · 6.82 KB
/
mdncheck.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var fs = require('fs');
var path = require('path');
var async = require('async');
var nm = require('nodemailer');
var config = require('./config.js');
var MailComposer = require('nodemailer/lib/mail-composer');
var xdrDisposition = require('./lib/xdrDisposition.js');
var logger = require('./logger.js');
var utils = require('./lib/utils.js');
var xdrConnector = require('./lib/xdrConnector.js');
var timeout = config.mdnTimeout;
var smtp = nm.createTransport({
host: config.smtpTargetServer.host,
port: config.smtpTargetServer.port,
secure: false,
authOptional: true,
tls: { rejectUnauthorized: false },
ignoreTLS: true
});
var doneFolder = config.logging.mdnFolder + 'done';
var processedFolder = config.logging.mdnFolder + 'processed';
var newFolder = config.logging.mdnFolder + 'new';
function buildDSN(dnTo, originalRecipient, originalMessageId, originalMessage, callback){
var domain = utils.getDomain(originalRecipient);
var mail = new MailComposer({
headers: [{contentType: 'multipart/report; report-type=delivery-status'}],
from: 'MAILER-DAEMON@' + domain,
to: dnTo,
subject: 'Undelivered Mail Returned to Sender',
text: 'This is the mail system at host ' + domain + '.\r\n\r\n' +
'I\'m sorry to have to inform you that your message could not\r\n' +
'be delivered to one or more recipients. It\'s attached below.\r\n\r\n' +
'For further assistance, please send mail to postmaster.\r\n' +
'If you do so, please include this problem report. You can\r\n' +
'delete your own text from the attached returned message.\r\n',
attachments: [{
contentType: 'message/disposition-notification',
contentTransferEncoding: '7bit',
raw: 'Content-Type: message/delivery-status\r\n' +
'Content-Description: Delivery report\r\n' +
'Content-Transfer-Encoding: 7bit\r\n' +
'Content-Disposition: inline\r\n\r\n' +
'Final-Recipient: rfc822;' + originalRecipient + '\r\n' +
'Original-Recipient: rfc822;' + originalRecipient + '\r\n' +
'Original-Message-ID: ' + originalMessageId + '\r\n' +
'Action: failed\r\n' +
'Status: 5.0.0\r\n' +
'Remote-MTA: dns; localhost\r\n' +
'Diagnostic-Code: smtp; 554 Transaction failed\r\n'
},{
contentType: 'message/rfc822',
headers: [{'Content-Description': 'Undelivered Message'}],
content: originalMessage
}]
});
mail.compile().build(callback);
}
function sendXDR(dnTo, originalRecipient, originalMessageId, callback) {
var message = xdrDisposition.createMessageDisposition('failure', dnTo, originalRecipient, originalMessageId);
var dest = utils.getDestinationProperties(dnTo);
xdrConnector.sendMessage(message, dest, logger, function (err) {
if (err) {
logger.error('Error while sending message using XDR: ' + err.toString());
return callback(err);
}
logger.info('Message delivered to XDR endpoint');
return callback();
});
}
function scan(dir) {
var now = new Date().getTime();
fs.readdir(dir, function(err, files) {
if(err) {
logger.debug(err);
return;
}
async.each(files, function(file, next) {
var filePath = dir + '/' + file;
fs.stat(filePath, function(err, stat) {
if (err) {
return next(err);
}
if(!stat.isFile() || new Date(stat.ctime).getTime() + timeout > now) {
return next();
}
var parts = file.split('+');
var originalMessageId = parts[0];
var dnTo = parts[1]; //original sender
var originalRecipient = parts[2]; //original recipient (unreachable/unresponsive)
var dest = utils.getDestinationProperties(dnTo);
logger.debug('DSN: ' + file);
if (dest && dest.type === 'xdr') {
sendXDR(dnTo, originalRecipient, originalMessageId, function(xdrErr) {
if(xdrErr) {
logger.debug(xdrErr);
return next();
}
fs.rename(filePath, path.join(doneFolder, file), function(renameErr){
if(renameErr){
logger.debug(renameErr);
}
return next();
});
});
} else {
fs.readFile(dir + '/' + file, function(err, msg){
if(err) {
logger.debug(err);
return next();
}
buildDSN(dnTo, originalRecipient, originalMessageId, msg, function(err1, message){
if(err1) {
logger.debug(err1);
return next();
}
smtp.sendMail({
envelope: {
from: 'MAILER-DAEMON@' + utils.getDomain(originalRecipient),
to: dnTo
},
raw: message.toString().replace('multipart/mixed;', 'multipart/report; report-type=delivery-status;\r\n')
}, function (error) {
if (error) {
logger.debug(error);
return next();
}
fs.rename(filePath, path.join(doneFolder, file), function(err2){
if(err2){
logger.debug(err2);
}
return next();
});
});
});
});
}
});
}, function(err3) {
console.log(dir + ' - all files checked');
if(err3) {
logger.debug(err3);
}
}
);
});
}
setInterval(function() {async.each([newFolder, processedFolder], scan, function(){})}, 15000);