From a526feac1950fc56aa8fac0dd9ced4cc89a4e498 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Tue, 22 Jun 2021 12:57:24 +0200 Subject: [PATCH] Diagnostic tool: testing API communication from Prosody to Peertube. --- CHANGELOG.md | 3 +- .../mod_http_peertubelivechat_test.lua | 50 +++++++++++++++++-- server/lib/diagnostic/prosody.ts | 21 ++++++++ server/lib/prosody/config.ts | 3 +- server/lib/prosody/config/content.ts | 5 +- server/lib/routers/api.ts | 8 +++ 6 files changed, 83 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8c4a5d2..0dbc3715 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ ### Features -* Diagnostic tool: testing API communication between Peertube and Prosody. +* Diagnostic tool: testing API communication from Peertube to Prosody. +* Diagnostic tool: testing API communication from Prosody to Peertube. ## v3.0.0 diff --git a/prosody-modules/mod_http_peertubelivechat_test/mod_http_peertubelivechat_test.lua b/prosody-modules/mod_http_peertubelivechat_test/mod_http_peertubelivechat_test.lua index 52a6748b..dd4d0576 100644 --- a/prosody-modules/mod_http_peertubelivechat_test/mod_http_peertubelivechat_test.lua +++ b/prosody-modules/mod_http_peertubelivechat_test/mod_http_peertubelivechat_test.lua @@ -1,10 +1,20 @@ local json = require "util.json"; +local async = require "util.async"; +local http = require "net.http"; module:depends"http"; -function check_auth(routes) +local apikey = assert(module:get_option_string("peertubelivechat_test_apikey", nil), "'peertubelivechat_test_apikey' is a required option"); +local peertube_url = assert(module:get_option_string("peertubelivechat_test_peertube_api_url", nil), "'peertubelivechat_test_peertube_api_url' is a required option"); + +local ex = { + headers = { + accept = "application/json"; + } +}; + +local function check_auth(routes) local function check_request_auth(event) - local apikey = module:get_option_string("peertubelivechat_test_apikey", "") if apikey == "" then return false, 500; end @@ -35,9 +45,43 @@ local function test_peertube_prosody(event) return json.encode(json_response); end +local function test_prosody_peertube(event) + local request, response = event.request, event.response; + + local ret, err; + http.request(peertube_url, ex, function (body, code) + if math.floor(code / 100) == 2 then + local parsed, parse_err = json.decode(body); + if not parsed then + module:log("debug", "Got invalid JSON from %s: %s", peertube_url, parse_err); + err = problems.format; + else + ret = parsed; + end + else + module:log("debug", "Rejected by API: ", body); + err = "Rejected by API"; + end + + local json_response; + if not ret then + json_response = { + ok = false; + error = err; + }; + else + json_response = ret; + end + response:send(json.encode(json_response)); + end); + + event.response.headers["Content-Type"] = "application/json"; + return true +end + module:provides("http", { route = check_auth { ["GET /test-peertube-prosody"] = test_peertube_prosody; - -- ["GET /test-prosody-peertube"] = test_prosody_peertube; + ["GET /test-prosody-peertube"] = test_prosody_peertube; }; }); diff --git a/server/lib/diagnostic/prosody.ts b/server/lib/diagnostic/prosody.ts index c1895696..1cb2bea3 100644 --- a/server/lib/diagnostic/prosody.ts +++ b/server/lib/diagnostic/prosody.ts @@ -113,6 +113,27 @@ export async function diagProsody (test: string, options: RegisterServerOptions) 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 } diff --git a/server/lib/prosody/config.ts b/server/lib/prosody/config.ts index f58b0259..5ee2afc1 100644 --- a/server/lib/prosody/config.ts +++ b/server/lib/prosody/config.ts @@ -85,6 +85,7 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise { 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) => {