Merge branch 'release/3.2.0' into prosody_room_list

This commit is contained in:
John Livingston
2021-06-22 13:47:39 +02:00
12 changed files with 208 additions and 10 deletions

View File

@ -1,8 +1,11 @@
import { getProsodyConfig, getWorkingDir } from '../prosody/config'
import { getProsodyAbout, testProsodyCorrectlyRunning } from '../prosody/ctl'
import { newResult, TestResult } from './utils'
import { getAPIKey } from '../apikey'
import * as fs from 'fs'
const got = require('got')
export async function diagProsody (test: string, options: RegisterServerOptions): Promise<TestResult> {
const result = newResult(test)
result.label = 'Builtin Prosody and ConverseJS'
@ -17,11 +20,15 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
// FIXME: these tests are very similar to tests in testProsodyCorrectlyRunning. Remove from here?
// Testing the prosody config file.
let prosodyPort: string
try {
const wantedConfig = await getProsodyConfig(options)
const filePath = wantedConfig.paths.config
result.messages.push(`Prosody will run on port '${wantedConfig.port}'`)
prosodyPort = wantedConfig.port
result.messages.push(`Prosody will use ${wantedConfig.baseApiUrl} as base uri from api calls`)
result.messages.push(`Prosody modules path will be '${wantedConfig.paths.modules}'`)
@ -67,7 +74,7 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
return result
}
const versionMatches = about.match(/^Prosody\s*(\d+)\.(\d+)\.(\d+)\s*$/mi)
const versionMatches = about.match(/^Prosody\s*(\d+)\.(\d+)(?:\.(\d+)| (nightly build \d+.*))\s*$/mi)
if (!versionMatches) {
result.messages.push({
level: 'error',
@ -77,7 +84,7 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
} else {
const major = versionMatches[1]
const minor = versionMatches[2]
const patch = versionMatches[3]
const patch = versionMatches[3] ?? versionMatches[4]
result.messages.push(`Prosody version is ${major}.${minor}.${patch}`)
if (major !== '0' && minor !== '11') {
result.messages.push({
@ -87,6 +94,48 @@ export async function diagProsody (test: string, options: RegisterServerOptions)
}
}
try {
const apiUrl = `http://localhost:${prosodyPort}/peertubelivechat_test/test-peertube-prosody`
const testResult = await got(apiUrl, {
method: 'GET',
headers: {
authorization: 'Bearer ' + await getAPIKey(options)
},
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
}
try {
const apiUrl = `http://localhost:${prosodyPort}/peertubelivechat_test/test-prosody-peertube`
const testResult = await got(apiUrl, {
method: 'GET',
headers: {
authorization: 'Bearer ' + await getAPIKey(options)
},
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
}
result.ok = true
return result
}

View File

@ -67,6 +67,7 @@ interface ProsodyConfig {
content: string
paths: ProsodyFilePaths
port: string
baseApiUrl: string
}
async function getProsodyConfig (options: RegisterServerOptions): Promise<ProsodyConfig> {
const logger = options.peertubeHelpers.logger
@ -80,11 +81,18 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
const paths = await getProsodyFilePaths(options)
const apikey = await getAPIKey(options)
const baseApiUrl = options.peertubeHelpers.config.getWebserverUrl() +
getBaseRouterRoute(options) +
'api/'
let baseApiUrl = await options.settingsManager.getSetting('prosody-peertube-uri') as string
if (baseApiUrl && !/^https?:\/\/[a-z0-9.-_]+(?::\d+)?$/.test(baseApiUrl)) {
throw new Error('Invalid prosody-peertube-uri')
}
if (!baseApiUrl) {
baseApiUrl = options.peertubeHelpers.config.getWebserverUrl()
}
baseApiUrl += getBaseRouterRoute(options) + 'api/'
const authApiUrl = baseApiUrl + 'user' // FIXME: should be protected by apikey, but mod_auth_http cant handle params
const roomApiUrl = baseApiUrl + 'room?apikey=' + apikey + '&jid={room.jid|jid_node}'
const testApiUrl = baseApiUrl + 'test?apikey=' + apikey
const config = new ProsodyConfigContent(paths, prosodyDomain)
config.useHttpAuthentication(authApiUrl)
@ -98,6 +106,8 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
config.useListRoomsApi(apikey)
config.useTestModule(apikey, testApiUrl)
let logLevel: ProsodyLogLevel | undefined
if (logger.level && (typeof logger.level === 'string')) {
if (logger.level === 'error' || logger.level === 'info' || logger.level === 'debug') {
@ -117,7 +127,8 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
return {
content,
paths,
port
port,
baseApiUrl
}
}

View File

@ -241,6 +241,12 @@ class ProsodyConfigContent {
this.muc.set('peertubelivechat_list_rooms_apikey', apikey)
}
useTestModule (prosodyApikey: string, apiurl: string): void {
this.global.add('modules_enabled', 'http_peertubelivechat_test')
this.global.set('peertubelivechat_test_apikey', prosodyApikey)
this.global.set('peertubelivechat_test_peertube_api_url', apiurl)
}
setLog (level: ProsodyLogLevel, syslog?: ProsodyLogLevel[]): void {
let log = ''
log += 'log = {\n'

View File

@ -34,6 +34,14 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
const router = getRouter()
const logger = peertubeHelpers.logger
router.get('/test', asyncMiddleware([
getCheckAPIKeyMiddleware(options),
async (req: Request, res: Response, _next: NextFunction) => {
logger.info('Test api call')
res.json({ ok: true })
}
]))
router.get('/room', asyncMiddleware([
getCheckAPIKeyMiddleware(options),
async (req: Request, res: Response, _next: NextFunction) => {

View File

@ -110,6 +110,18 @@ Change it if this port is already in use on your server.<br>
You can close this port on your firewall, it will not be accessed from the outer world.`
})
registerSetting({
name: 'prosody-peertube-uri',
label: 'Peertube url for API calls',
type: 'input',
default: '',
private: true,
descriptionHTML:
`Please let this settings empty if you don't know what you are doing.<br>
In some rare case, Prosody can't call Peertube's API from its public URI.
You can use this field to customise Peertube's URI for Prosody modules (for example with «http://localhost:9000»).`
})
registerSetting({
name: 'chat-server',
label: 'XMPP service server',