Refresh remote server infos when outdated.
This commit is contained in:
parent
e719dc3079
commit
2961513d22
@ -13,6 +13,7 @@ interface DebugContent {
|
|||||||
renewSelfSignedCertInterval?: number
|
renewSelfSignedCertInterval?: number
|
||||||
logRotateCheckInterval?: number
|
logRotateCheckInterval?: number
|
||||||
logRotateEvery?: number
|
logRotateEvery?: number
|
||||||
|
remoteServerInfosMaxAge?: number
|
||||||
prosodyDebuggerOptions?: ProsodyDebuggerOptions
|
prosodyDebuggerOptions?: ProsodyDebuggerOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,6 +21,7 @@ type DebugNumericValue = 'renewCertCheckInterval'
|
|||||||
| 'renewSelfSignedCertInterval'
|
| 'renewSelfSignedCertInterval'
|
||||||
| 'logRotateEvery'
|
| 'logRotateEvery'
|
||||||
| 'logRotateCheckInterval'
|
| 'logRotateCheckInterval'
|
||||||
|
| 'remoteServerInfosMaxAge'
|
||||||
|
|
||||||
let debugContent: DebugContent | null | false = null
|
let debugContent: DebugContent | null | false = null
|
||||||
function _readDebugFile (options: RegisterServerOptions): DebugContent | false {
|
function _readDebugFile (options: RegisterServerOptions): DebugContent | false {
|
||||||
@ -52,6 +54,7 @@ function _readDebugFile (options: RegisterServerOptions): DebugContent | false {
|
|||||||
debugContent.logRotateEvery = _getNumericOptions(options, json, 'log_rotate_every')
|
debugContent.logRotateEvery = _getNumericOptions(options, json, 'log_rotate_every')
|
||||||
debugContent.renewCertCheckInterval = _getNumericOptions(options, json, 'renew_cert_check_interval')
|
debugContent.renewCertCheckInterval = _getNumericOptions(options, json, 'renew_cert_check_interval')
|
||||||
debugContent.renewSelfSignedCertInterval = _getNumericOptions(options, json, 'renew_self_signed_cert_interval')
|
debugContent.renewSelfSignedCertInterval = _getNumericOptions(options, json, 'renew_self_signed_cert_interval')
|
||||||
|
debugContent.remoteServerInfosMaxAge = _getNumericOptions(options, json, 'remote_server_infos_max_age')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('Failed to read the debug_mode file content:', err)
|
logger.error('Failed to read the debug_mode file content:', err)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import { hasRemoteServerInfos, storeRemoteServerInfos } from './storage'
|
|||||||
import { getBaseRouterRoute } from '../helpers'
|
import { getBaseRouterRoute } from '../helpers'
|
||||||
import { canonicalizePluginUri } from '../uri/canonicalize'
|
import { canonicalizePluginUri } from '../uri/canonicalize'
|
||||||
import { sanitizePeertubeLiveChatServerInfos } from './sanitize'
|
import { sanitizePeertubeLiveChatServerInfos } from './sanitize'
|
||||||
|
import { debugNumericParameter } from '../debug'
|
||||||
import { URL } from 'url'
|
import { URL } from 'url'
|
||||||
const got = require('got')
|
const got = require('got')
|
||||||
|
|
||||||
@ -42,8 +43,11 @@ async function fetchMissingRemoteServerInfos (
|
|||||||
const logger = options.peertubeHelpers.logger
|
const logger = options.peertubeHelpers.logger
|
||||||
logger.debug(`remoteServerInfos: checking if we have remote server infos for host ${remoteInstanceUrl}.`)
|
logger.debug(`remoteServerInfos: checking if we have remote server infos for host ${remoteInstanceUrl}.`)
|
||||||
|
|
||||||
// FIXME: add a max age.
|
// maxAge: max allowed aged for stored remote server infos (in milliseconds).
|
||||||
if (await hasRemoteServerInfos(options, remoteInstanceUrl)) {
|
// In production: 24 hours
|
||||||
|
// In debug mode: 1hour
|
||||||
|
const maxAge = debugNumericParameter(options, 'remoteServerInfosMaxAge', 3600000, 3600 * 1000 * 24)
|
||||||
|
if (await hasRemoteServerInfos(options, remoteInstanceUrl, maxAge)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,8 +171,13 @@ async function storeRemoteServerInfos (
|
|||||||
* Indicate if we have the remote hosts informations.
|
* Indicate if we have the remote hosts informations.
|
||||||
* @param options server options
|
* @param options server options
|
||||||
* @param host host domain
|
* @param host host domain
|
||||||
|
* @param maxAge if given, the max age (in milliseconds) allowed for remote server informations
|
||||||
*/
|
*/
|
||||||
async function hasRemoteServerInfos (options: RegisterServerOptions, hostParam: any): Promise<boolean> {
|
async function hasRemoteServerInfos (
|
||||||
|
options: RegisterServerOptions,
|
||||||
|
hostParam: any,
|
||||||
|
maxAge?: number
|
||||||
|
): Promise<boolean> {
|
||||||
const host = sanitizeXMPPHostFromInstanceUrl(options, hostParam)
|
const host = sanitizeXMPPHostFromInstanceUrl(options, hostParam)
|
||||||
if (!host) {
|
if (!host) {
|
||||||
return false
|
return false
|
||||||
@ -187,7 +192,36 @@ async function hasRemoteServerInfos (options: RegisterServerOptions, hostParam:
|
|||||||
host,
|
host,
|
||||||
'last-update'
|
'last-update'
|
||||||
)
|
)
|
||||||
return fs.existsSync(filePath)
|
if (!fs.existsSync(filePath)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (maxAge === undefined) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// We must check the 'last-update' to be newer than maxAge
|
||||||
|
try {
|
||||||
|
const content = await fs.promises.readFile(filePath, {
|
||||||
|
encoding: 'utf-8'
|
||||||
|
})
|
||||||
|
const json = JSON.parse(content)
|
||||||
|
if (!json) { return false }
|
||||||
|
if (typeof json !== 'object') { return false }
|
||||||
|
if (!json.timestamp) { return false }
|
||||||
|
if ((typeof json.timestamp) !== 'number') { return false }
|
||||||
|
const now = (new Date()).getTime()
|
||||||
|
if (now - (json.timestamp as number) > maxAge) {
|
||||||
|
options.peertubeHelpers.logger.info(
|
||||||
|
`Remote informations for server ${host} are outdated.`
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
options.peertubeHelpers.logger.error('Failed reading the last-update file:', err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _getFilePath (
|
async function _getFilePath (
|
||||||
|
Loading…
Reference in New Issue
Block a user