forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
664 lines (572 loc) · 19.9 KB
/
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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
'use strict';
// smtp network server
const daemon = require('daemon');
const fs = require('fs');
const os = require('os');
const path = require('path');
const tls = require('tls');
// let log = require('why-is-node-running');
const tls_socket = require('./tls_socket');
const conn = require('./connection');
const outbound = require('./outbound');
const async = require('async');
const cluster = require('cluster');
const constants = require('haraka-constants');
const Server = exports;
Server.logger = require('./logger');
Server.config = require('haraka-config');
Server.plugins = require('./plugins');
Server.notes = {};
const logger = Server.logger;
// Need these here so we can run hooks
logger.add_log_methods(Server, 'server');
Server.listeners = [];
Server.load_smtp_ini = () => {
Server.cfg = Server.config.get('smtp.ini', {
booleans: [
'-main.daemonize',
'-main.graceful_shutdown',
],
}, () => {
Server.load_smtp_ini();
});
if (Server.cfg.main.nodes === undefined) {
logger.logwarn(`smtp.ini.nodes unset, using 1, see https://github.com/haraka/Haraka/wiki/Performance-Tuning`)
}
const defaults = {
inactivity_timeout: 300,
daemon_log_file: '/var/log/haraka.log',
daemon_pid_file: '/var/run/haraka.pid',
force_shutdown_timeout: 30,
smtps_port: 465,
nodes: 1,
};
for (const key in defaults) {
if (Server.cfg.main[key] !== undefined) continue;
Server.cfg.main[key] = defaults[key];
}
}
Server.load_http_ini = () => {
Server.http = {};
Server.http.cfg = Server.config.get('http.ini', () => {
Server.load_http_ini();
}).main;
}
Server.load_smtp_ini();
Server.load_http_ini();
Server.daemonize = function () {
const c = this.cfg.main;
if (!c.daemonize) return;
if (!process.env.__daemon) {
// Remove process.on('exit') listeners otherwise
// we get a spurious 'Exiting' log entry.
process.removeAllListeners('exit');
logger.lognotice('Daemonizing...');
}
const log_fd = fs.openSync(c.daemon_log_file, 'a');
daemon({ cwd: process.cwd(), stdout: log_fd });
// We are the daemon from here on...
const npid = require('npid');
try {
npid.create(c.daemon_pid_file).removeOnExit();
}
catch (err) {
logger.logerror(err.message);
logger.dump_and_exit(1);
}
}
Server.flushQueue = domain => {
if (!Server.cluster) {
outbound.flush_queue(domain);
return;
}
for (const id in cluster.workers) {
cluster.workers[id].send({event: 'outbound.flush_queue', domain});
}
}
let gracefull_in_progress = false;
Server.gracefulRestart = () => {
Server._graceful();
}
Server.stopListeners = () => {
logger.loginfo('Shutting down listeners');
Server.listeners.forEach(server => {
server.close();
});
Server.listeners = [];
}
Server.performShutdown = () => {
if (Server.cfg.main.graceful_shutdown) {
return Server.gracefulShutdown();
}
logger.loginfo("Shutting down.");
process.exit(0);
}
Server.gracefulShutdown = () => {
Server.stopListeners();
Server._graceful(() => {
// log();
logger.loginfo("Failed to shutdown naturally. Exiting.");
process.exit(0);
});
}
Server._graceful = shutdown => {
if (!Server.cluster && shutdown) {
['outbound', 'cfreader', 'plugins'].forEach(module => {
process.emit('message', {event: module + '.shutdown'});
});
const t = setTimeout(shutdown, Server.cfg.main.force_shutdown_timeout * 1000);
return t.unref();
}
if (gracefull_in_progress) {
logger.lognotice("Restart currently in progress - ignoring request");
return;
}
gracefull_in_progress = true;
// TODO: Make these configurable
const disconnect_timeout = 30;
const exit_timeout = 30;
cluster.removeAllListeners('exit');
// we reload using eachLimit where limit = num_workers - 1
// this kills all-but-one workers in parallel, leaving one running
// for new connections, and then restarts that one last worker.
const worker_ids = Object.keys(cluster.workers);
let limit = worker_ids.length - 1;
if (limit < 2) limit = 1;
async.eachLimit(worker_ids, limit, (id, cb) => {
logger.lognotice(`Killing node: ${id}`);
const worker = cluster.workers[id];
['outbound', 'cfreader', 'plugins'].forEach(module => {
worker.send({event: module + '.shutdown'});
})
worker.disconnect();
let disconnect_received = false;
const disconnect_timer = setTimeout(() => {
if (!disconnect_received) {
logger.logcrit("Disconnect never received by worker. Killing.");
worker.kill();
}
}, disconnect_timeout * 1000);
worker.once("disconnect", () => {
clearTimeout(disconnect_timer);
disconnect_received = true;
logger.lognotice("Disconnect complete");
let dead = false;
const timer = setTimeout(() => {
if (!dead) {
logger.logcrit(`Worker ${id} failed to shutdown. Killing.`);
worker.kill();
}
}, exit_timeout * 1000);
worker.once("exit", () => {
dead = true;
clearTimeout(timer);
if (shutdown) cb();
});
});
if (shutdown) return;
const newWorker = cluster.fork();
newWorker.once("listening", () => {
logger.lognotice("Replacement worker online.");
newWorker.on('exit', (code, signal) => {
cluster_exit_listener(newWorker, code, signal);
});
cb();
});
}, err => {
// err can basically never happen, but fuckit...
if (err) logger.logerror(err);
if (shutdown) {
logger.loginfo("Workers closed. Shutting down master process subsystems");
['outbound', 'cfreader', 'plugins'].forEach(module => {
process.emit('message', {event: module + '.shutdown'});
})
const t2 = setTimeout(shutdown, Server.cfg.main.force_shutdown_timeout * 1000);
return t2.unref();
}
gracefull_in_progress = false;
logger.lognotice(`Reload complete, workers: ${JSON.stringify(Object.keys(cluster.workers))}`);
});
}
Server.drainPools = () => {
if (!Server.cluster) {
return outbound.drain_pools();
}
for (const id in cluster.workers) {
cluster.workers[id].send({event: 'outbound.drain_pools'});
}
}
Server.sendToMaster = (command, params) => {
// console.log("Send to master: ", command);
if (Server.cluster) {
if (Server.cluster.isMaster) {
Server.receiveAsMaster(command, params);
}
else {
process.send({cmd: command, params});
}
}
else {
Server.receiveAsMaster(command, params);
}
}
Server.receiveAsMaster = (command, params) => {
if (!Server[command]) {
logger.logerror(`Invalid command: ${command}`);
return;
}
Server[command].apply(Server, params);
}
function messageHandler (worker, msg, handle) {
// sunset Haraka v3 (Node < 6)
if (arguments.length === 2) {
handle = msg;
msg = worker;
worker = undefined;
}
// console.log("received cmd: ", msg);
if (msg && msg.cmd) {
Server.receiveAsMaster(msg.cmd, msg.params);
}
}
Server.get_listen_addrs = (cfg, port) => {
if (!port) port = 25;
let listeners = [];
if (cfg && cfg.listen) {
listeners = cfg.listen.split(/\s*,\s*/);
if (listeners[0] === '') listeners = [];
for (let i=0; i < listeners.length; i++) {
if (/:[0-9]{1,5}$/.test(listeners[i])) continue;
listeners[i] = `${listeners[i]}:${port}`;
}
}
if (cfg.port) {
let host = cfg.listen_host;
if (!host) {
host = '[::0]';
Server.default_host = true;
}
listeners.unshift(`${host}:${cfg.port}`);
}
if (listeners.length) return listeners;
Server.default_host = true;
listeners.push(`[::0]:${port}`);
return listeners;
}
Server.createServer = params => {
const c = Server.cfg.main;
for (const key in params) {
if (typeof params[key] === 'function') continue;
c[key] = params[key];
}
Server.notes = {};
Server.plugins.server = Server;
Server.plugins.load_plugins();
const inactivity_timeout = (c.inactivity_timeout || 300) * 1000;
if (!cluster || !c.nodes) {
Server.daemonize(c);
Server.setup_smtp_listeners(Server.plugins, 'master', inactivity_timeout);
return;
}
// Cluster
Server.cluster = cluster;
// Cluster Workers
if (!cluster.isMaster) {
Server.setup_smtp_listeners(Server.plugins, 'child', inactivity_timeout);
return;
}
else {
// console.log("Setting up message handler");
cluster.on('message', messageHandler);
}
// Cluster Master
// We fork workers in init_master_respond so that plugins
// can put handlers on cluster events before they are emitted.
Server.plugins.run_hooks('init_master', Server);
}
Server.load_default_tls_config = done => {
// this fn exists solely for testing
if (Server.config.root_path != tls_socket.config.root_path) {
logger.loginfo(`resetting tls_config.config path to ${Server.config.root_path}`);
tls_socket.config = tls_socket.config.module_config(path.dirname(Server.config.root_path));
}
tls_socket.getSocketOpts('*', (opts) => {
done(opts);
});
}
Server.get_smtp_server = (host, port, inactivity_timeout, done) => {
let server;
function onConnect (client) {
client.setTimeout(inactivity_timeout);
const connection = conn.createConnection(client, server);
if (!server.has_tls) return;
connection.setTLS({
cipher: client.getCipher(),
verified: client.authorized,
verifyError: client.authorizationError,
peerCertificate: client.getPeerCertificate(),
});
}
if (port === Server.cfg.main.smtps_port) {
logger.loginfo('getting SocketOpts for SMTPS server');
tls_socket.getSocketOpts('*', opts => {
logger.loginfo(`Creating TLS server on ${host}:${port}`);
opts.rejectUnauthorized = tls_socket.get_rejectUnauthorized(opts.rejectUnauthorized, port, tls_socket.cfg.main.requireAuthorized)
server = tls.createServer(opts, onConnect);
tls_socket.addOCSP(server);
server.has_tls=true;
server.on('resumeSession', (id, rsDone) => {
logger.loginfo('client requested TLS resumeSession');
rsDone(null, null);
})
Server.listeners.push(server);
done(server);
})
}
else {
server = tls_socket.createServer(onConnect);
server.has_tls = false;
Server.listeners.push(server);
done(server);
}
}
Server.setup_smtp_listeners = (plugins2, type, inactivity_timeout) => {
async.each(
Server.get_listen_addrs(Server.cfg.main), // array of listeners
function setupListener (host_port, listenerDone) {
const hp = /^\[?([^\]]+)\]?:(\d+)$/.exec(host_port);
if (!hp) return listenerDone(
new Error('Invalid "listen" format in smtp.ini'));
const host = hp[1];
const port = parseInt(hp[2], 10);
Server.get_smtp_server(host, port, inactivity_timeout, (server) => {
if (!server) return listenerDone();
server.notes = Server.notes;
if (Server.cluster) server.cluster = Server.cluster;
server
.on('listening', function () {
const addr = this.address();
logger.lognotice(`Listening on ${addr.address}:${addr.port}`);
listenerDone();
})
.on('close', () => {
logger.loginfo(`Listener ${host}:${port} stopped`);
})
.on('error', e => {
if (e.code !== 'EAFNOSUPPORT') return listenerDone(e);
// Fallback from IPv6 to IPv4 if not supported
// But only if we supplied the default of [::0]:25
if (/^::0/.test(host) && Server.default_host) {
server.listen(port, '0.0.0.0', 0);
return;
}
// Pass error to callback
listenerDone(e);
})
.listen(port, host, 0);
});
},
function runInitHooks (err) {
if (err) {
logger.logerror(`Failed to setup listeners: ${err.message}`);
return logger.dump_and_exit(-1);
}
Server.listening();
plugins2.run_hooks(`init_${type}`, Server);
}
);
}
Server.setup_http_listeners = () => {
if (!Server.http.cfg) return;
if (!Server.http.cfg.listen) return;
const listeners = Server.get_listen_addrs(Server.http.cfg, 80);
if (!listeners.length) return;
try {
Server.http.express = require('express');
logger.loginfo('express loaded at Server.http.express');
}
catch (err) {
logger.logerror('express failed to load. No http server. ' +
' Try installing express with: npm install -g express');
return;
}
const app = Server.http.express();
Server.http.app = app;
logger.loginfo('express app is at Server.http.app');
function setupListener (host_port, cb) {
const hp = /^\[?([^\]]+)\]?:(\d+)$/.exec(host_port);
if (!hp) {
return cb(new Error('Invalid format for listen in http.ini'));
}
if (443 == hp[2]) {
// clone the default TLS opts
const tlsOpts = Object.assign({}, tls_socket.certsByHost['*']);
tlsOpts.requestCert = false; // not appropriate for HTTPS
// console.log(tlsOpts);
Server.http.server = require('https').createServer(tlsOpts, app);
}
else {
Server.http.server = require('http').createServer(app);
}
Server.listeners.push(Server.http.server);
Server.http.server.on('listening', function () {
const addr = this.address();
logger.lognotice(`Listening on ${addr.address}:${addr.port}`);
cb();
});
Server.http.server.on('error', e => {
logger.logerror(e);
cb(e);
});
Server.http.server.listen(hp[2], hp[1], 0);
}
function registerRoutes (err) {
if (err) {
logger.logerror(`Failed to setup http routes: ${err.message}`);
}
Server.plugins.run_hooks('init_http', Server);
app.use(Server.http.express.static(Server.get_http_docroot()));
app.use(Server.handle404);
}
async.each(listeners, setupListener, registerRoutes);
}
Server.init_master_respond = (retval, msg) => {
if (!(retval === constants.ok || retval === constants.cont)) {
Server.logerror(`init_master returned error${((msg) ? `: ${msg}` : '')}`);
return logger.dump_and_exit(1);
}
const c = Server.cfg.main;
Server.ready = 1;
// Load the queue if we're just one process
if (!(cluster && c.nodes)) {
outbound.load_queue();
Server.setup_http_listeners();
return;
}
// Running under cluster, fork children here, so that
// cluster events can be registered in init_master hooks.
outbound.scan_queue_pids((err, pids) => {
if (err) {
Server.logcrit("Scanning queue failed. Shutting down.");
return logger.dump_and_exit(1);
}
Server.daemonize();
// Fork workers
const workers = (c.nodes === 'cpus') ? os.cpus().length : c.nodes;
const new_workers = [];
for (let i=0; i<workers; i++) {
new_workers.push(cluster.fork({ CLUSTER_MASTER_PID: process.pid }));
}
for (let j=0; j<pids.length; j++) {
new_workers[j % new_workers.length]
.send({event: 'outbound.load_pid_queue', data: pids[j]});
}
cluster.on('online', worker => {
logger.lognotice(
'worker started',
{ worker: worker.id, pid: worker.process.pid }
);
});
cluster.on('listening', (worker, address) => {
logger.lognotice(`worker ${worker.id} listening on ${address.address}:${address.port}`);
});
cluster.on('exit', cluster_exit_listener);
});
}
function cluster_exit_listener (worker, code, signal) {
if (signal) {
logger.lognotice(`worker ${worker.id} killed by signal ${signal}`);
}
else if (code !== 0) {
logger.lognotice(`worker ${worker.id} exited with error code: ${code}`);
}
if (signal || code !== 0) {
// Restart worker
const new_worker = cluster.fork({
CLUSTER_MASTER_PID: process.pid
});
new_worker.send({
event: 'outbound.load_pid_queue', data: worker.process.pid,
});
}
}
Server.init_child_respond = (retval, msg) => {
switch (retval) {
case constants.ok:
case constants.cont:
Server.setup_http_listeners();
return;
}
const pid = process.env.CLUSTER_MASTER_PID;
Server.logerror(`init_child returned error ${((msg) ? `: ${msg}` : '')}`);
try {
if (pid) {
process.kill(pid);
Server.logerror(`Killing master (pid=${pid})`);
}
}
catch (err) {
Server.logerror('Terminating child');
}
logger.dump_and_exit(1);
}
Server.listening = () => {
const c = Server.cfg.main;
// Drop privileges
if (c.group) {
Server.lognotice(`Switching from current gid: ${process.getgid()}`);
process.setgid(c.group);
Server.lognotice(`New gid: ${process.getgid()}`);
}
if (c.user) {
Server.lognotice(`Switching from current uid: ${process.getuid()}`);
process.setuid(c.user);
Server.lognotice(`New uid: ${process.getuid()}`);
}
Server.ready = 1;
}
Server.init_http_respond = () => {
logger.loginfo('init_http_respond');
let WebSocketServer;
try { WebSocketServer = require('ws').Server; }
catch (e) {
logger.logerror(`unable to load ws.
did you: npm install -g ws?`);
return;
}
if (!WebSocketServer) {
logger.logerror('ws failed to load');
return;
}
Server.http.wss = new WebSocketServer({ server: Server.http.server });
logger.loginfo('Server.http.wss loaded');
Server.plugins.run_hooks('init_wss', Server);
}
Server.init_wss_respond = () => {
logger.loginfo('init_wss_respond');
// logger.logdebug(arguments);
}
Server.get_http_docroot = () => {
if (Server.http.cfg.docroot) return Server.http.cfg.docroot;
Server.http.cfg.docroot = path.join(
(process.env.HARAKA || __dirname),
'/html'
);
logger.loginfo(`using html docroot: ${Server.http.cfg.docroot}`);
return Server.http.cfg.docroot;
}
Server.handle404 = (req, res) => {
// abandon all hope, serve up a 404
const docroot = Server.get_http_docroot();
// respond with html page
if (req.accepts('html')) {
res.status(404).sendFile('404.html', { root: docroot });
return;
}
// respond with json
if (req.accepts('json')) {
res.status(404).send({ err: 'Not found' });
return;
}
res.status(404).send('Not found!');
}