Configuration file / token-based auth support

This commit is contained in:
Tim Ambler
2018-08-02 15:12:26 +00:00
parent d9fca62c17
commit 07013d3326
6 changed files with 132 additions and 4 deletions
+25
View File
@@ -11,6 +11,7 @@ import ClientManager from './lib/ClientManager';
const debug = Debug('localtunnel:server');
export default function(opt) {
opt = opt || {};
const validHosts = (opt.domain) ? [opt.domain] : undefined;
@@ -27,6 +28,30 @@ export default function(opt) {
const app = new Koa();
const router = new Router();
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
}
});
if (opt.require_token) {
app.use((ctx, next) => {
const token = ctx.request.query.token;
if (!token || opt.tokens.indexOf(token) === -1) {
const err = new Error(`Missing or unknown token.`);
err.status = 511;
err.code = 'TOKEN_ERROR';
throw err;
} else {
return next();
}
});
}
router.get('/api/status', async (ctx, next) => {
const stats = manager.stats;