2023-05-24 13:09:56 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2023-05-24 14:09:55 +00:00
|
|
|
import { hasRemoteServerInfos, storeRemoteServerInfos } from './storage'
|
2023-05-24 13:09:56 +00:00
|
|
|
import { getBaseRouterRoute } from '../helpers'
|
|
|
|
import { canonicalizePluginUri } from '../uri/canonicalize'
|
2023-05-24 14:09:55 +00:00
|
|
|
import { sanitizePeertubeLiveChatServerInfos } from './sanitize'
|
2023-05-24 14:26:57 +00:00
|
|
|
import { debugNumericParameter } from '../debug'
|
2023-05-24 13:09:56 +00:00
|
|
|
import { URL } from 'url'
|
|
|
|
const got = require('got')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns remote server connection informations.
|
|
|
|
* If these informations are not available (because we receive no ActivityPub
|
|
|
|
* data from this remote server), we will fetch them on a dedicated url.
|
|
|
|
*
|
|
|
|
* This information will also be stored.
|
|
|
|
*
|
|
|
|
* For all remote videos that our instance federated, remote server information
|
|
|
|
* are sent using ActivityPub.
|
|
|
|
* But there is a case in which we need information about potentially unknown
|
|
|
|
* servers.
|
|
|
|
*
|
|
|
|
* Use case:
|
|
|
|
* - server A: our server, proposing video V
|
|
|
|
* - server B: server that follows ours (or used to watch V, without following A)
|
|
|
|
* - user from B connect to the B XMPP server
|
|
|
|
* - server B has server A connection informations (got it using ActivityPub)
|
|
|
|
* - but, when using Websocket S2S, server A needs information from B, that he never receives
|
|
|
|
*
|
|
|
|
* Indeed, the XMPP S2S dialback mecanism will try to connect back to
|
|
|
|
* server A, and transmit a secret key, to ensure that all incomming connection
|
|
|
|
* are valid.
|
|
|
|
*
|
|
|
|
* For more informations about dialback: https://xmpp.org/extensions/xep-0220.html
|
|
|
|
*
|
|
|
|
* @param options server options
|
|
|
|
* @param remoteInstanceUrl remote instance url to check (as readed in the request header)
|
|
|
|
* @returns true if the remote instance is ok
|
|
|
|
*/
|
2023-05-24 14:09:55 +00:00
|
|
|
async function fetchMissingRemoteServerInfos (
|
2023-05-24 13:09:56 +00:00
|
|
|
options: RegisterServerOptions,
|
|
|
|
remoteInstanceUrl: string
|
2023-05-24 14:09:55 +00:00
|
|
|
): Promise<void> {
|
2023-05-24 13:09:56 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
logger.debug(`remoteServerInfos: checking if we have remote server infos for host ${remoteInstanceUrl}.`)
|
|
|
|
|
2023-05-24 14:26:57 +00:00
|
|
|
// maxAge: max allowed aged for stored remote server infos (in milliseconds).
|
|
|
|
// In production: 24 hours
|
|
|
|
// In debug mode: 1hour
|
|
|
|
const maxAge = debugNumericParameter(options, 'remoteServerInfosMaxAge', 3600000, 3600 * 1000 * 24)
|
|
|
|
if (await hasRemoteServerInfos(options, remoteInstanceUrl, maxAge)) {
|
2023-05-24 14:09:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
let url: string
|
|
|
|
try {
|
|
|
|
const u = new URL(remoteInstanceUrl)
|
|
|
|
|
|
|
|
// Assuming that the path on the remote instance is the same as on this one
|
|
|
|
// (but canonicalized to remove the plugin version)
|
|
|
|
u.pathname = getBaseRouterRoute(options) + 'api/federation_server_infos'
|
|
|
|
url = canonicalizePluginUri(options, u.toString(), {
|
|
|
|
protocol: 'http',
|
|
|
|
removePluginVersion: true
|
|
|
|
})
|
|
|
|
} catch (_err) {
|
|
|
|
logger.info('remoteServerInfos: Invalid remote instance url provided: ' + remoteInstanceUrl)
|
2023-05-24 14:09:55 +00:00
|
|
|
return
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
logger.debug('remoteServerInfos: We must check remote server infos using url: ' + url)
|
|
|
|
const response = await got(url, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {},
|
|
|
|
responseType: 'json'
|
|
|
|
}).json()
|
|
|
|
|
|
|
|
if (!response) {
|
|
|
|
logger.info('remoteServerInfos: Invalid remote server options')
|
2023-05-24 14:09:55 +00:00
|
|
|
return
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:19:45 +00:00
|
|
|
const serverInfos = sanitizePeertubeLiveChatServerInfos(options, response, remoteInstanceUrl)
|
2023-05-24 14:09:55 +00:00
|
|
|
if (serverInfos) {
|
|
|
|
await storeRemoteServerInfos(options, serverInfos)
|
|
|
|
}
|
2023-05-24 13:09:56 +00:00
|
|
|
} catch (_err) {
|
|
|
|
logger.info('remoteServerInfos: Can\'t get remote instance informations using url ' + url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2023-05-24 14:09:55 +00:00
|
|
|
fetchMissingRemoteServerInfos
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|