1 var ws
= require('socket.io'),
2 events
= require('events'),
3 http
= require('http'),
4 https
= require('https'),
5 util
= require('util'),
9 _
= require('underscore'),
10 config
= require('./configuration.js'),
11 Client
= require('./client.js').Client
,
12 HttpHandler
= require('./httphandler.js').HttpHandler
,
13 rehash
= require('./rehash.js');
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
) {
32 events
.EventEmitter
.call(this);
34 http_handler
= new HttpHandler(web_config
);
38 key
: fs
.readFileSync(__dirname
+ '/' + web_config
.ssl_key
),
39 cert
: fs
.readFileSync(__dirname
+ '/' + web_config
.ssl_cert
)
42 // Do we have an intermediate certificate?
43 if (typeof web_config
.ssl_ca
!== 'undefined') {
44 opts
.ca
= fs
.readFileSync(__dirname
+ '/' + web_config
.ssl_ca
);
48 hs
= https
.createServer(opts
, handleHttpRequest
);
50 // Start socket.io listening on this weblistener
51 this.ws
= ws
.listen(hs
, {ssl
: true});
52 hs
.listen(web_config
.port
, web_config
.address
);
54 console
.log('Listening on ' + web_config
.address
+ ':' + web_config
.port
.toString() + ' with SSL');
57 // Start some plain-text server up
58 hs
= http
.createServer(handleHttpRequest
);
60 // Start socket.io listening on this weblistener
61 this.ws
= ws
.listen(hs
, {ssl
: false});
62 hs
.listen(web_config
.port
, web_config
.address
);
64 console
.log('Listening on ' + web_config
.address
+ ':' + web_config
.port
.toString() + ' without SSL');
67 this.ws
.set('log level', 1);
68 this.ws
.enable('browser client minification');
69 this.ws
.enable('browser client etag');
70 this.ws
.set('transports', transports
);
71 this.ws
.set('resource', (config
.get().http_base_path
|| '') + '/transport');
73 this.ws
.of('/kiwi').authorization(authoriseConnection
)
74 .on('connection', function () {
75 newConnection
.apply(that
, arguments
);
78 this.ws
.of('/kiwi').on('error', console
.log
);
80 util
.inherits(WebListener
, events
.EventEmitter
);
84 function handleHttpRequest(request
, response
) {
85 var uri
= url
.parse(request
.url
, true);
87 // If this isn't a socket.io request, pass it onto the http handler
88 if (uri
.pathname
.substr(0, 10) !== '/socket.io') {
89 http_handler
.serve(request
, response
);
95 * Get the reverse DNS entry for this connection.
96 * Used later on for webirc, etc functionality
98 function authoriseConnection(handshakeData
, callback
) {
99 var address
= handshakeData
.address
.address
;
101 // If a forwarded-for header is found, switch the source address
102 if (handshakeData
.headers
['x-forwarded-for']) {
103 // Check we're connecting from a whitelisted proxy
104 if (!config
.get().http_proxies
|| config
.get().http_proxies
.indexOf(address
) < 0) {
105 console
.log('Unlisted proxy:', address
);
106 callback(null, false);
110 // We're sent from a whitelisted proxy, replace the hosts
111 address
= handshakeData
.headers
['x-forwarded-for'];
114 dns
.reverse(address
, function (err
, domains
) {
115 if (err
|| domains
.length
=== 0) {
116 handshakeData
.revdns
= address
;
118 handshakeData
.revdns
= _
.first(domains
) || address
;
121 // All is well, authorise the connection
122 callback(null, true);
126 function newConnection(websocket
) {
127 //console.log(websocket);
128 this.emit('connection', new Client(websocket
));
135 module
.exports
= WebListener
;