2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2021-12-11 16:12:04 +00:00
|
|
|
import { getProsodyConfig, getProsodyConfigContentForDiagnostic, getWorkingDir } from '../prosody/config'
|
2023-04-07 12:34:10 +00:00
|
|
|
import { checkProsody, getProsodyAbout, testProsodyCorrectlyRunning } from '../prosody/ctl'
|
2021-04-12 15:53:12 +00:00
|
|
|
import { newResult, TestResult } from './utils'
|
2021-06-22 08:26:45 +00:00
|
|
|
import { getAPIKey } from '../apikey'
|
2021-04-13 15:13:41 +00:00
|
|
|
import * as fs from 'fs'
|
2021-04-12 15:53:12 +00:00
|
|
|
|
2021-06-22 08:26:45 +00:00
|
|
|
const got = require('got')
|
|
|
|
|
2021-04-12 18:52:21 +00:00
|
|
|
export async function diagProsody (test: string, options: RegisterServerOptions): Promise<TestResult> {
|
2021-04-12 15:53:12 +00:00
|
|
|
const result = newResult(test)
|
|
|
|
result.label = 'Builtin Prosody and ConverseJS'
|
2021-04-12 18:52:21 +00:00
|
|
|
|
|
|
|
try {
|
2021-05-11 13:37:34 +00:00
|
|
|
const workingDir = await getWorkingDir(options)
|
2021-06-02 10:41:28 +00:00
|
|
|
result.messages.push('The working dir is: ' + workingDir)
|
2021-04-12 18:52:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
result.messages.push('Error when requiring the working dir: ' + (error as string))
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:15:43 +00:00
|
|
|
// FIXME: these tests are very similar to tests in testProsodyCorrectlyRunning. Remove from here?
|
2021-04-13 15:13:41 +00:00
|
|
|
// Testing the prosody config file.
|
2021-06-22 08:26:45 +00:00
|
|
|
let prosodyPort: string
|
2021-07-20 00:52:58 +00:00
|
|
|
let prosodyHost: string
|
2022-11-18 16:48:20 +00:00
|
|
|
let prosodyErrorLogPath: string
|
2021-04-13 15:13:41 +00:00
|
|
|
try {
|
2021-04-15 10:17:08 +00:00
|
|
|
const wantedConfig = await getProsodyConfig(options)
|
2021-12-11 16:12:04 +00:00
|
|
|
const filePath = wantedConfig.paths.config
|
2022-11-18 16:48:20 +00:00
|
|
|
prosodyErrorLogPath = wantedConfig.paths.error
|
2021-04-15 10:17:08 +00:00
|
|
|
|
2021-04-16 13:23:08 +00:00
|
|
|
result.messages.push(`Prosody will run on port '${wantedConfig.port}'`)
|
2021-06-22 08:26:45 +00:00
|
|
|
prosodyPort = wantedConfig.port
|
2021-07-20 00:52:58 +00:00
|
|
|
prosodyHost = wantedConfig.host
|
2021-04-16 13:23:08 +00:00
|
|
|
|
2021-06-22 11:23:01 +00:00
|
|
|
result.messages.push(`Prosody will use ${wantedConfig.baseApiUrl} as base uri from api calls`)
|
|
|
|
|
2022-12-12 18:17:43 +00:00
|
|
|
if (!wantedConfig.paths.exec) {
|
|
|
|
result.messages.push({
|
|
|
|
level: 'error',
|
|
|
|
message: 'Error: no Prosody server.'
|
|
|
|
})
|
|
|
|
if (process.arch !== 'x64' && process.arch !== 'x86_64') {
|
|
|
|
result.messages.push({
|
|
|
|
level: 'error',
|
|
|
|
message: 'Error: your CPU is a ' +
|
|
|
|
process.arch + ', ' +
|
|
|
|
'which is not compatible with the plugin. ' +
|
|
|
|
'Please read the plugin installation documentation for a workaround.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:54:08 +00:00
|
|
|
result.messages.push(`Prosody path will be '${wantedConfig.paths.exec}'`)
|
|
|
|
|
2022-11-22 10:35:38 +00:00
|
|
|
if (wantedConfig.paths.appImageToExtract) {
|
|
|
|
result.messages.push(`Prosody will be using the '${wantedConfig.paths.appImageToExtract}' AppImage`)
|
|
|
|
} else {
|
|
|
|
result.messages.push('Prosody will not be using any AppImage')
|
|
|
|
}
|
|
|
|
|
|
|
|
result.messages.push(`Prosody AppImage extract path will be '${wantedConfig.paths.appImageExtractPath}'`)
|
|
|
|
|
2021-04-29 14:50:30 +00:00
|
|
|
result.messages.push(`Prosody modules path will be '${wantedConfig.paths.modules}'`)
|
|
|
|
|
2021-08-05 13:41:49 +00:00
|
|
|
result.messages.push(`Prosody rooms will be grouped by '${wantedConfig.roomType}'.`)
|
|
|
|
|
2021-12-01 11:57:15 +00:00
|
|
|
if (wantedConfig.logByDefault) {
|
|
|
|
result.messages.push('By default, room content will be archived.')
|
|
|
|
} else {
|
|
|
|
result.messages.push('By default, room content will not be archived.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('error' in wantedConfig.logExpiration) {
|
|
|
|
result.messages.push({
|
|
|
|
level: 'error',
|
|
|
|
message: 'Errors: Room logs expiration value is not valid. Using the default value.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
result.messages.push(`Room content will be saved for '${wantedConfig.logExpiration.value}'`)
|
|
|
|
|
2021-12-11 16:12:04 +00:00
|
|
|
await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist.
|
|
|
|
result.messages.push(`The prosody configuration file (${filePath}) exists`)
|
|
|
|
const actualContent = await fs.promises.readFile(filePath, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
})
|
|
|
|
|
|
|
|
result.debug.push({
|
|
|
|
title: 'Current prosody configuration',
|
|
|
|
// we have to hide secret keys and other values.
|
|
|
|
// But here, we haven't them for actualContent.
|
|
|
|
// So we will use values in wantedConfig, hopping it is enough.
|
|
|
|
message: getProsodyConfigContentForDiagnostic(wantedConfig, actualContent)
|
|
|
|
})
|
|
|
|
|
|
|
|
const wantedContent = wantedConfig.content
|
|
|
|
if (actualContent === wantedContent) {
|
|
|
|
result.messages.push('Prosody configuration file content is correct.')
|
|
|
|
} else {
|
|
|
|
result.messages.push('Prosody configuration file content is not correct.')
|
2021-04-14 14:14:56 +00:00
|
|
|
result.debug.push({
|
2021-12-11 16:12:04 +00:00
|
|
|
title: 'Prosody configuration should be',
|
|
|
|
// we have to hide secret keys and other values:
|
|
|
|
message: getProsodyConfigContentForDiagnostic(wantedConfig)
|
2021-04-14 14:14:56 +00:00
|
|
|
})
|
2021-12-11 16:12:04 +00:00
|
|
|
return result
|
2021-04-13 15:13:41 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-12-11 16:12:04 +00:00
|
|
|
result.messages.push('Error when requiring the prosody config file: ' + (error as string))
|
2021-04-13 15:13:41 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-04-13 16:00:45 +00:00
|
|
|
const isCorrectlyRunning = await testProsodyCorrectlyRunning(options)
|
|
|
|
if (isCorrectlyRunning.messages.length) {
|
|
|
|
result.messages.push(...isCorrectlyRunning.messages)
|
|
|
|
}
|
2021-04-14 14:14:56 +00:00
|
|
|
|
|
|
|
const about = await getProsodyAbout(options)
|
|
|
|
result.debug.push({
|
|
|
|
title: 'Prosody version',
|
|
|
|
message: about
|
|
|
|
})
|
|
|
|
|
2021-04-13 16:00:45 +00:00
|
|
|
if (!isCorrectlyRunning.ok) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-06-22 11:28:15 +00:00
|
|
|
const versionMatches = about.match(/^Prosody\s*(\d+)\.(\d+)(?:\.(\d+)| (nightly build \d+.*))\s*$/mi)
|
2021-05-02 14:16:19 +00:00
|
|
|
if (!versionMatches) {
|
|
|
|
result.messages.push({
|
|
|
|
level: 'error',
|
|
|
|
message: 'Errors: cant find prosody version.'
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
} else {
|
|
|
|
const major = versionMatches[1]
|
|
|
|
const minor = versionMatches[2]
|
2021-06-22 11:28:15 +00:00
|
|
|
const patch = versionMatches[3] ?? versionMatches[4]
|
2021-05-02 14:16:19 +00:00
|
|
|
result.messages.push(`Prosody version is ${major}.${minor}.${patch}`)
|
|
|
|
if (major !== '0' && minor !== '11') {
|
|
|
|
result.messages.push({
|
|
|
|
level: parseInt(minor) < 11 ? 'error' : 'warning',
|
|
|
|
message: 'Warning: recommended Prosody version is 0.11.x'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 08:26:45 +00:00
|
|
|
try {
|
|
|
|
const apiUrl = `http://localhost:${prosodyPort}/peertubelivechat_test/test-peertube-prosody`
|
|
|
|
const testResult = await got(apiUrl, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
2021-07-20 00:52:58 +00:00
|
|
|
authorization: 'Bearer ' + await getAPIKey(options),
|
|
|
|
host: prosodyHost
|
2021-06-22 08:26:45 +00:00
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
resolveBodyOnly: true
|
|
|
|
})
|
|
|
|
if (testResult.ok === true) {
|
|
|
|
result.messages.push('API Peertube -> Prosody is OK')
|
|
|
|
} else {
|
|
|
|
result.messages.push('API Peertube -> Prosody is KO. Response was: ' + JSON.stringify(testResult))
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
result.messages.push('Error when calling Prosody test api (test-peertube-prosody): ' + (error as string))
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-06-22 10:57:24 +00:00
|
|
|
try {
|
|
|
|
const apiUrl = `http://localhost:${prosodyPort}/peertubelivechat_test/test-prosody-peertube`
|
|
|
|
const testResult = await got(apiUrl, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
2021-07-20 00:52:58 +00:00
|
|
|
authorization: 'Bearer ' + await getAPIKey(options),
|
|
|
|
host: prosodyHost
|
2021-06-22 10:57:24 +00:00
|
|
|
},
|
|
|
|
responseType: 'json',
|
|
|
|
resolveBodyOnly: true
|
|
|
|
})
|
|
|
|
if (testResult.ok === true) {
|
|
|
|
result.messages.push('API Prosody -> Peertube is OK')
|
|
|
|
} else {
|
|
|
|
result.messages.push('API Prosody -> Peertube is KO. Response was: ' + JSON.stringify(testResult))
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
result.messages.push('Error when calling Prosody test api (test-prosody-peertube): ' + (error as string))
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-04-07 12:34:10 +00:00
|
|
|
const check = await checkProsody(options)
|
|
|
|
result.debug.push({
|
|
|
|
title: 'Prosody check',
|
|
|
|
message: check
|
|
|
|
})
|
|
|
|
|
2022-11-18 16:48:20 +00:00
|
|
|
// Checking if there is a Prosody error log, and returning last lines.
|
|
|
|
try {
|
|
|
|
await fs.promises.access(prosodyErrorLogPath, fs.constants.R_OK) // throw an error if file does not exist.
|
|
|
|
result.messages.push(`The prosody error log (${prosodyErrorLogPath}) exists`)
|
|
|
|
const errorLogContent = await fs.promises.readFile(prosodyErrorLogPath, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
})
|
|
|
|
|
|
|
|
let logLines = errorLogContent.split(/\r?\n/)
|
|
|
|
if (logLines.length > 50) {
|
|
|
|
logLines = logLines.slice(-50)
|
|
|
|
}
|
|
|
|
|
|
|
|
result.debug.push({
|
|
|
|
title: 'Prosody error log (last 50 lines)',
|
|
|
|
message: logLines.join('\n')
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
// Error should be because file does not exists. This is not an error case, just ignoring.
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:52:21 +00:00
|
|
|
result.ok = true
|
2021-04-12 15:53:12 +00:00
|
|
|
return result
|
|
|
|
}
|