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'), |
44197e59 | 10 | Client = require('./client.js').Client, |
186531ed D |
11 | HttpHandler = require('./http-handler.js').HttpHandler; |
12 | ||
13 | // Instance of HttpHandler | |
14 | var http_handler; | |
15 | ||
a8bf3ea4 JA |
16 | |
17 | var WebListener = function (config, transports) { | |
186531ed | 18 | var hs, |
a8bf3ea4 JA |
19 | opts, |
20 | that = this; | |
21 | ||
22 | events.EventEmitter.call(this); | |
23 | ||
186531ed | 24 | http_handler = new HttpHandler(config); |
a8bf3ea4 JA |
25 | |
26 | if (config.secure) { | |
27 | opts = { | |
28 | key: fs.readFileSync(__dirname + '/' + config.ssl_key), | |
29 | cert: fs.readFileSync(__dirname + '/' + config.ssl_cert) | |
30 | }; | |
186531ed | 31 | |
a8bf3ea4 JA |
32 | // Do we have an intermediate certificate? |
33 | if (typeof config.ssl_ca !== 'undefined') { | |
34 | opts.ca = fs.readFileSync(__dirname + '/' + config.ssl_ca); | |
35 | } | |
186531ed D |
36 | |
37 | ||
38 | hs = https.createServer(opts, handleHttpRequest); | |
a8bf3ea4 | 39 | |
186531ed | 40 | // Start socket.io listening on this weblistener |
a8bf3ea4 JA |
41 | this.ws = ws.listen(hs, {secure: true}); |
42 | hs.listen(config.port, config.address); | |
186531ed | 43 | |
a8bf3ea4 JA |
44 | console.log('Listening on ' + config.address + ':' + config.port.toString() + ' with SSL'); |
45 | } else { | |
186531ed | 46 | |
a8bf3ea4 | 47 | // Start some plain-text server up |
186531ed D |
48 | hs = http.createServer(handleHttpRequest); |
49 | ||
50 | // Start socket.io listening on this weblistener | |
a8bf3ea4 JA |
51 | this.ws = ws.listen(hs, {secure: false}); |
52 | hs.listen(config.port, config.address); | |
186531ed | 53 | |
a8bf3ea4 JA |
54 | console.log('Listening on ' + config.address + ':' + config.port.toString() + ' without SSL'); |
55 | } | |
56 | ||
57 | this.ws.set('log level', 1); | |
58 | this.ws.enable('browser client minification'); | |
59 | this.ws.enable('browser client etag'); | |
60 | this.ws.set('transports', transports); | |
61 | ||
62 | this.ws.of('/kiwi').authorization(authorisation).on('connection', function () { | |
63 | connection.apply(that, arguments); | |
64 | }); | |
65 | this.ws.of('/kiwi').on('error', console.log); | |
66 | }; | |
67 | util.inherits(WebListener, events.EventEmitter); | |
68 | ||
186531ed D |
69 | |
70 | ||
71 | function handleHttpRequest(request, response) { | |
72 | var uri = url.parse(request.url, true); | |
73 | ||
74 | // If this isn't a socket.io request, pass it onto the http handler | |
75 | if (uri.pathname.substr(0, 10) !== '/socket.io') { | |
76 | http_handler.serve(request, response); | |
77 | } | |
78 | } | |
79 | ||
a8bf3ea4 JA |
80 | |
81 | var authorisation = function (handshakeData, callback) { | |
82 | dns.reverse(handshakeData.address.address, function (err, domains) { | |
83 | handshakeData.revdns = (err) ? handshakeData.address.address : _.first(domains); | |
84 | callback(null, true); | |
85 | }); | |
86 | }; | |
87 | ||
88 | var connection = function (websocket) { | |
89 | //console.log(websocket); | |
90 | this.emit('connection', new Client(websocket)); | |
91 | }; | |
186531ed D |
92 | |
93 | ||
94 | ||
95 | ||
96 | ||
97 | module.exports = WebListener; |