From f590cf2c7b58585bb464779ba2f309b491d30cca Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 6 May 2021 13:31:55 +0200 Subject: [PATCH] Builtin Prosody: use Peertube domain instead of localhost. --- server/lib/prosody/config.ts | 7 ++++--- server/lib/prosody/config/affiliations.ts | 7 +++++-- server/lib/prosody/config/content.ts | 20 +++++++++++--------- server/lib/prosody/config/domain.ts | 12 ++++++++++++ server/lib/routers/api.ts | 10 +++++++--- server/lib/routers/webchat.ts | 6 ++++-- 6 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 server/lib/prosody/config/domain.ts diff --git a/server/lib/prosody/config.ts b/server/lib/prosody/config.ts index 0f5b8963..6fac737a 100644 --- a/server/lib/prosody/config.ts +++ b/server/lib/prosody/config.ts @@ -3,6 +3,7 @@ import * as path from 'path' import { pluginName, getBaseRouter } from '../helpers' import { ProsodyFilePaths } from './config/paths' import { ProsodyConfigContent } from './config/content' +import { getProsodyDomain } from './config/domain' import { getAPIKey } from '../apikey' async function getWorkingDir ({ peertubeHelpers, storageManager }: RegisterServerOptions): Promise { @@ -92,7 +93,7 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise { const peertubeHelpers = options.peertubeHelpers + const prosodyDomain = await getProsodyDomain(options) // Get all admins and moderators const [results] = await peertubeHelpers.database.query( 'SELECT "username" FROM "user"' + @@ -19,7 +22,7 @@ async function getVideoAffiliations (options: RegisterServerOptions, video: MVid if (!('username' in result)) { throw new Error('getVideoAffiliations: no username field in result') } - const jid = (result.username as string) + '@localhost' + const jid = (result.username as string) + '@' + prosodyDomain r[jid] = 'owner' } @@ -29,7 +32,7 @@ async function getVideoAffiliations (options: RegisterServerOptions, video: MVid if (!video.remote) { // don't add the video owner if it is a remote video! const userName = await _getVideoOwnerUsername(options, video) - const userJid = userName + '@localhost' + const userJid = userName + '@' + prosodyDomain r[userJid] = 'admin' } } catch (error) { diff --git a/server/lib/prosody/config/content.ts b/server/lib/prosody/config/content.ts index 234e2f3a..e626a3ac 100644 --- a/server/lib/prosody/config/content.ts +++ b/server/lib/prosody/config/content.ts @@ -103,13 +103,15 @@ class ProsodyConfigContent { anon: ProsodyConfigVirtualHost muc: ProsodyConfigComponent log: string + prosodyDomain: string - constructor (paths: ProsodyFilePaths) { + constructor (paths: ProsodyFilePaths, prosodyDomain: string) { this.paths = paths this.global = new ProsodyConfigGlobal() this.log = '' - this.anon = new ProsodyConfigVirtualHost('anon.localhost') - this.muc = new ProsodyConfigComponent('muc', 'room.localhost') + this.prosodyDomain = prosodyDomain + this.anon = new ProsodyConfigVirtualHost('anon.' + prosodyDomain) + this.muc = new ProsodyConfigComponent('muc', 'room.' + prosodyDomain) this.global.set('daemonize', false) this.global.set('allow_registration', false) @@ -157,7 +159,7 @@ class ProsodyConfigContent { } useHttpAuthentication (url: string): void { - this.authenticated = new ProsodyConfigVirtualHost('localhost') + this.authenticated = new ProsodyConfigVirtualHost(this.prosodyDomain) this.authenticated.set('authentication', 'http') this.authenticated.set('modules_enabled', ['ping', 'auth_http']) @@ -165,7 +167,7 @@ class ProsodyConfigContent { this.authenticated.set('http_auth_url', url) } - usePeertubeBosh (peertubeDomain: string, port: string): void { + usePeertubeBosh (prosodyDomain: string, port: string): void { this.global.set('c2s_require_encryption', false) this.global.set('interfaces', ['127.0.0.1', '::1']) this.global.set('c2s_ports', []) @@ -183,8 +185,8 @@ class ProsodyConfigContent { this.anon.set('allow_anonymous_s2s', false) this.anon.add('modules_enabled', 'http') this.anon.add('modules_enabled', 'bosh') - this.anon.set('http_host', peertubeDomain) - this.anon.set('http_external_url', 'http://' + peertubeDomain) + this.anon.set('http_host', prosodyDomain) + this.anon.set('http_external_url', 'http://' + prosodyDomain) this.muc.set('restrict_room_creation', 'local') @@ -193,8 +195,8 @@ class ProsodyConfigContent { this.authenticated.set('allow_anonymous_s2s', false) this.authenticated.add('modules_enabled', 'http') this.authenticated.add('modules_enabled', 'bosh') - this.authenticated.set('http_host', peertubeDomain) - this.authenticated.set('http_external_url', 'http://' + peertubeDomain) + this.authenticated.set('http_host', prosodyDomain) + this.authenticated.set('http_external_url', 'http://' + prosodyDomain) } } diff --git a/server/lib/prosody/config/domain.ts b/server/lib/prosody/config/domain.ts new file mode 100644 index 00000000..42b897f8 --- /dev/null +++ b/server/lib/prosody/config/domain.ts @@ -0,0 +1,12 @@ +async function getProsodyDomain (options: RegisterServerOptions): Promise { + const url = options.peertubeHelpers.config.getWebserverUrl() + const matches = url.match(/^https?:\/\/([^:/]*)(:\d+)?(\/|$)/) + if (!matches) { + throw new Error(`Cant get a domain name from url '${url}'`) + } + return matches[1] +} + +export { + getProsodyDomain +} diff --git a/server/lib/routers/api.ts b/server/lib/routers/api.ts index 7ff84e5c..75cb305f 100644 --- a/server/lib/routers/api.ts +++ b/server/lib/routers/api.ts @@ -5,6 +5,7 @@ import { getCheckAPIKeyMiddleware } from '../middlewares/apikey' import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered } from '../prosody/auth' import { getAuthUser, getUserNickname } from '../helpers' import { Affiliations, getVideoAffiliations } from '../prosody/config/affiliations' +import { getProsodyDomain } from '../prosody/config/domain' // See here for description: https://modules.prosody.im/mod_muc_http_defaults.html interface RoomDefaults { @@ -100,10 +101,11 @@ async function initApiRouter (options: RegisterServerOptions): Promise { res.sendStatus(403) return } + const prosodyDomain = await getProsodyDomain(options) const password: string = await prosodyRegisterUser(user.username) const nickname: string | undefined = await getUserNickname(options, user) res.status(200).json({ - jid: user.username + '@localhost', + jid: user.username + '@' + prosodyDomain, password: password, nickname: nickname }) @@ -130,10 +132,11 @@ async function initApiRouter (options: RegisterServerOptions): Promise { res.status(200).send('false') return } + const prosodyDomain = await getProsodyDomain(options) const user = req.query.user const server = req.query.server const pass = req.query.pass - if (server !== 'localhost') { + if (server !== prosodyDomain) { logger.warn(`Cannot call check_password on user on server ${server as string}.`) res.status(200).send('false') return @@ -160,9 +163,10 @@ async function initApiRouter (options: RegisterServerOptions): Promise { res.status(200).send('false') return } + const prosodyDomain = await getProsodyDomain(options) const user = req.query.user const server = req.query.server - if (server !== 'localhost') { + if (server !== prosodyDomain) { logger.warn(`Cannot call user_exists on user on server ${server as string}.`) res.status(200).send('false') return diff --git a/server/lib/routers/webchat.ts b/server/lib/routers/webchat.ts index ccf24253..e224dee8 100644 --- a/server/lib/routers/webchat.ts +++ b/server/lib/routers/webchat.ts @@ -2,6 +2,7 @@ import type { Router, RequestHandler, Request, Response, NextFunction } from 'ex import type { ProxyOptions } from 'express-http-proxy' import { getBaseRouter } from '../helpers' import { asyncMiddleware } from '../middlewares/async' +import { getProsodyDomain } from '../prosody/config/domain' import * as path from 'path' const bodyParser = require('body-parser') @@ -35,8 +36,9 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise