forked from barkinet/zigbang-email-verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (134 loc) · 4.63 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
'use strict';
const dns = require('dns');
const P = require('bluebird');
const _ = require('lodash');
const randomstring = require('randomstring');
const netsend = require('./lib/netsend');
class VerifyError extends Error {
constructor ( message, extra ) {
super();
Error.captureStackTrace( this, this.constructor );
this.name = 'CustomError';
this.message = message;
if( extra ) this.extra = extra;
}
}
P.config({cancellation: true});
const dnsResolveMx = P.promisify(dns.resolveMx, {context: dns});
module.exports = {
verify(opts) {
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; // eslint-disable-line
if(!emailRegex.test(opts.to)) {
return P.resolve('INVALID');
}
const emailSplited = opts.to.split('@');
const emailHost = emailSplited[1];
const debug = opts.debug ? (_.isFunction(opts.debug) ? opts.debug : console.info) : () => {};
const timeout = opts.timeout ? opts.timeout : 5000;
const generateRandomEmail = function() {
debug('Domain...', emailHost);
let radomString = randomstring.generate(32);
return radomString + '@' + emailHost;
};
return new P((resolve, reject) => {
const jobDnsResolveMx = dnsResolveMx(emailHost).then(results => {
if(_.isEmpty(results)) {
throw new VerifyError('', 'MXRECORD_FAIL');
}
return results;
}, () => {
throw new VerifyError('', 'MXRECORD_FAIL');
});
const jobNetConnect = jobDnsResolveMx.then(results => {
debug('RESOLVE MX RECORD');
const exchange = _(results).sortBy(v => v.priority).take(1).value()[0].exchange;
debug('\t' + exchange);
return netsend({port: 25, host: exchange}).catch(() => {
throw new VerifyError('', 'CONN_FAIL');
});
});
const jobVerify = jobNetConnect.then(netConn => {
debug('CONNECTED SMTP SERVER');
return netConn.response().then(resmsg => {
debug('\t' + resmsg[0]);
if(resmsg[0].substr(0, 3) !== '220') {
throw new VerifyError('', 'VERIFY_FAIL');
}
const writeMsg = 'HELO ' + opts.helo;
debug(writeMsg);
netConn.write(writeMsg);
return netConn.response();
}).then(resmsg => {
debug('\t' + resmsg[0]);
if(resmsg[0].substr(0, 3) !== '250') {
throw new VerifyError('', 'VERIFY_FAIL');
}
const writeMsg = `MAIL FROM: <${opts.from}>`;
debug(writeMsg);
netConn.write(writeMsg);
return netConn.response();
}).then(resmsg => {
debug('\t' + resmsg[0]);
if(resmsg[0].substr(0, 3) !== '250') {
throw new VerifyError('', 'VERIFY_FAIL');
}
const writeMsg = `RCPT TO: <${opts.to}>`;
debug(writeMsg);
netConn.write(writeMsg);
return netConn.response();
}).then(resmsg => {
debug('\t' + resmsg[0]);
if(resmsg[0].substr(0, 3) === '250') {
if(opts.catchalltest === true) {
debug('MAILBOX EXIST..CHECKING FOR CATCHALL');
let randomUser = generateRandomEmail();
debug('RANDOM USER: ', randomUser);
const writeMsg = `RCPT TO: <${randomUser}>`;
debug(writeMsg);
netConn.write(writeMsg);
return netConn.response().then(resmsg => {
if(resmsg[0].substr(0, 3) === '250') {
return 'CATCH_ALL';
} else {
return 'EXIST';
}
});
} else {
return 'EXIST';
}
} else {
return 'NOT_EXIST';
}
}).finally(() => {
netConn.end();
});
});
const mainJob = jobVerify.then(results => {
resolve(results);
}).catch(VerifyError, (err) => {
resolve(err.extra);
}).catch((err) => {
debug(err);
resolve('UNKNOWN');
});
const mainJobTimeout = setTimeout(() => {
mainJob.cancel();
if(jobDnsResolveMx.isPending()) {
return resolve('MXRECORD_TIMEOUT');
}
if(jobNetConnect.isPending()) {
return resolve('CONN_TIMEOUT');
}
if(jobVerify.isPending()) {
return resolve('VERIFY_TIMEOUT');
}
return resolve('UNKNOWN');
}, timeout);
mainJob.finally(() => {
if(!mainJob.isCancelled()) {
clearTimeout(mainJobTimeout);
}
});
});
}
};