bunch of changes
This commit is contained in:
+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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user