Add client token authentication #92
@@ -1 +1,2 @@
|
|||||||
node_modules
|
node_modules
|
||||||
|
.vscode/
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ const server = CreateServer({
|
|||||||
max_tcp_sockets: argv['max-sockets'],
|
max_tcp_sockets: argv['max-sockets'],
|
||||||
secure: argv.secure,
|
secure: argv.secure,
|
||||||
domain: argv.domain,
|
domain: argv.domain,
|
||||||
|
tokens: {
|
||||||
|
// 'clientid': 'token'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(argv.port, argv.address, () => {
|
server.listen(argv.port, argv.address, () => {
|
||||||
|
|||||||
+10
-5
@@ -11,6 +11,8 @@ class ClientManager {
|
|||||||
constructor(opt) {
|
constructor(opt) {
|
||||||
this.opt = opt || {};
|
this.opt = opt || {};
|
||||||
|
|
||||||
|
this.opt.tokens = this.opt.tokens || {};
|
||||||
|
|
||||||
// id -> client instance
|
// id -> client instance
|
||||||
this.clients = new Map();
|
this.clients = new Map();
|
||||||
|
|
||||||
@@ -26,15 +28,18 @@ class ClientManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create a new tunnel with `id`
|
// create a new tunnel with `id`
|
||||||
// if the id is already used, a random id is assigned
|
async newClient (id, token) {
|
||||||
// if the tunnel could not be created, throws an error
|
|
||||||
async newClient(id) {
|
|
||||||
const clients = this.clients;
|
const clients = this.clients;
|
||||||
const stats = this.stats;
|
const stats = this.stats;
|
||||||
|
|
||||||
// can't ask for id already is use
|
if (clients[id] !== undefined) {
|
||||||
if (clients[id]) {
|
if(this.opt.tokens[id] !== undefined) {
|
||||||
|
return Promise.reject(new Error('Client is already connected. Try again later.'));
|
||||||
|
}
|
||||||
|
|
||||||
id = hri.random();
|
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;
|
const maxSockets = this.opt.max_tcp_sockets;
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@
|
|||||||
"ws": "5.1.1"
|
"ws": "5.1.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha --check-leaks --require esm './**/*.test.js'",
|
"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"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,13 +104,21 @@ export default function(opt) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug('making new client with id %s', reqId);
|
try {
|
||||||
const info = await manager.newClient(reqId);
|
debug('making new client with id %s', reqId);
|
||||||
|
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;
|
||||||
ctx.body = info;
|
ctx.body = info;
|
||||||
return;
|
}
|
||||||
|
catch(err) {
|
||||||
|
debug('Error: %s with id %s', err.message, reqId);
|
||||||
|
ctx.status = 403;
|
||||||
|
ctx.body = {
|
||||||
|
message: err.message,
|
||||||
|
};
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const server = http.createServer();
|
const server = http.createServer();
|
||||||
|
|||||||
Reference in New Issue
Block a user