diff --git a/server/lib/diagnostic/prosody.ts b/server/lib/diagnostic/prosody.ts index 8290a336..89f5dbd2 100644 --- a/server/lib/diagnostic/prosody.ts +++ b/server/lib/diagnostic/prosody.ts @@ -1,4 +1,4 @@ -import { getProsodyConfigContent, getProsodyConfigPath, getWorkingDir } from '../prosody/config' +import { getProsodyConfig, getWorkingDir } from '../prosody/config' import { getProsodyAbout, testProsodyCorrectlyRunning } from '../prosody/ctl' import { newResult, TestResult } from './utils' import * as fs from 'fs' @@ -18,7 +18,9 @@ export async function diagProsody (test: string, options: RegisterServerOptions) // FIXME: these tests are very similar to tests in testProsodyCorrectlyRunning. Remove from here? // Testing the prosody config file. try { - const filePath = await getProsodyConfigPath(options) + const wantedConfig = await getProsodyConfig(options) + const filePath = wantedConfig.paths.config + await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist. result.messages.push(`The prosody configuration file (${filePath}) exists`) const actualContent = await fs.promises.readFile(filePath, { @@ -30,7 +32,7 @@ export async function diagProsody (test: string, options: RegisterServerOptions) message: actualContent }) - const wantedContent = await getProsodyConfigContent(options) + const wantedContent = wantedConfig.content if (actualContent === wantedContent) { result.messages.push('Prosody configuration file content is correct.') } else { diff --git a/server/lib/prosody/config.ts b/server/lib/prosody/config.ts index c3e923ed..e7b4de73 100644 --- a/server/lib/prosody/config.ts +++ b/server/lib/prosody/config.ts @@ -75,14 +75,20 @@ async function getProsodyFilePaths (options: RegisterServerOptions): Promise { +interface ProsodyConfig { + content: string + paths: ProsodyFilePaths + port: string +} +async function getProsodyConfig (options: RegisterServerOptions): Promise { const logger = options.peertubeHelpers.logger - logger.debug('Calling getProsodyConfigContent') + logger.debug('Calling getProsodyConfig') + const port = '5280' const peertubeDomain = 'localhost' const paths = await getProsodyFilePaths(options) const logMode: LogMode = 'debug' - return ` + const content = ` admins = { } plugin_paths = { } @@ -153,36 +159,35 @@ Component "room.localhost" "muc" muc_room_default_change_subject = false muc_room_default_history_length = 20 ` + return { + content, + paths, + port + } } -async function getProsodyConfigPath (options: RegisterServerOptions): Promise { - const logger = options.peertubeHelpers.logger - logger.debug('Calling getProsodyConfigPath') - - const paths = await getProsodyFilePaths(options) - return paths.config -} - -async function writeProsodyConfig (options: RegisterServerOptions): Promise { +async function writeProsodyConfig (options: RegisterServerOptions): Promise { const logger = options.peertubeHelpers.logger logger.debug('Calling writeProsodyConfig') logger.debug('Ensuring that the working dir exists') await ensureWorkingDir(options) logger.debug('Computing the Prosody config content') - const content = await getProsodyConfigContent(options) + const config = await getProsodyConfig(options) + const content = config.content + const fileName = config.paths.config - const fileName = await getProsodyConfigPath(options) logger.info(`Writing prosody configuration file to ${fileName}`) await fs.promises.writeFile(fileName, content) logger.debug('Prosody configuration file writen') + + return config } export { - getProsodyConfigContent, + getProsodyConfig, getWorkingDir, ensureWorkingDir, getProsodyFilePaths, - getProsodyConfigPath, writeProsodyConfig } diff --git a/server/lib/prosody/ctl.ts b/server/lib/prosody/ctl.ts index f1ff0bd4..834db331 100644 --- a/server/lib/prosody/ctl.ts +++ b/server/lib/prosody/ctl.ts @@ -1,4 +1,5 @@ -import { getProsodyConfigContent, getProsodyConfigPath, getProsodyFilePaths, writeProsodyConfig } from './config' +import { getProsodyConfig, getProsodyFilePaths, writeProsodyConfig } from './config' +import { changeHttpBindRoute } from '../routers/webchat' import * as fs from 'fs' import * as child_process from 'child_process' @@ -99,14 +100,16 @@ async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Pro result.ok = false // more tests to come try { - const filePath = await getProsodyConfigPath(options) + const wantedConfig = await getProsodyConfig(options) + const filePath = wantedConfig.paths.config + await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist. result.messages.push(`The prosody configuration file (${filePath}) exists`) const actualContent = await fs.promises.readFile(filePath, { encoding: 'utf-8' }) - const wantedContent = await getProsodyConfigContent(options) + const wantedContent = wantedConfig.content if (actualContent === wantedContent) { result.messages.push('Prosody configuration file content is correct.') } else { @@ -150,9 +153,9 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise { return new Promise((resolve) => { setTimeout(resolve, ms) diff --git a/server/lib/routers/webchat.ts b/server/lib/routers/webchat.ts index edb3e4da..ae942fd4 100644 --- a/server/lib/routers/webchat.ts +++ b/server/lib/routers/webchat.ts @@ -1,17 +1,23 @@ -import type { Router, Request, Response, NextFunction } from 'express' +import type { Router, RequestHandler, Request, Response, NextFunction } from 'express' import { getBaseRouter } from '../helpers' import * as path from 'path' const fs = require('fs').promises +// const httpProxy = require('http-proxy') + +let httpBindRoute: RequestHandler + +async function initWebchatRouter (options: RegisterServerOptions): Promise { + const { + getRouter, + peertubeHelpers, + settingsManager + } = options -async function initWebchatRouter ({ - getRouter, - peertubeHelpers, - settingsManager -}: RegisterServerOptions): Promise { const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../../conversejs/index.html')) - const router = getRouter() - router.get('/', async (req: Request, res: Response, next: NextFunction) => { + const router: Router = getRouter() + // eslint-disable-next-line @typescript-eslint/no-misused-promises + router.get('/', async (req: Request, res: Response, next: NextFunction): Promise => { try { const settings = await settingsManager.getSettings([ 'chat-use-prosody', 'chat-use-builtin', 'chat-room', 'chat-server', @@ -25,7 +31,7 @@ async function initWebchatRouter ({ if (settings['chat-use-prosody']) { server = 'localhost' room = '{{VIDEO_UUID}}@room.localhost' - boshUri = getBaseRouter() + 'http-bind' + boshUri = getBaseRouter() + 'webchat/http-bind' wsUri = '' } else if (settings['chat-use-builtin']) { if (!settings['chat-server']) { @@ -79,12 +85,42 @@ async function initWebchatRouter ({ res.type('html') res.send(page) } catch (error) { - return next(error) + next(error) } }) + + changeHttpBindRoute(options, null) + router.all('/http-bind', (req: Request, res: Response, next: NextFunction) => { + httpBindRoute(req, res, next) + }) return router } -export { - initWebchatRouter +function changeHttpBindRoute ({ peertubeHelpers }: RegisterServerOptions, port: string | null): void { + const logger = peertubeHelpers.logger + logger.info('Changing http-bind port for ' + (port ?? 'null')) + if (port !== null && !/^\d+$/.test(port)) { + logger.error('Port is not valid. Replacing by null') + port = null + } + if (port === null) { + httpBindRoute = (_req: Request, res: Response, _next: NextFunction) => { + res.status(404) + res.send('Not found') + } + } else { + logger.error('Not implemented yet') + // const proxy = new httpProxy.HttpProxy() + // httpBindRoute = (req: Request, res: Response, _next: NextFunction) => { + // proxy.proxyRequest(req, res, { + // host: 'localhost', + // port: port + // }) + // } + } +} + +export { + initWebchatRouter, + changeHttpBindRoute }