From 224c3ac9cd9805567d8970a974ae029eec927a42 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 6 Jun 2018 10:56:38 +0000 Subject: [PATCH] handle extension for auth --- bin/telebitd.js | 26 ++++++++++++++++++-------- lib/handlers.js | 11 +++++++++-- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/bin/telebitd.js b/bin/telebitd.js index edf7445..c8576f4 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -130,20 +130,30 @@ function applyConfig(config) { , debug: state.config.debug || state.config.greenlock.debug }); + try { + // TODO specify extensions in config file + state.extensions = require('../lib/extensions'); + } catch(e) { + if (state.debug) { console.log('[DEBUG] no extensions loaded', e); } + state.extensions = {}; + } require('../lib/handlers').create(state); // adds directly to config for now... //require('cluster-store').create().then(function (store) { //program.store = store; + state.authenticate = function (opts) { - try { - state.extensions = require('./extensions'); - return state.extensions.authenticate({ - state: state - , auth: opts.auth - }); - } catch(e) { - // ignore + if (state.extensions.authenticate) { + try { + return state.extensions.authenticate({ + state: state + , auth: opts.auth + }); + } catch(e) { + console.error('Extension Error:'); + console.error(e); + } } return state.defaults.authenticate(opts.auth); }; diff --git a/lib/handlers.js b/lib/handlers.js index 2acae41..b0ac8e1 100644 --- a/lib/handlers.js +++ b/lib/handlers.js @@ -116,9 +116,16 @@ module.exports.create = function (state) { // var serveAdmin = require('serve-static')(__dirname + '/../admin', { redirect: true }); var finalhandler = require('finalhandler'); - state.httpTunnelServer = http.createServer(function (req, res) { - res.setHeader('connection', 'close'); + state.defaults.webadmin = function (req, res) { serveAdmin(req, res, finalhandler(req, res)); + }; + state.httpTunnelServer = http.createServer(function (req, res) { + //res.setHeader('connection', 'close'); + if (state.extensions.webadmin) { + state.extensions.webadmin(state, req, res); + } else { + state.defaults.webadmin(req, res); + } }); Object.keys(state.tlsOptions).forEach(function (key) { tunnelAdminTlsOpts[key] = state.tlsOptions[key];