Add delete tunnel endpoint #151

Closed
brbrr wants to merge 7 commits from updates into master
12 changed files with 132 additions and 1114 deletions
+15
View File
@@ -0,0 +1,15 @@
module.exports = {
// env: {
// es2021: true,
// node: true,
// },
// extends: [
// 'airbnb-base',
// ],
// parserOptions: {
// ecmaVersion: 12,
// sourceType: 'module',
// },
// rules: {
// },
};
+2 -1
View File
@@ -10,4 +10,5 @@ RUN yarn install --production && yarn cache clean
COPY . /app COPY . /app
ENV NODE_ENV production ENV NODE_ENV production
ENTRYPOINT ["node", "-r", "esm", "./bin/server"] ENV DEBUG *
ENTRYPOINT ["node", "-r", "esm", "./bin/server", "--secure", "--max-sockets", "20"]
+23 -4
View File
@@ -55,13 +55,14 @@ class Client extends EventEmitter {
this.emit('close'); this.emit('close');
} }
handleRequest(req, res) { handleRequest(req, res, retry = true) {
this.debug('> %s', req.url); this.debug('> %s', req.url);
const opt = { const opt = {
path: req.url, path: req.url,
agent: this.agent, agent: this.agent,
method: req.method, method: req.method,
headers: req.headers headers: req.headers,
timeout: 120000 // 2 min, default node timeout
}; };
const clientReq = http.request(opt, (clientRes) => { const clientReq = http.request(opt, (clientRes) => {
@@ -70,7 +71,12 @@ class Client extends EventEmitter {
res.writeHead(clientRes.statusCode, clientRes.headers); res.writeHead(clientRes.statusCode, clientRes.headers);
// using pump is deliberate - see the pump docs for why // using pump is deliberate - see the pump docs for why
pump(clientRes, res); pump(clientRes, res, (err) => {
if (err) {
this.debug('> PIPE ERROR, %s', err);
this.debug(err);
}
});
}); });
// this can happen when underlying agent produces an error // this can happen when underlying agent produces an error
@@ -78,10 +84,23 @@ class Client extends EventEmitter {
// if we have already sent headers? // if we have already sent headers?
clientReq.once('error', (err) => { clientReq.once('error', (err) => {
// TODO(roman): if headers not sent - respond with gateway unavailable // TODO(roman): if headers not sent - respond with gateway unavailable
this.debug(' < ERROR %s', req.url);
this.debug(err);
this.debug(JSON.stringify( err ));
if (retry) {
this.debug('< RETRY');
this.handleRequest( req, res, false);
}
}); });
// using pump is deliberate - see the pump docs for why // using pump is deliberate - see the pump docs for why
pump(req, clientReq); pump(req, clientReq, (err) => {
if (err) {
this.debug('< PIPE ERROR, %s', err);
this.debug(err);
}
});
} }
handleUpgrade(req, socket) { handleUpgrade(req, socket) {
+9 -3
View File
@@ -4,7 +4,7 @@ import { Duplex } from 'stream';
import WebSocket from 'ws'; import WebSocket from 'ws';
import net from 'net'; import net from 'net';
import Client from './Client'; import Client from './Client.js';
class DummySocket extends Duplex { class DummySocket extends Duplex {
constructor(options) { constructor(options) {
@@ -57,12 +57,17 @@ class DummyAgent extends http.Agent {
} }
createConnection(options, cb) { createConnection(options, cb) {
cb(null, new DummySocket()); const soc = new DummySocket();
// console.log( 'QWEQWEQE', options, cb);
cb(null, soc);
return soc
} }
} }
describe('Client', () => { describe('Client', () => {
it('should handle request', async () => { it('should handle request', async () => {
console.log('!!!!!!', 'qwfqwfwqfw');
const agent = new DummyAgent(); const agent = new DummyAgent();
const client = new Client({ agent }); const client = new Client({ agent });
@@ -85,6 +90,7 @@ describe('Client', () => {
}); });
req.end(); req.end();
}); });
console.log(res.headers)
assert.equal(res.headers['x-powered-by'], 'dummy'); assert.equal(res.headers['x-powered-by'], 'dummy');
server.close(); server.close();
}); });
@@ -154,4 +160,4 @@ describe('Client', () => {
netClient.destroy(); netClient.destroy();
server.close(); server.close();
}); });
}); });
+13 -14
View File
@@ -1,8 +1,8 @@
import { hri } from 'human-readable-ids'; import { hri } from 'human-readable-ids';
import Debug from 'debug'; import Debug from 'debug';
import Client from './Client'; import Client from './Client.js';
import TunnelAgent from './TunnelAgent'; import TunnelAgent from './TunnelAgent.js';
// Manage sets of clients // Manage sets of clients
// //
@@ -20,27 +20,22 @@ class ClientManager {
}; };
this.debug = Debug('lt:ClientManager'); 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` // create a new tunnel with `id`
// if the id is already used, a random id is assigned // if the id is already used, a random id is assigned
// if the tunnel could not be created, throws an error // if the tunnel could not be created, throws an error
async newClient(id) { async newClient(id) {
const clients = this.clients;
const stats = this.stats;
// can't ask for id already is use // can't ask for id already is use
if (clients[id]) { if (this.clients[id]) {
id = hri.random(); id = hri.random();
} }
const maxSockets = this.opt.max_tcp_sockets; const maxTcpSockets = this.opt.max_tcp_sockets;
const agent = new TunnelAgent({ const agent = new TunnelAgent({
clientId: id, clientId: id,
maxSockets: 10, maxTcpSockets,
}); });
const client = new Client({ const client = new Client({
@@ -50,7 +45,7 @@ class ClientManager {
// add to clients map immediately // add to clients map immediately
// avoiding races with other clients requesting same id // avoiding races with other clients requesting same id
clients[id] = client; this.clients[id] = client;
client.once('close', () => { client.once('close', () => {
this.removeClient(id); this.removeClient(id);
@@ -59,11 +54,11 @@ class ClientManager {
// try/catch used here to remove client id // try/catch used here to remove client id
try { try {
const info = await agent.listen(); const info = await agent.listen();
++stats.tunnels; ++this.stats.tunnels;
return { return {
id: id, id: id,
port: info.port, port: info.port,
max_conn_count: maxSockets, max_conn_count: maxTcpSockets,
}; };
} }
catch (err) { catch (err) {
@@ -73,6 +68,10 @@ class ClientManager {
} }
} }
getClients() {
return Object.keys(this.clients);
}
removeClient(id) { removeClient(id) {
this.debug('removing client: %s', id); this.debug('removing client: %s', id);
const client = this.clients[id]; const client = this.clients[id];
+1 -1
View File
@@ -1,7 +1,7 @@
import assert from 'assert'; import assert from 'assert';
import net from 'net'; import net from 'net';
import ClientManager from './ClientManager'; import ClientManager from './ClientManager.js';
describe('ClientManager', () => { describe('ClientManager', () => {
it('should construct with no tunnels', () => { it('should construct with no tunnels', () => {
+10 -7
View File
@@ -1,10 +1,10 @@
import { Agent } from 'http'; import { Agent } from 'http';
import net from 'net'; import net from 'net';
import assert from 'assert';
import log from 'book'; import log from 'book';
import Debug from 'debug'; import Debug from 'debug';
const DEFAULT_MAX_SOCKETS = 10; const DEFAULT_MAX_SOCKETS = 10;
const DEFAULT_SOCKET_TIMEOUT = 10 * 60 * 1000;
// Implements an http.Agent interface to a pool of tunnel sockets // Implements an http.Agent interface to a pool of tunnel sockets
// A tunnel socket is a connection _from_ a client that will // A tunnel socket is a connection _from_ a client that will
@@ -33,6 +33,8 @@ class TunnelAgent extends Agent {
// new tcp server to service requests for this client // new tcp server to service requests for this client
this.server = net.createServer(); this.server = net.createServer();
this._socketTimeout = options.socketTimeout || DEFAULT_SOCKET_TIMEOUT;
// flag to avoid double starts // flag to avoid double starts
this.started = false; this.started = false;
@@ -46,15 +48,14 @@ class TunnelAgent extends Agent {
} }
listen() { listen() {
const server = this.server;
if (this.started) { if (this.started) {
throw new Error('already started'); throw new Error('already started');
} }
this.started = true; this.started = true;
server.on('close', this._onClose.bind(this)); this.server.on('close', this._onClose.bind(this));
server.on('connection', this._onConnection.bind(this)); this.server.on('connection', this._onConnection.bind(this));
server.on('error', (err) => { this.server.on('error', (err) => {
// These errors happen from killed connections, we don't worry about them // These errors happen from killed connections, we don't worry about them
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') { if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return; return;
@@ -63,8 +64,8 @@ class TunnelAgent extends Agent {
}); });
return new Promise((resolve) => { return new Promise((resolve) => {
server.listen(() => { this.server.listen(() => {
const port = server.address().port; const port = this.server.address().port;
this.debug('tcp server listening on port: %d', port); this.debug('tcp server listening on port: %d', port);
resolve({ resolve({
@@ -88,6 +89,8 @@ class TunnelAgent extends Agent {
// new socket connection from client for tunneling requests to client // new socket connection from client for tunneling requests to client
_onConnection(socket) { _onConnection(socket) {
console.log('!!!!!!', socket);
socket.setTimeout(this._socketTimeout);
// no more socket connections allowed // no more socket connections allowed
if (this.connectedSockets >= this.maxTcpSockets) { if (this.connectedSockets >= this.maxTcpSockets) {
this.debug('no more sockets allowed'); this.debug('no more sockets allowed');
+15 -1
View File
@@ -2,7 +2,7 @@ import http from 'http';
import net from 'net'; import net from 'net';
import assert from 'assert'; import assert from 'assert';
import TunnelAgent from './TunnelAgent'; import TunnelAgent from './TunnelAgent.js';
describe('TunnelAgent', () => { describe('TunnelAgent', () => {
it('should create an empty agent', async () => { it('should create an empty agent', async () => {
@@ -180,4 +180,18 @@ describe('TunnelAgent', () => {
connectedSockets: 0, connectedSockets: 0,
}); });
}); });
it('should disconnect clients by timeout', async () => {
const agent = new TunnelAgent({socketTimeout: 600});
const info = await agent.listen();
const offlinePromise = new Promise(resolve => agent.once('offline', resolve));
const sock = net.createConnection({ port: info.port });
await new Promise(resolve => sock.once('connect', resolve));
await offlinePromise;
agent.destroy();
sock.destroy();
});
}); });
+18 -10
View File
@@ -1,19 +1,19 @@
{ {
"type": "module",
"author": "Roman Shtylman <[email protected]>", "author": "Roman Shtylman <[email protected]>",
"name": "localtunnel-server", "name": "localtunnel-server",
"description": "expose localhost to the world", "description": "expose localhost to the world",
"version": "0.0.8", "version": "0.0.8",
"license": "MIT", "license": "MIT",
"repository": { "engines": {
"type": "git", "node": ">=12"
"url": "git://github.com/localtunnel/server.git"
}, },
"dependencies": { "dependencies": {
"book": "1.3.3", "book": "1.3.3",
"debug": "3.1.0", "debug": "3.1.0",
"esm": "3.0.34", "esm": "3.2.25",
"human-readable-ids": "1.0.3", "human-readable-ids": "1.0.4",
"koa": "2.5.1", "koa": "2.13.0",
"koa-router": "7.4.0", "koa-router": "7.4.0",
"localenv": "0.2.2", "localenv": "0.2.2",
"optimist": "0.6.1", "optimist": "0.6.1",
@@ -21,14 +21,22 @@
"tldjs": "2.3.1" "tldjs": "2.3.1"
}, },
"devDependencies": { "devDependencies": {
"mocha": "5.1.1", "eslint": "7.20.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-import": "2.22.1",
"mocha": "8.3.0",
"node-dev": "3.1.3", "node-dev": "3.1.3",
"supertest": "3.1.0", "supertest": "3.1.0",
"ws": "5.1.1" "ws": "5.1.1"
}, },
"scripts": { "scripts": {
"test": "mocha --check-leaks --require esm './**/*.test.js'", "test": "mocha --check-leaks ./**/*.test.js",
"start": "./bin/server", "start": "node -r esm bin/server --port 1234 --max-sockets 30",
"dev": "node-dev bin/server --port 3000" "start-docker": "docker run -d --restart always --name localtunnel --net host defunctzombie/localtunnel-server:latest --port 1234",
"dev": "node-dev bin/server --port 3000",
"deploy": "docker build --tag a8c-lt:1.0 . && docker container rm -f $(docker ps -aqf 'name=lt') && docker run -d --restart always --name lt --net host a8c-lt:1.0 --port 1234",
"log:nginx": "sudo tail -f /var/log/nginx/error.log",
"log:docker": "docker logs lt -f --tail 2000",
"log:docker-grep": "docker logs lt 2>&1 | grep"
} }
} }
+21 -5
View File
@@ -15,7 +15,6 @@ export default function(opt) {
const validHosts = (opt.domain) ? [opt.domain] : undefined; const validHosts = (opt.domain) ? [opt.domain] : undefined;
const myTldjs = tldjs.fromUserSettings({ validHosts }); const myTldjs = tldjs.fromUserSettings({ validHosts });
const landingPage = opt.landing || 'https://localtunnel.github.io/www/';
function GetClientIdFromHostname(hostname) { function GetClientIdFromHostname(hostname) {
return myTldjs.getSubdomain(hostname); return myTldjs.getSubdomain(hostname);
@@ -29,9 +28,9 @@ export default function(opt) {
const router = new Router(); const router = new Router();
router.get('/api/status', async (ctx, next) => { router.get('/api/status', async (ctx, next) => {
const stats = manager.stats;
ctx.body = { ctx.body = {
tunnels: stats.tunnels, tunnelsCount: manager.stats.tunnels,
tunnels: manager.getClients(),
mem: process.memoryUsage(), mem: process.memoryUsage(),
}; };
}); });
@@ -50,6 +49,20 @@ export default function(opt) {
}; };
}); });
router.get('/api/tunnels/:id/delete', async (ctx, next) => {
const clientId = ctx.params.id;
const client = manager.getClient(clientId);
if (!client) {
ctx.throw(404);
return;
}
manager.removeClient(clientId);
ctx.body = {
delete_status: "success",
};
});
app.use(router.routes()); app.use(router.routes());
app.use(router.allowedMethods()); app.use(router.allowedMethods());
@@ -76,7 +89,8 @@ export default function(opt) {
} }
// no new client request, send to landing page // no new client request, send to landing page
ctx.redirect(landingPage); const apiStatusUrl = schema + '://'+ ctx.request.host + '/api/status';
ctx.redirect(ctx.request.host);
}); });
// anything after the / path is a request for a specific client name // anything after the / path is a request for a specific client name
@@ -129,13 +143,15 @@ export default function(opt) {
const clientId = GetClientIdFromHostname(hostname); const clientId = GetClientIdFromHostname(hostname);
if (!clientId) { if (!clientId) {
appCallback(req, res); appCallback(req, res);
debug('ClientID for host %s was not found :(', hostname);
return; return;
} }
debug('Identified client ID as: %s', clientId);
const client = manager.getClient(clientId); const client = manager.getClient(clientId);
if (!client) { if (!client) {
res.statusCode = 404; res.statusCode = 404;
res.end('404'); res.end("Can't find active tunnel");
return; return;
} }
+5 -2
View File
@@ -1,10 +1,13 @@
import request from 'supertest'; import request from 'supertest';
import assert from 'assert'; import assert from 'assert';
import { Server as WebSocketServer } from 'ws'; // import { Server as WebSocketServer } from 'ws';
import WebSocket from 'ws'; import WebSocket from 'ws';
import net from 'net'; import net from 'net';
import createServer from './server'; import createServer from './server.js';
const WebSocketServer = WebSocket.Server;
describe('Server', () => { describe('Server', () => {
it('server starts and stops', async () => { it('server starts and stops', async () => {
-1066
View File
File diff suppressed because it is too large Load Diff