2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterServerOptions, MVideoThumbnail } from '@peertube/peertube-types'
|
2021-04-15 10:17:08 +00:00
|
|
|
import type { Router, RequestHandler, Request, Response, NextFunction } from 'express'
|
2021-04-15 13:21:58 +00:00
|
|
|
import type { ProxyOptions } from 'express-http-proxy'
|
2021-11-18 10:08:12 +00:00
|
|
|
import type {
|
2022-10-10 16:08:20 +00:00
|
|
|
ProsodyListRoomsResult, ProsodyListRoomsResultRoom
|
2021-11-18 10:08:12 +00:00
|
|
|
} from '../../../shared/lib/types'
|
2022-08-23 16:53:12 +00:00
|
|
|
import { getBaseRouterRoute, getBaseRouterCanonicalRoute, getBaseStaticRoute, isUserAdmin } from '../helpers'
|
2021-05-03 18:37:23 +00:00
|
|
|
import { asyncMiddleware } from '../middlewares/async'
|
2021-05-06 11:31:55 +00:00
|
|
|
import { getProsodyDomain } from '../prosody/config/domain'
|
2021-06-12 01:52:45 +00:00
|
|
|
import { getAPIKey } from '../apikey'
|
2021-08-05 16:25:27 +00:00
|
|
|
import { getChannelInfosById, getChannelNameById } from '../database/channel'
|
2021-11-19 15:45:10 +00:00
|
|
|
import { isAutoColorsAvailable, areAutoColorsValid, AutoColors } from '../../../shared/lib/autocolors'
|
2021-04-08 00:43:13 +00:00
|
|
|
import * as path from 'path'
|
2021-04-16 11:46:51 +00:00
|
|
|
const bodyParser = require('body-parser')
|
2021-06-12 01:52:45 +00:00
|
|
|
const got = require('got')
|
2021-04-16 11:42:07 +00:00
|
|
|
|
2021-04-08 00:43:13 +00:00
|
|
|
const fs = require('fs').promises
|
2021-04-15 13:21:58 +00:00
|
|
|
const proxy = require('express-http-proxy')
|
2021-04-15 10:17:08 +00:00
|
|
|
|
|
|
|
let httpBindRoute: RequestHandler
|
2021-07-20 00:52:58 +00:00
|
|
|
interface ProsodyHttpBindInfo {
|
|
|
|
host: string
|
|
|
|
port: string
|
|
|
|
}
|
|
|
|
let currentProsodyHttpBindInfo: ProsodyHttpBindInfo | null = null
|
2021-04-15 10:17:08 +00:00
|
|
|
|
|
|
|
async function initWebchatRouter (options: RegisterServerOptions): Promise<Router> {
|
|
|
|
const {
|
|
|
|
getRouter,
|
|
|
|
peertubeHelpers,
|
|
|
|
settingsManager
|
|
|
|
} = options
|
2021-04-08 00:43:13 +00:00
|
|
|
|
2021-04-09 17:29:44 +00:00
|
|
|
const converseJSIndex = await fs.readFile(path.resolve(__dirname, '../../conversejs/index.html'))
|
2021-04-08 00:43:13 +00:00
|
|
|
|
2021-04-15 10:17:08 +00:00
|
|
|
const router: Router = getRouter()
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
2021-08-05 16:25:27 +00:00
|
|
|
router.get('/room/:roomKey', asyncMiddleware(
|
2021-05-03 18:37:23 +00:00
|
|
|
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
|
2021-05-07 14:45:55 +00:00
|
|
|
res.removeHeader('X-Frame-Options') // this route can be opened in an iframe
|
|
|
|
|
2021-08-05 16:25:27 +00:00
|
|
|
const roomKey = req.params.roomKey
|
2021-04-08 00:43:13 +00:00
|
|
|
const settings = await settingsManager.getSettings([
|
2021-11-18 10:08:12 +00:00
|
|
|
'prosody-room-type',
|
2021-11-19 15:45:10 +00:00
|
|
|
'converse-theme', 'converse-autocolors'
|
2021-04-08 00:43:13 +00:00
|
|
|
])
|
|
|
|
|
2021-04-14 16:47:23 +00:00
|
|
|
let room: string
|
2021-05-04 11:00:44 +00:00
|
|
|
let authenticationUrl: string = ''
|
2022-01-07 18:20:28 +00:00
|
|
|
let advancedControls: boolean = false // auto join the chat in viewer mode, if not logged in
|
|
|
|
let autoViewerMode: boolean = false
|
2022-01-04 16:42:03 +00:00
|
|
|
let forceReadonly: 'true' | 'false' | 'noscroll' = 'false'
|
2021-11-18 10:08:12 +00:00
|
|
|
let converseJSTheme: string = settings['converse-theme'] as string
|
2022-01-16 16:50:11 +00:00
|
|
|
let transparent: boolean = false
|
2021-11-18 10:08:12 +00:00
|
|
|
if (!/^\w+$/.test(converseJSTheme)) {
|
|
|
|
converseJSTheme = 'peertube'
|
|
|
|
}
|
2022-10-10 16:08:20 +00:00
|
|
|
const prosodyDomain = await getProsodyDomain(options)
|
|
|
|
const jid = 'anon.' + prosodyDomain
|
|
|
|
if (req.query.forcetype === '1') {
|
|
|
|
// We come from the room list in the settings page.
|
|
|
|
// Here we don't read the prosody-room-type settings,
|
|
|
|
// but use the roomKey format.
|
|
|
|
// NB: there is no extra security. Any user can add this parameter.
|
|
|
|
// This is not an issue: the setting will be tested at the room creation.
|
|
|
|
// No room can be created in the wrong mode.
|
|
|
|
if (/^channel\.\d+$/.test(roomKey)) {
|
|
|
|
room = 'channel.{{CHANNEL_ID}}@room.' + prosodyDomain
|
2021-08-05 13:41:49 +00:00
|
|
|
} else {
|
2022-10-10 16:08:20 +00:00
|
|
|
room = '{{VIDEO_UUID}}@room.' + prosodyDomain
|
2021-08-05 13:41:49 +00:00
|
|
|
}
|
2022-10-10 16:08:20 +00:00
|
|
|
} else {
|
|
|
|
if (settings['prosody-room-type'] === 'channel') {
|
|
|
|
room = 'channel.{{CHANNEL_ID}}@room.' + prosodyDomain
|
2022-01-07 18:20:28 +00:00
|
|
|
} else {
|
2022-10-10 16:08:20 +00:00
|
|
|
room = '{{VIDEO_UUID}}@room.' + prosodyDomain
|
2021-04-14 16:47:23 +00:00
|
|
|
}
|
2022-10-10 16:08:20 +00:00
|
|
|
}
|
2022-08-23 16:54:28 +00:00
|
|
|
// Here we are using getBaseRouterCanonicalRoute,
|
|
|
|
// which correspond to a path without the plugin version.
|
|
|
|
// We are doing this, so the path is predictible,
|
|
|
|
// and can be optimized in the nginx configuration (to bypass Peertube).
|
2022-08-23 16:53:12 +00:00
|
|
|
const boshUri = getBaseRouterCanonicalRoute(options) + 'webchat/http-bind'
|
2022-10-10 16:08:20 +00:00
|
|
|
const wsUri = ''
|
|
|
|
authenticationUrl = options.peertubeHelpers.config.getWebserverUrl() +
|
|
|
|
getBaseRouterRoute(options) +
|
|
|
|
'api/auth'
|
|
|
|
advancedControls = true
|
|
|
|
if (req.query._readonly === 'true') {
|
|
|
|
forceReadonly = 'true'
|
|
|
|
} else if (req.query._readonly === 'noscroll') {
|
|
|
|
forceReadonly = 'noscroll'
|
2021-04-14 16:47:23 +00:00
|
|
|
} else {
|
2022-10-10 16:08:20 +00:00
|
|
|
autoViewerMode = true // auto join the chat in viewer mode, if not logged in
|
|
|
|
}
|
|
|
|
if (req.query._transparent === 'true') {
|
|
|
|
transparent = true
|
2021-04-08 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:25:27 +00:00
|
|
|
let video: MVideoThumbnail | undefined
|
|
|
|
let channelId: number
|
|
|
|
const channelMatches = roomKey.match(/^channel\.(\d+)$/)
|
|
|
|
if (channelMatches?.[1]) {
|
|
|
|
channelId = parseInt(channelMatches[1])
|
2022-10-10 16:08:20 +00:00
|
|
|
// Here we are on a channel room...
|
2021-08-05 16:25:27 +00:00
|
|
|
const channelInfos = await getChannelInfosById(options, channelId)
|
|
|
|
if (!channelInfos) {
|
|
|
|
throw new Error('Channel not found')
|
|
|
|
}
|
|
|
|
channelId = channelInfos.id
|
|
|
|
} else {
|
|
|
|
const uuid = roomKey // must be a video UUID.
|
|
|
|
video = await peertubeHelpers.videos.loadByIdOrUUID(uuid)
|
|
|
|
if (!video) {
|
|
|
|
throw new Error('Video not found')
|
|
|
|
}
|
|
|
|
channelId = video.channelId
|
2021-04-08 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let page = '' + (converseJSIndex as string)
|
2021-05-18 16:17:13 +00:00
|
|
|
const baseStaticUrl = getBaseStaticRoute(options)
|
|
|
|
page = page.replace(/{{BASE_STATIC_URL}}/g, baseStaticUrl)
|
2021-08-05 13:41:49 +00:00
|
|
|
page = page.replace(/{{JID}}/g, jid)
|
2021-08-03 22:22:19 +00:00
|
|
|
// Computing the room name...
|
2021-08-05 16:25:27 +00:00
|
|
|
if (room.includes('{{VIDEO_UUID}}')) {
|
|
|
|
if (!video) {
|
|
|
|
throw new Error('Missing video')
|
|
|
|
}
|
|
|
|
room = room.replace(/{{VIDEO_UUID}}/g, video.uuid)
|
|
|
|
}
|
|
|
|
room = room.replace(/{{CHANNEL_ID}}/g, `${channelId}`)
|
2021-08-04 15:32:47 +00:00
|
|
|
if (room.includes('{{CHANNEL_NAME}}')) {
|
2021-08-05 16:25:27 +00:00
|
|
|
const channelName = await getChannelNameById(options, channelId)
|
2021-08-03 22:22:19 +00:00
|
|
|
if (channelName === null) {
|
|
|
|
throw new Error('Channel not found')
|
|
|
|
}
|
|
|
|
if (!/^[a-zA-Z0-9_.]+$/.test(channelName)) {
|
|
|
|
// FIXME: see if there is a response here https://github.com/Chocobozzz/PeerTube/issues/4301 for allowed chars
|
|
|
|
peertubeHelpers.logger.error(`Invalid channel name, contains unauthorized chars: '${channelName}'`)
|
|
|
|
throw new Error('Invalid channel name, contains unauthorized chars')
|
|
|
|
}
|
|
|
|
room = room.replace(/{{CHANNEL_NAME}}/g, channelName)
|
|
|
|
}
|
2021-11-19 15:45:10 +00:00
|
|
|
|
|
|
|
let autocolorsStyles = ''
|
|
|
|
if (
|
|
|
|
settings['converse-autocolors'] &&
|
2022-10-10 16:08:20 +00:00
|
|
|
isAutoColorsAvailable(settings['converse-theme'] as string)
|
2021-11-19 15:45:10 +00:00
|
|
|
) {
|
|
|
|
peertubeHelpers.logger.debug('Trying to load AutoColors...')
|
|
|
|
const autocolors: AutoColors = {
|
|
|
|
mainForeground: req.query._ac_mainForeground?.toString() ?? '',
|
|
|
|
mainBackground: req.query._ac_mainBackground?.toString() ?? '',
|
|
|
|
greyForeground: req.query._ac_greyForeground?.toString() ?? '',
|
|
|
|
greyBackground: req.query._ac_greyBackground?.toString() ?? '',
|
|
|
|
menuForeground: req.query._ac_menuForeground?.toString() ?? '',
|
|
|
|
menuBackground: req.query._ac_menuBackground?.toString() ?? '',
|
|
|
|
inputForeground: req.query._ac_inputForeground?.toString() ?? '',
|
|
|
|
inputBackground: req.query._ac_inputBackground?.toString() ?? '',
|
|
|
|
buttonForeground: req.query._ac_buttonForeground?.toString() ?? '',
|
|
|
|
buttonBackground: req.query._ac_buttonBackground?.toString() ?? '',
|
|
|
|
link: req.query._ac_link?.toString() ?? '',
|
|
|
|
linkHover: req.query._ac_linkHover?.toString() ?? ''
|
|
|
|
}
|
|
|
|
const autoColorsTest = areAutoColorsValid(autocolors)
|
|
|
|
if (autoColorsTest === true) {
|
|
|
|
autocolorsStyles = `
|
|
|
|
<style>
|
|
|
|
:root {
|
|
|
|
--peertube-main-foreground: ${autocolors.mainForeground};
|
|
|
|
--peertube-main-background: ${autocolors.mainBackground};
|
|
|
|
--peertube-grey-foreground: ${autocolors.greyForeground};
|
|
|
|
--peertube-grey-background: ${autocolors.greyBackground};
|
|
|
|
--peertube-menu-foreground: ${autocolors.menuForeground};
|
|
|
|
--peertube-menu-background: ${autocolors.menuBackground};
|
|
|
|
--peertube-input-foreground: ${autocolors.inputForeground};
|
|
|
|
--peertube-input-background: ${autocolors.inputBackground};
|
|
|
|
--peertube-button-foreground: ${autocolors.buttonForeground};
|
|
|
|
--peertube-button-background: ${autocolors.buttonBackground};
|
|
|
|
--peertube-link: ${autocolors.link};
|
|
|
|
--peertube-link-hover: ${autocolors.linkHover};
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
`
|
|
|
|
} else {
|
|
|
|
peertubeHelpers.logger.error('Provided AutoColors are invalid.', autoColorsTest)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
peertubeHelpers.logger.debug('No AutoColors.')
|
|
|
|
}
|
2021-12-14 12:02:15 +00:00
|
|
|
|
2021-08-03 22:22:19 +00:00
|
|
|
// ... then inject it in the page.
|
2021-04-08 00:43:13 +00:00
|
|
|
page = page.replace(/{{ROOM}}/g, room)
|
2021-04-14 16:47:23 +00:00
|
|
|
page = page.replace(/{{BOSH_SERVICE_URL}}/g, boshUri)
|
|
|
|
page = page.replace(/{{WS_SERVICE_URL}}/g, wsUri)
|
2021-05-04 11:00:44 +00:00
|
|
|
page = page.replace(/{{AUTHENTICATION_URL}}/g, authenticationUrl)
|
2021-05-05 15:20:06 +00:00
|
|
|
page = page.replace(/{{ADVANCEDCONTROLS}}/g, advancedControls ? 'true' : 'false')
|
2022-01-07 18:20:28 +00:00
|
|
|
page = page.replace(/{{AUTOVIEWERMODE}}/g, autoViewerMode ? 'true' : 'false')
|
2021-11-18 10:08:12 +00:00
|
|
|
page = page.replace(/{{CONVERSEJS_THEME}}/g, converseJSTheme)
|
2021-11-19 15:45:10 +00:00
|
|
|
page = page.replace(/{{CONVERSEJS_AUTOCOLORS}}/g, autocolorsStyles)
|
2022-01-04 16:42:03 +00:00
|
|
|
page = page.replace(/{{FORCEREADONLY}}/g, forceReadonly)
|
2022-01-16 16:50:11 +00:00
|
|
|
page = page.replace(/{{TRANSPARENT}}/g, transparent ? 'true' : 'false')
|
2021-04-08 00:43:13 +00:00
|
|
|
|
|
|
|
res.status(200)
|
|
|
|
res.type('html')
|
|
|
|
res.send(page)
|
|
|
|
}
|
2021-05-03 18:37:23 +00:00
|
|
|
))
|
2021-04-15 10:17:08 +00:00
|
|
|
|
|
|
|
changeHttpBindRoute(options, null)
|
2021-04-16 11:44:24 +00:00
|
|
|
router.all('/http-bind',
|
|
|
|
bodyParser.raw({ type: 'text/xml' }),
|
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
|
|
httpBindRoute(req, res, next)
|
|
|
|
}
|
|
|
|
)
|
2021-06-12 01:52:45 +00:00
|
|
|
|
|
|
|
router.get('/prosody-list-rooms', asyncMiddleware(
|
|
|
|
async (req: Request, res: Response, _next: NextFunction) => {
|
|
|
|
if (!res.locals.authenticated) {
|
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!await isUserAdmin(options, res)) {
|
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:52:58 +00:00
|
|
|
if (!currentProsodyHttpBindInfo) {
|
2021-06-12 01:52:45 +00:00
|
|
|
throw new Error('It seems that prosody is not binded... Cant list rooms.')
|
|
|
|
}
|
2021-07-20 00:52:58 +00:00
|
|
|
const apiUrl = `http://localhost:${currentProsodyHttpBindInfo.port}/peertubelivechat_list_rooms/list-rooms`
|
2021-06-12 01:52:45 +00:00
|
|
|
peertubeHelpers.logger.debug('Calling list rooms API on url: ' + apiUrl)
|
|
|
|
const rooms = await got(apiUrl, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
2021-07-20 00:52:58 +00:00
|
|
|
authorization: 'Bearer ' + await getAPIKey(options),
|
|
|
|
host: currentProsodyHttpBindInfo.host
|
2021-06-12 01:52:45 +00:00
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
resolveBodyOnly: true
|
|
|
|
})
|
|
|
|
|
2021-08-05 16:25:27 +00:00
|
|
|
if (Array.isArray(rooms)) {
|
|
|
|
for (let i = 0; i < rooms.length; i++) {
|
|
|
|
const room: ProsodyListRoomsResultRoom = rooms[i]
|
|
|
|
const matches = room.localpart.match(/^channel\.(\d+)$/)
|
|
|
|
if (matches?.[1]) {
|
|
|
|
const channelId = parseInt(matches[1])
|
|
|
|
const channelInfos = await getChannelInfosById(options, channelId)
|
|
|
|
if (channelInfos) {
|
|
|
|
room.channel = {
|
|
|
|
id: channelInfos.id,
|
|
|
|
name: channelInfos.name,
|
|
|
|
displayName: channelInfos.displayName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 01:52:45 +00:00
|
|
|
res.status(200)
|
|
|
|
const r: ProsodyListRoomsResult = {
|
|
|
|
ok: true,
|
|
|
|
rooms: rooms
|
|
|
|
}
|
|
|
|
res.json(r)
|
|
|
|
}
|
|
|
|
))
|
|
|
|
|
2021-04-09 17:29:44 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:52:58 +00:00
|
|
|
function changeHttpBindRoute (
|
|
|
|
{ peertubeHelpers }: RegisterServerOptions,
|
|
|
|
prosodyHttpBindInfo: ProsodyHttpBindInfo | null
|
|
|
|
): void {
|
2021-04-15 10:17:08 +00:00
|
|
|
const logger = peertubeHelpers.logger
|
2021-07-20 00:52:58 +00:00
|
|
|
if (prosodyHttpBindInfo && !/^\d+$/.test(prosodyHttpBindInfo.port)) {
|
|
|
|
logger.error(`Port '${prosodyHttpBindInfo.port}' is not valid. Replacing by null`)
|
|
|
|
prosodyHttpBindInfo = null
|
2021-04-15 10:17:08 +00:00
|
|
|
}
|
2021-07-20 00:52:58 +00:00
|
|
|
|
|
|
|
if (!prosodyHttpBindInfo) {
|
|
|
|
logger.info('Changing http-bind port for null')
|
|
|
|
currentProsodyHttpBindInfo = null
|
2021-04-15 10:17:08 +00:00
|
|
|
httpBindRoute = (_req: Request, res: Response, _next: NextFunction) => {
|
|
|
|
res.status(404)
|
|
|
|
res.send('Not found')
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-20 00:52:58 +00:00
|
|
|
logger.info('Changing http-bind port for ' + prosodyHttpBindInfo.port + ', on host ' + prosodyHttpBindInfo.host)
|
2021-04-15 13:21:58 +00:00
|
|
|
const options: ProxyOptions = {
|
|
|
|
https: false,
|
|
|
|
proxyReqPathResolver: async (_req: Request): Promise<string> => {
|
|
|
|
return '/http-bind' // should not be able to access anything else
|
|
|
|
},
|
2021-05-06 12:04:16 +00:00
|
|
|
// preserveHostHdr: true,
|
2021-04-16 11:42:07 +00:00
|
|
|
parseReqBody: true // Note that setting this to false overrides reqAsBuffer and reqBodyEncoding below.
|
|
|
|
// FIXME: should we remove cookies?
|
2021-04-15 13:21:58 +00:00
|
|
|
}
|
2021-07-20 00:52:58 +00:00
|
|
|
currentProsodyHttpBindInfo = prosodyHttpBindInfo
|
|
|
|
httpBindRoute = proxy('http://localhost:' + prosodyHttpBindInfo.port, options)
|
2021-04-15 10:17:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 17:29:44 +00:00
|
|
|
export {
|
2021-04-15 10:17:08 +00:00
|
|
|
initWebchatRouter,
|
|
|
|
changeHttpBindRoute
|
2021-04-08 00:43:13 +00:00
|
|
|
}
|