(opt, cb)
| 5 | var debug = require('debug')('localtunnel-server'); |
| 6 | |
| 7 | var Proxy = function(opt, cb) { |
| 8 | if (!(this instanceof Proxy)) { |
| 9 | return new Proxy(opt, cb); |
| 10 | } |
| 11 | |
| 12 | var self = this; |
| 13 | |
| 14 | self.sockets = []; |
| 15 | self.waiting = []; |
| 16 | |
| 17 | var id = opt.id; |
| 18 | |
| 19 | // default max is 10 |
| 20 | var max_tcp_sockets = opt.max_tcp_sockets || 10; |
| 21 | |
| 22 | // new tcp server to service requests for this client |
| 23 | var client_server = net.createServer(); |
| 24 | |
| 25 | client_server.on('error', function(err) { |
| 26 | if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | log.error(err); |
| 31 | }); |
| 32 | |
| 33 | // track initial user connection setup |
| 34 | var conn_timeout; |
| 35 | |
| 36 | // user has 5 seconds to connect before their slot is given up |
| 37 | function maybe_tcp_close() { |
| 38 | clearTimeout(conn_timeout); |
| 39 | conn_timeout = setTimeout(function() { |
| 40 | |
| 41 | // sometimes the server is already closed but the event has not fired? |
| 42 | try { |
| 43 | clearTimeout(conn_timeout); |
| 44 | client_server.close(); |
| 45 | } catch (err) { |
| 46 | cleanup(); |
| 47 | } |
| 48 | }, 5000); |
| 49 | } |
| 50 | |
| 51 | maybe_tcp_close(); |
| 52 | |
| 53 | function cleanup() { |
| 54 | debug('closed tcp socket for client(%s)', id); |
| 55 | |
| 56 | clearTimeout(conn_timeout); |
| 57 | |
| 58 | // clear waiting by ending responses, (requests?) |
| 59 | self.waiting.forEach(function(waiting) { |
| 60 | waiting(null); |
| 61 | }); |
| 62 | |
| 63 | self.emit('end'); |
| 64 | } |
no test coverage detected