1 var ws
= require('socket.io'),
2 events
= require('events'),
3 http
= require('http'),
4 https
= require('https'),
5 util
= require('util'),
10 Client
= require('./client.js').Client
,
11 HttpHandler
= require('./httphandler.js').HttpHandler
,
12 rehash
= require('./rehash.js');
16 rehash
.on('rehashed', function (files
) {
17 Client
= require('./client.js').Client
;
18 HttpHandler
= require('./httphandler.js').HttpHandler
;
22 // Instance of HttpHandler
26 var WebListener = function (web_config
, transports
) {
27 var hs
, opts
, ws_opts
,
31 events
.EventEmitter
.call(this);
33 http_handler
= new HttpHandler(web_config
);
35 // Standard options for the socket.io connections
44 key
: fs
.readFileSync(__dirname
+ '/' + web_config
.ssl_key
),
45 cert
: fs
.readFileSync(__dirname
+ '/' + web_config
.ssl_cert
)
48 // Do we have an intermediate certificate?
49 if (typeof web_config
.ssl_ca
!== 'undefined') {
50 opts
.ca
= fs
.readFileSync(__dirname
+ '/' + web_config
.ssl_ca
);
54 hs
= https
.createServer(opts
, handleHttpRequest
);
56 // Start socket.io listening on this weblistener
57 this.ws
= ws
.listen(hs
, _
.extend({ssl
: true}, ws_opts
));
58 hs
.listen(web_config
.port
, web_config
.address
);
60 console
.log('Listening on ' + web_config
.address
+ ':' + web_config
.port
.toString() + ' with SSL');
63 // Start some plain-text server up
64 hs
= http
.createServer(handleHttpRequest
);
66 // Start socket.io listening on this weblistener
67 this.ws
= ws
.listen(hs
, _
.extend({ssl
: false}, ws_opts
));
68 hs
.listen(web_config
.port
, web_config
.address
);
70 console
.log('Listening on ' + web_config
.address
+ ':' + web_config
.port
.toString() + ' without SSL');
73 this.ws
.enable('browser client minification');
74 this.ws
.enable('browser client etag');
75 this.ws
.set('transports', transports
);
76 this.ws
.set('resource', (global
.config
.http_base_path
|| '') + '/transport');
78 this.ws
.of('/kiwi').authorization(authoriseConnection
)
79 .on('connection', function () {
80 newConnection
.apply(that
, arguments
);
83 this.ws
.of('/kiwi').on('error', console
.log
);
85 util
.inherits(WebListener
, events
.EventEmitter
);
89 function handleHttpRequest(request
, response
) {
90 var uri
= url
.parse(request
.url
, true);
92 // If this isn't a socket.io request, pass it onto the http handler
93 if (uri
.pathname
.substr(0, 10) !== '/socket.io') {
94 http_handler
.serve(request
, response
);
100 * Get the reverse DNS entry for this connection.
101 * Used later on for webirc, etc functionality
103 function authoriseConnection(handshakeData
, callback
) {
104 var address
= handshakeData
.address
.address
;
106 // If a forwarded-for header is found, switch the source address
107 if (handshakeData
.headers
['x-forwarded-for']) {
108 // Check we're connecting from a whitelisted proxy
109 if (!global
.config
.http_proxies
|| global
.config
.http_proxies
.indexOf(address
) < 0) {
110 console
.log('Unlisted proxy:', address
);
111 callback(null, false);
115 // We're sent from a whitelisted proxy, replace the hosts
116 address
= handshakeData
.headers
['x-forwarded-for'];
119 handshakeData
.real_address
= address
;
121 // If enabled, don't go over the connection limit
122 if (global
.config
.max_client_conns
&& global
.config
.max_client_conns
> 0) {
123 if (global
.clients
.numOnAddress(address
) + 1 > global
.config
.max_client_conns
) {
124 return callback(null, false);
128 dns
.reverse(address
, function (err
, domains
) {
129 if (err
|| domains
.length
=== 0) {
130 handshakeData
.revdns
= address
;
132 handshakeData
.revdns
= _
.first(domains
) || address
;
135 // All is well, authorise the connection
136 callback(null, true);
140 function newConnection(websocket
) {
141 var client
, that
= this;
142 client
= new Client(websocket
);
143 client
.on('destroy', function () {
144 that
.emit('destroy', this);
146 this.emit('connection', client
);
153 module
.exports
= WebListener
;