mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 13:35:53 +00:00
refactor to use http agent interface for tunnels
This commit is contained in:
10
README.md
10
README.md
@@ -43,6 +43,16 @@ You will be assigned a URL similar to `heavy-puma-9.sub.example.com:1234`.
|
|||||||
|
|
||||||
If your server is acting as a reverse proxy (i.e. nginx) and is able to listen on port 80, then you do not need the `:1234` part of the hostname for the `lt` client.
|
If your server is acting as a reverse proxy (i.e. nginx) and is able to listen on port 80, then you do not need the `:1234` part of the hostname for the `lt` client.
|
||||||
|
|
||||||
|
## REST API
|
||||||
|
|
||||||
|
### POST /api/tunnels
|
||||||
|
|
||||||
|
Create a new tunnel. A LocalTunnel client posts to this enpoint to request a new tunnel with a specific name or a randomly assigned name.
|
||||||
|
|
||||||
|
### GET /api/status
|
||||||
|
|
||||||
|
General server information.
|
||||||
|
|
||||||
## Deploy
|
## Deploy
|
||||||
|
|
||||||
You can deploy your own localtunnel server using the prebuilt docker image.
|
You can deploy your own localtunnel server using the prebuilt docker image.
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
import http from 'http';
|
|
||||||
import util from 'util';
|
|
||||||
import assert from 'assert';
|
|
||||||
|
|
||||||
// binding agent will return a given options.socket as the socket for the agent
|
|
||||||
// this is useful if you already have a socket established and want the request
|
|
||||||
// to use that socket instead of making a new one
|
|
||||||
function BindingAgent(options) {
|
|
||||||
options = options || {};
|
|
||||||
http.Agent.call(this, options);
|
|
||||||
|
|
||||||
this.socket = options.socket;
|
|
||||||
assert(this.socket, 'socket is required for BindingAgent');
|
|
||||||
this.createConnection = create_connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
util.inherits(BindingAgent, http.Agent);
|
|
||||||
|
|
||||||
function create_connection(port, host, options) {
|
|
||||||
return this.socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default BindingAgent;
|
|
||||||
69
lib/Client.js
Normal file
69
lib/Client.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import http from 'http';
|
||||||
|
|
||||||
|
import TunnelAgent from './TunnelAgent';
|
||||||
|
|
||||||
|
// A client encapsulates req/res handling using an agent
|
||||||
|
//
|
||||||
|
// If an agent is destroyed, the request handling will error
|
||||||
|
// The caller is responsible for handling a failed request
|
||||||
|
class Client {
|
||||||
|
constructor(options) {
|
||||||
|
this.agent = options.agent;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRequest(req, res) {
|
||||||
|
const opt = {
|
||||||
|
path: req.url,
|
||||||
|
agent: this.agent,
|
||||||
|
method: req.method,
|
||||||
|
headers: req.headers
|
||||||
|
};
|
||||||
|
|
||||||
|
const clientReq = http.request(opt, (clientRes) => {
|
||||||
|
// write response code and headers
|
||||||
|
res.writeHead(clientRes.statusCode, clientRes.headers);
|
||||||
|
clientRes.pipe(res);
|
||||||
|
});
|
||||||
|
|
||||||
|
// this can happen when underlying agent produces an error
|
||||||
|
// in our case we 504 gateway error this?
|
||||||
|
// if we have already sent headers?
|
||||||
|
clientReq.once('error', (err) => {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
req.pipe(clientReq);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleUpgrade(req, socket) {
|
||||||
|
this.agent.createConnection({}, (err, conn) => {
|
||||||
|
// any errors getting a connection mean we cannot service this request
|
||||||
|
if (err) {
|
||||||
|
socket.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// socket met have disconnected while we waiting for a socket
|
||||||
|
if (!socket.readable || !socket.writable) {
|
||||||
|
socket.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// websocket requests are special in that we simply re-create the header info
|
||||||
|
// then directly pipe the socket data
|
||||||
|
// avoids having to rebuild the request and handle upgrades via the http client
|
||||||
|
const arr = [`${req.method} ${req.url} HTTP/${req.httpVersion}`];
|
||||||
|
for (let i=0 ; i < (req.rawHeaders.length-1) ; i+=2) {
|
||||||
|
arr.push(`${req.rawHeaders[i]}: ${req.rawHeaders[i+1]}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
arr.push('');
|
||||||
|
arr.push('');
|
||||||
|
|
||||||
|
conn.pipe(socket).pipe(conn);
|
||||||
|
conn.write(arr.join('\r\n'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Client;
|
||||||
158
lib/Client.test.js
Normal file
158
lib/Client.test.js
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
import assert from 'assert';
|
||||||
|
import http from 'http';
|
||||||
|
import { Duplex } from 'stream';
|
||||||
|
import EventEmitter from 'events';
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
import net from 'net';
|
||||||
|
|
||||||
|
import Client from './Client';
|
||||||
|
|
||||||
|
class DummySocket extends Duplex {
|
||||||
|
constructor(options) {
|
||||||
|
super(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
_write(chunk, encoding, callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
_read(size) {
|
||||||
|
this.push('HTTP/1.1 304 Not Modified\r\nX-Powered-By: dummy\r\n\r\n\r\n');
|
||||||
|
this.push(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DummyWebsocket extends Duplex {
|
||||||
|
constructor(options) {
|
||||||
|
super(options);
|
||||||
|
this.sentHeader = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_write(chunk, encoding, callback) {
|
||||||
|
const str = chunk.toString();
|
||||||
|
// if chunk contains `GET / HTTP/1.1` -> queue headers
|
||||||
|
// otherwise echo back received data
|
||||||
|
if (str.indexOf('GET / HTTP/1.1') === 0) {
|
||||||
|
const arr = [
|
||||||
|
'HTTP/1.1 101 Switching Protocols',
|
||||||
|
'Upgrade: websocket',
|
||||||
|
'Connection: Upgrade',
|
||||||
|
];
|
||||||
|
this.push(arr.join('\r\n'));
|
||||||
|
this.push('\r\n\r\n');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.push(str);
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
_read(size) {
|
||||||
|
// nothing to implement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DummyAgent extends http.Agent {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
createConnection(options, cb) {
|
||||||
|
cb(null, new DummySocket());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Client', () => {
|
||||||
|
it('should handle request', async () => {
|
||||||
|
const agent = new DummyAgent();
|
||||||
|
const client = new Client({ agent });
|
||||||
|
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
client.handleRequest(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
await new Promise(resolve => server.listen(resolve));
|
||||||
|
|
||||||
|
const address = server.address();
|
||||||
|
const opt = {
|
||||||
|
host: 'localhost',
|
||||||
|
port: address.port,
|
||||||
|
path: '/',
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await new Promise((resolve) => {
|
||||||
|
const req = http.get(opt, (res) => {
|
||||||
|
resolve(res);
|
||||||
|
});
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
assert.equal(res.headers['x-powered-by'], 'dummy');
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle upgrade', async () => {
|
||||||
|
// need a websocket server and a socket for it
|
||||||
|
class DummyWebsocketAgent extends http.Agent {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
createConnection(options, cb) {
|
||||||
|
cb(null, new DummyWebsocket());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const agent = new DummyWebsocketAgent();
|
||||||
|
const client = new Client({ agent });
|
||||||
|
|
||||||
|
const server = http.createServer();
|
||||||
|
server.on('upgrade', (req, socket, head) => {
|
||||||
|
client.handleUpgrade(req, socket);
|
||||||
|
});
|
||||||
|
|
||||||
|
await new Promise(resolve => server.listen(resolve));
|
||||||
|
|
||||||
|
const address = server.address();
|
||||||
|
|
||||||
|
const netClient = await new Promise((resolve) => {
|
||||||
|
const newClient = net.createConnection({ port: address.port }, () => {
|
||||||
|
resolve(newClient);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const out = [
|
||||||
|
'GET / HTTP/1.1',
|
||||||
|
'Connection: Upgrade',
|
||||||
|
'Upgrade: websocket'
|
||||||
|
];
|
||||||
|
|
||||||
|
netClient.write(out.join('\r\n') + '\r\n\r\n');
|
||||||
|
|
||||||
|
{
|
||||||
|
const data = await new Promise((resolve) => {
|
||||||
|
netClient.once('data', (chunk) => {
|
||||||
|
resolve(chunk.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const exp = [
|
||||||
|
'HTTP/1.1 101 Switching Protocols',
|
||||||
|
'Upgrade: websocket',
|
||||||
|
'Connection: Upgrade',
|
||||||
|
];
|
||||||
|
assert.equal(exp.join('\r\n') + '\r\n\r\n', data);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
netClient.write('foobar');
|
||||||
|
const data = await new Promise((resolve) => {
|
||||||
|
netClient.once('data', (chunk) => {
|
||||||
|
resolve(chunk.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
assert.equal('foobar', data);
|
||||||
|
}
|
||||||
|
|
||||||
|
netClient.destroy();
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,35 +1,30 @@
|
|||||||
import Proxy from './Proxy';
|
import { hri } from 'human-readable-ids';
|
||||||
|
import Debug from 'debug';
|
||||||
|
|
||||||
// maybe remove?
|
import Client from './Client';
|
||||||
import on_finished from 'on-finished';
|
import TunnelAgent from './TunnelAgent';
|
||||||
import http from 'http';
|
|
||||||
import pump from 'pump';
|
|
||||||
import { hri } from "human-readable-ids";
|
|
||||||
|
|
||||||
import BindingAgent from './BindingAgent';
|
|
||||||
|
|
||||||
const NoOp = () => {};
|
|
||||||
|
|
||||||
// Manage sets of clients
|
// Manage sets of clients
|
||||||
//
|
//
|
||||||
// A client is a "user session" established to service a remote localtunnel client
|
// A client is a "user session" established to service a remote localtunnel client
|
||||||
class ClientManager {
|
class ClientManager {
|
||||||
constructor(opt) {
|
constructor(opt) {
|
||||||
this.opt = opt;
|
this.opt = opt || {};
|
||||||
|
|
||||||
this.reqId = 0;
|
|
||||||
|
|
||||||
// id -> client instance
|
// id -> client instance
|
||||||
this.clients = Object.create(null);
|
this.clients = new Map();
|
||||||
|
|
||||||
// statistics
|
// statistics
|
||||||
this.stats = {
|
this.stats = {
|
||||||
tunnels: 0
|
tunnels: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.debug = Debug('lt:ClientManager');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
async newClient(id) {
|
async newClient(id) {
|
||||||
const clients = this.clients;
|
const clients = this.clients;
|
||||||
const stats = this.stats;
|
const stats = this.stats;
|
||||||
@@ -39,161 +34,68 @@ class ClientManager {
|
|||||||
id = hri.random();
|
id = hri.random();
|
||||||
}
|
}
|
||||||
|
|
||||||
const popt = {
|
const maxSockets = this.opt.max_tcp_sockets;
|
||||||
id: id,
|
const agent = new TunnelAgent({
|
||||||
max_tcp_sockets: this.opt.max_tcp_sockets
|
maxSockets: 10,
|
||||||
};
|
});
|
||||||
|
|
||||||
const client = Proxy(popt);
|
agent.on('online', () => {
|
||||||
|
this.debug('client online %s', id);
|
||||||
|
});
|
||||||
|
|
||||||
|
agent.on('offline', () => {
|
||||||
|
// TODO(roman): grace period for re-connecting
|
||||||
|
// this period is short as the client is expected to maintain connections actively
|
||||||
|
// if they client does not reconnect on a dropped connection they need to re-establish
|
||||||
|
this.debug('client offline %s', id);
|
||||||
|
this.removeClient(id);
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO(roman): an agent error removes the client, the user needs to re-connect?
|
||||||
|
// how does a user realize they need to re-connect vs some random client being assigned same port?
|
||||||
|
agent.once('error', (err) => {
|
||||||
|
this.removeClient(id);
|
||||||
|
});
|
||||||
|
|
||||||
|
const client = new Client({ agent });
|
||||||
|
|
||||||
// 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;
|
clients[id] = client;
|
||||||
|
|
||||||
client.on('end', () => {
|
// try/catch used here to remove client id
|
||||||
--stats.tunnels;
|
try {
|
||||||
delete clients[id];
|
const info = await agent.listen();
|
||||||
});
|
++stats.tunnels;
|
||||||
|
return {
|
||||||
return new Promise((resolve, reject) => {
|
id: id,
|
||||||
// each local client has a tcp server to link with the remove localtunnel client
|
port: info.port,
|
||||||
// this starts the server and waits until it is listening
|
max_conn_count: maxSockets,
|
||||||
client.start((err, info) => {
|
};
|
||||||
if (err) {
|
}
|
||||||
// clear the reserved client id
|
catch (err) {
|
||||||
delete clients[id];
|
this.removeClient(id);
|
||||||
reject(err);
|
// rethrow error for upstream to handle
|
||||||
return;
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
++stats.tunnels;
|
removeClient(id) {
|
||||||
info.id = id;
|
const client = this.clients[id];
|
||||||
resolve(info);
|
if (!client) {
|
||||||
});
|
return;
|
||||||
});
|
}
|
||||||
|
--this.stats.tunnels;
|
||||||
|
delete this.clients[id];
|
||||||
|
client.agent.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
hasClient(id) {
|
hasClient(id) {
|
||||||
return this.clients[id];
|
return this.clients[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle http request
|
getClient(id) {
|
||||||
handleRequest(clientId, req, res) {
|
return this.clients[id];
|
||||||
const client = this.clients[clientId];
|
|
||||||
if (!client) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const reqId = this.reqId;
|
|
||||||
this.reqId = this.reqId + 1;
|
|
||||||
|
|
||||||
let endRes = () => {
|
|
||||||
endRes = NoOp;
|
|
||||||
res.end();
|
|
||||||
};
|
|
||||||
|
|
||||||
on_finished(res, () => {
|
|
||||||
endRes = NoOp;
|
|
||||||
});
|
|
||||||
|
|
||||||
client.nextSocket((clientSocket) => {
|
|
||||||
// response ended before we even got a socket to respond on
|
|
||||||
if (endRes === NoOp) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// happens when client upstream is disconnected (or disconnects)
|
|
||||||
// and the proxy iterates the waiting list and clears the callbacks
|
|
||||||
// we gracefully inform the user and kill their conn
|
|
||||||
// without this, the browser will leave some connections open
|
|
||||||
// and try to use them again for new requests
|
|
||||||
// TODO(roman) we could instead have a timeout above
|
|
||||||
// if no socket becomes available within some time,
|
|
||||||
// we just tell the user no resource available to service request
|
|
||||||
if (!clientSocket) {
|
|
||||||
endRes();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const agent = new BindingAgent({
|
|
||||||
socket: clientSocket,
|
|
||||||
});
|
|
||||||
|
|
||||||
const opt = {
|
|
||||||
path: req.url,
|
|
||||||
agent: agent,
|
|
||||||
method: req.method,
|
|
||||||
headers: req.headers
|
|
||||||
};
|
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
// what if error making this request?
|
|
||||||
const clientReq = http.request(opt, (clientRes) => {
|
|
||||||
// write response code and headers
|
|
||||||
res.writeHead(clientRes.statusCode, clientRes.headers);
|
|
||||||
|
|
||||||
// when this pump is done, we end our response
|
|
||||||
pump(clientRes, res, (err) => {
|
|
||||||
endRes();
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// we don't care about when this ends, only if there is error
|
|
||||||
pump(req, clientReq, (err) => {
|
|
||||||
if (err) {
|
|
||||||
endRes();
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle http upgrade
|
|
||||||
handleUpgrade(clientId, req, sock) {
|
|
||||||
const client = this.clients[clientId];
|
|
||||||
if (!client) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
client.nextSocket(async (clientSocket) => {
|
|
||||||
if (!sock.readable || !sock.writable) {
|
|
||||||
sock.end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// happens when client upstream is disconnected (or disconnects)
|
|
||||||
// and the proxy iterates the waiting list and clears the callbacks
|
|
||||||
// we gracefully inform the user and kill their conn
|
|
||||||
// without this, the browser will leave some connections open
|
|
||||||
// and try to use them again for new requests
|
|
||||||
// TODO(roman) we could instead have a timeout above
|
|
||||||
// if no socket becomes available within some time,
|
|
||||||
// we just tell the user no resource available to service request
|
|
||||||
if (!clientSocket) {
|
|
||||||
sock.end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// websocket requests are special in that we simply re-create the header info
|
|
||||||
// then directly pipe the socket data
|
|
||||||
// avoids having to rebuild the request and handle upgrades via the http client
|
|
||||||
const arr = [`${req.method} ${req.url} HTTP/${req.httpVersion}`];
|
|
||||||
for (let i=0 ; i < (req.rawHeaders.length-1) ; i+=2) {
|
|
||||||
arr.push(`${req.rawHeaders[i]}: ${req.rawHeaders[i+1]}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
arr.push('');
|
|
||||||
arr.push('');
|
|
||||||
|
|
||||||
clientSocket.pipe(sock).pipe(clientSocket);
|
|
||||||
clientSocket.write(arr.join('\r\n'));
|
|
||||||
|
|
||||||
await new Promise((resolve) => {
|
|
||||||
sock.once('end', resolve);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
51
lib/ClientManager.test.js
Normal file
51
lib/ClientManager.test.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import assert from 'assert';
|
||||||
|
import net from 'net';
|
||||||
|
|
||||||
|
import ClientManager from './ClientManager';
|
||||||
|
|
||||||
|
describe('ClientManager', () => {
|
||||||
|
it('should construct with no tunnels', () => {
|
||||||
|
const manager = new ClientManager();
|
||||||
|
assert.equal(manager.stats.tunnels, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a new client with random id', async () => {
|
||||||
|
const manager = new ClientManager();
|
||||||
|
const client = await manager.newClient();
|
||||||
|
assert(manager.hasClient(client.id));
|
||||||
|
manager.removeClient(client.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a new client with id', async () => {
|
||||||
|
const manager = new ClientManager();
|
||||||
|
const client = await manager.newClient('foobar');
|
||||||
|
assert(manager.hasClient('foobar'));
|
||||||
|
manager.removeClient('foobar');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a new client with random id if previous exists', async () => {
|
||||||
|
const manager = new ClientManager();
|
||||||
|
const clientA = await manager.newClient('foobar');
|
||||||
|
const clientB = await manager.newClient('foobar');
|
||||||
|
assert(clientA.id, 'foobar');
|
||||||
|
assert(manager.hasClient(clientB.id));
|
||||||
|
assert(clientB.id != clientA.id);
|
||||||
|
manager.removeClient(clientB.id);
|
||||||
|
manager.removeClient('foobar');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove client once it goes offline', async () => {
|
||||||
|
const manager = new ClientManager();
|
||||||
|
const client = await manager.newClient('foobar');
|
||||||
|
|
||||||
|
const socket = await new Promise((resolve) => {
|
||||||
|
const netClient = net.createConnection({ port: client.port }, () => {
|
||||||
|
resolve(netClient);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const closePromise = new Promise(resolve => socket.once('close', resolve));
|
||||||
|
socket.end();
|
||||||
|
await closePromise;
|
||||||
|
assert(!manager.hasClient('foobar'));
|
||||||
|
});
|
||||||
|
});
|
||||||
193
lib/Proxy.js
193
lib/Proxy.js
@@ -1,193 +0,0 @@
|
|||||||
import net from 'net';
|
|
||||||
import EventEmitter from 'events';
|
|
||||||
import log from 'book';
|
|
||||||
import Debug from 'debug';
|
|
||||||
|
|
||||||
const debug = Debug('localtunnel:server');
|
|
||||||
|
|
||||||
const Proxy = function(opt) {
|
|
||||||
if (!(this instanceof Proxy)) {
|
|
||||||
return new Proxy(opt);
|
|
||||||
}
|
|
||||||
|
|
||||||
const self = this;
|
|
||||||
|
|
||||||
self.sockets = [];
|
|
||||||
self.waiting = [];
|
|
||||||
self.id = opt.id;
|
|
||||||
|
|
||||||
self.activeSockets = 0;
|
|
||||||
|
|
||||||
// default max is 10
|
|
||||||
self.max_tcp_sockets = opt.max_tcp_sockets || 10;
|
|
||||||
|
|
||||||
// new tcp server to service requests for this client
|
|
||||||
self.server = net.createServer();
|
|
||||||
|
|
||||||
// track initial user connection setup
|
|
||||||
self.conn_timeout = undefined;
|
|
||||||
|
|
||||||
self.debug = Debug(`localtunnel:server:${self.id}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
Proxy.prototype.__proto__ = EventEmitter.prototype;
|
|
||||||
|
|
||||||
Proxy.prototype.start = function(cb) {
|
|
||||||
const self = this;
|
|
||||||
const server = self.server;
|
|
||||||
|
|
||||||
if (self.started) {
|
|
||||||
cb(new Error('already started'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self.started = true;
|
|
||||||
|
|
||||||
server.on('close', self._cleanup.bind(self));
|
|
||||||
server.on('connection', self._handle_socket.bind(self));
|
|
||||||
|
|
||||||
server.on('error', function(err) {
|
|
||||||
// where do these errors come from?
|
|
||||||
// other side creates a connection and then is killed?
|
|
||||||
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.error(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(function() {
|
|
||||||
const port = server.address().port;
|
|
||||||
self.debug('tcp server listening on port: %d', port);
|
|
||||||
|
|
||||||
cb(null, {
|
|
||||||
// port for lt client tcp connections
|
|
||||||
port: port,
|
|
||||||
// maximum number of tcp connections allowed by lt client
|
|
||||||
max_conn_count: self.max_tcp_sockets
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
self._maybe_destroy();
|
|
||||||
};
|
|
||||||
|
|
||||||
Proxy.prototype._maybe_destroy = function() {
|
|
||||||
const self = this;
|
|
||||||
|
|
||||||
clearTimeout(self.conn_timeout);
|
|
||||||
|
|
||||||
// After last socket is gone, we give opportunity to connect again quickly
|
|
||||||
self.conn_timeout = setTimeout(function() {
|
|
||||||
// sometimes the server is already closed but the event has not fired?
|
|
||||||
try {
|
|
||||||
clearTimeout(self.conn_timeout);
|
|
||||||
self.server.close();
|
|
||||||
}
|
|
||||||
catch (err) {
|
|
||||||
self._cleanup();
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// new socket connection from client for tunneling requests to client
|
|
||||||
Proxy.prototype._handle_socket = function(socket) {
|
|
||||||
const self = this;
|
|
||||||
|
|
||||||
// no more socket connections allowed
|
|
||||||
if (self.activeSockets >= self.max_tcp_sockets) {
|
|
||||||
return socket.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
self.activeSockets = self.activeSockets + 1;
|
|
||||||
|
|
||||||
self.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
|
|
||||||
|
|
||||||
// a single connection is enough to keep client id slot open
|
|
||||||
clearTimeout(self.conn_timeout);
|
|
||||||
|
|
||||||
socket.once('close', function(had_error) {
|
|
||||||
self.activeSockets = self.activeSockets - 1;
|
|
||||||
self.debug('closed socket (error: %s)', had_error);
|
|
||||||
|
|
||||||
// what if socket was servicing a request at this time?
|
|
||||||
// then it will be put back in available after right?
|
|
||||||
// we need a list of sockets servicing requests?
|
|
||||||
|
|
||||||
// remove this socket
|
|
||||||
const idx = self.sockets.indexOf(socket);
|
|
||||||
if (idx >= 0) {
|
|
||||||
self.sockets.splice(idx, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// need to track total sockets, not just active available
|
|
||||||
self.debug('remaining client sockets: %s', self.sockets.length);
|
|
||||||
|
|
||||||
// no more sockets for this ident
|
|
||||||
if (self.sockets.length === 0) {
|
|
||||||
self.debug('all sockets disconnected');
|
|
||||||
self._maybe_destroy();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// close will be emitted after this
|
|
||||||
socket.on('error', function(err) {
|
|
||||||
// we don't log here to avoid logging crap for misbehaving clients
|
|
||||||
socket.destroy();
|
|
||||||
});
|
|
||||||
|
|
||||||
self.sockets.push(socket);
|
|
||||||
self._process_waiting();
|
|
||||||
};
|
|
||||||
|
|
||||||
Proxy.prototype._process_waiting = function() {
|
|
||||||
const self = this;
|
|
||||||
const fn = self.waiting.shift();
|
|
||||||
if (fn) {
|
|
||||||
self.debug('handling queued request');
|
|
||||||
self.nextSocket(fn);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Proxy.prototype._cleanup = function() {
|
|
||||||
const self = this;
|
|
||||||
self.debug('closed tcp socket for client(%s)', self.id);
|
|
||||||
|
|
||||||
clearTimeout(self.conn_timeout);
|
|
||||||
|
|
||||||
// clear waiting by ending responses, (requests?)
|
|
||||||
self.waiting.forEach(handler => handler(null));
|
|
||||||
|
|
||||||
self.emit('end');
|
|
||||||
};
|
|
||||||
|
|
||||||
Proxy.prototype.nextSocket = async function(fn) {
|
|
||||||
const self = this;
|
|
||||||
|
|
||||||
// socket is a tcp connection back to the user hosting the site
|
|
||||||
const sock = self.sockets.shift();
|
|
||||||
if (!sock) {
|
|
||||||
self.debug('no more clients, queue callback');
|
|
||||||
self.waiting.push(fn);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.debug('processing request');
|
|
||||||
await fn(sock);
|
|
||||||
|
|
||||||
if (!sock.destroyed) {
|
|
||||||
self.debug('retuning socket');
|
|
||||||
self.sockets.push(sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
// no sockets left to process waiting requests
|
|
||||||
if (self.sockets.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
self._process_waiting();
|
|
||||||
};
|
|
||||||
|
|
||||||
Proxy.prototype._done = function() {
|
|
||||||
const self = this;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Proxy;
|
|
||||||
171
lib/TunnelAgent.js
Normal file
171
lib/TunnelAgent.js
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
// Implements an http.Agent interface to a pool of tunnel sockets
|
||||||
|
// A tunnel socket is a connection _from_ a client that will
|
||||||
|
// service http requests. This agent is usable wherever one can use an http.Agent
|
||||||
|
class TunnelAgent extends Agent {
|
||||||
|
constructor(options = {}) {
|
||||||
|
super({
|
||||||
|
keepAlive: true,
|
||||||
|
// only allow keepalive to hold on to one socket
|
||||||
|
// this prevents it from holding on to all the sockets so they can be used for upgrades
|
||||||
|
maxFreeSockets: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
// sockets we can hand out via createConnection
|
||||||
|
this.availableSockets = [];
|
||||||
|
|
||||||
|
// when a createConnection cannot return a socket, it goes into a queue
|
||||||
|
// once a socket is available it is handed out to the next callback
|
||||||
|
this.waitingCreateConn = [];
|
||||||
|
|
||||||
|
this.debug = Debug('lt:TunnelAgent');
|
||||||
|
|
||||||
|
// track maximum allowed sockets
|
||||||
|
this.activeSockets = 0;
|
||||||
|
this.maxTcpSockets = options.maxTcpSockets || DEFAULT_MAX_SOCKETS;
|
||||||
|
|
||||||
|
// new tcp server to service requests for this client
|
||||||
|
this.server = net.createServer();
|
||||||
|
|
||||||
|
// flag to avoid double starts
|
||||||
|
this.started = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) => {
|
||||||
|
// where do these errors come from?
|
||||||
|
// other side creates a connection and then is killed?
|
||||||
|
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
log.error(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
server.listen(() => {
|
||||||
|
const port = server.address().port;
|
||||||
|
this.debug('tcp server listening on port: %d', port);
|
||||||
|
|
||||||
|
resolve({
|
||||||
|
// port for lt client tcp connections
|
||||||
|
port: port,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_onClose() {
|
||||||
|
this.debug('closed tcp socket');
|
||||||
|
clearTimeout(this.connTimeout);
|
||||||
|
// we will not invoke these callbacks?
|
||||||
|
// TODO(roman): we could invoke these with errors...?
|
||||||
|
// this makes downstream have to handle this
|
||||||
|
this.waitingCreateConn = [];
|
||||||
|
this.emit('end');
|
||||||
|
}
|
||||||
|
|
||||||
|
// new socket connection from client for tunneling requests to client
|
||||||
|
_onConnection(socket) {
|
||||||
|
// no more socket connections allowed
|
||||||
|
if (this.activeSockets >= this.maxTcpSockets) {
|
||||||
|
this.debug('no more sockets allowed');
|
||||||
|
socket.destroy();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a new socket becomes available
|
||||||
|
if (this.activeSockets == 0) {
|
||||||
|
this.emit('online');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.activeSockets += 1;
|
||||||
|
this.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
|
||||||
|
|
||||||
|
// a single connection is enough to keep client id slot open
|
||||||
|
clearTimeout(this.connTimeout);
|
||||||
|
|
||||||
|
socket.once('close', (had_error) => {
|
||||||
|
this.debug('closed socket (error: %s)', had_error);
|
||||||
|
this.debug('removing socket');
|
||||||
|
this.activeSockets -= 1;
|
||||||
|
// remove the socket from available list
|
||||||
|
const idx = this.availableSockets.indexOf(socket);
|
||||||
|
if (idx >= 0) {
|
||||||
|
this.availableSockets.splice(idx, 1);
|
||||||
|
}
|
||||||
|
// need to track total sockets, not just active available
|
||||||
|
this.debug('remaining client sockets: %s', this.availableSockets.length);
|
||||||
|
// no more sockets for this session
|
||||||
|
// the session will become inactive if client does not reconnect
|
||||||
|
if (this.availableSockets.length <= 0) {
|
||||||
|
this.debug('all sockets disconnected');
|
||||||
|
this.emit('offline');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// close will be emitted after this
|
||||||
|
socket.once('error', (err) => {
|
||||||
|
// we do not log these errors, sessions can drop from clients for many reasons
|
||||||
|
// these are not actionable errors for our server
|
||||||
|
socket.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
// make socket available for those waiting on sockets
|
||||||
|
this.availableSockets.push(socket);
|
||||||
|
|
||||||
|
// flush anyone waiting on sockets
|
||||||
|
this._callWaitingCreateConn();
|
||||||
|
}
|
||||||
|
|
||||||
|
// invoke when a new socket is available and there may be waiting createConnection calls
|
||||||
|
_callWaitingCreateConn() {
|
||||||
|
const fn = this.waitingCreateConn.shift();
|
||||||
|
if (!fn) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug('handling queued request');
|
||||||
|
this.createConnection({}, fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch a socket from the available socket pool for the agent
|
||||||
|
// if no socket is available, queue
|
||||||
|
// cb(err, socket)
|
||||||
|
createConnection(options, cb) {
|
||||||
|
// socket is a tcp connection back to the user hosting the site
|
||||||
|
const sock = this.availableSockets.shift();
|
||||||
|
|
||||||
|
// no available sockets
|
||||||
|
// wait until we have one
|
||||||
|
if (!sock) {
|
||||||
|
this.waitingCreateConn.push(cb);
|
||||||
|
this.debug('waiting');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug('socket given');
|
||||||
|
cb(null, sock);
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy() {
|
||||||
|
this.server.close();
|
||||||
|
super.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TunnelAgent;
|
||||||
176
lib/TunnelAgent.test.js
Normal file
176
lib/TunnelAgent.test.js
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
import http from 'http';
|
||||||
|
import net from 'net';
|
||||||
|
import assert from 'assert';
|
||||||
|
|
||||||
|
import TunnelAgent from './TunnelAgent';
|
||||||
|
|
||||||
|
describe('TunnelAgent', () => {
|
||||||
|
it('should create an empty agent', async () => {
|
||||||
|
const agent = new TunnelAgent();
|
||||||
|
assert.equal(agent.started, false);
|
||||||
|
|
||||||
|
const info = await agent.listen();
|
||||||
|
assert.ok(info.port > 0);
|
||||||
|
agent.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a new server and accept connections', async () => {
|
||||||
|
const agent = new TunnelAgent();
|
||||||
|
assert.equal(agent.started, false);
|
||||||
|
|
||||||
|
const info = await agent.listen();
|
||||||
|
const sock = net.createConnection({ port: info.port });
|
||||||
|
|
||||||
|
// in this test we wait for the socket to be connected
|
||||||
|
await new Promise(resolve => sock.once('connect', resolve));
|
||||||
|
|
||||||
|
const agentSock = await new Promise((resolve, reject) => {
|
||||||
|
agent.createConnection({}, (err, sock) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
resolve(sock);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
agentSock.write('foo');
|
||||||
|
await new Promise(resolve => sock.once('readable', resolve));
|
||||||
|
|
||||||
|
assert.equal('foo', sock.read().toString());
|
||||||
|
agent.destroy();
|
||||||
|
sock.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reject connections over the max', async () => {
|
||||||
|
const agent = new TunnelAgent({
|
||||||
|
maxTcpSockets: 2,
|
||||||
|
});
|
||||||
|
assert.equal(agent.started, false);
|
||||||
|
|
||||||
|
const info = await agent.listen();
|
||||||
|
const sock1 = net.createConnection({ port: info.port });
|
||||||
|
const sock2 = net.createConnection({ port: info.port });
|
||||||
|
|
||||||
|
// two valid socket connections
|
||||||
|
const p1 = new Promise(resolve => sock1.once('connect', resolve));
|
||||||
|
const p2 = new Promise(resolve => sock2.once('connect', resolve));
|
||||||
|
await Promise.all([p1, p2]);
|
||||||
|
|
||||||
|
const sock3 = net.createConnection({ port: info.port });
|
||||||
|
const p3 = await new Promise(resolve => sock3.once('close', resolve));
|
||||||
|
|
||||||
|
agent.destroy();
|
||||||
|
sock1.destroy();
|
||||||
|
sock2.destroy();
|
||||||
|
sock3.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should queue createConnection requests', async () => {
|
||||||
|
const agent = new TunnelAgent();
|
||||||
|
assert.equal(agent.started, false);
|
||||||
|
|
||||||
|
const info = await agent.listen();
|
||||||
|
|
||||||
|
// create a promise for the next connection
|
||||||
|
let fulfilled = false;
|
||||||
|
const waitSockPromise = new Promise((resolve, reject) => {
|
||||||
|
agent.createConnection({}, (err, sock) => {
|
||||||
|
fulfilled = true;
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
resolve(sock);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// check that the next socket is not yet available
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
assert(!fulfilled);
|
||||||
|
|
||||||
|
// connect, this will make a socket available
|
||||||
|
const sock = net.createConnection({ port: info.port });
|
||||||
|
await new Promise(resolve => sock.once('connect', resolve));
|
||||||
|
|
||||||
|
const anotherAgentSock = await waitSockPromise;
|
||||||
|
agent.destroy();
|
||||||
|
sock.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should should emit a online event when a socket connects', async () => {
|
||||||
|
const agent = new TunnelAgent();
|
||||||
|
const info = await agent.listen();
|
||||||
|
|
||||||
|
const onlinePromise = new Promise(resolve => agent.once('online', resolve));
|
||||||
|
|
||||||
|
const sock = net.createConnection({ port: info.port });
|
||||||
|
await new Promise(resolve => sock.once('connect', resolve));
|
||||||
|
|
||||||
|
await onlinePromise;
|
||||||
|
agent.destroy();
|
||||||
|
sock.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should emit offline event when socket disconnects', async () => {
|
||||||
|
const agent = new TunnelAgent();
|
||||||
|
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));
|
||||||
|
|
||||||
|
sock.end();
|
||||||
|
await offlinePromise;
|
||||||
|
agent.destroy();
|
||||||
|
sock.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should emit offline event only when last socket disconnects', async () => {
|
||||||
|
const agent = new TunnelAgent();
|
||||||
|
const info = await agent.listen();
|
||||||
|
|
||||||
|
const offlinePromise = new Promise(resolve => agent.once('offline', resolve));
|
||||||
|
|
||||||
|
const sockA = net.createConnection({ port: info.port });
|
||||||
|
await new Promise(resolve => sockA.once('connect', resolve));
|
||||||
|
const sockB = net.createConnection({ port: info.port });
|
||||||
|
await new Promise(resolve => sockB.once('connect', resolve));
|
||||||
|
|
||||||
|
sockA.end();
|
||||||
|
|
||||||
|
const timeout = new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
await Promise.race([offlinePromise, timeout]);
|
||||||
|
|
||||||
|
sockB.end();
|
||||||
|
await offlinePromise;
|
||||||
|
|
||||||
|
agent.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should error an http request', async () => {
|
||||||
|
class ErrorAgent extends http.Agent {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
createConnection(options, cb) {
|
||||||
|
cb(new Error('foo'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const agent = new ErrorAgent();
|
||||||
|
|
||||||
|
const opt = {
|
||||||
|
host: 'localhost',
|
||||||
|
port: 1234,
|
||||||
|
path: '/',
|
||||||
|
agent: agent,
|
||||||
|
};
|
||||||
|
|
||||||
|
const err = await new Promise((resolve) => {
|
||||||
|
const req = http.get(opt, (res) => {});
|
||||||
|
req.once('error', resolve);
|
||||||
|
});
|
||||||
|
assert.equal(err.message, 'foo');
|
||||||
|
});
|
||||||
|
});
|
||||||
14
package.json
14
package.json
@@ -11,23 +11,21 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"book": "1.3.3",
|
"book": "1.3.3",
|
||||||
"debug": "3.1.0",
|
"debug": "3.1.0",
|
||||||
"esm": "3.0.14",
|
"esm": "3.0.34",
|
||||||
"human-readable-ids": "1.0.3",
|
"human-readable-ids": "1.0.3",
|
||||||
"koa": "2.4.1",
|
"koa": "2.5.1",
|
||||||
"localenv": "0.2.2",
|
"localenv": "0.2.2",
|
||||||
"on-finished": "2.3.0",
|
|
||||||
"optimist": "0.6.1",
|
"optimist": "0.6.1",
|
||||||
"pump": "2.0.0",
|
|
||||||
"tldjs": "2.3.1"
|
"tldjs": "2.3.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"localtunnel": "1.8.0",
|
"mocha": "5.1.1",
|
||||||
"mocha": "2.5.3",
|
|
||||||
"node-dev": "3.1.3",
|
"node-dev": "3.1.3",
|
||||||
"ws": "0.8.0"
|
"supertest": "3.1.0",
|
||||||
|
"ws": "5.1.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha --ui qunit --reporter spec",
|
"test": "mocha --check-leaks --require esm './**/*.test.js'",
|
||||||
"start": "./bin/server",
|
"start": "./bin/server",
|
||||||
"dev": "node-dev bin/server --port 3000"
|
"dev": "node-dev bin/server --port 3000"
|
||||||
}
|
}
|
||||||
|
|||||||
34
server.js
34
server.js
@@ -14,6 +14,7 @@ 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);
|
||||||
@@ -53,9 +54,9 @@ export default function(opt) {
|
|||||||
|
|
||||||
const isNewClientRequest = ctx.query['new'] !== undefined;
|
const isNewClientRequest = ctx.query['new'] !== undefined;
|
||||||
if (isNewClientRequest) {
|
if (isNewClientRequest) {
|
||||||
const req_id = hri.random();
|
const reqId = hri.random();
|
||||||
debug('making new client with id %s', req_id);
|
debug('making new client with id %s', reqId);
|
||||||
const info = await manager.newClient(req_id);
|
const info = await manager.newClient(reqId);
|
||||||
|
|
||||||
const url = schema + '://' + info.id + '.' + ctx.request.host;
|
const url = schema + '://' + info.id + '.' + ctx.request.host;
|
||||||
info.url = url;
|
info.url = url;
|
||||||
@@ -64,7 +65,7 @@ export default function(opt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// no new client request, send to landing page
|
// no new client request, send to landing page
|
||||||
ctx.redirect('https://localtunnel.github.io/www/');
|
ctx.redirect(landingPage);
|
||||||
});
|
});
|
||||||
|
|
||||||
// anything after the / path is a request for a specific client name
|
// anything after the / path is a request for a specific client name
|
||||||
@@ -80,10 +81,10 @@ export default function(opt) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const req_id = parts[1];
|
const reqId = parts[1];
|
||||||
|
|
||||||
// limit requested hostnames to 63 characters
|
// limit requested hostnames to 63 characters
|
||||||
if (! /^(?:[a-z0-9][a-z0-9\-]{4,63}[a-z0-9]|[a-z0-9]{4,63})$/.test(req_id)) {
|
if (! /^(?:[a-z0-9][a-z0-9\-]{4,63}[a-z0-9]|[a-z0-9]{4,63})$/.test(reqId)) {
|
||||||
const msg = 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.';
|
const msg = 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.';
|
||||||
ctx.status = 403;
|
ctx.status = 403;
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
@@ -92,8 +93,8 @@ export default function(opt) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug('making new client with id %s', req_id);
|
debug('making new client with id %s', reqId);
|
||||||
const info = await manager.newClient(req_id);
|
const info = await manager.newClient(reqId);
|
||||||
|
|
||||||
const url = schema + '://' + info.id + '.' + ctx.request.host;
|
const url = schema + '://' + info.id + '.' + ctx.request.host;
|
||||||
info.url = url;
|
info.url = url;
|
||||||
@@ -104,6 +105,7 @@ export default function(opt) {
|
|||||||
const server = http.createServer();
|
const server = http.createServer();
|
||||||
|
|
||||||
const appCallback = app.callback();
|
const appCallback = app.callback();
|
||||||
|
|
||||||
server.on('request', (req, res) => {
|
server.on('request', (req, res) => {
|
||||||
// without a hostname, we won't know who the request is for
|
// without a hostname, we won't know who the request is for
|
||||||
const hostname = req.headers.host;
|
const hostname = req.headers.host;
|
||||||
@@ -119,13 +121,14 @@ export default function(opt) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (manager.hasClient(clientId)) {
|
const client = manager.getClient(clientId);
|
||||||
manager.handleRequest(clientId, req, res);
|
if (!client) {
|
||||||
|
res.statusCode = 404;
|
||||||
|
res.end('404');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.statusCode = 404;
|
client.handleRequest(req, res);
|
||||||
res.end('404');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on('upgrade', (req, socket, head) => {
|
server.on('upgrade', (req, socket, head) => {
|
||||||
@@ -141,12 +144,13 @@ export default function(opt) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (manager.hasClient(clientId)) {
|
const client = manager.getClient(clientId);
|
||||||
manager.handleUpgrade(clientId, req, socket);
|
if (!client) {
|
||||||
|
sock.destroy();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.destroy();
|
client.handleUpgrade(req, socket);
|
||||||
});
|
});
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
|
|||||||
85
server.test.js
Normal file
85
server.test.js
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import request from 'supertest';
|
||||||
|
import assert from 'assert';
|
||||||
|
import { Server as WebSocketServer } from 'ws';
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
import net from 'net';
|
||||||
|
|
||||||
|
import createServer from './server';
|
||||||
|
|
||||||
|
describe('Server', () => {
|
||||||
|
it('server starts and stops', async () => {
|
||||||
|
const server = createServer();
|
||||||
|
await new Promise(resolve => server.listen(resolve));
|
||||||
|
await new Promise(resolve => server.close(resolve));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should redirect root requests to landing page', async () => {
|
||||||
|
const server = createServer();
|
||||||
|
const res = await request(server).get('/');
|
||||||
|
assert.equal('https://localtunnel.github.io/www/', res.headers.location);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support custom base domains', async () => {
|
||||||
|
const server = createServer({
|
||||||
|
domain: 'domain.example.com',
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await request(server).get('/');
|
||||||
|
assert.equal('https://localtunnel.github.io/www/', res.headers.location);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reject long domain name requests', async () => {
|
||||||
|
const server = createServer();
|
||||||
|
const res = await request(server).get('/thisdomainisoutsidethesizeofwhatweallowwhichissixtythreecharacters');
|
||||||
|
assert.equal(res.body.message, 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should upgrade websocket requests', async () => {
|
||||||
|
const hostname = 'websocket-test';
|
||||||
|
const server = createServer({
|
||||||
|
domain: 'example.com',
|
||||||
|
});
|
||||||
|
await new Promise(resolve => server.listen(resolve));
|
||||||
|
|
||||||
|
const res = await request(server).get('/websocket-test');
|
||||||
|
const localTunnelPort = res.body.port;
|
||||||
|
|
||||||
|
const wss = await new Promise((resolve) => {
|
||||||
|
const wsServer = new WebSocketServer({ port: 0 }, () => {
|
||||||
|
resolve(wsServer);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const websocketServerPort = wss.address().port;
|
||||||
|
|
||||||
|
const ltSocket = net.createConnection({ port: localTunnelPort });
|
||||||
|
const wsSocket = net.createConnection({ port: websocketServerPort });
|
||||||
|
ltSocket.pipe(wsSocket).pipe(ltSocket);
|
||||||
|
|
||||||
|
wss.once('connection', (ws) => {
|
||||||
|
ws.once('message', (message) => {
|
||||||
|
ws.send(message);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const ws = new WebSocket('http://localhost:' + server.address().port, {
|
||||||
|
headers: {
|
||||||
|
host: hostname + '.example.com',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('open', () => {
|
||||||
|
ws.send('something');
|
||||||
|
});
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
ws.once('message', (msg) => {
|
||||||
|
assert.equal(msg, 'something');
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
wss.close();
|
||||||
|
await new Promise(resolve => server.close(resolve));
|
||||||
|
});
|
||||||
|
});
|
||||||
166
test/basic.js
166
test/basic.js
@@ -1,166 +0,0 @@
|
|||||||
import http from 'http';
|
|
||||||
import url from 'url';
|
|
||||||
import assert from 'assert';
|
|
||||||
import localtunnel from 'localtunnel';
|
|
||||||
|
|
||||||
import CreateServer from '../server';
|
|
||||||
|
|
||||||
const localtunnel_server = CreateServer();
|
|
||||||
|
|
||||||
process.on('uncaughtException', (err) => {
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason, promise) => {
|
|
||||||
console.error(reason);
|
|
||||||
});
|
|
||||||
|
|
||||||
suite('basic');
|
|
||||||
|
|
||||||
var lt_server_port;
|
|
||||||
|
|
||||||
before('set up localtunnel server', function(done) {
|
|
||||||
var server = localtunnel_server.listen(function() {
|
|
||||||
lt_server_port = server.address().port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('landing page', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'localhost',
|
|
||||||
port: lt_server_port,
|
|
||||||
headers: {
|
|
||||||
host: 'example.com'
|
|
||||||
},
|
|
||||||
path: '/'
|
|
||||||
}
|
|
||||||
|
|
||||||
var req = http.request(opt, function(res) {
|
|
||||||
res.setEncoding('utf8');
|
|
||||||
assert.equal(res.headers.location, 'https://localtunnel.github.io/www/')
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
req.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
before('set up local http server', function(done) {
|
|
||||||
var server = http.createServer();
|
|
||||||
server.on('request', function(req, res) {
|
|
||||||
res.write('foo');
|
|
||||||
res.end();
|
|
||||||
});
|
|
||||||
server.listen(function() {
|
|
||||||
var port = server.address().port;
|
|
||||||
|
|
||||||
test._fake_port = port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
before('set up localtunnel client', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert.ifError(err);
|
|
||||||
var url = tunnel.url;
|
|
||||||
assert.ok(new RegExp('^http:\/\/.*localhost:' + lt_server_port + '$').test(url));
|
|
||||||
test._fake_url = url;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('query localtunnel server w/ ident', function(done) {
|
|
||||||
var uri = test._fake_url;
|
|
||||||
var hostname = url.parse(uri).hostname;
|
|
||||||
|
|
||||||
var opt = {
|
|
||||||
host: 'localhost',
|
|
||||||
port: lt_server_port,
|
|
||||||
headers: {
|
|
||||||
host: hostname + '.tld'
|
|
||||||
},
|
|
||||||
path: '/'
|
|
||||||
}
|
|
||||||
|
|
||||||
var req = http.request(opt, function(res) {
|
|
||||||
res.setEncoding('utf8');
|
|
||||||
var body = '';
|
|
||||||
|
|
||||||
res.on('data', function(chunk) {
|
|
||||||
body += chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
res.on('end', function() {
|
|
||||||
assert.equal('foo', body);
|
|
||||||
|
|
||||||
// TODO(shtylman) shutdown client
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
req.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('request specific domain', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
subdomain: 'abcd'
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert.ifError(err);
|
|
||||||
var url = tunnel.url;
|
|
||||||
assert.ok(new RegExp('^http:\/\/.*localhost:' + lt_server_port + '$').test(url));
|
|
||||||
test._fake_url = url;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('request domain with dash', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
subdomain: 'abcd-1234'
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert.ifError(err);
|
|
||||||
var url = tunnel.url;
|
|
||||||
assert.ok(new RegExp('^http:\/\/.*localhost:' + lt_server_port + '$').test(url));
|
|
||||||
test._fake_url = url;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('request domain that is too long', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
subdomain: 'thisdomainisoutsidethesizeofwhatweallowwhichissixtythreecharacters'
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert(err);
|
|
||||||
assert.equal(err.message, 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('request uppercase domain', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
subdomain: 'ABCD'
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert(err);
|
|
||||||
assert.equal(err.message, 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
after('shutdown', function() {
|
|
||||||
localtunnel_server.close();
|
|
||||||
});
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
import http from 'http';
|
|
||||||
import url from 'url';
|
|
||||||
import assert from 'assert';
|
|
||||||
import localtunnel from 'localtunnel';
|
|
||||||
|
|
||||||
import CreateServer from '../server';
|
|
||||||
|
|
||||||
const localtunnel_server = CreateServer({
|
|
||||||
domain: 'domain.example.com',
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('uncaughtException', (err) => {
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason, promise) => {
|
|
||||||
console.error(reason);
|
|
||||||
});
|
|
||||||
|
|
||||||
suite('domain');
|
|
||||||
|
|
||||||
var lt_server_port;
|
|
||||||
|
|
||||||
before('set up localtunnel server', function(done) {
|
|
||||||
var server = localtunnel_server.listen(function() {
|
|
||||||
lt_server_port = server.address().port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('landing page', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'localhost',
|
|
||||||
port: lt_server_port,
|
|
||||||
headers: {
|
|
||||||
host: 'domain.example.com'
|
|
||||||
},
|
|
||||||
path: '/'
|
|
||||||
}
|
|
||||||
|
|
||||||
var req = http.request(opt, function(res) {
|
|
||||||
res.setEncoding('utf8');
|
|
||||||
assert.equal(res.headers.location, 'https://localtunnel.github.io/www/')
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
req.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
after('shutdown', function() {
|
|
||||||
localtunnel_server.close();
|
|
||||||
});
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--check-leaks
|
|
||||||
--reporter spec
|
|
||||||
--ui qunit
|
|
||||||
--require esm
|
|
||||||
105
test/queue.js
105
test/queue.js
@@ -1,105 +0,0 @@
|
|||||||
import http from 'http';
|
|
||||||
import url from 'url';
|
|
||||||
import assert from 'assert';
|
|
||||||
import localtunnel from 'localtunnel';
|
|
||||||
|
|
||||||
import CreateServer from '../server';
|
|
||||||
|
|
||||||
suite('queue');
|
|
||||||
|
|
||||||
var localtunnel_server = CreateServer({
|
|
||||||
max_tcp_sockets: 1
|
|
||||||
});
|
|
||||||
|
|
||||||
var server;
|
|
||||||
var lt_server_port;
|
|
||||||
|
|
||||||
before('set up localtunnel server', function(done) {
|
|
||||||
var lt_server = localtunnel_server.listen(function() {
|
|
||||||
lt_server_port = lt_server.address().port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
before('set up local http server', function(done) {
|
|
||||||
server = http.createServer();
|
|
||||||
server.on('request', function(req, res) {
|
|
||||||
// respond sometime later
|
|
||||||
setTimeout(function() {
|
|
||||||
res.setHeader('x-count', req.headers['x-count']);
|
|
||||||
res.end('foo');
|
|
||||||
}, 500);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(function() {
|
|
||||||
var port = server.address().port;
|
|
||||||
|
|
||||||
test._fake_port = port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
before('set up localtunnel client', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert.ifError(err);
|
|
||||||
var url = tunnel.url;
|
|
||||||
assert.ok(new RegExp('^http:\/\/.*localhost:' + lt_server_port + '$').test(url));
|
|
||||||
test._fake_url = url;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('query localtunnel server w/ ident', function(done) {
|
|
||||||
var uri = test._fake_url;
|
|
||||||
var hostname = url.parse(uri).hostname;
|
|
||||||
|
|
||||||
var count = 0;
|
|
||||||
var opt = {
|
|
||||||
host: 'localhost',
|
|
||||||
port: lt_server_port,
|
|
||||||
agent: false,
|
|
||||||
headers: {
|
|
||||||
host: hostname + '.tld'
|
|
||||||
},
|
|
||||||
path: '/'
|
|
||||||
}
|
|
||||||
|
|
||||||
var num_requests = 2;
|
|
||||||
var responses = 0;
|
|
||||||
|
|
||||||
function maybe_done() {
|
|
||||||
if (++responses >= num_requests) {
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function make_req() {
|
|
||||||
opt.headers['x-count'] = count++;
|
|
||||||
http.get(opt, function(res) {
|
|
||||||
res.setEncoding('utf8');
|
|
||||||
var body = '';
|
|
||||||
|
|
||||||
res.on('data', function(chunk) {
|
|
||||||
body += chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
res.on('end', function() {
|
|
||||||
assert.equal('foo', body);
|
|
||||||
maybe_done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i=0 ; i<num_requests ; ++i) {
|
|
||||||
make_req();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
after('shutdown', function() {
|
|
||||||
localtunnel_server.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
import http from 'http';
|
|
||||||
import url from 'url';
|
|
||||||
import assert from 'assert';
|
|
||||||
import localtunnel from 'localtunnel';
|
|
||||||
|
|
||||||
import CreateServer from '../server';
|
|
||||||
|
|
||||||
const localtunnel_server = CreateServer({
|
|
||||||
max_tcp_sockets: 2
|
|
||||||
});
|
|
||||||
|
|
||||||
var lt_server_port
|
|
||||||
|
|
||||||
suite('simple');
|
|
||||||
|
|
||||||
test('set up localtunnel server', function(done) {
|
|
||||||
var server = localtunnel_server.listen(function() {
|
|
||||||
lt_server_port = server.address().port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('set up local http server', function(done) {
|
|
||||||
var server = http.createServer(function(req, res) {
|
|
||||||
res.end('hello world!');
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(function() {
|
|
||||||
test._fake_port = server.address().port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('set up localtunnel client', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert.ifError(err);
|
|
||||||
var url = tunnel.url;
|
|
||||||
assert.ok(new RegExp('^http:\/\/.*localhost:' + lt_server_port + '$').test(url));
|
|
||||||
test._fake_url = url;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should respond to request', function(done) {
|
|
||||||
var hostname = url.parse(test._fake_url).hostname;
|
|
||||||
var opt = {
|
|
||||||
host: 'localhost',
|
|
||||||
port: lt_server_port,
|
|
||||||
headers: {
|
|
||||||
host: hostname + '.tld'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
http.get(opt, function(res) {
|
|
||||||
var body = '';
|
|
||||||
res.setEncoding('utf-8');
|
|
||||||
res.on('data', function(chunk) {
|
|
||||||
body += chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
res.on('end', function() {
|
|
||||||
assert.equal(body, 'hello world!');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
import http from 'http';
|
|
||||||
import url from 'url';
|
|
||||||
import assert from 'assert';
|
|
||||||
import localtunnel from 'localtunnel';
|
|
||||||
import WebSocket from 'ws';
|
|
||||||
import { Server as WebSocketServer } from 'ws';
|
|
||||||
|
|
||||||
import CreateServer from '../server';
|
|
||||||
|
|
||||||
const localtunnel_server = CreateServer({
|
|
||||||
max_tcp_sockets: 2
|
|
||||||
});
|
|
||||||
|
|
||||||
var lt_server_port
|
|
||||||
|
|
||||||
suite('websocket');
|
|
||||||
|
|
||||||
before('set up localtunnel server', function(done) {
|
|
||||||
var server = localtunnel_server.listen(function() {
|
|
||||||
lt_server_port = server.address().port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
before('set up local websocket server', function(done) {
|
|
||||||
var wss = new WebSocketServer({ port: 0 }, function() {
|
|
||||||
test._fake_port = wss._server.address().port;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
wss.on('error', function(err) {
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
wss.on('connection', function connection(ws) {
|
|
||||||
ws.on('error', function(err) {
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on('message', function incoming(message) {
|
|
||||||
ws.send(message);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
before('set up localtunnel client', function(done) {
|
|
||||||
var opt = {
|
|
||||||
host: 'http://localhost:' + lt_server_port,
|
|
||||||
};
|
|
||||||
|
|
||||||
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
|
||||||
assert.ifError(err);
|
|
||||||
var url = tunnel.url;
|
|
||||||
assert.ok(new RegExp('^http:\/\/.*localhost:' + lt_server_port + '$').test(url));
|
|
||||||
test._fake_url = url;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('websocket server request', function(done) {
|
|
||||||
var hostname = url.parse(test._fake_url).hostname;
|
|
||||||
var ws = new WebSocket('http://localhost:' + lt_server_port, {
|
|
||||||
headers: {
|
|
||||||
host: hostname + '.tld'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on('message', function(msg) {
|
|
||||||
assert.equal(msg, 'something');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on('open', function open() {
|
|
||||||
ws.send('something');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user