Commit | Line | Data |
---|---|---|
a8bf3ea4 JA |
1 | var ws = require('socket.io'), |
2 | events = require('events'), | |
3 | http = require('http'), | |
4 | https = require('https'), | |
5 | util = require('util'), | |
6 | fs = require('fs'), | |
7 | dns = require('dns'), | |
186531ed | 8 | url = require('url'), |
a8bf3ea4 | 9 | _ = require('underscore'), |
8b0eb787 | 10 | config = require('./configuration.js'), |
1286229a D |
11 | Client = require('./client.js').Client, |
12 | HttpHandler = require('./httphandler.js').HttpHandler, | |
13 | rehash = require('./rehash.js'); | |
14 | ||
15 | ||
16 | ||
17 | rehash.on('rehashed', function (files) { | |
18 | Client = require('./client.js').Client; | |
2f1e8a71 | 19 | HttpHandler = require('./httphandler.js').HttpHandler; |
1286229a D |
20 | }); |
21 | ||
186531ed D |
22 | |
23 | // Instance of HttpHandler | |
24 | var http_handler; | |
25 | ||
a8bf3ea4 | 26 | |
8b0eb787 | 27 | var WebListener = function (web_config, transports) { |
186531ed | 28 | var hs, |
a8bf3ea4 JA |
29 | opts, |
30 | that = this; | |
31 | ||
32 | events.EventEmitter.call(this); | |
33 | ||
8b0eb787 | 34 | http_handler = new HttpHandler(web_config); |
a8bf3ea4 | 35 | |
8b0eb787 | 36 | if (web_config.ssl) { |
a8bf3ea4 | 37 | opts = { |
8b0eb787 D |
38 | key: fs.readFileSync(__dirname + '/' + web_config.ssl_key), |
39 | cert: fs.readFileSync(__dirname + '/' + web_config.ssl_cert) | |
a8bf3ea4 | 40 | }; |
186531ed | 41 | |
a8bf3ea4 | 42 | // Do we have an intermediate certificate? |
8b0eb787 D |
43 | if (typeof web_config.ssl_ca !== 'undefined') { |
44 | opts.ca = fs.readFileSync(__dirname + '/' + web_config.ssl_ca); | |
a8bf3ea4 | 45 | } |
186531ed D |
46 | |
47 | ||
48 | hs = https.createServer(opts, handleHttpRequest); | |
a8bf3ea4 | 49 | |
186531ed | 50 | // Start socket.io listening on this weblistener |
3be87cdd | 51 | this.ws = ws.listen(hs, {ssl: true}); |
8b0eb787 | 52 | hs.listen(web_config.port, web_config.address); |
186531ed | 53 | |
8b0eb787 | 54 | console.log('Listening on ' + web_config.address + ':' + web_config.port.toString() + ' with SSL'); |
a8bf3ea4 | 55 | } else { |
186531ed | 56 | |
a8bf3ea4 | 57 | // Start some plain-text server up |
186531ed D |
58 | hs = http.createServer(handleHttpRequest); |
59 | ||
60 | // Start socket.io listening on this weblistener | |
3be87cdd | 61 | this.ws = ws.listen(hs, {ssl: false}); |
8b0eb787 | 62 | hs.listen(web_config.port, web_config.address); |
186531ed | 63 | |
8b0eb787 | 64 | console.log('Listening on ' + web_config.address + ':' + web_config.port.toString() + ' without SSL'); |
a8bf3ea4 JA |
65 | } |
66 | ||
11dbb00f D |
67 | this.ws.set('log level', 0); |
68 | this.ws.set('log color', false); | |
a8bf3ea4 JA |
69 | this.ws.enable('browser client minification'); |
70 | this.ws.enable('browser client etag'); | |
71 | this.ws.set('transports', transports); | |
8b0eb787 | 72 | this.ws.set('resource', (config.get().http_base_path || '') + '/transport'); |
a8bf3ea4 | 73 | |
c6e3ed44 D |
74 | this.ws.of('/kiwi').authorization(authoriseConnection) |
75 | .on('connection', function () { | |
76 | newConnection.apply(that, arguments); | |
77 | } | |
78 | ); | |
a8bf3ea4 JA |
79 | this.ws.of('/kiwi').on('error', console.log); |
80 | }; | |
81 | util.inherits(WebListener, events.EventEmitter); | |
82 | ||
186531ed D |
83 | |
84 | ||
85 | function handleHttpRequest(request, response) { | |
86 | var uri = url.parse(request.url, true); | |
87 | ||
88 | // If this isn't a socket.io request, pass it onto the http handler | |
89 | if (uri.pathname.substr(0, 10) !== '/socket.io') { | |
90 | http_handler.serve(request, response); | |
91 | } | |
92 | } | |
93 | ||
a8bf3ea4 | 94 | |
15fefff7 D |
95 | /** |
96 | * Get the reverse DNS entry for this connection. | |
97 | * Used later on for webirc, etc functionality | |
98 | */ | |
772a4bb6 | 99 | function authoriseConnection(handshakeData, callback) { |
c6e3ed44 D |
100 | var address = handshakeData.address.address; |
101 | ||
102 | // If a forwarded-for header is found, switch the source address | |
103 | if (handshakeData.headers['x-forwarded-for']) { | |
104 | // Check we're connecting from a whitelisted proxy | |
105 | if (!config.get().http_proxies || config.get().http_proxies.indexOf(address) < 0) { | |
106 | console.log('Unlisted proxy:', address); | |
107 | callback(null, false); | |
108 | return; | |
109 | } | |
110 | ||
111 | // We're sent from a whitelisted proxy, replace the hosts | |
112 | address = handshakeData.headers['x-forwarded-for']; | |
113 | } | |
114 | ||
115 | dns.reverse(address, function (err, domains) { | |
15fefff7 | 116 | if (err || domains.length === 0) { |
c6e3ed44 | 117 | handshakeData.revdns = address; |
15fefff7 | 118 | } else { |
c6e3ed44 | 119 | handshakeData.revdns = _.first(domains) || address; |
15fefff7 D |
120 | } |
121 | ||
c6e3ed44 | 122 | // All is well, authorise the connection |
a8bf3ea4 JA |
123 | callback(null, true); |
124 | }); | |
15fefff7 | 125 | } |
a8bf3ea4 | 126 | |
772a4bb6 | 127 | function newConnection(websocket) { |
a8bf3ea4 JA |
128 | //console.log(websocket); |
129 | this.emit('connection', new Client(websocket)); | |
15fefff7 | 130 | } |
186531ed D |
131 | |
132 | ||
133 | ||
134 | ||
135 | ||
136 | module.exports = WebListener; |