-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
168 lines (134 loc) · 4.25 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
'use strict';
const dns = require('dns'),
net = require('net'),
logger = require('./logger');
const ninvoke = (module, fn, ...args) => new Promise((resolve, reject) => {
module[fn](...args, (err, res) => {
if (err) return reject(err);
resolve(res);
});
});
/**
* @see http://www.serversmtp.com/en/smtp-error
* @param {String} smtpReply A response from the SMTP server.
* @return {Bool} True if the error is recognized as a mailbox
* missing error.
*/
const isInvalidMailboxError = smtpReply => {
if (
smtpReply &&
/^(510|511|513|550|551|553)/.test(smtpReply) &&
!/(junk|spam|openspf|spoofing|host|rbl.+blocked)/ig.test(smtpReply)
) return true;
return false;
};
/**
* @see https://www.ietf.org/mail-archive/web/ietf-smtp/current/msg06344.html
* @param {String} smtpReply A message from the SMTP server.
* @return {Boolean} True if this is a multiline greet.
*/
const isMultilineGreet = smtpReply => {
return smtpReply && /^(250|220)-/.test(smtpReply);
};
class EmailValidator {
constructor(options = { }) {
this.options = Object.assign({
timeout: 10000,
verifyDomain: true,
verifyMailbox: true
}, options);
this.log = options.logger || logger;
}
async verify(address) {
const result = { wellFormed: false, validDomain: null, validMailbox: null };
let local;
let domain;
let mxRecords;
try {
[local, domain] = EmailValidator.extractAddressParts(address);
} catch (err) {
this.log.debug('Failed on wellFormed check', err);
return result;
}
result.wellFormed = true;
// save a DNS call
if (!this.options.verifyDomain && !this.options.verifyMailbox) return result;
try {
mxRecords = await EmailValidator.resolveMxRecords(domain);
this.log.debug('Found MX records', mxRecords);
} catch (err) {
this.log.debug('Failed to resolve MX records', err);
mxRecords = [];
}
if (this.options.verifyDomain) {
result.validDomain = mxRecords && mxRecords.length > 0;
}
if (this.options.verifyMailbox) {
result.validMailbox = await EmailValidator.verifyMailbox(
local, domain, mxRecords, this.options.timeout, this.log
);
}
return result;
}
static isEmail(address) {
return address.includes('@');
}
static extractAddressParts(address) {
if (!EmailValidator.isEmail(address)) {
throw new Error(`"${address}" is not a valid email address`);
}
return address.split('@');
}
static async resolveMxRecords(domain) {
const records = await ninvoke(dns, 'resolveMx', domain);
records.sort((a, b) => a.priority > b.priority);
return records.map(record => record.exchange);
}
static async verifyMailbox(local, domain, [mxRecord], timeout, log) {
if (!mxRecord || /yahoo/.test(mxRecord)) {
log.debug('Cannot verify due to missing or unsupported MX record', mxRecord);
return null;
}
return new Promise(resolve => {
const socket = net.connect(25, mxRecord);
let resTimeout;
const ret = result => {
if (ret.resolved) return;
if (!socket.destroyed) {
socket.write('QUIT\r\n');
socket.end();
}
clearTimeout(resTimeout);
resolve(result);
ret.resolved = true;
};
const messages = [
`HELO ${domain}`,
`MAIL FROM: <${local}@${domain}>`,
`RCPT TO: <${local}@${domain}>`
];
socket.on('data', data => {
data = data.toString();
log.debug('Mailbox: got data', data);
if (isInvalidMailboxError(data)) return ret(false);
if (!data.includes(220) && !data.includes(250)) return ret(null);
if (isMultilineGreet(data)) return;
if (messages.length > 0) {
const message = messages.shift();
log.debug('Mailbox: writing message', message);
return socket.write(message + '\r\n');
}
ret(true);
});
socket.on('error', err => {
log.debug('Mailbox: error in socket', err);
ret(null);
});
resTimeout = setTimeout(() => {
log.debug(`Mailbox: timed out (${timeout} ms)`);
ret(null);
}, timeout);
});
}
}
module.exports = EmailValidator;