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'),
13 range_check
= require('range_check');
17 rehash
.on('rehashed', function (files
) {
18 Client
= require('./client.js').Client
;
19 HttpHandler
= require('./httphandler.js').HttpHandler
;
23 // Instance of HttpHandler
27 var WebListener = function (web_config
, transports
) {
28 var hs
, opts
, ws_opts
,
32 events
.EventEmitter
.call(this);
34 http_handler
= new HttpHandler(web_config
);
36 // Standard options for the socket.io connections
45 key
: fs
.readFileSync(web_config
.ssl_key
),
46 cert
: fs
.readFileSync(web_config
.ssl_cert
)
49 // Do we have an intermediate certificate?
50 if (typeof web_config
.ssl_ca
!== 'undefined') {
52 if (typeof web_config
.ssl_ca
.map
!== 'undefined') {
53 opts
.ca
= web_config
.ssl_ca
.map(function (f
) { return fs
.readFileSync(f
); });
56 opts
.ca
= fs
.readFileSync(web_config
.ssl_ca
);
60 hs
= https
.createServer(opts
, handleHttpRequest
);
62 // Start socket.io listening on this weblistener
63 this.ws
= ws
.listen(hs
, _
.extend({ssl
: true}, ws_opts
));
64 hs
.listen(web_config
.port
, web_config
.address
, function () {
65 that
.emit('listening');
69 // Start some plain-text server up
70 hs
= http
.createServer(handleHttpRequest
);
72 // Start socket.io listening on this weblistener
73 this.ws
= ws
.listen(hs
, _
.extend({ssl
: false}, ws_opts
));
74 hs
.listen(web_config
.port
, web_config
.address
, function () {
75 that
.emit('listening');
79 hs
.on('error', function (err
) {
80 that
.emit('error', err
);
83 this.ws
.enable('browser client minification');
84 this.ws
.enable('browser client etag');
85 this.ws
.set('transports', transports
);
86 this.ws
.set('resource', (global
.config
.http_base_path
|| '') + '/transport');
88 this.ws
.of('/kiwi').authorization(authoriseConnection
)
89 .on('connection', function () {
90 newConnection
.apply(that
, arguments
);
93 this.ws
.of('/kiwi').on('error', console
.log
);
95 util
.inherits(WebListener
, events
.EventEmitter
);
99 function handleHttpRequest(request
, response
) {
100 var uri
= url
.parse(request
.url
, true);
102 // If this isn't a socket.io request, pass it onto the http handler
103 if (uri
.pathname
.substr(0, 10) !== '/socket.io') {
104 http_handler
.serve(request
, response
);
110 * Get the reverse DNS entry for this connection.
111 * Used later on for webirc, etc functionality
113 function authoriseConnection(handshakeData
, callback
) {
114 var address
= handshakeData
.address
.address
;
116 // If a forwarded-for header is found, switch the source address
117 if (handshakeData
.headers
[global
.config
.http_proxy_ip_header
|| 'x-forwarded-for']) {
118 // Check we're connecting from a whitelisted proxy
119 if (!global
.config
.http_proxies
|| !range_check
.in_range(address
, global
.config
.http_proxies
)) {
120 console
.log('Unlisted proxy:', address
);
121 callback(null, false);
125 // We're sent from a whitelisted proxy, replace the hosts
126 address
= handshakeData
.headers
['x-forwarded-for'];
129 handshakeData
.real_address
= address
;
131 // If enabled, don't go over the connection limit
132 if (global
.config
.max_client_conns
&& global
.config
.max_client_conns
> 0) {
133 if (global
.clients
.numOnAddress(address
) + 1 > global
.config
.max_client_conns
) {
134 return callback(null, false);
140 dns
.reverse(address
, function (err
, domains
) {
141 if (err
|| domains
.length
=== 0) {
142 handshakeData
.revdns
= address
;
144 handshakeData
.revdns
= _
.first(domains
) || address
;
147 // All is well, authorise the connection
148 callback(null, true);
151 handshakeData
.revdns
= address
;
152 callback(null, true);
156 function newConnection(websocket
) {
157 var client
, that
= this;
158 client
= new Client(websocket
);
159 client
.on('dispose', function () {
160 that
.emit('client_dispose', this);
162 this.emit('connection', client
);
169 module
.exports
= WebListener
;