Utility, cleanup, etc.

This commit is contained in:
Bryan Ashby 2023-01-02 22:25:32 -07:00
parent fb5858e90f
commit 99e9ebbec9
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
2 changed files with 37 additions and 25 deletions

View File

@ -1,8 +1,6 @@
const WebHandlerModule = require('../../../web_handler_module'); const WebHandlerModule = require('../../../web_handler_module');
const Config = require('../../../config').get; const Config = require('../../../config').get;
const { Errors, ErrorReasons } = require('../../../enig_error'); const { Errors, ErrorReasons } = require('../../../enig_error');
const WebServerPackageName = require('../web').moduleInfo.packageName;
const { WellKnownLocations } = require('../web'); const { WellKnownLocations } = require('../web');
const _ = require('lodash'); const _ = require('lodash');
@ -35,7 +33,7 @@ Achievement Points: %ACHIEVEMENT_POINTS%
// //
// WebFinger: https://www.rfc-editor.org/rfc/rfc7033 // WebFinger: https://www.rfc-editor.org/rfc/rfc7033
// //
exports.getModule = class WebFingerServerModule extends WebHandlerModule { exports.getModule = class WebFingerWebHandler extends WebHandlerModule {
constructor() { constructor() {
super(); super();
} }
@ -43,20 +41,17 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
init(cb) { init(cb) {
const config = Config(); const config = Config();
if (!_.get(config, 'contentServers.web.handlers.webFinger.enabled')) { if (!WebHandlerModule.isEnabled('webFinger')) {
return cb(null); return cb(null);
} }
const { getServer } = require('../../../listening_server');
// we rely on the web server // we rely on the web server
this.webServer = getServer(WebServerPackageName); this.webServer = WebHandlerModule.getWebServer();
const ws = this._webServer(); if (!this.webServer || !this.webServer.isEnabled()) {
if (!ws || !ws.isEnabled()) {
return cb(Errors.UnexpectedState('Cannot access web server!')); return cb(Errors.UnexpectedState('Cannot access web server!'));
} }
const domain = ws.getDomain(); const domain = this.webServer.getDomain();
if (!domain) { if (!domain) {
return cb(Errors.UnexpectedState('Web server does not have "domain" set')); return cb(Errors.UnexpectedState('Web server does not have "domain" set'));
} }
@ -67,21 +62,25 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
new RegExp(`^acct:(.+)@${domain}$`), new RegExp(`^acct:(.+)@${domain}$`),
// profile page // profile page
// https://webfinger.net/rel/profile-page/ // https://webfinger.net/rel/profile-page/
new RegExp(`^${ws.buildUrl(WellKnownLocations.Internal + '/wf/@')}(.+)$`), new RegExp(
`^${this.webServer.buildUrl(WellKnownLocations.Internal + '/wf/@')}(.+)$`
),
// self URL // self URL
new RegExp( new RegExp(
`^${ws.buildUrl(WellKnownLocations.Internal + '/ap/users/')}(.+)$` `^${this.webServer.buildUrl(
WellKnownLocations.Internal + '/ap/users/'
)}(.+)$`
), ),
]; ];
ws.addRoute({ this.webServer.addRoute({
method: 'GET', method: 'GET',
// https://www.rfc-editor.org/rfc/rfc7033.html#section-10.1 // https://www.rfc-editor.org/rfc/rfc7033.html#section-10.1
path: /^\/\.well-known\/webfinger\/?\?/, path: /^\/\.well-known\/webfinger\/?\?/,
handler: this._webFingerRequestHandler.bind(this), handler: this._webFingerRequestHandler.bind(this),
}); });
ws.addRoute({ this.webServer.addRoute({
method: 'GET', method: 'GET',
path: /^\/_enig\/wf\/@.+$/, path: /^\/_enig\/wf\/@.+$/,
handler: this._profileRequestHandler.bind(this), handler: this._profileRequestHandler.bind(this),
@ -90,10 +89,6 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
return cb(null); return cb(null);
} }
_webServer() {
return this.webServer.instance;
}
_profileRequestHandler(req, resp) { _profileRequestHandler(req, resp) {
const url = new URL(req.url, `https://${req.headers.host}`); const url = new URL(req.url, `https://${req.headers.host}`);
@ -174,7 +169,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
'contentServers.web.handlers.webFinger.profileTemplate' 'contentServers.web.handlers.webFinger.profileTemplate'
); );
if (templateFile) { if (templateFile) {
templateFile = this._webServer().resolveTemplatePath(templateFile); templateFile = this.webServer.resolveTemplatePath(templateFile);
} }
fs.readFile(templateFile || '', 'utf8', (err, data) => { fs.readFile(templateFile || '', 'utf8', (err, data) => {
if (err) { if (err) {
@ -196,7 +191,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
const resource = url.searchParams.get('resource'); const resource = url.searchParams.get('resource');
if (!resource) { if (!resource) {
return this._webServer().respondWithError( return this.webServer.respondWithError(
resp, resp,
400, 400,
'"resource" is required', '"resource" is required',
@ -218,7 +213,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
return Log.warn({ error: err.message }, `WebFinger failed: ${req.url}`); return Log.warn({ error: err.message }, `WebFinger failed: ${req.url}`);
} }
const domain = this._webServer().getDomain(); const domain = this.webServer.getDomain();
const body = JSON.stringify({ const body = JSON.stringify({
subject: `acct:${user.username}@${domain}`, subject: `acct:${user.username}@${domain}`,
@ -241,7 +236,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
} }
_profileUrl(user) { _profileUrl(user) {
return this._webServer().buildUrl( return this.webServer.buildUrl(
WellKnownLocations.Internal + `/wf/@${user.username}` WellKnownLocations.Internal + `/wf/@${user.username}`
); );
} }
@ -256,7 +251,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
} }
_selfUrl(user) { _selfUrl(user) {
return this._webServer().buildUrl( return this.webServer.buildUrl(
WellKnownLocations.Internal + `/ap/users/${user.username}` WellKnownLocations.Internal + `/ap/users/${user.username}`
); );
} }
@ -275,7 +270,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
_subscribeLink() { _subscribeLink() {
return { return {
rel: 'http://ostatus.org/schema/1.0/subscribe', rel: 'http://ostatus.org/schema/1.0/subscribe',
template: this._webServer().buildUrl( template: this.webServer.buildUrl(
WellKnownLocations.Internal + '/ap/authorize_interaction?uri={uri}' WellKnownLocations.Internal + '/ap/authorize_interaction?uri={uri}'
), ),
}; };
@ -291,7 +286,7 @@ exports.getModule = class WebFingerServerModule extends WebHandlerModule {
} }
_notFound(resp) { _notFound(resp) {
this._webServer().respondWithError( this.webServer.respondWithError(
resp, resp,
404, 404,
'Resource not found', 'Resource not found',

View File

@ -1,4 +1,5 @@
const { PluginModule } = require('./plugin_module'); const { PluginModule } = require('./plugin_module');
const Config = require('./config').get;
module.exports = class WebHandlerModule extends PluginModule { module.exports = class WebHandlerModule extends PluginModule {
constructor(options) { constructor(options) {
@ -9,4 +10,20 @@ module.exports = class WebHandlerModule extends PluginModule {
// to be implemented! // to be implemented!
return cb(null); return cb(null);
} }
static isEnabled(handlerName) {
const config = Config();
const handlers = config.contentServers?.web?.handlers;
return handlers && true === handlers[handlerName]?.enabled;
}
static getWebServer() {
const { getServer } = require('./listening_server');
const WebServerPackageName = require('./servers/content/web').moduleInfo
.packageName;
const ws = getServer(WebServerPackageName);
if (ws) {
return ws.instance;
}
}
}; };