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: {
|
||||||
|
// },
|
||||||
|
};
|
||||||
+8
-2
@@ -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();
|
||||||
});
|
});
|
||||||
|
|||||||
+9
-11
@@ -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,20 +20,14 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,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);
|
||||||
@@ -60,7 +54,7 @@ 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,
|
||||||
@@ -74,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,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
@@ -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
|
||||||
@@ -34,6 +34,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;
|
||||||
this.closed = false;
|
this.closed = 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
@@ -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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+8
-5
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
|
"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",
|
||||||
@@ -21,13 +21,16 @@
|
|||||||
"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": "node -r esm bin/server --port 1234 --max-sockets 30",
|
"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",
|
"start-docker": "docker run -d --restart always --name localtunnel --net host defunctzombie/localtunnel-server:latest --port 1234",
|
||||||
"dev": "node-dev bin/server --port 3000",
|
"dev": "node-dev bin/server --port 3000",
|
||||||
|
|||||||
@@ -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(),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -90,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
|
||||||
@@ -147,10 +147,11 @@ export default function(opt) {
|
|||||||
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
@@ -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 () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user