forked from prawnsalad/KiwiIRC
-
Notifications
You must be signed in to change notification settings - Fork 2
/
kiwi.js
executable file
·350 lines (264 loc) · 9.56 KB
/
kiwi.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
var _ = require('lodash'),
winston = require('winston'),
WebListener = require('./weblistener.js'),
config = require('./configuration.js'),
modules = require('./modules.js'),
Identd = require('./identd.js'),
Proxy = require('./proxy.js'),
ControlInterface = require('./controlinterface.js');
process.chdir(__dirname + '/../');
// Get our own version from package.json
global.build_version = require('../package.json').version;
// Load the configuration
require('./helpers/configloader.js')();
// If we're not running in the forground and we have a log file.. switch
// console.log to output to a file
if (process.argv.indexOf('-f') === -1 && global.config && global.config.log) {
(function () {
var log_file_name = global.config.log;
if (log_file_name[0] !== '/') {
log_file_name = __dirname + '/../' + log_file_name;
}
winston.add(winston.transports.File, {
filename: log_file_name,
json: false,
timestamp: function() {
var year, month, day, time_string,
d = new Date();
year = String(d.getFullYear());
month = String(d.getMonth() + 1);
if (month.length === 1) {
month = "0" + month;
}
day = String(d.getDate());
if (day.length === 1) {
day = "0" + day;
}
// Take the time from the existing toTimeString() format
time_string = (new Date()).toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
return year + "-" + month + "-" + day + ' ' + time_string;
}
});
winston.remove(winston.transports.Console);
})();
}
// Make sure we have a valid config file and at least 1 server
if (!global.config || Object.keys(global.config).length === 0) {
winston.error('Couldn\'t find a valid config.js file (Did you copy the config.example.js file yet?)');
process.exit(1);
}
if ((!global.config.servers) || (global.config.servers.length < 1)) {
winston.error('No servers defined in config file');
process.exit(2);
}
// Create a plugin interface
global.modules = new modules.Publisher();
// Register as the active interface
modules.registerPublisher(global.modules);
// Load any modules in the config
if (global.config.module_dir) {
(global.config.modules || []).forEach(function (module_name) {
if (modules.load(module_name)) {
winston.info('Module %s loaded successfully', module_name);
} else {
winston.warn('Module %s failed to load', module_name);
}
});
}
// Holder for all the connected clients
global.clients = {
clients: Object.create(null),
addresses: Object.create(null),
// Local and foriegn port pairs for identd lookups
// {'65483_6667': client_obj, '54356_6697': client_obj}
port_pairs: {},
add: function (client) {
this.clients[client.hash] = client;
if (typeof this.addresses[client.real_address] === 'undefined') {
this.addresses[client.real_address] = Object.create(null);
}
this.addresses[client.real_address][client.hash] = client;
},
remove: function (client) {
if (typeof this.clients[client.hash] !== 'undefined') {
delete this.clients[client.hash];
delete this.addresses[client.real_address][client.hash];
if (Object.keys(this.addresses[client.real_address]).length < 1) {
delete this.addresses[client.real_address];
}
}
},
numOnAddress: function (addr) {
if (typeof this.addresses[addr] !== 'undefined') {
return Object.keys(this.addresses[addr]).length;
} else {
return 0;
}
},
broadcastKiwiCommand: function (command, data, callback) {
var clients = [];
// Get an array of clients for us to work with
for (var client in global.clients.clients) {
clients.push(global.clients.clients[client]);
}
// Sending of the command in batches
var sendCommandBatch = function (list) {
var batch_size = 100,
cutoff;
if (list.length >= batch_size) {
// If we have more clients than our batch size, call ourself with the next batch
setTimeout(function () {
sendCommandBatch(list.slice(batch_size));
}, 200);
cutoff = batch_size;
} else {
cutoff = list.length;
}
list.slice(0, cutoff).forEach(function (client) {
if (!client.disposed) {
client.sendKiwiCommand(command, data);
}
});
if (cutoff === list.length && typeof callback === 'function') {
callback();
}
};
sendCommandBatch(clients);
}
};
global.servers = {
servers: Object.create(null),
addConnection: function (connection) {
var host = connection.irc_host.hostname;
if (!this.servers[host]) {
this.servers[host] = [];
}
this.servers[host].push(connection);
},
removeConnection: function (connection) {
var host = connection.irc_host.hostname
if (this.servers[host]) {
this.servers[host] = _.without(this.servers[host], connection);
if (this.servers[host].length === 0) {
delete this.servers[host];
}
}
},
numOnHost: function (host) {
if (this.servers[host]) {
return this.servers[host].length;
} else {
return 0;
}
}
};
/**
* When a new config is loaded, send out an alert to the clients so
* so they can reload it
*/
config.on('loaded', function () {
global.clients.broadcastKiwiCommand('reconfig');
});
/*
* Identd server
*/
if (global.config.identd && global.config.identd.enabled) {
var identd_resolve_user = function(port_here, port_there) {
var key = port_here.toString() + '_' + port_there.toString();
if (typeof global.clients.port_pairs[key] == 'undefined') {
return;
}
return global.clients.port_pairs[key].username;
};
var identd_server = new Identd({
bind_addr: global.config.identd.address,
bind_port: global.config.identd.port,
user_id: identd_resolve_user
});
identd_server.start();
}
/*
* Web listeners
*/
// Start up a weblistener for each found in the config
_.each(global.config.servers, function (server) {
if (server.type == 'proxy') {
// Start up a kiwi proxy server
var serv = new Proxy.ProxyServer();
serv.listen(server.port, server.address, server);
serv.on('listening', function() {
winston.info('Kiwi proxy listening on %s:%s %s SSL', server.address, server.port, (server.ssl ? 'with' : 'without'));
});
serv.on('socket_connected', function(pipe) {
// SSL connections have the raw socket as a property
var socket = pipe.irc_socket.socket ?
pipe.irc_socket.socket :
pipe.irc_socket;
pipe.identd_pair = socket.localPort.toString() + '_' + socket.remotePort.toString();
global.clients.port_pairs[pipe.identd_pair] = pipe.meta;
});
serv.on('connection_close', function(pipe) {
delete global.clients.port_pairs[pipe.identd_pair];
});
} else {
// Start up a kiwi web server
var wl = new WebListener(server, global.config.transports);
wl.on('connection', function (client) {
clients.add(client);
});
wl.on('client_dispose', function (client) {
clients.remove(client);
});
wl.on('listening', function () {
winston.info('Listening on %s:%s %s SSL', server.address, server.port, (server.ssl ? 'with' : 'without'));
webListenerRunning();
});
wl.on('error', function (err) {
winston.info('Error listening on %s:%s: %s', server.address, server.port, err.code);
// TODO: This should probably be refactored. ^JA
webListenerRunning();
});
}
});
// Once all the listeners are listening, set the processes UID/GID
var num_listening = 0;
function webListenerRunning() {
num_listening++;
if (num_listening === global.config.servers.length) {
setProcessUid();
}
}
/*
* Process settings
*/
// Set process title
process.title = 'kiwiirc';
// Change UID/GID
function setProcessUid() {
if ((global.config.group) && (global.config.group !== '')) {
process.setgid(global.config.group);
}
if ((global.config.user) && (global.config.user !== '')) {
process.setuid(global.config.user);
}
}
// Make sure Kiwi doesn't simply quit on an exception
process.on('uncaughtException', function (e) {
winston.error('[Uncaught exception] %s', e, {stack: e.stack});
});
process.on('SIGUSR1', function() {
if (config.loadConfig()) {
winston.info('New config file loaded');
} else {
winston.info('No new config file was loaded');
}
});
process.on('SIGUSR2', function() {
winston.info('Connected clients: %s', _.size(global.clients.clients));
winston.info('Num. remote hosts: %s', _.size(global.clients.addresses));
});
/*
* Listen for runtime commands
*/
process.stdin.resume();
new ControlInterface(process.stdin, process.stdout, {prompt: ''});