sub #215
@@ -1,4 +0,0 @@
|
|||||||
language: node_js
|
|
||||||
sudo: false
|
|
||||||
node_js:
|
|
||||||
- "9.2"
|
|
||||||
+5
-5
@@ -1,15 +1,15 @@
|
|||||||
FROM node:10.1.0-alpine
|
FROM oven/bun:1 as base
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package.json /app/
|
COPY package.json /app/
|
||||||
COPY yarn.lock /app/
|
COPY bun.lockb /app/
|
||||||
|
|
||||||
RUN yarn install --production && yarn cache clean
|
RUN bun install --frozen-lockfile --production
|
||||||
|
|
||||||
COPY . /app
|
COPY . /app
|
||||||
|
|
||||||
ENV NODE_ENV production
|
ENV NODE_ENV production
|
||||||
ENTRYPOINT ["node", "-r", "esm", "./bin/server"]
|
ENTRYPOINT ["bun", "run", "start"]
|
||||||
|
|
||||||
EXPOSE 1234
|
EXPOSE 1234 1235
|
||||||
-70
@@ -1,70 +0,0 @@
|
|||||||
#!/usr/bin/env node -r esm
|
|
||||||
|
|
||||||
import 'localenv';
|
|
||||||
import optimist from 'optimist';
|
|
||||||
import Debug from 'debug';
|
|
||||||
import CreateServer from '../server';
|
|
||||||
|
|
||||||
const debug = Debug('localtunnel');
|
|
||||||
|
|
||||||
const argv = optimist
|
|
||||||
.usage('Usage: $0 --port [num]')
|
|
||||||
.options('secure', {
|
|
||||||
default: false,
|
|
||||||
describe: 'use this flag to indicate proxy over https'
|
|
||||||
})
|
|
||||||
.options('port', {
|
|
||||||
default: '80',
|
|
||||||
describe: 'listen on this port for outside requests'
|
|
||||||
})
|
|
||||||
.options('address', {
|
|
||||||
default: '0.0.0.0',
|
|
||||||
describe: 'IP address to bind to'
|
|
||||||
})
|
|
||||||
.options('domain', {
|
|
||||||
describe: 'Specify the base domain name. This is optional if hosting localtunnel from a regular example.com domain. This is required if hosting a localtunnel server from a subdomain (i.e. lt.example.dom where clients will be client-app.lt.example.come)',
|
|
||||||
})
|
|
||||||
.options('max-sockets', {
|
|
||||||
default: 10,
|
|
||||||
describe: 'maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)'
|
|
||||||
})
|
|
||||||
.argv;
|
|
||||||
|
|
||||||
if (argv.help) {
|
|
||||||
console.log("Showing help");
|
|
||||||
optimist.showHelp();
|
|
||||||
process.exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
const server = CreateServer({
|
|
||||||
max_tcp_sockets: argv['max-sockets'],
|
|
||||||
secure: argv.secure,
|
|
||||||
domain: argv.domain,
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(argv.port, argv.address, () => {
|
|
||||||
debug('server listening on port: %d', server.address().port);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('SIGINT', () => {
|
|
||||||
console.error("SIGINT");
|
|
||||||
process.exit();
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('SIGTERM', () => {
|
|
||||||
console.warn("SIGTERM");
|
|
||||||
// process.exit();
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('uncaughtException', (err) => {
|
|
||||||
console.error("err:");
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason, promise) => {
|
|
||||||
console.error("reason:");
|
|
||||||
console.error(reason);
|
|
||||||
});
|
|
||||||
|
|
||||||
// vim: ft=javascript
|
|
||||||
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { serve } from "bun";
|
||||||
|
|
||||||
|
const port = 3000;
|
||||||
|
|
||||||
|
serve({
|
||||||
|
port,
|
||||||
|
fetch: async (req) => {
|
||||||
|
const url = new URL(req.url);
|
||||||
|
console.log(req.method, url.pathname, url.search);
|
||||||
|
|
||||||
|
const bod = await req.text();
|
||||||
|
bod && console.log("Body: ", bod);
|
||||||
|
|
||||||
|
const res = new Response(`hello from localhost:${port}`, {
|
||||||
|
status: 200,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`Serving on port ${port}!`);
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
# fly.toml app configuration file generated for tunnel on 2024-03-18T21:37:28-04:00
|
||||||
|
#
|
||||||
|
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
|
||||||
|
#
|
||||||
|
|
||||||
|
app = 'tunnel'
|
||||||
|
primary_region = 'yul'
|
||||||
|
|
||||||
|
[build]
|
||||||
|
|
||||||
|
[http_service]
|
||||||
|
internal_port = 1234
|
||||||
|
force_https = false
|
||||||
|
auto_stop_machines = true
|
||||||
|
auto_start_machines = true
|
||||||
|
min_machines_running = 0
|
||||||
|
processes = ['app']
|
||||||
|
|
||||||
|
[[vm]]
|
||||||
|
memory = '1gb'
|
||||||
|
cpu_kind = 'shared'
|
||||||
|
cpus = 1
|
||||||
|
|
||||||
|
# [[services]]
|
||||||
|
# internal_port = 1235
|
||||||
|
# protocol = "tcp"
|
||||||
|
|
||||||
|
[[services.ports]]
|
||||||
|
handlers = ["tls", "http"]
|
||||||
|
port = 443
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import "localenv";
|
||||||
|
import optimist from "optimist";
|
||||||
|
import Debug from "debug";
|
||||||
|
import { CreateServer } from "./server";
|
||||||
|
import { type AddressInfo } from "ws";
|
||||||
|
|
||||||
|
const debug = Debug("localtunnel");
|
||||||
|
|
||||||
|
const argv = optimist
|
||||||
|
.usage("Usage: $0 --port [num]")
|
||||||
|
.options("secure", {
|
||||||
|
default: false,
|
||||||
|
describe: "use this flag to indicate proxy over https",
|
||||||
|
})
|
||||||
|
.options("port", {
|
||||||
|
default: "80",
|
||||||
|
describe: "listen on this port for outside requests",
|
||||||
|
})
|
||||||
|
.options("address", {
|
||||||
|
default: "0.0.0.0",
|
||||||
|
describe: "IP address to bind to",
|
||||||
|
})
|
||||||
|
.options("domain", {
|
||||||
|
describe:
|
||||||
|
"Specify the base domain name. This is optional if hosting localtunnel from a regular example.com domain. This is required if hosting a localtunnel server from a subdomain (i.e. lt.example.dom where clients will be client-app.lt.example.come)",
|
||||||
|
})
|
||||||
|
.options("max-sockets", {
|
||||||
|
default: 10,
|
||||||
|
describe:
|
||||||
|
"maximum number of tcp sockets each client is allowed to establish at one time (the tunnels)",
|
||||||
|
}).argv;
|
||||||
|
|
||||||
|
if (argv.help) {
|
||||||
|
console.log("Showing help");
|
||||||
|
optimist.showHelp();
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
const server = CreateServer({
|
||||||
|
max_tcp_sockets: argv["max-sockets"],
|
||||||
|
secure: argv.secure,
|
||||||
|
domain: argv.domain,
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(argv.port, argv.address, () => {
|
||||||
|
debug("server listening on port: %d", (server.address() as AddressInfo).port);
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("SIGINT", () => {
|
||||||
|
console.error("SIGINT");
|
||||||
|
process.exit();
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("SIGTERM", () => {
|
||||||
|
console.warn("SIGTERM");
|
||||||
|
// process.exit();
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("uncaughtException", (err) => {
|
||||||
|
console.error("err:");
|
||||||
|
console.error(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("unhandledRejection", (reason, promise) => {
|
||||||
|
console.error("reason:");
|
||||||
|
console.error(reason);
|
||||||
|
});
|
||||||
-132
@@ -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;
|
|
||||||
@@ -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;
|
|
||||||
@@ -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
@@ -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
@@ -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
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
import { serve, type Socket } from "bun";
|
||||||
|
import { uid, parseResponse, wait } from "./utils";
|
||||||
|
|
||||||
|
const clientSockets = new Map<string, Socket>();
|
||||||
|
const clientData = new Map<string, any>();
|
||||||
|
|
||||||
|
const port = Bun.env.PORT || 1234;
|
||||||
|
const protocol = Bun.env.PROTOCOL || "http";
|
||||||
|
|
||||||
|
serve({
|
||||||
|
port,
|
||||||
|
fetch: async (req: Request) => {
|
||||||
|
const url = new URL(req.url);
|
||||||
|
const hostname = `${url.hostname}${url.port && `:${url.port}`}`;
|
||||||
|
|
||||||
|
// For healthchecks
|
||||||
|
if (url.pathname === "/healthz") return new Response("healthy");
|
||||||
|
|
||||||
|
if (url.searchParams.has("new")) {
|
||||||
|
const clientId = uid();
|
||||||
|
|
||||||
|
const server = Bun.listen({
|
||||||
|
hostname: "localhost",
|
||||||
|
port: 0, // Let the OS assign an available port
|
||||||
|
socket: {
|
||||||
|
data(_socket, data) {
|
||||||
|
clientData.set(clientId, data);
|
||||||
|
},
|
||||||
|
open(socket) {
|
||||||
|
console.log(`Client ${clientId} connected`);
|
||||||
|
clientSockets.set(clientId, socket);
|
||||||
|
},
|
||||||
|
close() {
|
||||||
|
console.log(`Client ${clientId} reconnecting`);
|
||||||
|
clientSockets.delete(clientId);
|
||||||
|
clientData.delete(clientId);
|
||||||
|
},
|
||||||
|
error(_socket, error) {
|
||||||
|
console.error(`Error for client ${clientId}:`, error);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
id: clientId,
|
||||||
|
port: server.port,
|
||||||
|
url: `${protocol}://${clientId}.${hostname}`,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
status: 200,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hostname.split(".").length === 3) {
|
||||||
|
return new Response("welcome to tunnel. hit ?new to get a URL.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const subdomain = hostname.split(".")[0];
|
||||||
|
const clientSocket = clientSockets.get(subdomain);
|
||||||
|
|
||||||
|
if (!clientSocket) {
|
||||||
|
console.warn(`Client ${subdomain} not found`);
|
||||||
|
return new Response("Client not found", { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Forwarding request to client ${subdomain}`);
|
||||||
|
|
||||||
|
// Forward the request to the connected client
|
||||||
|
console.log(req.method, url.pathname, url.search);
|
||||||
|
clientSocket.write(
|
||||||
|
`${req.method} ${url.pathname}${url.search} HTTP/1.1\r\n`
|
||||||
|
);
|
||||||
|
for (const [header, value] of req.headers.entries()) {
|
||||||
|
clientSocket.write(`${header}: ${value}\r\n`);
|
||||||
|
}
|
||||||
|
clientSocket.write("\r\n");
|
||||||
|
|
||||||
|
if (req.body) {
|
||||||
|
const reader = req.body.getReader();
|
||||||
|
let chunk;
|
||||||
|
while (!(chunk = await reader.read()).done) {
|
||||||
|
clientSocket.write(chunk.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let responseData = clientData.get(subdomain);
|
||||||
|
let attempts = 10;
|
||||||
|
|
||||||
|
while (!responseData) {
|
||||||
|
await wait(100);
|
||||||
|
responseData = clientData.get(subdomain);
|
||||||
|
if (attempts < 1) {
|
||||||
|
return new Response("Client did not respond", { status: 504 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { body, headers } = parseResponse(responseData);
|
||||||
|
|
||||||
|
return new Response(body, { headers, status: 200 });
|
||||||
|
},
|
||||||
|
error(error) {
|
||||||
|
console.error(error);
|
||||||
|
return new Response(error.message, { status: 500 });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`Listening on port ${port}`);
|
||||||
+7
-12
@@ -1,17 +1,15 @@
|
|||||||
{
|
{
|
||||||
"author": "Roman Shtylman <[email protected]>",
|
"name": "tunnel",
|
||||||
"name": "localtunnel-server",
|
|
||||||
"description": "expose localhost to the world",
|
"description": "expose localhost to the world",
|
||||||
"version": "0.0.8",
|
"version": "0.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/localtunnel/server.git"
|
"url": "https://github.com/rubriclab/tunnel"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"book": "1.3.3",
|
|
||||||
"debug": "3.1.0",
|
"debug": "3.1.0",
|
||||||
"esm": "3.0.34",
|
"human-id": "^4.1.1",
|
||||||
"human-readable-ids": "1.0.3",
|
"human-readable-ids": "1.0.3",
|
||||||
"koa": "2.5.1",
|
"koa": "2.5.1",
|
||||||
"koa-router": "7.4.0",
|
"koa-router": "7.4.0",
|
||||||
@@ -21,14 +19,11 @@
|
|||||||
"tldjs": "2.3.1"
|
"tldjs": "2.3.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "5.1.1",
|
"@types/bun": "^1.0.8",
|
||||||
"node-dev": "3.1.3",
|
|
||||||
"supertest": "3.1.0",
|
|
||||||
"ws": "5.1.1"
|
"ws": "5.1.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha --check-leaks --require esm './**/*.test.js'",
|
"start": "bun run opus.ts",
|
||||||
"start": "./bin/server --domain tunnel.rubric.sh",
|
"dev": "bun run --watch index.ts --domain ted.rubric.sh --port 3001"
|
||||||
"dev": "node-dev bin/server --port 3000"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,168 +0,0 @@
|
|||||||
import log from 'book';
|
|
||||||
import Koa from 'koa';
|
|
||||||
import tldjs from 'tldjs';
|
|
||||||
import Debug from 'debug';
|
|
||||||
import http from 'http';
|
|
||||||
import { hri } from 'human-readable-ids';
|
|
||||||
import Router from 'koa-router';
|
|
||||||
|
|
||||||
import ClientManager from './lib/ClientManager';
|
|
||||||
|
|
||||||
const debug = Debug('localtunnel:server');
|
|
||||||
|
|
||||||
export default function(opt) {
|
|
||||||
opt = opt || {};
|
|
||||||
|
|
||||||
const validHosts = (opt.domain) ? [opt.domain] : undefined;
|
|
||||||
const myTldjs = tldjs.fromUserSettings({ validHosts });
|
|
||||||
const landingPage = opt.landing || 'https://localtunnel.github.io/www/';
|
|
||||||
|
|
||||||
function GetClientIdFromHostname(hostname) {
|
|
||||||
return myTldjs.getSubdomain(hostname);
|
|
||||||
}
|
|
||||||
|
|
||||||
const manager = new ClientManager(opt);
|
|
||||||
|
|
||||||
const schema = opt.secure ? 'https' : 'http';
|
|
||||||
|
|
||||||
const app = new Koa();
|
|
||||||
const router = new Router();
|
|
||||||
|
|
||||||
router.get('/api/status', async (ctx, next) => {
|
|
||||||
const stats = manager.stats;
|
|
||||||
ctx.body = {
|
|
||||||
tunnels: stats.tunnels,
|
|
||||||
mem: process.memoryUsage(),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/api/tunnels/:id/status', async (ctx, next) => {
|
|
||||||
const clientId = ctx.params.id;
|
|
||||||
const client = manager.getClient(clientId);
|
|
||||||
if (!client) {
|
|
||||||
ctx.throw(404);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const stats = client.stats();
|
|
||||||
ctx.body = {
|
|
||||||
connected_sockets: stats.connectedSockets,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
app.use(router.routes());
|
|
||||||
app.use(router.allowedMethods());
|
|
||||||
|
|
||||||
// root endpoint
|
|
||||||
app.use(async (ctx, next) => {
|
|
||||||
const path = ctx.request.path;
|
|
||||||
|
|
||||||
// skip anything not on the root path
|
|
||||||
if (path !== '/') {
|
|
||||||
await next();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isNewClientRequest = ctx.query['new'] !== undefined;
|
|
||||||
if (isNewClientRequest) {
|
|
||||||
const reqId = hri.random();
|
|
||||||
debug('making new client with id %s', reqId);
|
|
||||||
const info = await manager.newClient(reqId);
|
|
||||||
|
|
||||||
const url = schema + '://' + info.id + '.' + ctx.request.host;
|
|
||||||
info.url = url;
|
|
||||||
ctx.body = info;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// no new client request, send to landing page
|
|
||||||
ctx.redirect(landingPage);
|
|
||||||
});
|
|
||||||
|
|
||||||
// anything after the / path is a request for a specific client name
|
|
||||||
// This is a backwards compat feature
|
|
||||||
app.use(async (ctx, next) => {
|
|
||||||
const parts = ctx.request.path.split('/');
|
|
||||||
|
|
||||||
// any request with several layers of paths is not allowed
|
|
||||||
// rejects /foo/bar
|
|
||||||
// allow /foo
|
|
||||||
if (parts.length !== 2) {
|
|
||||||
await next();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const reqId = parts[1];
|
|
||||||
|
|
||||||
// limit requested hostnames to 63 characters
|
|
||||||
if (! /^(?:[a-z0-9][a-z0-9\-]{4,63}[a-z0-9]|[a-z0-9]{4,63})$/.test(reqId)) {
|
|
||||||
const msg = 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.';
|
|
||||||
ctx.status = 403;
|
|
||||||
ctx.body = {
|
|
||||||
message: msg,
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug('making new client with id %s', reqId);
|
|
||||||
const info = await manager.newClient(reqId);
|
|
||||||
|
|
||||||
const url = schema + '://' + info.id + '.' + ctx.request.host;
|
|
||||||
info.url = url;
|
|
||||||
ctx.body = info;
|
|
||||||
return;
|
|
||||||
});
|
|
||||||
|
|
||||||
const server = http.createServer();
|
|
||||||
|
|
||||||
const appCallback = app.callback();
|
|
||||||
|
|
||||||
server.on('request', (req, res) => {
|
|
||||||
// without a hostname, we won't know who the request is for
|
|
||||||
const hostname = req.headers.host;
|
|
||||||
if (!hostname) {
|
|
||||||
res.statusCode = 400;
|
|
||||||
res.end('Host header is required');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const clientId = GetClientIdFromHostname(hostname);
|
|
||||||
if (!clientId) {
|
|
||||||
appCallback(req, res);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = manager.getClient(clientId);
|
|
||||||
if (!client) {
|
|
||||||
res.statusCode = 404;
|
|
||||||
res.end('404');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
client.handleRequest(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.on('upgrade', (req, socket, head) => {
|
|
||||||
const hostname = req.headers.host;
|
|
||||||
if (!hostname) {
|
|
||||||
socket.destroy();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const clientId = GetClientIdFromHostname(hostname);
|
|
||||||
if (!clientId) {
|
|
||||||
socket.destroy();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = manager.getClient(clientId);
|
|
||||||
if (!client) {
|
|
||||||
socket.destroy();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
client.handleUpgrade(req, socket);
|
|
||||||
});
|
|
||||||
|
|
||||||
return server;
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
import Koa from "koa";
|
||||||
|
import tldjs from "tldjs";
|
||||||
|
import Debug from "debug";
|
||||||
|
import http from "http";
|
||||||
|
import { hri } from "human-readable-ids";
|
||||||
|
import Router from "koa-router";
|
||||||
|
import { ClientManager } from "./lib/manager";
|
||||||
|
|
||||||
|
export type Options = {
|
||||||
|
domain: string;
|
||||||
|
secure: boolean;
|
||||||
|
landing?: string;
|
||||||
|
max_tcp_sockets?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const debug = Debug("localtunnel:server");
|
||||||
|
|
||||||
|
export const CreateServer = (opt: Options) => {
|
||||||
|
opt = opt || {};
|
||||||
|
|
||||||
|
const validHosts = opt.domain ? [opt.domain] : undefined;
|
||||||
|
const myTldjs = tldjs.fromUserSettings({ validHosts });
|
||||||
|
const landingPage = opt.landing || "https://localtunnel.github.io/www/";
|
||||||
|
|
||||||
|
function getClientIdFromHostname(hostname: string) {
|
||||||
|
return myTldjs.getSubdomain(hostname);
|
||||||
|
}
|
||||||
|
|
||||||
|
const manager = new ClientManager(opt);
|
||||||
|
|
||||||
|
const schema = opt.secure ? "https" : "http";
|
||||||
|
|
||||||
|
const app = new Koa();
|
||||||
|
const router = new Router();
|
||||||
|
|
||||||
|
router.get("/api/status", async (ctx, _next) => {
|
||||||
|
const stats = manager.stats;
|
||||||
|
ctx.body = {
|
||||||
|
tunnels: stats.tunnels,
|
||||||
|
mem: process.memoryUsage(),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/api/tunnels/:id/status", async (ctx, _next) => {
|
||||||
|
const clientId = ctx.params.id;
|
||||||
|
const client = manager.getClient(clientId);
|
||||||
|
if (!client) {
|
||||||
|
ctx.throw(404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stats = client.stats();
|
||||||
|
ctx.body = {
|
||||||
|
connected_sockets: stats.connectedSockets,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use(router.routes());
|
||||||
|
app.use(router.allowedMethods());
|
||||||
|
|
||||||
|
// root endpoint
|
||||||
|
app.use(async (ctx, next) => {
|
||||||
|
const path = ctx.request.path;
|
||||||
|
|
||||||
|
// skip anything not on the root path
|
||||||
|
if (path !== "/") {
|
||||||
|
await next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isNewClientRequest = ctx.query["new"] !== undefined;
|
||||||
|
if (isNewClientRequest) {
|
||||||
|
const reqId = hri.random();
|
||||||
|
|
||||||
|
debug("making new client with id %s", reqId);
|
||||||
|
|
||||||
|
const info = await manager.newClient(reqId);
|
||||||
|
|
||||||
|
const url = schema + "://" + info.id + "." + ctx.request.host;
|
||||||
|
|
||||||
|
info.url = url;
|
||||||
|
ctx.body = info;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// no new client request, send to landing page
|
||||||
|
ctx.redirect(landingPage);
|
||||||
|
});
|
||||||
|
|
||||||
|
// anything after the / path is a request for a specific client name
|
||||||
|
// This is a backwards compat feature
|
||||||
|
app.use(async (ctx, next) => {
|
||||||
|
const parts = ctx.request.path.split("/");
|
||||||
|
|
||||||
|
// any request with several layers of paths is not allowed
|
||||||
|
// rejects /foo/bar; allow /foo
|
||||||
|
if (parts.length !== 2) {
|
||||||
|
await next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reqId = parts[1];
|
||||||
|
|
||||||
|
// limit requested hostnames to 63 characters
|
||||||
|
if (!/^(?:[a-z0-9][a-z0-9\-]{4,63}[a-z0-9]|[a-z0-9]{4,63})$/.test(reqId)) {
|
||||||
|
const msg =
|
||||||
|
"Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.";
|
||||||
|
ctx.status = 403;
|
||||||
|
ctx.body = {
|
||||||
|
message: msg,
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug("making new client with id %s", reqId);
|
||||||
|
const info = await manager.newClient(reqId);
|
||||||
|
|
||||||
|
const url = schema + "://" + info.id + "." + ctx.request.host;
|
||||||
|
info.url = url;
|
||||||
|
ctx.body = info;
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
|
||||||
|
const server = http.createServer();
|
||||||
|
|
||||||
|
const appCallback = app.callback();
|
||||||
|
|
||||||
|
server.on("request", (req, res) => {
|
||||||
|
// without a hostname, we won't know who the request is for
|
||||||
|
const hostname = req.headers.host;
|
||||||
|
if (!hostname) {
|
||||||
|
res.statusCode = 400;
|
||||||
|
res.end("Host header is required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientId = getClientIdFromHostname(hostname);
|
||||||
|
if (!clientId) {
|
||||||
|
appCallback(req, res);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = manager.getClient(clientId);
|
||||||
|
if (!client) {
|
||||||
|
res.statusCode = 404;
|
||||||
|
res.end("404");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.handleRequest(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on("upgrade", (req, socket, head) => {
|
||||||
|
console.log(`\n\n\n\x1b[33m==>UPGRADE \x1b[0m\n`);
|
||||||
|
const hostname = req.headers.host;
|
||||||
|
if (!hostname) {
|
||||||
|
socket.destroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientId = getClientIdFromHostname(hostname);
|
||||||
|
if (!clientId) {
|
||||||
|
socket.destroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = manager.getClient(clientId);
|
||||||
|
if (!client) {
|
||||||
|
socket.destroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.handleUpgrade(req, socket);
|
||||||
|
});
|
||||||
|
|
||||||
|
return server;
|
||||||
|
};
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { humanId } from "human-id";
|
||||||
|
|
||||||
|
export const uid = () => {
|
||||||
|
return humanId({ capitalize: false, separator: "-" });
|
||||||
|
};
|
||||||
|
|
||||||
|
export const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
||||||
|
|
||||||
|
export const parseResponse = (buffer: Buffer) => {
|
||||||
|
const asString = Buffer.from(buffer).toString();
|
||||||
|
const [responseHeaders, body] = asString.split("\r\n\r\n");
|
||||||
|
|
||||||
|
const headers = new Headers();
|
||||||
|
|
||||||
|
responseHeaders
|
||||||
|
.trim()
|
||||||
|
.split("\r\n")
|
||||||
|
.forEach((header) => {
|
||||||
|
const [name, value] = header.split(": ");
|
||||||
|
if (!name.includes("HTTP")) headers.append(name, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
return { body, headers };
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user