1 var fs
= require('fs'),
3 util
= require('util'),
4 WebListener
= require('./weblistener.js'),
5 config
= require('./configuration.js'),
6 rehash
= require('./rehash.js'),
7 modules
= require('./modules.js'),
8 Identd
= require('./identd.js');
12 process
.chdir(__dirname
+ '/../');
16 // If we're not running in the forground and we have a log file.. switch
17 // console.log to output to a file
18 if (process
.argv
.indexOf('-f') === -1 && global
.config
&& global
.config
.log
) {
20 var log_file_name
= global
.config
.log
;
22 if (log_file_name
[0] !== '/') {
23 log_file_name
= __dirname
+ '/../' + log_file_name
;
28 console
.log = function() {
29 var logfile
= fs
.openSync(log_file_name
, 'a'),
32 out
= util
.format
.apply(util
, arguments
);
34 // Make sure we out somthing to log and we have an open file
35 if (!out
|| !logfile
) return;
38 fs
.writeSync(logfile
, out
, null);
40 fs
.closeSync(logfile
);
47 // Make sure we have a valid config file and at least 1 server
48 if (!global
.config
|| Object
.keys(global
.config
).length
=== 0) {
49 console
.log('Couldn\'t find a valid config.js file (Did you copy the config.example.js file yet?)');
53 if ((!global
.config
.servers
) || (global
.config
.servers
.length
< 1)) {
54 console
.log('No servers defined in config file');
61 // Create a plugin interface
62 global
.modules
= new modules
.Publisher();
64 // Register as the active interface
65 modules
.registerPublisher(global
.modules
);
67 // Load any modules in the config
68 if (global
.config
.module_dir
) {
69 (global
.config
.modules
|| []).forEach(function (module_name
) {
70 if (modules
.load(module_name
)) {
71 console
.log('Module ' + module_name
+ ' loaded successfuly');
73 console
.log('Module ' + module_name
+ ' failed to load');
81 // Holder for all the connected clients
83 clients
: Object
.create(null),
84 addresses
: Object
.create(null),
86 // Local and foriegn port pairs for identd lookups
87 // {'65483_6667': client_obj, '54356_6697': client_obj}
90 add: function (client
) {
91 this.clients
[client
.hash
] = client
;
92 if (typeof this.addresses
[client
.real_address
] === 'undefined') {
93 this.addresses
[client
.real_address
] = Object
.create(null);
95 this.addresses
[client
.real_address
][client
.hash
] = client
;
98 remove: function (client
) {
99 if (typeof this.clients
[client
.hash
] !== 'undefined') {
100 delete this.clients
[client
.hash
];
101 delete this.addresses
[client
.real_address
][client
.hash
];
102 if (Object
.keys(this.addresses
[client
.real_address
]).length
< 1) {
103 delete this.addresses
[client
.real_address
];
108 numOnAddress: function (addr
) {
109 if (typeof this.addresses
[addr
] !== 'undefined') {
110 return Object
.keys(this.addresses
[addr
]).length
;
116 broadcastKiwiCommand: function (command
, data
, callback
) {
119 // Get an array of clients for us to work with
120 for (var client
in global
.clients
.clients
) {
121 clients
.push(global
.clients
.clients
[client
]);
125 // Sending of the command in batches
126 var sendCommandBatch = function (list
) {
127 var batch_size
= 100,
130 if (list
.length
>= batch_size
) {
131 // If we have more clients than our batch size, call ourself with the next batch
132 setTimeout(function () {
133 sendCommandBatch(list
.slice(batch_size
));
139 cutoff
= list
.length
;
142 list
.slice(0, cutoff
).forEach(function (client
) {
143 if (!client
.disposed
) {
144 client
.sendKiwiCommand(command
, data
);
148 if (cutoff
=== list
.length
&& typeof callback
=== 'function') {
153 sendCommandBatch(clients
);
158 servers
: Object
.create(null),
160 addConnection: function (connection
) {
161 var host
= connection
.irc_host
.hostname
;
162 if (!this.servers
[host
]) {
163 this.servers
[host
] = [];
165 this.servers
[host
].push(connection
);
168 removeConnection: function (connection
) {
169 var host
= connection
.irc_host
.hostname
170 if (this.servers
[host
]) {
171 this.servers
[host
] = _
.without(this.servers
[host
], connection
);
172 if (this.servers
[host
].length
=== 0) {
173 delete this.servers
[host
];
178 numOnHost: function (host
) {
179 if (this.servers
[host
]) {
180 return this.servers
[host
].length
;
190 * When a new config is loaded, send out an alert to the clients so
191 * so they can reload it
193 config
.on('loaded', function () {
194 global
.clients
.broadcastKiwiCommand('reconfig');
202 if (global
.config
.identd
&& global
.config
.identd
.enabled
) {
203 var identd_resolve_user = function(port_here
, port_there
) {
204 var key
= port_here
.toString() + '_' + port_there
.toString();
206 if (typeof global
.clients
.port_pairs
[key
] == 'undefined') {
210 return global
.clients
.port_pairs
[key
].username
;
213 var identd_server
= new Identd({
214 bind_addr
: global
.config
.identd
.address
,
215 bind_port
: global
.config
.identd
.port
,
216 user_id
: identd_resolve_user
219 identd_server
.start();
230 // Start up a weblistener for each found in the config
231 _
.each(global
.config
.servers
, function (server
) {
232 var wl
= new WebListener(server
, global
.config
.transports
);
234 wl
.on('connection', function (client
) {
238 wl
.on('client_dispose', function (client
) {
239 clients
.remove(client
);
242 wl
.on('listening', function () {
243 console
.log('Listening on %s:%s %s SSL', server
.address
, server
.port
, (server
.ssl
? 'with' : 'without'));
244 webListenerRunning();
247 wl
.on('error', function (err
) {
248 console
.log('Error listening on %s:%s: %s', server
.address
, server
.port
, err
.code
);
249 // TODO: This should probably be refactored. ^JA
250 webListenerRunning();
254 // Once all the listeners are listening, set the processes UID/GID
255 var num_listening
= 0;
256 function webListenerRunning() {
258 if (num_listening
=== global
.config
.servers
.length
) {
271 process
.title
= 'kiwiirc';
274 function setProcessUid() {
275 if ((global
.config
.group
) && (global
.config
.group
!== '')) {
276 process
.setgid(global
.config
.group
);
278 if ((global
.config
.user
) && (global
.config
.user
!== '')) {
279 process
.setuid(global
.config
.user
);
284 // Make sure Kiwi doesn't simply quit on an exception
285 process
.on('uncaughtException', function (e
) {
286 console
.log('[Uncaught exception] ' + e
);
287 console
.log(e
.stack
);
291 process
.on('SIGUSR1', function() {
292 if (config
.loadConfig()) {
293 console
.log('New config file loaded');
295 console
.log("No new config file was loaded");
300 process
.on('SIGUSR2', function() {
301 console
.log('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
302 console
.log('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
307 * Listen for runtime commands
310 process
.stdin
.resume();
311 process
.stdin
.on('data', function (buffered
) {
312 var data
= buffered
.toString().trim(),
313 data_parts
= data
.split(' '),
314 cmd
= data_parts
[0] || null;
318 console
.log('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
319 console
.log('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
323 if (config
.loadConfig()) {
324 console
.log('New config file loaded');
326 console
.log("No new config file was loaded");
335 console
.log('Rehashed');
342 var num_clients
= _
.size(global
.clients
.clients
),
343 packet
= {}, parts_idx
;
345 if (num_clients
=== 0) {
346 console
.log('No connected clients');
350 // For each word in the line minus the last, add it to the packet
351 for(parts_idx
=1; parts_idx
<data_parts
.length
-1; parts_idx
++){
352 packet
[data_parts
[parts_idx
]] = true;
355 packet
.kiwi_server
= data_parts
[parts_idx
];
357 console
.log('Broadcasting jumpserver to ' + num_clients
.toString() + ' clients..');
358 global
.clients
.broadcastKiwiCommand('jumpserver', packet
, function() {
359 console
.log('Broadcast complete.');
366 console
.log('Unrecognised command: ' + data
);