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-30 14:22:58 +00:00
|
|
|
import { ProsodyFilePaths } from './config/paths'
|
|
|
|
import { ProsodyConfigContent } from './config/content'
|
2021-04-12 18:52:21 +00:00
|
|
|
|
|
|
|
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
|
|
|
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-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-12 18:52:21 +00:00
|
|
|
|
2021-05-03 18:37:23 +00:00
|
|
|
const baseApiUrl = options.peertubeHelpers.config.getWebserverUrl() +
|
2021-04-30 14:22:58 +00:00
|
|
|
getBaseRouter() +
|
2021-05-03 18:37:23 +00:00
|
|
|
'api/'
|
|
|
|
const authApiUrl = baseApiUrl + 'user'
|
|
|
|
const roomApiUrl = baseApiUrl + 'room?jid={room.jid|jid_node}'
|
2021-04-12 18:52:21 +00:00
|
|
|
|
2021-04-30 14:22:58 +00:00
|
|
|
const config = new ProsodyConfigContent(paths)
|
2021-05-03 18:37:23 +00:00
|
|
|
config.useHttpAuthentication(authApiUrl)
|
2021-04-30 14:22:58 +00:00
|
|
|
config.usePeertubeBosh(peertubeDomain, port)
|
|
|
|
config.useMucHttpDefault(roomApiUrl)
|
|
|
|
config.setArchive('1w') // Remove archived messages after 1 week
|
|
|
|
config.setLog(process.env.NODE_ENV === 'test' ? 'debug' : 'info')
|
|
|
|
const content = config.write()
|
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
|
|
|
}
|