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: {
|
||||
// },
|
||||
};
|
||||
+9
-3
@@ -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();
|
||||
});
|
||||
@@ -154,4 +160,4 @@ describe('Client', () => {
|
||||
netClient.destroy();
|
||||
server.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
+9
-11
@@ -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,20 +20,14 @@ 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();
|
||||
}
|
||||
|
||||
@@ -51,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);
|
||||
@@ -60,7 +54,7 @@ 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,
|
||||
@@ -74,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
|
||||
@@ -33,6 +33,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;
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
+8
-5
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"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",
|
||||
@@ -21,13 +21,16 @@
|
||||
"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'",
|
||||
"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",
|
||||
|
||||
@@ -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(),
|
||||
};
|
||||
});
|
||||
@@ -90,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
|
||||
@@ -147,10 +147,11 @@ export default function(opt) {
|
||||
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