-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathsmtp-server.js
More file actions
190 lines (162 loc) · 6.04 KB
/
smtp-server.js
File metadata and controls
190 lines (162 loc) · 6.04 KB
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
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
const fs = require('node:fs');
const tls = require('node:tls');
const RateLimiter = require('async-ratelimiter');
const bytes = require('@forwardemail/bytes');
const ms = require('ms');
const pify = require('pify');
const { SMTPServer } = require('smtp-server');
const _ = require('#helpers/lodash');
const config = require('#config');
const createMtaStsCache = require('#helpers/create-mta-sts-cache');
const createTangerine = require('#helpers/create-tangerine');
// eslint-disable-next-line import/no-unassigned-import
require('#helpers/polyfill-towellformed');
const env = require('#config/env');
const isLockingError = require('#helpers/is-locking-error');
const isMongoError = require('#helpers/is-mongo-error');
const isRedisError = require('#helpers/is-redis-error');
const isRetryableError = require('#helpers/is-retryable-error');
const logger = require('#helpers/logger');
const onAuth = require('#helpers/on-auth');
const onClose = require('#helpers/on-close');
const onConnect = require('#helpers/on-connect');
const onData = require('#helpers/on-data');
const onMailFrom = require('#helpers/on-mail-from');
const onRcptTo = require('#helpers/on-rcpt-to');
const MAX_BYTES = bytes(env.SMTP_MESSAGE_MAX_SIZE);
// Force enable TLS 1.0 (if node_args approach is not used, safety net)
if (env.SMTP_TLS_MIN_VERSION === 'TLSv1') {
tls.DEFAULT_MIN_VERSION = 'TLSv1';
}
class SMTP {
constructor(
options = {},
secure = env.SMTP_PORT === 465 ||
env.SMTP_PORT === 2465 ||
env.SMTP_PORT === 2455
) {
this.client = options.client;
const resolver = createTangerine(this.client, logger);
this.resolver = resolver;
//
// MTA-STS cache (shared with mx-connect for outbound delivery)
//
this.cache = createMtaStsCache(this.client);
//
// NOTE: hard-coded values for now (switch to env later)
// (current limit is 10 failed login attempts per hour)
//
this.rateLimiter = new RateLimiter({
db: this.client,
max: config.smtpLimitMessages,
duration: config.smtpLimitDuration,
namespace: config.smtpLimitNamespace
});
this.logger = logger;
// setup our smtp server which listens for incoming email
// TODO: <https://github.com/nodemailer/smtp-server/issues/177>
this.server = new SMTPServer({
// <https://github.com/nodemailer/smtp-server/pull/222>
resolver,
// <https://github.com/nodemailer/smtp-server/pull/192>
authRequiredMessage: config.authRequiredMessage,
//
// TODO: customize `heloResponse`
// <https://github.com/nodemailer/smtp-server/commit/7c8279f4c2ea3d19d27efaa54d8378f12de3f986>
//
//
// most of these options mirror the FE forwarding server options
//
hideENHANCEDSTATUSCODES: false,
hideDSN: false,
size: MAX_BYTES,
onData: onData.bind(this),
onConnect: onConnect.bind(this),
onClose: onClose.bind(this),
onAuth: onAuth.bind(this),
onMailFrom: onMailFrom.bind(this),
onRcptTo: onRcptTo.bind(this),
// NOTE: we don't need to set a value for maxClients
// since we have rate limiting enabled by IP
// maxClients: Infinity, // default is Infinity
// allow 3m to process bulk RCPT TO
socketTimeout: config.socketTimeout,
// <https://brooker.co.za/blog/2024/05/09/nagle.html>
// <https://nodejs.org/api/net.html#netcreateserveroptions-connectionlistener>
noDelay: true,
// default closeTimeout is 30s
closeTimeout: ms('30s'),
// <https://github.com/nodemailer/smtp-server/issues/177>
disableReverseLookup: true,
logger: this.logger,
disabledCommands: secure ? ['STARTTLS'] : [],
secure,
needsUpgrade: secure,
authMethods: ['PLAIN', 'LOGIN'], // XOAUTH2, CRAM-MD5
// just in case smtp-server changes default and patch semver bump (unlikely but safeguard )
allowInsecureAuth:
config.env === 'production' ? false : env.SMTP_ALLOW_INSECURE_AUTH,
authOptional: false,
// <https://github.com/nodemailer/wildduck/issues/563>
// hide8BITMIME: true,
// Enable REQUIRETLS support (RFC 8689)
//
// NOTE: REQUIRETLS is now enabled in production to support
// security vendors that require this extension
//
hideREQUIRETLS: false,
// keys and TLS options together
...(config.env === 'production'
? {
key: fs.readFileSync(env.WEB_SSL_KEY_PATH),
cert: fs.readFileSync(env.WEB_SSL_CERT_PATH),
ca: fs.readFileSync(env.WEB_SSL_CA_PATH),
...(env.SMTP_TLS_MIN_VERSION
? { minVersion: env.SMTP_TLS_MIN_VERSION }
: {}),
...(env.SMTP_TLS_MAX_VERSION
? { maxVersion: env.SMTP_TLS_MAX_VERSION }
: {})
}
: {
...(env.SMTP_TLS_MIN_VERSION
? { minVersion: env.SMTP_TLS_MIN_VERSION }
: {}),
...(env.SMTP_TLS_MAX_VERSION
? { maxVersion: env.SMTP_TLS_MAX_VERSION }
: {})
}),
// override with any options passed (useful for testing)
..._.omit(options, ['client'])
});
// override logger
this.server.logger = this.logger;
// kind of hacky but I filed a GH issue
// <https://github.com/nodemailer/smtp-server/issues/135>
this.server.address = this.server.server.address.bind(this.server.server);
this.server.on('error', (err) => {
err.is_server_error = true;
if (
!isRetryableError(err) ||
isRedisError(err) ||
isMongoError(err) ||
isLockingError(err)
)
logger.error(err);
else logger.debug(err);
});
this.listen = this.listen.bind(this);
this.close = this.close.bind(this);
}
async listen(port = env.SMTP_PORT, host = '::', ...args) {
await pify(this.server.listen).bind(this.server)(port, host, ...args);
}
async close() {
await pify(this.server.close).bind(this.server);
}
}
module.exports = SMTP;