Various fix and wip

This commit is contained in:
John Livingston
2023-09-19 15:54:56 +02:00
parent f97e54d499
commit 2289d79c28
8 changed files with 231 additions and 122 deletions

View File

@ -71,7 +71,8 @@ class BotsCtl {
const moderationBotProcess = child_process.spawn('npm', execArgs, {
cwd: __dirname, // must be in the livechat plugin tree, so that npm can found the package.
env: {
...process.env // will include NODE_ENV and co
...process.env, // will include NODE_ENV and co
NODE_TLS_REJECT_UNAUTHORIZED: '0' // Prosody use self-signed certificates, the bot must accept themp
}
})
moderationBotProcess.stdout?.on('data', (data) => {
@ -114,25 +115,27 @@ class BotsCtl {
this.moderationBotProcess as ReturnType<typeof child_process.spawn>
let resolved = false
// Trying to kill, and force kill if it takes more than 2 seconds
// Trying to kill, and force kill if it takes more than 1 seconds
const timeout = setTimeout(() => {
this.logger.error('Moderation bot was not killed within 2 seconds, force killing')
moderationBotProcess.kill('SIGKILL')
try {
this.logger.error('Moderation bot was not killed within 1 seconds, force killing')
moderationBotProcess.kill('SIGKILL')
} catch (_err) {}
resolved = true
resolve()
}, 2000)
}, 1000)
moderationBotProcess.on('exit', () => {
if (resolved) { return }
resolved = true
if (timeout) { clearTimeout(timeout) }
resolve()
if (timeout) { clearTimeout(timeout) }
})
moderationBotProcess.on('close', () => {
if (resolved) { return }
resolved = true
if (timeout) { clearTimeout(timeout) }
resolve()
if (timeout) { clearTimeout(timeout) }
})
moderationBotProcess.kill()
} catch (err) {

View File

@ -204,12 +204,14 @@ class BotConfiguration {
public configurationPaths (): {
moderation: {
globalFile: string
globalDir: string
roomConfDir: string
}
} {
return {
moderation: {
globalFile: this.moderationBotGlobalConf,
globalDir: this.confDir,
roomConfDir: this.roomConfDir
}
}

View File

@ -174,8 +174,14 @@ async function getProsodyConfig (options: RegisterServerOptionsV5): Promise<Pros
// enableRemoteChatConnections: local users can communicate with external rooms
const enableRemoteChatConnections = !(settings['federation-dont-publish-remotely'] as boolean)
let certificates: ProsodyConfigCertificates = false
const useBots = !settings['disable-channel-configuration']
const bots: ProsodyConfig['bots'] = {}
// Note: for the bots to connect, we must allow multiplexing.
// This will be done on the http (BOSH/Websocket) port, as it only listen on localhost.
// TODO: to improve performance, try to avoid multiplexing, and find a better way for bots to connect.
const useMultiplexing = useBots
const apikey = await getAPIKey(options)
valuesToHideInDiagnostic.set('APIKey', apikey)
@ -222,7 +228,7 @@ async function getProsodyConfig (options: RegisterServerOptionsV5): Promise<Pros
}
config.useHttpAuthentication(authApiUrl)
const useWS = !!options.registerWebSocketRoute // this comes with Peertube >=5.0.0, and is a prerequisite to websocket
config.usePeertubeBoshAndWebsocket(prosodyDomain, port, publicServerUrl, useWS)
config.usePeertubeBoshAndWebsocket(prosodyDomain, port, publicServerUrl, useWS, useMultiplexing)
config.useMucHttpDefault(roomApiUrl)
if (enableC2S) {
@ -294,10 +300,12 @@ async function getProsodyConfig (options: RegisterServerOptionsV5): Promise<Pros
config.usePeertubeVCards(basePeertubeUrl)
config.useAnonymousRandomVCards(paths.avatars)
if (!settings['disable-channel-configuration']) {
if (useBots) {
config.useBotsVirtualHost()
bots.moderation = await BotConfiguration.singleton().getModerationBotGlobalConf()
valuesToHideInDiagnostic.set('BotPassword', bots.moderation.connection.password)
if (bots.moderation?.connection?.password) {
valuesToHideInDiagnostic.set('BotPassword', bots.moderation.connection.password)
}
}
config.useTestModule(apikey, testApiUrl)

View File

@ -1,5 +1,6 @@
import type { ProsodyFilePaths } from './paths'
import type { ExternalComponent } from './components'
import { BotConfiguration } from '../../configuration/bot'
import { userInfo } from 'os'
type ConfigEntryValue = boolean | number | string | ConfigEntryValue[]
@ -221,14 +222,39 @@ class ProsodyConfigContent {
this.authenticated.set('http_auth_url', url)
}
usePeertubeBoshAndWebsocket (prosodyDomain: string, port: string, publicServerUrl: string, useWS: boolean): void {
/**
* Activate BOSH (and optionnaly Websocket).
* @param prosodyDomain prosody domain
* @param port port to use for BOSH and Websocket interfaces
* @param publicServerUrl public server url
* @param useWS activate Websocket or not
* @param multiplexing activate multiplexing on port. Note: it will only listen on localhost interfaces.
*/
usePeertubeBoshAndWebsocket (
prosodyDomain: string,
port: string,
publicServerUrl: string,
useWS: boolean,
multiplexing: boolean
): void {
// Note: don't activate other http_interface or https_interfaces than localhost.
// Elsewhere it would be a security issue.
this.global.set('c2s_require_encryption', false)
this.global.set('interfaces', ['127.0.0.1', '::1'])
this.global.set('c2s_ports', [])
this.global.set('c2s_interfaces', ['127.0.0.1', '::1'])
this.global.set('s2s_ports', [])
this.global.set('s2s_interfaces', ['127.0.0.1', '::1'])
this.global.set('http_ports', [port])
if (!multiplexing) {
this.global.set('http_ports', [port])
} else {
// Note: don't activate other http_interface or https_interfaces than localhost.
// Elsewhere it would be a security issue.
this.global.add('modules_enabled', 'net_multiplex')
this.global.set('ports', [port])
// FIXME: this generates Prosody error logs saying that BOSH/Websocket won't work... even if it is not true.
this.global.set('http_ports', [])
}
this.global.set('http_interfaces', ['127.0.0.1', '::1'])
this.global.set('https_ports', [])
this.global.set('https_interfaces', ['127.0.0.1', '::1'])
@ -410,7 +436,12 @@ class ProsodyConfigContent {
*/
useBotsVirtualHost (): void {
this.bot = new ProsodyConfigVirtualHost('bot.' + this.prosodyDomain)
this.bot.set('modules_enabled', ['ping'])
this.bot.set('modules_enabled', ['ping', 'bot_peertubelivechat'])
const configurationPaths = BotConfiguration.singleton().configurationPaths()
if (configurationPaths.moderation?.globalDir) {
this.bot.set('livechat_bot_conf_folder', configurationPaths.moderation.globalDir)
}
// TODO: bot vcards
}