From 6a103f3e61e5c32177c32c33f02af01dc864b239 Mon Sep 17 00:00:00 2001 From: Maxim Rubis Date: Wed, 6 Jun 2018 08:05:34 -0400 Subject: [PATCH 1/3] Add client token authentication --- bin/server | 3 +++ lib/ClientManager.js | 13 ++++++++----- server.js | 20 ++++++++++++++------ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/bin/server b/bin/server index a6bba9d..c4cf740 100755 --- a/bin/server +++ b/bin/server @@ -42,6 +42,9 @@ const server = CreateServer({ max_tcp_sockets: argv['max-sockets'], secure: argv.secure, domain: argv.domain, + tokens: { + // 'clientid': 'token' + } }); server.listen(argv.port, argv.address, () => { diff --git a/lib/ClientManager.js b/lib/ClientManager.js index e1a7838..65686f1 100644 --- a/lib/ClientManager.js +++ b/lib/ClientManager.js @@ -26,15 +26,18 @@ class ClientManager { } // 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) { + async newClient (id, token) { const clients = this.clients; const stats = this.stats; - // can't ask for id already is use - if (clients[id]) { + if (clients[id] !== undefined) { + if(this.opt.tokens[id] !== undefined) { + return Promise.reject(new Error('Client is already connected. Try again later.')); + } + id = hri.random(); + } else if(this.opt.tokens[id] !== undefined && this.opt.tokens[id] !== token) { + return Promise.reject(new Error('Token is not found or correct.')); } const maxSockets = this.opt.max_tcp_sockets; diff --git a/server.js b/server.js index 5a53c47..b5ef92e 100644 --- a/server.js +++ b/server.js @@ -104,13 +104,21 @@ export default function(opt) { return; } - debug('making new client with id %s', reqId); - const info = await manager.newClient(reqId); + try { + debug('making new client with id %s', reqId); + const info = await manager.newClient(reqId); - const url = schema + '://' + info.id + '.' + ctx.request.host; - info.url = url; - ctx.body = info; - return; + const url = schema + '://' + info.id + '.' + ctx.request.host; + info.url = url; + ctx.body = info; + } + catch(err) { + debug('Error: %s with id %s', err.message, reqId); + ctx.status = 403; + ctx.body = { + message: err.message, + }; + } }); const server = http.createServer(); -- 2.54.0 From 604aa8598d1c89c272a545d529828d3f7007ca51 Mon Sep 17 00:00:00 2001 From: Maxim Rubis Date: Thu, 7 Jun 2018 09:13:17 -0400 Subject: [PATCH 2/3] Fixed: set default empty tokens configuration if nothing is passed. Fixed: NPM on Windows doesn't like path in single quotes. --- lib/ClientManager.js | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ClientManager.js b/lib/ClientManager.js index 65686f1..9c62d03 100644 --- a/lib/ClientManager.js +++ b/lib/ClientManager.js @@ -11,6 +11,8 @@ class ClientManager { constructor(opt) { this.opt = opt || {}; + this.opt.tokens = this.opt.tokens || {}; + // id -> client instance this.clients = new Map(); diff --git a/package.json b/package.json index 0693b8d..c166166 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "ws": "5.1.1" }, "scripts": { - "test": "mocha --check-leaks --require esm './**/*.test.js'", + "test": "mocha --check-leaks --require esm ./**/*.test.js", "start": "./bin/server", "dev": "node-dev bin/server --port 3000" } -- 2.54.0 From dcb6cf89c5c280c7c26c038f7dbb9802e99acceb Mon Sep 17 00:00:00 2001 From: Maxim Rubis Date: Fri, 5 Oct 2018 07:44:56 -0400 Subject: [PATCH 3/3] Ignore .vscode folder --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3c3629e..da8df9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.vscode/ -- 2.54.0