Migrate to Bun. Not yet tidy.

This commit is contained in:
tedspare
2024-03-18 23:22:59 -04:00
parent 8744289c30
commit 675b527833
20 changed files with 886 additions and 1729 deletions
-132
View File
@@ -1,132 +0,0 @@
import http from 'http';
import Debug from 'debug';
import pump from 'pump';
import EventEmitter from 'events';
// A client encapsulates req/res handling using an agent
//
// If an agent is destroyed, the request handling will error
// The caller is responsible for handling a failed request
class Client extends EventEmitter {
constructor(options) {
super();
const agent = this.agent = options.agent;
const id = this.id = options.id;
this.debug = Debug(`lt:Client[${this.id}]`);
// client is given a grace period in which they can connect before they are _removed_
this.graceTimeout = setTimeout(() => {
this.close();
}, 1000).unref();
agent.on('online', () => {
this.debug('client online %s', id);
clearTimeout(this.graceTimeout);
});
agent.on('offline', () => {
this.debug('client offline %s', id);
// if there was a previous timeout set, we don't want to double trigger
clearTimeout(this.graceTimeout);
// client is given a grace period in which they can re-connect before they are _removed_
this.graceTimeout = setTimeout(() => {
this.close();
}, 1000).unref();
});
// 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.close();
});
}
stats() {
return this.agent.stats();
}
close() {
clearTimeout(this.graceTimeout);
this.agent.destroy();
this.emit('close');
}
handleRequest(req, res) {
this.debug('> %s', req.url);
const opt = {
path: req.url,
agent: this.agent,
method: req.method,
headers: req.headers
};
const clientReq = http.request(opt, (clientRes) => {
this.debug('< %s', req.url);
// write response code and headers
res.writeHead(clientRes.statusCode, clientRes.headers);
// using pump is deliberate - see the pump docs for why
pump(clientRes, res);
});
// this can happen when underlying agent produces an error
// in our case we 504 gateway error this?
// if we have already sent headers?
clientReq.once('error', (err) => {
// TODO(roman): if headers not sent - respond with gateway unavailable
});
// using pump is deliberate - see the pump docs for why
pump(req, clientReq);
}
handleUpgrade(req, socket) {
this.debug('> [up] %s', req.url);
socket.once('error', (err) => {
// These client side errors can happen if the client dies while we are reading
// We don't need to surface these in our logs.
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
console.error(err);
});
this.agent.createConnection({}, (err, conn) => {
this.debug('< [up] %s', req.url);
// any errors getting a connection mean we cannot service this request
if (err) {
socket.end();
return;
}
// socket met have disconnected while we waiting for a socket
if (!socket.readable || !socket.writable) {
conn.destroy();
socket.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('');
// using pump is deliberate - see the pump docs for why
pump(conn, socket);
pump(socket, conn);
conn.write(arr.join('\r\n'));
});
}
}
export default Client;
-96
View File
@@ -1,96 +0,0 @@
import { hri } from 'human-readable-ids';
import Debug from 'debug';
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 || {};
// id -> client instance
this.clients = new Map();
// statistics
this.stats = {
tunnels: 0
};
this.debug = Debug('lt:ClientManager');
// This is totally wrong :facepalm: this needs to be per-client...
this.graceTimeout = null;
}
// create a new tunnel with `id`
// if the id is already used, a random id is assigned
// if the tunnel could not be created, throws an error
async newClient(id) {
const clients = this.clients;
const stats = this.stats;
// can't ask for id already is use
if (clients[id]) {
id = hri.random();
}
const maxSockets = this.opt.max_tcp_sockets;
const agent = new TunnelAgent({
clientId: id,
maxSockets: 10,
});
const client = new Client({
id,
agent,
});
// add to clients map immediately
// avoiding races with other clients requesting same id
clients[id] = client;
client.once('close', () => {
this.removeClient(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;
}
}
removeClient(id) {
this.debug('removing client: %s', id);
const client = this.clients[id];
if (!client) {
return;
}
--this.stats.tunnels;
delete this.clients[id];
client.close();
}
hasClient(id) {
return !!this.clients[id];
}
getClient(id) {
return this.clients[id];
}
}
export default ClientManager;
-175
View File
@@ -1,175 +0,0 @@
import { Agent } from 'http';
import net from 'net';
import assert from 'assert';
import log from 'book';
import Debug from 'debug';
const DEFAULT_MAX_SOCKETS = 10;
// Implements an http.Agent interface to a pool of tunnel sockets
// A tunnel socket is a connection _from_ a client that will
// service http requests. This agent is usable wherever one can use an http.Agent
class TunnelAgent extends Agent {
constructor(options = {}) {
super({
keepAlive: true,
// only allow keepalive to hold on to one socket
// this prevents it from holding on to all the sockets so they can be used for upgrades
maxFreeSockets: 1,
});
// sockets we can hand out via createConnection
this.availableSockets = [];
// when a createConnection cannot return a socket, it goes into a queue
// once a socket is available it is handed out to the next callback
this.waitingCreateConn = [];
this.debug = Debug(`lt:TunnelAgent[${options.clientId}]`);
// track maximum allowed sockets
this.connectedSockets = 0;
this.maxTcpSockets = options.maxTcpSockets || DEFAULT_MAX_SOCKETS;
// new tcp server to service requests for this client
this.server = net.createServer();
// flag to avoid double starts
this.started = false;
this.closed = false;
}
stats() {
return {
connectedSockets: this.connectedSockets,
};
}
listen() {
const server = this.server;
if (this.started) {
throw new Error('already started');
}
this.started = true;
server.on('close', this._onClose.bind(this));
server.on('connection', this._onConnection.bind(this));
server.on('error', (err) => {
// These errors happen from killed connections, we don't worry about them
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
log.error(err);
});
return new Promise((resolve) => {
server.listen(() => {
const port = server.address().port;
this.debug('tcp server listening on port: %d', port);
resolve({
// port for lt client tcp connections
port: port,
});
});
});
}
_onClose() {
this.closed = true;
this.debug('closed tcp socket');
// flush any waiting connections
for (const conn of this.waitingCreateConn) {
conn(new Error('closed'), null);
}
this.waitingCreateConn = [];
this.emit('end');
}
// new socket connection from client for tunneling requests to client
_onConnection(socket) {
// no more socket connections allowed
if (this.connectedSockets >= this.maxTcpSockets) {
this.debug('no more sockets allowed');
socket.destroy();
return false;
}
socket.once('close', (hadError) => {
this.debug('closed socket (error: %s)', hadError);
this.connectedSockets -= 1;
// remove the socket from available list
const idx = this.availableSockets.indexOf(socket);
if (idx >= 0) {
this.availableSockets.splice(idx, 1);
}
this.debug('connected sockets: %s', this.connectedSockets);
if (this.connectedSockets <= 0) {
this.debug('all sockets disconnected');
this.emit('offline');
}
});
// close will be emitted after this
socket.once('error', (err) => {
// we do not log these errors, sessions can drop from clients for many reasons
// these are not actionable errors for our server
socket.destroy();
});
if (this.connectedSockets === 0) {
this.emit('online');
}
this.connectedSockets += 1;
this.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
// if there are queued callbacks, give this socket now and don't queue into available
const fn = this.waitingCreateConn.shift();
if (fn) {
this.debug('giving socket to queued conn request');
setTimeout(() => {
fn(null, socket);
}, 0);
return;
}
// make socket available for those waiting on sockets
this.availableSockets.push(socket);
}
// fetch a socket from the available socket pool for the agent
// if no socket is available, queue
// cb(err, socket)
createConnection(options, cb) {
if (this.closed) {
cb(new Error('closed'));
return;
}
this.debug('create connection');
// socket is a tcp connection back to the user hosting the site
const sock = this.availableSockets.shift();
// no available sockets
// wait until we have one
if (!sock) {
this.waitingCreateConn.push(cb);
this.debug('waiting connected: %s', this.connectedSockets);
this.debug('waiting available: %s', this.availableSockets.length);
return;
}
this.debug('socket given');
cb(null, sock);
}
destroy() {
this.server.close();
super.destroy();
}
}
export default TunnelAgent;
+199
View File
@@ -0,0 +1,199 @@
import { Agent } from "http";
import net from "net";
import Debug from "debug";
import { type AddressInfo } from "ws";
const DEFAULT_MAX_SOCKETS = 10;
// Implements an http.Agent interface to a pool of tunnel sockets
// A tunnel socket is a connection _from_ a client that will
// service http requests. This agent is usable wherever one can use an http.Agent
export class TunnelAgent extends Agent {
public availableSockets: any[];
public waitingCreateConn: any[];
public debug: any;
public connectedSockets: number;
public maxTcpSockets: number;
public server: net.Server;
public started: boolean;
public closed: boolean;
constructor(options) {
super({
keepAlive: true,
// only allow keepalive to hold on to one socket
// this prevents it from holding on to all the sockets so they can be used for upgrades
maxFreeSockets: 1,
});
// sockets we can hand out via createConnection
this.availableSockets = [];
// when a createConnection cannot return a socket, it goes into a queue
// once a socket is available it is handed out to the next callback
this.waitingCreateConn = [];
this.debug = Debug(`lt:TunnelAgent[${options.clientId}]`);
// track maximum allowed sockets
this.connectedSockets = 0;
this.maxTcpSockets = options.maxTcpSockets || DEFAULT_MAX_SOCKETS;
// new tcp server to service requests for this client
this.server = net.createServer((e) => {
console.log(`\n\n\n\x1b[33m==>NET \x1b[0m\n`);
console.log(e);
});
// flag to avoid double starts
this.started = false;
this.closed = false;
}
stats() {
return { connectedSockets: this.connectedSockets };
}
listen(): Promise<{ port: number }> {
const server = this.server;
if (this.started) throw new Error("already started");
this.started = true;
server.on("close", (e) => {
this._onClose.bind(this);
});
server.on("connection", (e) => {
console.log(`\n\n\n\x1b[33m==>CXN \x1b[0m\n`);
console.log(e);
return this._onConnection.bind(this);
});
server.on("error", (err: any) => {
console.log(`\n\n\n\x1b[33m==>HERE1 \x1b[0m\n`);
// These errors happen from killed connections, we don't worry about them
if (err.code == "ECONNRESET" || err.code == "ETIMEDOUT") {
return;
}
console.error(err);
});
return new Promise((resolve) => {
server.listen(() => {
const port = (server.address() as AddressInfo).port;
this.debug("tcp server listening on port: %d", port);
// port for lt client tcp connections
resolve({
port,
});
});
});
}
_onClose() {
this.closed = true;
this.debug("closed tcp socket");
// flush any waiting connections
for (const conn of this.waitingCreateConn) {
conn(new Error("closed"), null);
}
this.waitingCreateConn = [];
this.emit("end");
}
// new socket connection from client for tunneling requests to client
_onConnection(socket) {
console.log(`\n\n\n\x1b[33m==>HERE2 \x1b[0m\n`);
// no more socket connections allowed
if (this.connectedSockets >= this.maxTcpSockets) {
this.debug("no more sockets allowed");
socket.destroy();
return false;
}
socket.once("close", (hadError) => {
this.debug("closed socket (error: %s)", hadError);
this.connectedSockets -= 1;
// remove the socket from available list
const idx = this.availableSockets.indexOf(socket);
if (idx >= 0) {
this.availableSockets.splice(idx, 1);
}
this.debug("connected sockets: %s", this.connectedSockets);
if (this.connectedSockets <= 0) {
this.debug("all sockets disconnected");
this.emit("offline");
}
});
// close will be emitted after this
socket.once("error", (err) => {
console.log(`\n\n\n\x1b[33m==>HERE3 \x1b[0m\n`);
// we do not log these errors, sessions can drop from clients for many reasons
// these are not actionable errors for our server
socket.destroy();
});
if (this.connectedSockets === 0) {
this.emit("online");
}
this.connectedSockets += 1;
this.debug(
"new connection from: %s:%s",
socket.address().address,
socket.address().port
);
// if there are queued callbacks, give this socket now and don't queue into available
const fn = this.waitingCreateConn.shift();
if (fn) {
this.debug("giving socket to queued conn request");
setTimeout(() => {
fn(null, socket);
}, 0);
return;
}
// make socket available for those waiting on sockets
this.availableSockets.push(socket);
}
// fetch a socket from the available socket pool for the agent
// if no socket is available, queue
// cb(err, socket)
createConnection(options, cb) {
console.log(`\n\n\n\x1b[33m==>HERE4 \x1b[0m\n`);
if (this.closed) {
cb(new Error("closed"));
return;
}
this.debug("create connection");
// socket is a tcp connection back to the user hosting the site
const sock = this.availableSockets.shift();
// no available sockets
// wait until we have one
if (!sock) {
this.waitingCreateConn.push(cb);
this.debug("waiting connected: %s", this.connectedSockets);
this.debug("waiting available: %s", this.availableSockets.length);
return;
}
this.debug("socket given");
cb(null, sock);
}
destroy() {
this.server.close();
super.destroy();
}
}
+142
View File
@@ -0,0 +1,142 @@
import http from "http";
import Debug from "debug";
import pump from "pump";
import EventEmitter from "events";
import { TunnelAgent } from "./agent";
// A client encapsulates req/res handling using an agent
// If an agent is destroyed, the request handling will error
// The caller is responsible for handling a failed request
export default class Client extends EventEmitter {
public agent: TunnelAgent;
public id: string;
public debug: any;
public graceTimeout: any;
constructor(options: { id: string; agent: TunnelAgent }) {
super();
const agent = (this.agent = options.agent);
const id = (this.id = options.id);
this.debug = Debug(`lt:Client[${this.id}]`);
// client is given a grace period in which they can connect before they are _removed_
this.graceTimeout = setTimeout(() => {
console.log(`Timed out. Closing client.`);
this.close();
}, 1000).unref();
agent.on("online", () => {
this.debug("client online %s", id);
clearTimeout(this.graceTimeout);
});
agent.on("offline", () => {
this.debug("client offline %s", id);
// if there was a previous timeout set, we don't want to double trigger
clearTimeout(this.graceTimeout);
// client is given a grace period in which they can re-connect before they are _removed_
this.graceTimeout = setTimeout(() => {
console.log(`Timed out #2. Closing client.`);
this.close();
}, 1000).unref();
});
// 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) => {
console.error(err);
this.close();
});
}
stats() {
return this.agent.stats();
}
close() {
clearTimeout(this.graceTimeout);
this.agent.destroy();
this.emit("close");
}
handleRequest(req, res) {
console.log(`\n\n\n\x1b[33m==>HANDLE REQ \x1b[0m\n`);
this.debug("> %s", req.url);
const opt = {
path: req.url,
agent: this.agent,
method: req.method,
headers: req.headers,
};
const clientReq = http.request(opt, (clientRes) => {
this.debug("< %s", req.url);
// write response code and headers
res.writeHead(clientRes.statusCode, clientRes.headers);
// using pump is deliberate - see the pump docs for why
pump(clientRes, res);
});
// this can happen when underlying agent produces an error
// in our case we 504 gateway error this if we have already sent headers
clientReq.once("error", (err) => {
console.log(`\n\n\n\x1b[33m==>CLIENT REQ ERR \x1b[0m\n`);
console.log(err);
// TODO: if headers not sent - respond with gateway unavailable
});
// using pump is deliberate - see the pump docs for why
pump(req, clientReq);
}
handleUpgrade(req, socket) {
this.debug("> [up] %s", req.url);
socket.once("error", (err) => {
// These client side errors can happen if the client dies while we are reading
// We don't need to surface these in our logs.
if (err.code == "ECONNRESET" || err.code == "ETIMEDOUT") {
return;
}
console.error(err);
});
this.agent.createConnection({}, (err, conn) => {
this.debug("< [up] %s", req.url);
// any errors getting a connection mean we cannot service this request
if (err) {
socket.end();
return;
}
// socket met have disconnected while we waiting for a socket
if (!socket.readable || !socket.writable) {
console.log(`\n\n\n\x1b[33m==>NOT READABLE OR WRITABLE \x1b[0m\n`);
conn.destroy();
socket.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("");
// using pump is deliberate - see the pump docs for why
pump(conn, socket);
pump(socket, conn);
conn.write(arr.join("\r\n"));
});
}
}
+102
View File
@@ -0,0 +1,102 @@
import { hri } from "human-readable-ids";
import Debug from "debug";
import Client from "./client";
import { TunnelAgent } from "./agent";
import { Options } from "../server";
// Manage sets of clients
// A client is a "user session" established to service a remote localtunnel client
export class ClientManager {
public opt: Options;
public clients: Map<string, Client>;
public stats: { tunnels: number };
public debug: any;
constructor(opt: Options) {
this.opt = opt || {};
// id -> client instance
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
// if the tunnel could not be created, throws an error
async newClient(id: string) {
const clients = this.clients;
const stats = this.stats;
// can't ask for id already is use
if (clients.has(id)) {
id = hri.random();
}
const maxSockets = this.opt.max_tcp_sockets;
const agent = new TunnelAgent({
clientId: id,
maxSockets: 10,
});
const client = new Client({ id, agent });
// add to clients map immediately to avoid races with other clients requesting same id
clients.set(id, client);
client.once("close", (e) => {
console.log(`\n\n\n\x1b[33m==>Closing: \x1b[0m\n`);
console.log(e);
this.removeClient(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,
url: "",
};
} catch (err) {
this.removeClient(id);
// rethrow error for upstream to handle
throw err;
}
}
getClient(id: string) {
return this.clients.get(id);
}
hasClient(id: string) {
return !!this.getClient(id);
}
removeClient(id: string) {
this.debug("removing client: %s", id);
const client = this.getClient(id);
if (!client) {
console.log(`\n\n\n\x1b[33m==>NO CLIENT \x1b[0m\n`);
return;
}
--this.stats.tunnels;
delete this.clients[id];
client.close();
}
}