forked from localtunnel/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
198 lines (152 loc) · 5.13 KB
/
proxy.js
File metadata and controls
198 lines (152 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var net = require('net');
var portastic = require('portastic');
var EventEmitter = require('events').EventEmitter;
var log = require('bookrc');
var debug = require('debug')('localtunnel-server');
var Proxy = function(opt, cb) {
if (!(this instanceof Proxy)) {
return new Proxy(opt, cb);
}
var self = this;
self.sockets = [];
self.waiting = [];
var id = opt.id;
// default max is 10
var max_tcp_sockets = opt.max_tcp_sockets || 10;
// default min port is 49152
var min_port = opt.min_port || 49152;
// default max port is 65535
var max_port = opt.max_port || 65535;
// new tcp server to service requests for this client
var client_server = net.createServer();
client_server.on('error', function(err) {
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
log.error(err);
});
// track initial user connection setup
var conn_timeout;
// user has 5 seconds to connect before their slot is given up
function maybe_tcp_close() {
clearTimeout(conn_timeout);
conn_timeout = setTimeout(function() {
// sometimes the server is already closed but the event has not fired?
try {
clearTimeout(conn_timeout);
client_server.close();
} catch (err) {
cleanup();
}
}, 5000);
}
maybe_tcp_close();
function cleanup() {
debug('closed tcp socket for client(%s)', id);
clearTimeout(conn_timeout);
// clear waiting by ending responses, (requests?)
self.waiting.forEach(function(waiting) {
waiting(null);
});
self.emit('end');
}
// no longer accepting connections for this id
client_server.on('close', cleanup);
// new tcp connection from lt client
client_server.on('connection', function(socket) {
// no more socket connections allowed
if (self.sockets.length >= max_tcp_sockets) {
return socket.end();
}
debug('new connection on port: %s', id);
// a single connection is enough to keep client id slot open
clearTimeout(conn_timeout);
socket.once('close', function(had_error) {
debug('client %s closed socket (error: %s)', id, had_error);
// what if socket was servicing a request at this time?
// then it will be put back in available after right?
// remove this socket
var idx = self.sockets.indexOf(socket);
if (idx >= 0) {
self.sockets.splice(idx, 1);
}
// need to track total sockets, not just active available
debug('remaining client sockets: %s', self.sockets.length);
// no more sockets for this ident
if (self.sockets.length === 0) {
debug('all client(%s) sockets disconnected', id);
maybe_tcp_close();
}
});
// close will be emitted after this
socket.on('error', function(err) {
// we don't log here to avoid logging crap for misbehaving clients
socket.destroy();
});
self.sockets.push(socket);
var wait_cb = self.waiting.shift();
if (wait_cb) {
debug('handling queued request');
self.next_socket(wait_cb);
}
});
portastic.find({
min: min_port,
max: max_port,
retrieve: 1
})
.then(function(ports) {
if (!ports[0]) {
throw new Error('no more free ports in range %d:%d', min_port, max_port);
}
var port = ports[0]
client_server.listen(port, function () {
debug('tcp server listening on port: %d', port);
cb(null, {
// port for lt client tcp connections
port: port,
// maximum number of tcp connections allowed by lt client
max_conn_count: max_tcp_sockets
});
});
});
};
Proxy.prototype.__proto__ = EventEmitter.prototype;
Proxy.prototype.next_socket = function(cb) {
var self = this;
// socket is a tcp connection back to the user hosting the site
var sock = self.sockets.shift();
// TODO how to handle queue?
// queue request
if (!sock) {
debug('no more client, queue callback');
return self.waiting.push(cb);
}
var done_called = false;
// put the socket back
function done() {
if (done_called) {
throw new Error('done called multiple times');
}
done_called = true;
if (!sock.destroyed) {
debug('retuning socket');
self.sockets.push(sock);
}
// no sockets left to process waiting requests
if (self.sockets.length === 0) {
return;
}
var wait = self.waiting.shift();
debug('processing queued cb');
if (wait) {
return self.next_socket(cb);
}
};
debug('processing request');
cb(sock, done);
};
Proxy.prototype._done = function() {
var self = this;
};
module.exports = Proxy;