2021-04-12 18:52:21 +00:00
|
|
|
import * as fs from 'fs'
|
2021-04-13 15:13:41 +00:00
|
|
|
import * as path from 'path'
|
2021-04-29 16:31:48 +00:00
|
|
|
import { pluginName, getBaseRouter } from '../helpers'
|
2021-04-12 18:52:21 +00:00
|
|
|
|
|
|
|
type LogMode = 'debug' | 'info'
|
|
|
|
|
|
|
|
async function getWorkingDir ({ peertubeHelpers, storageManager }: RegisterServerOptions): Promise<string> {
|
2021-04-14 15:10:22 +00:00
|
|
|
const logger = peertubeHelpers.logger
|
|
|
|
logger.debug('Calling getWorkingDir')
|
|
|
|
|
2021-04-12 18:52:21 +00:00
|
|
|
const tmpBaseDir = '/tmp/'
|
|
|
|
let value: string = await storageManager.getData('tempDirId')
|
2021-04-13 15:13:41 +00:00
|
|
|
|
|
|
|
function getPath (value: string): string {
|
|
|
|
return path.resolve(tmpBaseDir, pluginName + '-' + value)
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:52:21 +00:00
|
|
|
while (!value) {
|
|
|
|
peertubeHelpers.logger.info('Generating an id for temp dir')
|
|
|
|
value = Math.random().toString(36).slice(2, 12)
|
2021-04-13 15:13:41 +00:00
|
|
|
const name = getPath(value)
|
2021-04-12 18:52:21 +00:00
|
|
|
if (fs.existsSync(name)) {
|
|
|
|
peertubeHelpers.logger.info('The folder ' + name + ' already exists, generating another name...')
|
|
|
|
value = ''
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
await storageManager.storeData('tempDirId', value)
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:10:22 +00:00
|
|
|
const dir = getPath(value)
|
|
|
|
logger.debug('getWorkingDir will return ' + dir)
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the working dir if needed, and returns it.
|
|
|
|
* NB: for now, I try to create a directory in /tmp/.
|
|
|
|
* To ensure that there is no conflict with another peertube instance,
|
|
|
|
* I used a randomly generated id that will be stored in database.
|
|
|
|
*/
|
|
|
|
async function ensureWorkingDir (options: RegisterServerOptions): Promise<string> {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
logger.debug('Calling ensureworkingDir')
|
|
|
|
|
2021-04-16 12:26:21 +00:00
|
|
|
const paths = await getProsodyFilePaths(options)
|
|
|
|
const dir = paths.dir
|
2021-04-14 15:10:22 +00:00
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
logger.info(`The working dir ${dir} does not exists, trying to create it`)
|
|
|
|
await fs.promises.mkdir(dir)
|
|
|
|
logger.debug(`Working dir ${dir} was created`)
|
2021-04-12 18:52:21 +00:00
|
|
|
}
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug(`Testing write access on ${dir}`)
|
|
|
|
await fs.promises.access(dir, fs.constants.W_OK) // will throw an error if no access
|
|
|
|
logger.debug(`Write access ok on ${dir}`)
|
2021-04-16 12:26:21 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(paths.data)) {
|
|
|
|
logger.info(`The data dir ${paths.data} does not exists, trying to create it`)
|
|
|
|
await fs.promises.mkdir(paths.data)
|
|
|
|
logger.debug(`Working dir ${paths.data} was created`)
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:10:22 +00:00
|
|
|
return dir
|
2021-04-12 18:52:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 15:13:41 +00:00
|
|
|
interface ProsodyFilePaths {
|
2021-04-14 13:26:00 +00:00
|
|
|
dir: string
|
2021-04-13 15:13:41 +00:00
|
|
|
pid: string
|
|
|
|
error: string
|
|
|
|
log: string
|
|
|
|
config: string
|
2021-04-16 12:26:21 +00:00
|
|
|
data: string
|
2021-04-29 14:50:30 +00:00
|
|
|
modules: string
|
2021-04-13 15:13:41 +00:00
|
|
|
}
|
|
|
|
async function getProsodyFilePaths (options: RegisterServerOptions): Promise<ProsodyFilePaths> {
|
2021-04-14 15:10:22 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
logger.debug('Calling getProsodyFilePaths')
|
|
|
|
|
2021-04-13 15:13:41 +00:00
|
|
|
const dir = await getWorkingDir(options)
|
|
|
|
return {
|
2021-04-14 13:26:00 +00:00
|
|
|
dir: dir,
|
2021-04-13 15:13:41 +00:00
|
|
|
pid: path.resolve(dir, 'prosody.pid'),
|
|
|
|
error: path.resolve(dir, 'prosody.err'),
|
|
|
|
log: path.resolve(dir, 'prosody.log'),
|
2021-04-16 12:26:21 +00:00
|
|
|
config: path.resolve(dir, 'prosody.cfg.lua'),
|
2021-04-29 14:50:30 +00:00
|
|
|
data: path.resolve(dir, 'data'),
|
|
|
|
modules: path.resolve(__dirname, '../../prosody-modules')
|
2021-04-13 15:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:50:30 +00:00
|
|
|
interface ProsodyModuleConfig {
|
|
|
|
module: string
|
|
|
|
options: Array<{
|
|
|
|
name: string
|
|
|
|
value: string
|
|
|
|
}>
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:17:08 +00:00
|
|
|
interface ProsodyConfig {
|
|
|
|
content: string
|
|
|
|
paths: ProsodyFilePaths
|
|
|
|
port: string
|
|
|
|
}
|
|
|
|
async function getProsodyConfig (options: RegisterServerOptions): Promise<ProsodyConfig> {
|
2021-04-14 15:10:22 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
2021-04-15 10:17:08 +00:00
|
|
|
logger.debug('Calling getProsodyConfig')
|
2021-04-14 15:10:22 +00:00
|
|
|
|
2021-04-16 13:13:46 +00:00
|
|
|
const port = (await options.settingsManager.getSetting('prosody-port') as string) || '52800'
|
|
|
|
if (!/^\d+$/.test(port)) {
|
|
|
|
throw new Error('Invalid port')
|
|
|
|
}
|
2021-04-12 18:52:21 +00:00
|
|
|
const peertubeDomain = 'localhost'
|
2021-04-13 15:13:41 +00:00
|
|
|
const paths = await getProsodyFilePaths(options)
|
2021-04-29 15:45:04 +00:00
|
|
|
const logMode: LogMode = process.env.NODE_ENV === 'test' ? 'debug' : 'info'
|
2021-04-29 14:50:30 +00:00
|
|
|
|
|
|
|
const mucModules: ProsodyModuleConfig[] = []
|
|
|
|
|
2021-04-29 16:31:48 +00:00
|
|
|
mucModules.push({
|
|
|
|
module: 'muc_http_defaults',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'muc_create_api_url',
|
|
|
|
value: options.peertubeHelpers.config.getWebserverUrl() + getBaseRouter() + 'api/room'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2021-04-29 14:50:30 +00:00
|
|
|
|
|
|
|
const mucModulesEnabled: string = mucModules.map(m => ' "' + m.module + '";').join('\n')
|
|
|
|
const mucModulesOptions: string = mucModules.map(m => {
|
|
|
|
return m.options.map(o => {
|
|
|
|
return ' ' + o.name + ' = "' + o.value + '"'
|
|
|
|
}).join('\n')
|
|
|
|
}).join('\n')
|
|
|
|
|
2021-04-15 10:17:08 +00:00
|
|
|
const content = `
|
2021-04-12 18:52:21 +00:00
|
|
|
|
2021-04-16 13:13:46 +00:00
|
|
|
daemonize = false
|
|
|
|
pidfile = "${paths.pid}"
|
2021-04-29 14:50:30 +00:00
|
|
|
plugin_paths = { "${paths.modules}" }
|
2021-04-16 12:26:21 +00:00
|
|
|
data_path = "${paths.data}"
|
2021-04-16 13:13:46 +00:00
|
|
|
interfaces = { "127.0.0.1" }
|
|
|
|
c2s_ports = { }
|
|
|
|
c2s_interfaces = { "127.0.0.1" }
|
|
|
|
s2s_ports = { }
|
|
|
|
s2s_interfaces = { "127.0.0.1" }
|
|
|
|
http_ports = { "${port}" }
|
|
|
|
http_interfaces = { "127.0.0.1" }
|
|
|
|
https_ports = { }
|
|
|
|
https_interfaces = { "127.0.0.1" }
|
|
|
|
|
|
|
|
admins = { }
|
2021-04-12 18:52:21 +00:00
|
|
|
|
|
|
|
modules_enabled = {
|
2021-04-16 12:26:21 +00:00
|
|
|
"roster"; -- Allow users to have a roster. Recommended ;)
|
|
|
|
"saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
|
2021-04-12 18:52:21 +00:00
|
|
|
"version"; -- Replies to server version requests
|
|
|
|
"uptime"; -- Report how long server has been running
|
|
|
|
"ping"; -- Replies to XMPP pings with pongs
|
|
|
|
|
|
|
|
"bosh"; -- Enable BOSH clients, aka "Jabber over HTTP"
|
|
|
|
|
|
|
|
"posix"; -- POSIX functionality, sends server to background, enables syslog, etc.
|
|
|
|
}
|
|
|
|
modules_disabled = {
|
2021-04-16 12:26:21 +00:00
|
|
|
-- "offline"; -- Store offline messages
|
|
|
|
-- "c2s"; -- Handle client connections
|
2021-04-12 18:52:21 +00:00
|
|
|
"s2s"; -- Handle server-to-server connections
|
|
|
|
}
|
|
|
|
|
|
|
|
allow_registration = false
|
|
|
|
|
|
|
|
c2s_require_encryption = false
|
|
|
|
|
|
|
|
archive_expires_after = "1w" -- Remove archived messages after 1 week
|
|
|
|
|
|
|
|
log = {
|
|
|
|
-- Log files (change 'info' to 'debug' for debug logs):
|
2021-04-13 15:13:41 +00:00
|
|
|
${logMode} = "${paths.log}";
|
|
|
|
error = "${paths.error}";
|
2021-04-12 18:52:21 +00:00
|
|
|
-- Syslog:
|
|
|
|
-- { levels = { "error" }; to = "syslog"; };
|
|
|
|
}
|
|
|
|
|
2021-04-16 12:39:25 +00:00
|
|
|
cross_domain_bosh = false
|
|
|
|
consider_bosh_secure = true
|
|
|
|
cross_domain_websocket = false
|
|
|
|
consider_websocket_secure = true
|
2021-04-12 18:52:21 +00:00
|
|
|
|
2021-04-16 12:26:21 +00:00
|
|
|
VirtualHost "localhost"
|
2021-04-12 18:52:21 +00:00
|
|
|
trusted_proxies = { "127.0.0.1", "::1" }
|
|
|
|
|
|
|
|
authentication = "anonymous"
|
|
|
|
allow_anonymous_s2s = false
|
|
|
|
modules_enabled = {
|
|
|
|
"http";
|
|
|
|
"bosh";
|
|
|
|
"ping";
|
|
|
|
}
|
|
|
|
http_host = "${peertubeDomain}"
|
2021-04-16 12:39:25 +00:00
|
|
|
http_external_url = "http://${peertubeDomain}"
|
2021-04-12 18:52:21 +00:00
|
|
|
|
|
|
|
Component "room.localhost" "muc"
|
|
|
|
restrict_room_creation = "local"
|
2021-04-29 14:50:30 +00:00
|
|
|
modules_enabled = {
|
|
|
|
${mucModulesEnabled}
|
|
|
|
}
|
2021-04-12 18:52:21 +00:00
|
|
|
muc_room_locking = false
|
|
|
|
muc_tombstones = false
|
|
|
|
muc_room_default_language = "en"
|
|
|
|
muc_room_default_public = true
|
|
|
|
muc_room_default_persistent = false
|
|
|
|
muc_room_default_members_only = false
|
|
|
|
muc_room_default_moderated = false
|
|
|
|
muc_room_default_public_jids = false
|
|
|
|
muc_room_default_change_subject = false
|
|
|
|
muc_room_default_history_length = 20
|
2021-04-29 14:50:30 +00:00
|
|
|
${mucModulesOptions}
|
2021-04-12 18:52:21 +00:00
|
|
|
`
|
2021-04-29 14:50:30 +00:00
|
|
|
|
2021-04-15 10:17:08 +00:00
|
|
|
return {
|
|
|
|
content,
|
|
|
|
paths,
|
|
|
|
port
|
|
|
|
}
|
2021-04-12 18:52:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 10:17:08 +00:00
|
|
|
async function writeProsodyConfig (options: RegisterServerOptions): Promise<ProsodyConfig> {
|
2021-04-13 15:13:41 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Calling writeProsodyConfig')
|
|
|
|
|
|
|
|
logger.debug('Ensuring that the working dir exists')
|
|
|
|
await ensureWorkingDir(options)
|
|
|
|
logger.debug('Computing the Prosody config content')
|
2021-04-15 10:17:08 +00:00
|
|
|
const config = await getProsodyConfig(options)
|
|
|
|
const content = config.content
|
|
|
|
const fileName = config.paths.config
|
2021-04-14 15:10:22 +00:00
|
|
|
|
2021-04-13 15:13:41 +00:00
|
|
|
logger.info(`Writing prosody configuration file to ${fileName}`)
|
|
|
|
await fs.promises.writeFile(fileName, content)
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Prosody configuration file writen')
|
2021-04-15 10:17:08 +00:00
|
|
|
|
|
|
|
return config
|
2021-04-13 15:13:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:52:21 +00:00
|
|
|
export {
|
2021-04-15 10:17:08 +00:00
|
|
|
getProsodyConfig,
|
2021-04-13 15:13:41 +00:00
|
|
|
getWorkingDir,
|
2021-04-14 15:10:22 +00:00
|
|
|
ensureWorkingDir,
|
2021-04-13 15:13:41 +00:00
|
|
|
getProsodyFilePaths,
|
|
|
|
writeProsodyConfig
|
2021-04-12 18:52:21 +00:00
|
|
|
}
|