-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
249 lines (221 loc) · 7.3 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
module.exports.find = function (options, mainCallback) {
var firstName = options.firstName;
var lastName = options.lastName;
var domainName = options.domainName;
var possibleEmails = options.possibleEmails;
var catchAll = options.catchAll;
// Find domainName
if (possibleEmails.length) {
domainName = possibleEmails[0].split('@')[1];
}
// Handling options
if ((!firstName || !lastName || !domainName) && !possibleEmails) {
throw new Error("Missing parameters in smtp_checker.find()");
}
else if (typeof mainCallback === 'undefined' && options) {
mainCallback = options;
options = {};
}
// Default Values
if (options && !options.sender) options.sender = '[email protected]';
if (options && !options.fqdn) options.fqdn = 'mail.example.org';
if (options && !options.timeout) options.timeout = 0;
if (options && !options.port) options.port = 25;
if (options && !options.verbose) options.verbose = false;
var async = require('async');
var responseLog = '';
async.waterfall(
[
getPossibleEmails(firstName, lastName, domainName, possibleEmails),
getSMTPAddress,
verifyEmail
],
function(error, info) {
mainCallback(error, info);
}
);
function logging(value) {
responseLog += value.toString();
if (!options.verbose) return;
console.log(value.toString());
}
function getPossibleEmails(firstName, lastName, domainName, givenPossibleEmails) {
return function(callback) {
var results = []
if (givenPossibleEmails.length > 0) {
results = givenPossibleEmails;
} else {
var fs = require('fs');
var format = require('string-template');
fs.readFile(__dirname + '/patterns.txt', function (error, data) {
if (error) { return callback(error); }
var content = data.toString().split('\n');
var binding = {
firstInitial: firstName[0],
lastInitial: lastName[0],
firstName: firstName,
lastName: lastName,
domainName: domainName
};
content.forEach(function(pattern) {
var result = format(pattern, binding);
if (results.indexOf(result) == -1) {
results.push(result);
}
});
});
}
if (catchAll) {
results.unshift('serverisprobablycatchall@' + domainName);
}
callback(null, results);
};
}
// Get the lowest priority MX Records to find the SMTP server
function getSMTPAddress(possibleEmails, callback) {
var dns = require('dns');
if(options.dns) {
try {
if(Array.isArray(options.dns)) {
dns.setServers(options.dns);
} else {
dns.setServers([options.dns]);
}
}
catch(e) {
throw new Error("[smtp_checker.js]: Invalid DNS Options");
}
}
dns.resolveMx(domainName, function(error, addresses) {
if (error || (typeof addresses === 'undefined')) {
return callback(error);
} else if (addresses && addresses.length <= 0) {
return callback('No MX Records for this domain' + domainName);
}
else {
var index = 0;
var priority = 10000;
for (var i = 0; i < addresses.length; i++) {
if (addresses[i].priority < priority) {
priority = addresses[i].priority;
index = i;
}
}
callback(null, possibleEmails, addresses[index].exchange);
}
});
}
function verifyEmail(possibleEmails, smtp_address, callback) {
var net = require('net');
var format = require('string-template');
var socket = net.createConnection(options.port, smtp_address);
var response = '';
var completed = false;
var ended = false;
var stage = 0;
var isCatchAll = null;
var foundEmails = [];
var maybeEmails = [];
var index = 0;
var command = '';
var commands = [
'EHLO {0}\r\n',
'MAIL FROM:<{0}>\r\n',
'RCPT TO:<{0}>\r\n',
'QUIT\r\n'
]
socket.on('data', function(data) {
response += data.toString();
completed = response.slice(-1) === '\n';
logging(response.trim());
if (completed) {
switch(stage) {
case 0: // EHLO
if (response.indexOf('220') > -1 && !ended) {
command = format(commands[stage], options.fqdn);
socket.write(command, function() {
stage += 1;
response = '';
logging(command);
});
} else {
socket.end();
}
break;
case 1: // MAIL FROM
if (response.indexOf('250') > -1 && !ended) {
command = format(commands[stage], options.sender);
socket.write(command, function() {
stage += 1;
response = '';
logging(command);
});
} else {
socket.end();
}
break;
case 2: // RCPT TO
if (response.indexOf('250') > -1 && !ended) {
command = format(commands[stage], possibleEmails[index]);
socket.write(command, function() {
stage += 1;
response = '';
logging('\r\n' + command);
});
} else {
socket.end();
}
break;
case 3: // RCPT Result
if (index === 0 && catchAll) {
if (response.indexOf('550') > -1 ||
response.indexOf('551') > -1 ||
response.indexOf('552') > -1 ||
response.indexOf('553') > -1 ||
response.indexOf('554') > -1) {
isCatchAll = false;
} else if (response.indexOf('250') > -1) {
isCatchAll = true;
}
}
else if (response.indexOf('250') > -1) { // Verified
foundEmails.push(possibleEmails[index]);
}
// Verify another
response = '';
index += 1;
if (index < possibleEmails.length && !isCatchAll) {
command = format(commands[2], possibleEmails[index]);
socket.write(command, function() {
response = '';
logging('\r\n' + command);
});
} else if(!ended) {
command = commands[commands.length-1]; // QUIT
stage = 4;
socket.write(command);
logging(command);
}
break;
case 4: // QUIT
socket.end();
}
}
}).on('connect', function(data) {
}).on('error', function(error) {
ended = true;
var result = { found: false, catchAll: isCatchAll, emails: foundEmails, response: responseLog };
if (catchAll) {
result.catchAll = isCatchAll;
}
return callback(error, result);
}).on('end', function() {
ended = true;
var result = { found: (foundEmails.length > 0) ? true : false, emails: foundEmails, response: responseLog };
if (catchAll) {
result.catchAll = isCatchAll;
}
return callback(null, result);
});
}
}