Add delete tunnel endpoint
#151
@@ -0,0 +1,15 @@
|
||||
module.exports = {
|
||||
// env: {
|
||||
// es2021: true,
|
||||
// node: true,
|
||||
// },
|
||||
// extends: [
|
||||
// 'airbnb-base',
|
||||
// ],
|
||||
// parserOptions: {
|
||||
// ecmaVersion: 12,
|
||||
// sourceType: 'module',
|
||||
// },
|
||||
// rules: {
|
||||
// },
|
||||
};
|
||||
+2
-1
@@ -10,4 +10,5 @@ RUN yarn install --production && yarn cache clean
|
||||
COPY . /app
|
||||
|
||||
ENV NODE_ENV production
|
||||
ENTRYPOINT ["node", "-r", "esm", "./bin/server"]
|
||||
ENV DEBUG *
|
||||
ENTRYPOINT ["node", "-r", "esm", "./bin/server", "--secure", "--max-sockets", "20"]
|
||||
|
||||
+23
-4
@@ -55,13 +55,14 @@ class Client extends EventEmitter {
|
||||
this.emit('close');
|
||||
}
|
||||
|
||||
handleRequest(req, res) {
|
||||
handleRequest(req, res, retry = true) {
|
||||
this.debug('> %s', req.url);
|
||||
const opt = {
|
||||
path: req.url,
|
||||
agent: this.agent,
|
||||
method: req.method,
|
||||
headers: req.headers
|
||||
headers: req.headers,
|
||||
timeout: 120000 // 2 min, default node timeout
|
||||
};
|
||||
|
||||
const clientReq = http.request(opt, (clientRes) => {
|
||||
@@ -70,7 +71,12 @@ class Client extends EventEmitter {
|
||||
res.writeHead(clientRes.statusCode, clientRes.headers);
|
||||
|
||||
// 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
|
||||
@@ -78,10 +84,23 @@ class Client extends EventEmitter {
|
||||
// if we have already sent headers?
|
||||
clientReq.once('error', (err) => {
|
||||
// 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
|
||||
pump(req, clientReq);
|
||||
pump(req, clientReq, (err) => {
|
||||
if (err) {
|
||||
this.debug('< PIPE ERROR, %s', err);
|
||||
this.debug(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
handleUpgrade(req, socket) {
|
||||
|
||||
+8
-2
@@ -4,7 +4,7 @@ import { Duplex } from 'stream';
|
||||
import WebSocket from 'ws';
|
||||
import net from 'net';
|
||||
|
||||
import Client from './Client';
|
||||
import Client from './Client.js';
|
||||
|
||||
class DummySocket extends Duplex {
|
||||
constructor(options) {
|
||||
@@ -57,12 +57,17 @@ class DummyAgent extends http.Agent {
|
||||
}
|
||||
|
||||
createConnection(options, cb) {
|
||||
cb(null, new DummySocket());
|
||||
const soc = new DummySocket();
|
||||
// console.log( 'QWEQWEQE', options, cb);
|
||||
cb(null, soc);
|
||||
return soc
|
||||
}
|
||||
}
|
||||
|
||||
describe('Client', () => {
|
||||
it('should handle request', async () => {
|
||||
console.log('!!!!!!', 'qwfqwfwqfw');
|
||||
|
||||
const agent = new DummyAgent();
|
||||
const client = new Client({ agent });
|
||||
|
||||
@@ -85,6 +90,7 @@ describe('Client', () => {
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
console.log(res.headers)
|
||||
assert.equal(res.headers['x-powered-by'], 'dummy');
|
||||
server.close();
|
||||
});
|
||||
|
||||
+13
-14
@@ -1,8 +1,8 @@
|
||||
import { hri } from 'human-readable-ids';
|
||||
import Debug from 'debug';
|
||||
|
||||
import Client from './Client';
|
||||
import TunnelAgent from './TunnelAgent';
|
||||
import Client from './Client.js';
|
||||
import TunnelAgent from './TunnelAgent.js';
|
||||
|
||||
// Manage sets of clients
|
||||
//
|
||||
@@ -20,27 +20,22 @@ class 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`
|
||||
// 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]) {
|
||||
if (this.clients[id]) {
|
||||
id = hri.random();
|
||||
}
|
||||
|
||||
const maxSockets = this.opt.max_tcp_sockets;
|
||||
const maxTcpSockets = this.opt.max_tcp_sockets;
|
||||
const agent = new TunnelAgent({
|
||||
clientId: id,
|
||||
maxSockets: 10,
|
||||
maxTcpSockets,
|
||||
|
||||
});
|
||||
|
||||
const client = new Client({
|
||||
@@ -50,7 +45,7 @@ class ClientManager {
|
||||
|
||||
// add to clients map immediately
|
||||
// avoiding races with other clients requesting same id
|
||||
clients[id] = client;
|
||||
this.clients[id] = client;
|
||||
|
||||
client.once('close', () => {
|
||||
this.removeClient(id);
|
||||
@@ -59,11 +54,11 @@ class ClientManager {
|
||||
// try/catch used here to remove client id
|
||||
try {
|
||||
const info = await agent.listen();
|
||||
++stats.tunnels;
|
||||
++this.stats.tunnels;
|
||||
return {
|
||||
id: id,
|
||||
port: info.port,
|
||||
max_conn_count: maxSockets,
|
||||
max_conn_count: maxTcpSockets,
|
||||
};
|
||||
}
|
||||
catch (err) {
|
||||
@@ -73,6 +68,10 @@ class ClientManager {
|
||||
}
|
||||
}
|
||||
|
||||
getClients() {
|
||||
return Object.keys(this.clients);
|
||||
}
|
||||
|
||||
removeClient(id) {
|
||||
this.debug('removing client: %s', id);
|
||||
const client = this.clients[id];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import assert from 'assert';
|
||||
import net from 'net';
|
||||
|
||||
import ClientManager from './ClientManager';
|
||||
import ClientManager from './ClientManager.js';
|
||||
|
||||
describe('ClientManager', () => {
|
||||
it('should construct with no tunnels', () => {
|
||||
|
||||
+10
-7
@@ -1,10 +1,10 @@
|
||||
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;
|
||||
const DEFAULT_SOCKET_TIMEOUT = 10 * 60 * 1000;
|
||||
|
||||
// Implements an http.Agent interface to a pool of tunnel sockets
|
||||
// A tunnel socket is a connection _from_ a client that will
|
||||
@@ -34,6 +34,8 @@ class TunnelAgent extends Agent {
|
||||
// new tcp server to service requests for this client
|
||||
this.server = net.createServer();
|
||||
|
||||
this._socketTimeout = options.socketTimeout || DEFAULT_SOCKET_TIMEOUT;
|
||||
|
||||
// flag to avoid double starts
|
||||
this.started = false;
|
||||
this.closed = false;
|
||||
@@ -46,15 +48,14 @@ class TunnelAgent extends Agent {
|
||||
}
|
||||
|
||||
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) => {
|
||||
this.server.on('close', this._onClose.bind(this));
|
||||
this.server.on('connection', this._onConnection.bind(this));
|
||||
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;
|
||||
@@ -63,8 +64,8 @@ class TunnelAgent extends Agent {
|
||||
});
|
||||
|
||||
return new Promise((resolve) => {
|
||||
server.listen(() => {
|
||||
const port = server.address().port;
|
||||
this.server.listen(() => {
|
||||
const port = this.server.address().port;
|
||||
this.debug('tcp server listening on port: %d', port);
|
||||
|
||||
resolve({
|
||||
@@ -88,6 +89,8 @@ class TunnelAgent extends Agent {
|
||||
|
||||
// new socket connection from client for tunneling requests to client
|
||||
_onConnection(socket) {
|
||||
console.log('!!!!!!', socket);
|
||||
socket.setTimeout(this._socketTimeout);
|
||||
// no more socket connections allowed
|
||||
if (this.connectedSockets >= this.maxTcpSockets) {
|
||||
this.debug('no more sockets allowed');
|
||||
|
||||
+15
-1
@@ -2,7 +2,7 @@ import http from 'http';
|
||||
import net from 'net';
|
||||
import assert from 'assert';
|
||||
|
||||
import TunnelAgent from './TunnelAgent';
|
||||
import TunnelAgent from './TunnelAgent.js';
|
||||
|
||||
describe('TunnelAgent', () => {
|
||||
it('should create an empty agent', async () => {
|
||||
@@ -180,4 +180,18 @@ describe('TunnelAgent', () => {
|
||||
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
@@ -1,19 +1,19 @@
|
||||
{
|
||||
"type": "module",
|
||||
"author": "Roman Shtylman <[email protected]>",
|
||||
"name": "localtunnel-server",
|
||||
"description": "expose localhost to the world",
|
||||
"version": "0.0.8",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/localtunnel/server.git"
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"dependencies": {
|
||||
"book": "1.3.3",
|
||||
"debug": "3.1.0",
|
||||
"esm": "3.0.34",
|
||||
"human-readable-ids": "1.0.3",
|
||||
"koa": "2.5.1",
|
||||
"esm": "3.2.25",
|
||||
"human-readable-ids": "1.0.4",
|
||||
"koa": "2.13.0",
|
||||
"koa-router": "7.4.0",
|
||||
"localenv": "0.2.2",
|
||||
"optimist": "0.6.1",
|
||||
@@ -21,14 +21,22 @@
|
||||
"tldjs": "2.3.1"
|
||||
},
|
||||
"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",
|
||||
"supertest": "3.1.0",
|
||||
"ws": "5.1.1"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --check-leaks --require esm './**/*.test.js'",
|
||||
"start": "./bin/server",
|
||||
"dev": "node-dev bin/server --port 3000"
|
||||
"test": "mocha --check-leaks ./**/*.test.js",
|
||||
"start": "node -r esm bin/server --port 1234 --max-sockets 30",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ export default function(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);
|
||||
@@ -29,9 +28,9 @@ export default function(opt) {
|
||||
const router = new Router();
|
||||
|
||||
router.get('/api/status', async (ctx, next) => {
|
||||
const stats = manager.stats;
|
||||
ctx.body = {
|
||||
tunnels: stats.tunnels,
|
||||
tunnelsCount: manager.stats.tunnels,
|
||||
tunnels: manager.getClients(),
|
||||
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.allowedMethods());
|
||||
|
||||
@@ -76,7 +89,8 @@ export default function(opt) {
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -129,13 +143,15 @@ export default function(opt) {
|
||||
const clientId = GetClientIdFromHostname(hostname);
|
||||
if (!clientId) {
|
||||
appCallback(req, res);
|
||||
debug('ClientID for host %s was not found :(', hostname);
|
||||
return;
|
||||
}
|
||||
|
||||
debug('Identified client ID as: %s', clientId);
|
||||
const client = manager.getClient(clientId);
|
||||
if (!client) {
|
||||
res.statusCode = 404;
|
||||
res.end('404');
|
||||
res.end("Can't find active tunnel");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+5
-2
@@ -1,10 +1,13 @@
|
||||
import request from 'supertest';
|
||||
import assert from 'assert';
|
||||
import { Server as WebSocketServer } from 'ws';
|
||||
// import { Server as WebSocketServer } from 'ws';
|
||||
import WebSocket from 'ws';
|
||||
import net from 'net';
|
||||
|
||||
import createServer from './server';
|
||||
import createServer from './server.js';
|
||||
|
||||
const WebSocketServer = WebSocket.Server;
|
||||
|
||||
|
||||
describe('Server', () => {
|
||||
it('server starts and stops', async () => {
|
||||
|
||||
Reference in New Issue
Block a user