Add client token authentication #92
@@ -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, () => {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user