mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 13:35:53 +00:00
refactor to use http agent interface for tunnels
This commit is contained in:
@@ -1,36 +1,31 @@
|
||||
import Proxy from './Proxy';
|
||||
import { hri } from 'human-readable-ids';
|
||||
import Debug from 'debug';
|
||||
|
||||
// maybe remove?
|
||||
import on_finished from 'on-finished';
|
||||
import http from 'http';
|
||||
import pump from 'pump';
|
||||
import { hri } from "human-readable-ids";
|
||||
|
||||
import BindingAgent from './BindingAgent';
|
||||
|
||||
const NoOp = () => {};
|
||||
import Client from './Client';
|
||||
import TunnelAgent from './TunnelAgent';
|
||||
|
||||
// Manage sets of clients
|
||||
//
|
||||
// A client is a "user session" established to service a remote localtunnel client
|
||||
class ClientManager {
|
||||
constructor(opt) {
|
||||
this.opt = opt;
|
||||
|
||||
this.reqId = 0;
|
||||
this.opt = opt || {};
|
||||
|
||||
// id -> client instance
|
||||
this.clients = Object.create(null);
|
||||
this.clients = new Map();
|
||||
|
||||
// statistics
|
||||
this.stats = {
|
||||
tunnels: 0
|
||||
};
|
||||
|
||||
this.debug = Debug('lt:ClientManager');
|
||||
}
|
||||
|
||||
// create a new tunnel with `id`
|
||||
// if the id is already used, a random id is assigned
|
||||
async newClient (id) {
|
||||
// if the tunnel could not be created, throws an error
|
||||
async newClient(id) {
|
||||
const clients = this.clients;
|
||||
const stats = this.stats;
|
||||
|
||||
@@ -39,161 +34,68 @@ class ClientManager {
|
||||
id = hri.random();
|
||||
}
|
||||
|
||||
const popt = {
|
||||
id: id,
|
||||
max_tcp_sockets: this.opt.max_tcp_sockets
|
||||
};
|
||||
const maxSockets = this.opt.max_tcp_sockets;
|
||||
const agent = new TunnelAgent({
|
||||
maxSockets: 10,
|
||||
});
|
||||
|
||||
const client = Proxy(popt);
|
||||
agent.on('online', () => {
|
||||
this.debug('client online %s', id);
|
||||
});
|
||||
|
||||
agent.on('offline', () => {
|
||||
// TODO(roman): grace period for re-connecting
|
||||
// this period is short as the client is expected to maintain connections actively
|
||||
// if they client does not reconnect on a dropped connection they need to re-establish
|
||||
this.debug('client offline %s', id);
|
||||
this.removeClient(id);
|
||||
});
|
||||
|
||||
// TODO(roman): an agent error removes the client, the user needs to re-connect?
|
||||
// how does a user realize they need to re-connect vs some random client being assigned same port?
|
||||
agent.once('error', (err) => {
|
||||
this.removeClient(id);
|
||||
});
|
||||
|
||||
const client = new Client({ agent });
|
||||
|
||||
// add to clients map immediately
|
||||
// avoiding races with other clients requesting same id
|
||||
clients[id] = client;
|
||||
|
||||
client.on('end', () => {
|
||||
--stats.tunnels;
|
||||
delete clients[id];
|
||||
});
|
||||
// try/catch used here to remove client id
|
||||
try {
|
||||
const info = await agent.listen();
|
||||
++stats.tunnels;
|
||||
return {
|
||||
id: id,
|
||||
port: info.port,
|
||||
max_conn_count: maxSockets,
|
||||
};
|
||||
}
|
||||
catch (err) {
|
||||
this.removeClient(id);
|
||||
// rethrow error for upstream to handle
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// each local client has a tcp server to link with the remove localtunnel client
|
||||
// this starts the server and waits until it is listening
|
||||
client.start((err, info) => {
|
||||
if (err) {
|
||||
// clear the reserved client id
|
||||
delete clients[id];
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
++stats.tunnels;
|
||||
info.id = id;
|
||||
resolve(info);
|
||||
});
|
||||
});
|
||||
removeClient(id) {
|
||||
const client = this.clients[id];
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
--this.stats.tunnels;
|
||||
delete this.clients[id];
|
||||
client.agent.destroy();
|
||||
}
|
||||
|
||||
hasClient(id) {
|
||||
return this.clients[id];
|
||||
}
|
||||
|
||||
// handle http request
|
||||
handleRequest(clientId, req, res) {
|
||||
const client = this.clients[clientId];
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
const reqId = this.reqId;
|
||||
this.reqId = this.reqId + 1;
|
||||
|
||||
let endRes = () => {
|
||||
endRes = NoOp;
|
||||
res.end();
|
||||
};
|
||||
|
||||
on_finished(res, () => {
|
||||
endRes = NoOp;
|
||||
});
|
||||
|
||||
client.nextSocket((clientSocket) => {
|
||||
// response ended before we even got a socket to respond on
|
||||
if (endRes === NoOp) {
|
||||
return;
|
||||
}
|
||||
|
||||
// happens when client upstream is disconnected (or disconnects)
|
||||
// and the proxy iterates the waiting list and clears the callbacks
|
||||
// we gracefully inform the user and kill their conn
|
||||
// without this, the browser will leave some connections open
|
||||
// and try to use them again for new requests
|
||||
// TODO(roman) we could instead have a timeout above
|
||||
// if no socket becomes available within some time,
|
||||
// we just tell the user no resource available to service request
|
||||
if (!clientSocket) {
|
||||
endRes();
|
||||
return;
|
||||
}
|
||||
|
||||
const agent = new BindingAgent({
|
||||
socket: clientSocket,
|
||||
});
|
||||
|
||||
const opt = {
|
||||
path: req.url,
|
||||
agent: agent,
|
||||
method: req.method,
|
||||
headers: req.headers
|
||||
};
|
||||
|
||||
return new Promise((resolve) => {
|
||||
// what if error making this request?
|
||||
const clientReq = http.request(opt, (clientRes) => {
|
||||
// write response code and headers
|
||||
res.writeHead(clientRes.statusCode, clientRes.headers);
|
||||
|
||||
// when this pump is done, we end our response
|
||||
pump(clientRes, res, (err) => {
|
||||
endRes();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
// we don't care about when this ends, only if there is error
|
||||
pump(req, clientReq, (err) => {
|
||||
if (err) {
|
||||
endRes();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// handle http upgrade
|
||||
handleUpgrade(clientId, req, sock) {
|
||||
const client = this.clients[clientId];
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
client.nextSocket(async (clientSocket) => {
|
||||
if (!sock.readable || !sock.writable) {
|
||||
sock.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// happens when client upstream is disconnected (or disconnects)
|
||||
// and the proxy iterates the waiting list and clears the callbacks
|
||||
// we gracefully inform the user and kill their conn
|
||||
// without this, the browser will leave some connections open
|
||||
// and try to use them again for new requests
|
||||
// TODO(roman) we could instead have a timeout above
|
||||
// if no socket becomes available within some time,
|
||||
// we just tell the user no resource available to service request
|
||||
if (!clientSocket) {
|
||||
sock.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// websocket requests are special in that we simply re-create the header info
|
||||
// then directly pipe the socket data
|
||||
// avoids having to rebuild the request and handle upgrades via the http client
|
||||
const arr = [`${req.method} ${req.url} HTTP/${req.httpVersion}`];
|
||||
for (let i=0 ; i < (req.rawHeaders.length-1) ; i+=2) {
|
||||
arr.push(`${req.rawHeaders[i]}: ${req.rawHeaders[i+1]}`);
|
||||
}
|
||||
|
||||
arr.push('');
|
||||
arr.push('');
|
||||
|
||||
clientSocket.pipe(sock).pipe(clientSocket);
|
||||
clientSocket.write(arr.join('\r\n'));
|
||||
|
||||
await new Promise((resolve) => {
|
||||
sock.once('end', resolve);
|
||||
});
|
||||
});
|
||||
getClient(id) {
|
||||
return this.clients[id];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user