1 var ws
= require('socket.io'),
2 events
= require('events'),
3 http
= require('http'),
4 https
= require('https'),
5 util
= require('util'),
10 spdy
= require('spdy'),
11 Client
= require('./client.js').Client
,
12 HttpHandler
= require('./httphandler.js').HttpHandler
,
13 rehash
= require('./rehash.js'),
14 range_check
= require('range_check');
18 rehash
.on('rehashed', function (files
) {
19 Client
= require('./client.js').Client
;
20 HttpHandler
= require('./httphandler.js').HttpHandler
;
24 // Instance of HttpHandler
28 var WebListener = function (web_config
, transports
) {
29 var hs
, opts
, ws_opts
,
33 events
.EventEmitter
.call(this);
35 http_handler
= new HttpHandler(web_config
);
37 // Standard options for the socket.io connections
46 key
: fs
.readFileSync(web_config
.ssl_key
),
47 cert
: fs
.readFileSync(web_config
.ssl_cert
)
50 // Do we have an intermediate certificate?
51 if (typeof web_config
.ssl_ca
!== 'undefined') {
53 if (typeof web_config
.ssl_ca
.map
!== 'undefined') {
54 opts
.ca
= web_config
.ssl_ca
.map(function (f
) { return fs
.readFileSync(f
); });
57 opts
.ca
= fs
.readFileSync(web_config
.ssl_ca
);
61 hs
= spdy
.createServer(opts
, handleHttpRequest
);
63 // Start socket.io listening on this weblistener
64 this.ws
= ws
.listen(hs
, _
.extend({ssl
: true}, ws_opts
));
65 hs
.listen(web_config
.port
, web_config
.address
, function () {
66 that
.emit('listening');
70 // Start some plain-text server up
71 hs
= http
.createServer(handleHttpRequest
);
73 // Start socket.io listening on this weblistener
74 this.ws
= ws
.listen(hs
, _
.extend({ssl
: false}, ws_opts
));
75 hs
.listen(web_config
.port
, web_config
.address
, function () {
76 that
.emit('listening');
80 hs
.on('error', function (err
) {
81 that
.emit('error', err
);
84 this.ws
.enable('browser client minification');
85 this.ws
.enable('browser client etag');
86 this.ws
.set('transports', transports
);
87 this.ws
.set('resource', (global
.config
.http_base_path
|| '') + '/transport');
89 this.ws
.of('/kiwi').authorization(authoriseConnection
)
90 .on('connection', function () {
91 newConnection
.apply(that
, arguments
);
94 this.ws
.of('/kiwi').on('error', console
.log
);
96 util
.inherits(WebListener
, events
.EventEmitter
);
100 function handleHttpRequest(request
, response
) {
101 var uri
= url
.parse(request
.url
, true);
103 // If this isn't a socket.io request, pass it onto the http handler
104 if (uri
.pathname
.substr(0, 10) !== '/socket.io') {
105 http_handler
.serve(request
, response
);
111 * Get the reverse DNS entry for this connection.
112 * Used later on for webirc, etc functionality
114 function authoriseConnection(handshakeData
, callback
) {
115 var address
= handshakeData
.address
.address
;
117 // If a forwarded-for header is found, switch the source address
118 if (handshakeData
.headers
[global
.config
.http_proxy_ip_header
|| 'x-forwarded-for']) {
119 // Check we're connecting from a whitelisted proxy
120 if (!global
.config
.http_proxies
|| !range_check
.in_range(address
, global
.config
.http_proxies
)) {
121 console
.log('Unlisted proxy:', address
);
122 callback(null, false);
126 // We're sent from a whitelisted proxy, replace the hosts
127 address
= handshakeData
.headers
['x-forwarded-for'];
130 handshakeData
.real_address
= address
;
132 // If enabled, don't go over the connection limit
133 if (global
.config
.max_client_conns
&& global
.config
.max_client_conns
> 0) {
134 if (global
.clients
.numOnAddress(address
) + 1 > global
.config
.max_client_conns
) {
135 return callback(null, false);
141 dns
.reverse(address
, function (err
, domains
) {
142 if (err
|| domains
.length
=== 0) {
143 handshakeData
.revdns
= address
;
145 handshakeData
.revdns
= _
.first(domains
) || address
;
148 // All is well, authorise the connection
149 callback(null, true);
152 handshakeData
.revdns
= address
;
153 callback(null, true);
157 function newConnection(websocket
) {
158 var client
, that
= this;
159 client
= new Client(websocket
);
160 client
.on('dispose', function () {
161 that
.emit('client_dispose', this);
163 this.emit('connection', client
);
170 module
.exports
= WebListener
;