-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathmx-server.js
More file actions
137 lines (116 loc) · 4.43 KB
/
mx-server.js
File metadata and controls
137 lines (116 loc) · 4.43 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
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
const fs = require('node:fs');
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 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);
// TODO: remove try/catch for isDenylisted/isSilent/isBackscatter
// and replace with catch (err) for onData to detect and store counter
// based off err.name detected or if it was combined then err.errors
class MX {
constructor(options = {}) {
this.client = options.client;
this.wsp = options.wsp;
this.resolver = createTangerine(this.client, logger);
this.cache = createMtaStsCache(this.client);
// TODO: rate limiting (?)
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({
//
// most of these options mirror the FE forwarding server options
//
hideENHANCEDSTATUSCODES: false,
hideDSN: true, // explicitly disable DSN support on MX server (SMTP only)
size: MAX_BYTES,
onData: onData.bind(this),
onConnect: onConnect.bind(this),
onClose: onClose.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: ['AUTH'],
secure: false,
needsUpgrade: false,
// <https://github.com/nodemailer/wildduck/issues/563>
// hide8BITMIME: true,
//
// Enable REQUIRETLS support (RFC 8689)
// <https://www.rfc-editor.org/rfc/rfc8689.pdf>
//
// NOTE: REQUIRETLS is now enabled in production to support
// security vendors that require it
//
hideREQUIRETLS: false,
// keys
...(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)
}
: {}),
// override with any options passed (useful for testing)
..._.omit(options, ['client', 'wsp'])
});
// 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.MX_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 = MX;