forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (130 loc) · 4.23 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
"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('./constants');
var os = require('os');
var cluster;
try { cluster = require('cluster') } // cluster can be installed with npm
catch (err) {
logger.logdebug("no cluster available, running single-process");
}
// Need these here so we can run hooks
for (var key in logger) {
if (key.match(/^log\w/)) {
exports[key] = (function (key) {
return function () {
var args = ["[server] "];
for (var i=0, l=arguments.length; i<l; i++) {
args.push(arguments[i]);
}
logger[key].apply(logger, args);
}
})(key);
}
}
var Server = exports;
var defaults = {
port: 25,
listen_host: '0.0.0.0',
inactivity_timeout: 600
};
function apply_defaults(obj) {
var key;
for (key in defaults) {
obj[key] = obj[key] || defaults[key];
}
}
Server.createServer = function (params) {
var config_data = config.get('smtp.ini');
var param_key;
for (param_key in params) {
if (typeof params[param_key] !== 'function') {
config_data.main[param_key] = params[param_key];
}
}
// config_data defaults
apply_defaults(config_data.main);
plugins.load_plugins();
var server = net.createServer(function (client) {
client.setTimeout((config_data.main.inactivity_timeout || 300) * 1000);
conn.createConnection(client, server);
});
server.notes = {};
if (cluster && config_data.main.nodes) {
var c = cluster(server);
var cluster_modules = config.get('cluster_modules', 'list');
if (config_data.main.nodes !== 'cpus') {
c.set('workers', config_data.main.nodes);
}
if (config_data.main.user) {
c.set('user', config_data.main.user);
}
for (var i=0,l=cluster_modules.length; i < l; i++) {
var matches = /^(\w+)\s*(?::\s*(.*))?$/.exec(cluster_modules[i]);
if (!matches) {
Server.logerror("cluster_modules in invalid format: " + cluster_modules[i]);
continue;
}
var module = matches[1];
var params = matches[2];
if (params) {
c.use(cluster[module](JSON.parse(params)));
}
else {
c.use(cluster[module]());
}
}
c.set('host', config_data.main.listen_host);
c.listen(parseInt(config_data.main.port));
c.on('listening', listening);
Server.cluster = c;
if (c.isMaster) {
plugins.run_hooks('init_master', Server);
}
if (c.isWorker) {
plugins.run_hooks('init_child', Server);
}
}
else {
server.listen(config_data.main.port, config_data.main.listen_host, listening);
plugins.run_hooks('init_master', Server);
}
};
Server.init_master_respond = function (retval, msg) {
Server.ready = 1;
switch(retval) {
case constants.ok:
case constants.cont:
break;
default:
Server.logerror("init_master stopped startup: " + msg);
process.exit();
}
}
Server.init_child_respond = function (retval, msg) {
switch(retval) {
case constants.ok:
case constants.cont:
break;
default:
Server.logerror("init_child returned with error. Killing Haraka. " + msg);
process.kill(process.env.CLUSTER_MASTER_PID);
}
}
function listening () {
var config_data = config.get('smtp.ini');
logger.lognotice("Listening on port " + config_data.main.port);
out.load_queue();
Server.ready = 1;
if (config_data.main.user) {
// drop privileges
Server.lognotice('Switching from current uid: ' + process.getuid());
process.setuid(config_data.main.user);
Server.lognotice('New uid: ' + process.getuid());
}
}