Add a route to kill tunnel if open with same JWT token

This commit is contained in:
Olivier MICHAUD
2021-05-19 22:28:42 +02:00
parent 1371d03fec
commit 1204fdf99a
5 changed files with 117 additions and 12 deletions
+40 -2
View File
@@ -61,6 +61,44 @@ export default function(opt) {
};
});
router.get('/api/tunnels/:id/kill', async (ctx, next) => {
const clientId = ctx.params.id;
if (!opt.jwt_shared_secret){
debug('disconnecting client with id %s, error: jwt_shared_secret is not used', clientId);
ctx.throw(403, {
success: false,
message: 'jwt_shared_secret is not used'
});
return;
}
if (!manager.hasClient(clientId)) {
debug('disconnecting client with id %s, error: client is not connected', clientId);
ctx.throw(404, {
success: false,
message: `client with id ${clientId} is not connected`
});
}
const securityToken = ctx.request.headers.authorization;
if (!manager.getClient(clientId).isSecurityTokenEqual(securityToken)) {
debug('disconnecting client with id %s, error: securityToken is not equal ', clientId);
ctx.throw(403, {
success: false,
message: `client with id ${clientId} has not the same securityToken than ${securityToken}`
});
}
debug('disconnecting client with id %s', clientId);
manager.removeClient(clientId);
ctx.statusCode = 200;
ctx.body = {
success: true,
message: `client with id ${clientId} is disconected`
};
});
app.use(router.routes());
app.use(router.allowedMethods());
@@ -78,7 +116,7 @@ export default function(opt) {
if (isNewClientRequest) {
const reqId = hri.random();
debug('making new client with id %s', reqId);
const info = await manager.newClient(reqId);
const info = await manager.newClient(reqId, opt.jwt_shared_secret ? ctx.request.headers.authorization : null);
const url = schema + '://' + info.id + '.' + ctx.request.host;
info.url = url;
@@ -116,7 +154,7 @@ export default function(opt) {
}
debug('making new client with id %s', reqId);
const info = await manager.newClient(reqId);
const info = await manager.newClient(reqId, opt.jwt_shared_secret ? ctx.request.headers.authorization : null);
const url = schema + '://' + info.id + '.' + ctx.request.host;
info.url = url;