Configuration file / token-based auth support
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user