forked from forwardemail/forwardemail.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pop3-server.js
154 lines (132 loc) · 4.98 KB
/
pop3-server.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
/*
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* WildDuck Mail Agent is licensed under the European Union Public License 1.2 or later.
* https://github.com/nodemailer/wildduck
*/
const fs = require('node:fs');
const os = require('node:os');
const MessageHandler = require('wildduck/lib/message-handler');
const POP3Server = require('wildduck/lib/pop3/server');
const RateLimiter = require('async-ratelimiter');
const pify = require('pify');
const AttachmentStorage = require('#helpers/attachment-storage');
const POP3Notifier = require('#helpers/imap-notifier');
const Indexer = require('#helpers/indexer');
const config = require('#config');
const createTangerine = require('#helpers/create-tangerine');
const env = require('#config/env');
const pop3 = require('#helpers/pop3');
const logger = require('#helpers/logger');
const onAuth = require('#helpers/on-auth');
const refreshSession = require('#helpers/refresh-session');
//
// TODO: we need to wrap top and add to parse-payload for all handlers
//
class POP3 {
constructor(
options = {},
secure = env.POP3_PORT === 995 || env.POP3_PORT === 2995
) {
this.client = options.client;
this.subscriber = options.subscriber;
this.wsp = options.wsp;
this.resolver = createTangerine(this.client, logger);
//
// 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;
const server = new POP3Server({
secure,
secured: false,
disableSTARTTLS: secure,
ignoreSTARTTLS: !secure,
useProxy: false,
ignoredHosts: [],
socketTimeout: config.socketTimeout,
// TODO: submit PR to add ability to customize version string
disableVersionString: true,
id: {
name: os.hostname(),
version: config.pkg.version,
//
// NOTE: version and vendor not used right now since `disableVersionString`
// and existing WildDuck POP3 server doesn't let you customize version string (yet)
//
vendor: config.pkg.author
},
logger: this.logger,
// <https://github.com/nodemailer/wildduck/issues/635>
// <https://github.com/nodemailer/wildduck/blob/b9349f6e8315873668d605e6567ced2d7b1c0c80/imap-core/lib/imap-server.js#L413-L419>
SNICallback(servername, cb) {
cb(null, server.secureContext.get(servername));
},
...(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),
ecdhCurve: 'auto'
}
: {})
});
// override logger
server.logger = this.logger;
server.loggelf = (...args) => this.logger.debug(...args);
server.onAuth = onAuth.bind(this);
server.onListMessages = pop3.onListMessages.bind(this);
server.onFetchMessage = pop3.onFetchMessage.bind(this);
server.onUpdate = pop3.onUpdate.bind(this);
// kind of hacky but I filed a GH issue
// <https://github.com/nodemailer/smtp-server/issues/135>
server.address = server.server.address.bind(server.server);
server.on('error', (err) => {
logger.error(err);
});
this.attachmentStorage = new AttachmentStorage();
this.indexer = new Indexer({ attachmentStorage: this.attachmentStorage });
// promisified version of prepare message from wildduck message handler
this.prepareMessage = pify(
MessageHandler.prototype.prepareMessage.bind({
indexer: this.indexer,
normalizeSubject: MessageHandler.prototype.normalizeSubject,
generateIndexedHeaders: MessageHandler.prototype.generateIndexedHeaders
})
);
//
// the notifier is utilized in the POP3 connection (see `wildduck/imap-core/lib/imap-connection.js`)
// in order to `getUpdates` and send them over the socket (e.g. `EXIST`, `EXPUNGE`, `FETCH`)
// <https://github.com/nodemailer/wildduck/issues/509>
//
server.notifier = new POP3Notifier({
publisher: this.client,
subscriber: this.subscriber
});
this.server = server;
this.refreshSession = refreshSession.bind(this);
this.listen = this.listen.bind(this);
this.close = this.close.bind(this);
}
async listen(port = env.POP3_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 = POP3;