forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
592 lines (508 loc) · 17.1 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
'use strict';
// smtp network server
var net = require('./tls_socket');
var logger = require('./logger');
var config = require('./config');
var conn = require('./connection');
var out = require('./outbound');
var plugins = require('./plugins');
var constants = require('haraka-constants');
var os = require('os');
var cluster = require('cluster');
var async = require('async');
var daemon = require('daemon');
var path = require('path');
// Need these here so we can run hooks
logger.add_log_methods(exports, 'server');
var Server = exports;
Server.load_smtp_ini = function () {
Server.cfg = config.get('smtp.ini', {
booleans: [
'-main.daemonize',
],
}, function () {
Server.load_smtp_ini();
});
var defaults = {
inactivity_timeout: 600,
daemon_log_file: '/var/log/haraka.log',
daemon_pid_file: '/var/run/haraka.pid',
force_shutdown_timeout: 30,
};
for (var key in defaults) {
if (Server.cfg.main[key] !== undefined) continue;
Server.cfg.main[key] = defaults[key];
}
};
Server.load_http_ini = function () {
Server.http = {};
Server.http.cfg = config.get('http.ini', function () {
Server.load_http_ini();
}).main;
};
Server.load_smtp_ini();
Server.load_http_ini();
Server.daemonize = function () {
var 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...');
}
var log_fd = require('fs').openSync(c.daemon_log_file, 'a');
daemon({stdout: log_fd});
// We are the daemon from here on...
var npid = require('npid');
try {
npid.create(c.daemon_pid_file).removeOnExit();
}
catch (err) {
logger.logerror(err.message);
logger.dump_and_exit(1);
}
};
Server.flushQueue = function (domain) {
if (!Server.cluster) {
out.flush_queue(domain);
return;
}
for (var id in cluster.workers) {
cluster.workers[id].send({event: 'outbound.flush_queue', domain: domain});
}
};
var gracefull_in_progress = false;
Server.gracefulRestart = function () {
Server._graceful();
}
Server.gracefulShutdown = function () {
Server._graceful(function () {
logger.loginfo("Failed to shutdown naturally. Exiting.");
process.exit(0);
});
}
Server._graceful = function (shutdown) {
if (!Server.cluster) {
if (shutdown) {
['outbound', 'cfreader', 'plugins'].forEach(function (module) {
process.emit('message', {event: module + '.shutdown'});
});
var 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
var disconnect_timeout = 30;
var exit_timeout = 30;
cluster.removeAllListeners('exit');
// only reload one worker at a time
// otherwise, we'll have a time when no connection handlers are running
var worker_ids = Object.keys(cluster.workers);
async.eachSeries(worker_ids, function (id, cb) {
logger.lognotice("Killing node: " + id);
var worker = cluster.workers[id];
['outbound', 'cfreader', 'plugins'].forEach(function (module) {
worker.send({event: module + '.shutdown'});
})
worker.disconnect();
var disconnect_received = false;
var disconnect_timer = setTimeout(function () {
if (!disconnect_received) {
logger.logcrit("Disconnect never received by worker. Killing.");
worker.kill();
}
}, disconnect_timeout * 1000);
worker.once("disconnect", function() {
clearTimeout(disconnect_timer);
disconnect_received = true;
logger.lognotice("Disconnect complete");
var dead = false;
var timer = setTimeout(function () {
if (!dead) {
logger.logcrit("Worker " + id + " failed to shutdown. Killing.");
worker.kill();
}
}, exit_timeout * 1000);
worker.once("exit", function () {
dead = true;
clearTimeout(timer);
if (shutdown) cb();
});
});
if (shutdown) return;
var newWorker = cluster.fork();
newWorker.once("listening", function() {
logger.lognotice("Replacement worker online.");
newWorker.on('exit', function (code, signal) {
cluster_exit_listener(newWorker, code, signal);
});
cb();
});
}, function (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(function (module) {
process.emit('message', {event: module + '.shutdown'});
})
var 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 = function () {
if (!Server.cluster) {
return out.drain_pools();
}
for (var id in cluster.workers) {
cluster.workers[id].send({event: 'outbound.drain_pools'});
}
};
Server.sendToMaster = function (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: params});
}
}
else {
Server.receiveAsMaster(command, params);
}
}
Server.receiveAsMaster = function (command, params) {
if (!Server[command]) {
logger.logerror("Invalid command: " + command);
}
Server[command].apply(Server, params);
}
function messageHandler (worker, msg) {
// console.log("received cmd: ", msg);
if (msg && msg.cmd) {
Server.receiveAsMaster(msg.cmd, msg.params);
}
}
Server.get_listen_addrs = function (cfg, port) {
if (!port) port = 25;
var listeners = [];
if (cfg && cfg.listen) {
listeners = cfg.listen.split(/\s*,\s*/);
if (listeners[0] === '') listeners = [];
for (var i=0; i < listeners.length; i++) {
if (/:[0-9]{1,5}$/.test(listeners[i])) continue;
listeners[i] = listeners[i] + ':' + port;
}
}
if (cfg.port) {
var 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 = function (params) {
var c = Server.cfg.main;
for (var key in params) {
if (typeof params[key] === 'function') continue;
c[key] = params[key];
}
Server.notes = {};
plugins.server = Server;
plugins.load_plugins();
var inactivity_timeout = (c.inactivity_timeout || 300) * 1000;
if (!cluster || !c.nodes) {
Server.daemonize(c);
Server.setup_smtp_listeners(plugins, 'master', inactivity_timeout);
return;
}
// Cluster
Server.cluster = cluster;
// Cluster Workers
if (!cluster.isMaster) {
Server.setup_smtp_listeners(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.
plugins.run_hooks('init_master', Server);
};
Server.get_smtp_server = function (host, port, inactivity_timeout) {
var server;
var conn_cb = function (client) {
client.setTimeout(inactivity_timeout);
conn.createConnection(client, server);
};
if (port !== '465') {
server = net.createServer(conn_cb);
return server;
}
var options = {
key: config.get('tls_key.pem', 'binary'),
cert: config.get('tls_cert.pem', 'binary'),
};
if (!options.key) {
logger.logerror("Missing tls_key.pem for port 465");
return;
}
if (!options.cert) {
logger.logerror("Missing tls_cert.pem for port 465");
return;
}
logger.logdebug("Creating TLS server on " + host + ':' + port);
server = require('tls').createServer(options, conn_cb);
server.has_tls=true;
return server;
};
Server.setup_smtp_listeners = function (plugins2, type, inactivity_timeout) {
var listeners = Server.get_listen_addrs(Server.cfg.main);
var runInitHooks = function (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);
};
var setupListener = function (host_port, cb) {
var hp = /^\[?([^\]]+)\]?:(\d+)$/.exec(host_port);
if (!hp) {
return cb(new Error(
'Invalid format for listen parameter in smtp.ini'));
}
var host = hp[1];
var port = hp[2];
var server = Server.get_smtp_server(host, port, inactivity_timeout);
if (!server) return cb();
server.unref();
server.notes = Server.notes;
if (Server.cluster) server.cluster = Server.cluster;
server.on('listening', function () {
var addr = this.address();
logger.lognotice("Listening on " + addr.address + ':' + addr.port);
cb();
});
// Fallback from IPv6 to IPv4 if not supported
// But only if we supplied the default of [::0]:25
server.on('error', function (e) {
if (e.code === 'EAFNOSUPPORT' &&
/^::0/.test(host) &&
Server.default_host) {
server.listen(port, '0.0.0.0');
}
else {
// Pass error to callback
cb(e);
}
});
server.listen(port, host);
};
async.each(listeners, setupListener, runInitHooks);
};
Server.setup_http_listeners = function () {
if (!Server.http.cfg) return;
if (!Server.http.cfg.listen) return;
var 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;
}
var app = Server.http.express();
Server.http.app = app;
logger.loginfo('express app is at Server.http.app');
var setupListener = function (host_port, cb) {
var hp = /^\[?([^\]]+)\]?:(\d+)$/.exec(host_port);
if (!hp) {
return cb(new Error('Invalid format for listen in http.ini'));
}
Server.http.server = require('http').createServer(app);
Server.http.server.unref();
Server.http.server.on('listening', function () {
var addr = this.address();
logger.lognotice('Listening on ' + addr.address + ':' + addr.port);
cb();
});
Server.http.server.on('error', function (e) {
logger.logerror(e);
cb(e);
});
Server.http.server.listen(hp[2], hp[1]);
};
var registerRoutes = function (err) {
if (err) {
logger.logerror('Failed to setup http routes: ' + err.message);
}
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 = function (retval, msg) {
if (!(retval === constants.ok || retval === constants.cont)) {
Server.logerror("init_master returned error" +
((msg) ? ': ' + msg : ''));
return logger.dump_and_exit(1);
}
var c = Server.cfg.main;
Server.ready = 1;
// Load the queue if we're just one process
if (!(cluster && c.nodes)) {
out.load_queue();
Server.setup_http_listeners();
return;
}
// Running under cluster, fork children here, so that
// cluster events can be registered in init_master hooks.
out.scan_queue_pids(function (err, pids) {
if (err) {
Server.logcrit("Scanning queue failed. Shutting down.");
return logger.dump_and_exit(1);
}
Server.daemonize();
// Fork workers
var workers = (c.nodes === 'cpus') ? os.cpus().length : c.nodes;
var new_workers = [];
for (var i=0; i<workers; i++) {
new_workers.push(cluster.fork({ CLUSTER_MASTER_PID: process.pid }));
}
for (var j=0; j<pids.length; j++) {
new_workers[j % new_workers.length]
.send({event: 'outbound.load_pid_queue', data: pids[j]});
}
cluster.on('online', function (worker) {
logger.lognotice('worker ' + worker.id + ' started pid=' +
worker.process.pid);
});
cluster.on('listening', function (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
var 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 = function (retval, msg) {
switch (retval) {
case constants.ok:
case constants.cont:
Server.setup_http_listeners();
return;
}
var 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 = function () {
var 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 = function () {
logger.loginfo('init_http_respond');
var WebSocketServer;
try { WebSocketServer = require('ws').Server; }
catch (e) {
logger.logerror('unable to load ws.\ndid 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');
plugins.run_hooks('init_wss', Server);
};
Server.init_wss_respond = function () {
logger.loginfo('init_wss_respond');
// logger.logdebug(arguments);
};
Server.get_http_docroot = function () {
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 = function(req, res){
// abandon all hope, serve up a 404
var 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!');
};