2023-05-24 13:09:56 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2023-05-24 14:09:55 +00:00
|
|
|
import type { LiveChatJSONLDAttributeV1, PeertubeXMPPServerInfos } from './types'
|
2023-04-20 16:28:08 +00:00
|
|
|
import { URL } from 'url'
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
/**
|
|
|
|
* Use this function for incoming remote informations.
|
|
|
|
* It will sanitize them, by checking everything is ok.
|
|
|
|
* It can also migrate from old format to the new one.
|
|
|
|
*
|
|
|
|
* This function can be used when informations are incoming,
|
|
|
|
* or when reading stored information (to automatically migrate them)
|
|
|
|
*
|
|
|
|
* @param chatInfos remote chat informations
|
|
|
|
* @returns a sanitized version of the remote chat informations
|
|
|
|
*/
|
|
|
|
function sanitizePeertubeLiveChatInfos (options: RegisterServerOptions, chatInfos: any): LiveChatJSONLDAttributeV1 {
|
2023-04-20 16:28:08 +00:00
|
|
|
if (chatInfos === false) { return false }
|
|
|
|
if (typeof chatInfos !== 'object') { return false }
|
|
|
|
|
|
|
|
if (chatInfos.type !== 'xmpp') { return false }
|
|
|
|
if ((typeof chatInfos.jid) !== 'string') { return false }
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!('xmppserver' in chatInfos)) {
|
|
|
|
// V0 format, migrating on the fly to v1.
|
|
|
|
return _sanitizePeertubeLiveChatInfosV0(options, chatInfos)
|
2023-04-20 16:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!chatInfos.xmppserver || (typeof chatInfos.xmppserver !== 'object')) {
|
|
|
|
return false
|
|
|
|
}
|
2023-05-24 14:09:55 +00:00
|
|
|
const xmppserver = sanitizePeertubeLiveChatServerInfos(options, chatInfos.xmppserver)
|
|
|
|
if (!xmppserver) { return false }
|
|
|
|
|
|
|
|
const r: LiveChatJSONLDAttributeV1 = {
|
|
|
|
type: chatInfos.type,
|
|
|
|
jid: chatInfos.jid,
|
|
|
|
xmppserver
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitizePeertubeLiveChatServerInfos (
|
|
|
|
options: RegisterServerOptions, xmppserver: any
|
|
|
|
): PeertubeXMPPServerInfos | false {
|
|
|
|
if (!xmppserver || (typeof xmppserver !== 'object')) {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-20 16:28:08 +00:00
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
if ((typeof xmppserver.host) !== 'string') { return false }
|
|
|
|
const host = _validateHost(xmppserver.host)
|
|
|
|
if (!host) { return false }
|
|
|
|
if ((typeof xmppserver.muc) !== 'string') { return false }
|
|
|
|
const muc = _validateHost(xmppserver.muc)
|
|
|
|
if (!muc) { return false }
|
2023-04-20 16:28:08 +00:00
|
|
|
|
2023-05-24 14:09:55 +00:00
|
|
|
const r: PeertubeXMPPServerInfos = {
|
|
|
|
host,
|
|
|
|
muc
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (xmppserver.directs2s) {
|
|
|
|
if ((typeof xmppserver.directs2s) === 'object') {
|
|
|
|
const port = xmppserver.directs2s.port
|
|
|
|
if ((typeof port === 'string') && /^\d+$/.test(port)) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.directs2s = {
|
2023-05-24 13:09:56 +00:00
|
|
|
port
|
|
|
|
}
|
2023-05-19 10:52:52 +00:00
|
|
|
}
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (xmppserver.websockets2s) {
|
|
|
|
if ((typeof xmppserver.websockets2s) === 'object') {
|
|
|
|
const url = xmppserver.websockets2s.url
|
|
|
|
if ((typeof url === 'string') && _validUrl(url, {
|
|
|
|
noSearchParams: true,
|
|
|
|
protocol: 'ws.'
|
|
|
|
})) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.websockets2s = {
|
2023-05-24 13:09:56 +00:00
|
|
|
url
|
|
|
|
}
|
2023-05-19 10:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
if (xmppserver.anonymous) {
|
|
|
|
const virtualhost = _validateHost(xmppserver.anonymous.virtualhost)
|
|
|
|
if (virtualhost) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.anonymous = {
|
2023-05-24 13:09:56 +00:00
|
|
|
virtualhost
|
|
|
|
}
|
2023-05-19 10:52:52 +00:00
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
const bosh = xmppserver.anonymous.bosh
|
|
|
|
if ((typeof bosh === 'string') && _validUrl(bosh, {
|
|
|
|
noSearchParams: true,
|
|
|
|
protocol: 'http.'
|
|
|
|
})) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.anonymous.bosh = bosh
|
2023-05-19 10:52:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
const websocket = xmppserver.anonymous.websocket
|
|
|
|
if ((typeof websocket === 'string') && _validUrl(websocket, {
|
|
|
|
noSearchParams: true,
|
|
|
|
protocol: 'ws.'
|
|
|
|
})) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.anonymous.websocket = websocket
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
2023-05-04 17:14:23 +00:00
|
|
|
}
|
2023-04-20 16:28:08 +00:00
|
|
|
}
|
2023-05-24 13:09:56 +00:00
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
interface URLConstraints {
|
|
|
|
protocol: 'http.' | 'ws.'
|
|
|
|
noSearchParams: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
function _validUrl (s: string, constraints: URLConstraints): boolean {
|
|
|
|
if ((typeof s) !== 'string') { return false }
|
|
|
|
if (s === '') { return false }
|
|
|
|
let url: URL
|
|
|
|
try {
|
|
|
|
url = new URL(s)
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints.protocol) {
|
|
|
|
if (constraints.protocol === 'http.') {
|
2023-04-21 14:33:17 +00:00
|
|
|
if (url.protocol !== 'https:' && url.protocol !== 'http:') {
|
2023-04-20 16:28:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if (constraints.protocol === 'ws.') {
|
2023-04-21 14:33:17 +00:00
|
|
|
if (url.protocol !== 'wss:' && url.protocol !== 'ws:') {
|
2023-04-20 16:28:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints.noSearchParams) {
|
|
|
|
if (url.search !== '') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
function _validateHost (s: any): false | string {
|
2023-05-19 10:52:52 +00:00
|
|
|
try {
|
2023-05-24 13:09:56 +00:00
|
|
|
if (typeof s !== 'string') { return false }
|
2023-05-19 10:52:52 +00:00
|
|
|
if (s.includes('/')) { return false }
|
|
|
|
const url = new URL('http://' + s)
|
|
|
|
return url.hostname
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
function _sanitizePeertubeLiveChatInfosV0 (options: RegisterServerOptions, chatInfos: any): LiveChatJSONLDAttributeV1 {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
logger.debug('We are have to migrate data from the old JSONLD format')
|
|
|
|
|
|
|
|
if (chatInfos === false) { return false }
|
|
|
|
if (typeof chatInfos !== 'object') { return false }
|
|
|
|
|
|
|
|
if (chatInfos.type !== 'xmpp') { return false }
|
|
|
|
if ((typeof chatInfos.jid) !== 'string') { return false }
|
|
|
|
|
|
|
|
// no link? invalid! dropping all.
|
|
|
|
if (!Array.isArray(chatInfos.links)) { return false }
|
|
|
|
|
|
|
|
const muc = _validateHost(chatInfos.jid.split('@')[1])
|
|
|
|
if (!muc) { return false }
|
|
|
|
if (!muc.startsWith('room.')) {
|
|
|
|
logger.error('We expected old format host to begin with "room.". Discarding.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const host = _validateHost(muc.replace(/^room\./, ''))
|
|
|
|
if (!host) { return false }
|
|
|
|
|
|
|
|
const r: LiveChatJSONLDAttributeV1 = {
|
|
|
|
type: chatInfos.type,
|
|
|
|
jid: chatInfos.jid,
|
|
|
|
xmppserver: {
|
|
|
|
host,
|
|
|
|
muc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const link of chatInfos.links) {
|
|
|
|
if ((typeof link) !== 'object') { continue }
|
|
|
|
if (['xmpp-bosh-anonymous', 'xmpp-websocket-anonymous'].includes(link.type)) {
|
|
|
|
if ((typeof link.jid) !== 'string') { continue }
|
|
|
|
if ((typeof link.url) !== 'string') { continue }
|
|
|
|
|
|
|
|
if (
|
|
|
|
!_validUrl(link.url, {
|
|
|
|
noSearchParams: true,
|
|
|
|
protocol: link.type === 'xmpp-websocket-anonymous' ? 'ws.' : 'http.'
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r.xmppserver.anonymous) {
|
|
|
|
r.xmppserver.anonymous = {
|
|
|
|
virtualhost: link.jid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (link.type === 'xmpp-bosh-anonymous') {
|
|
|
|
r.xmppserver.anonymous.bosh = link.url
|
|
|
|
} else if (link.type === 'xmpp-websocket-anonymous') {
|
|
|
|
r.xmppserver.anonymous.websocket = link.url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-05-24 14:09:55 +00:00
|
|
|
function sanitizeXMPPHost (options: RegisterServerOptions, host: any): false | string {
|
|
|
|
return _validateHost(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitizeXMPPHostFromInstanceUrl (_options: RegisterServerOptions, s: any): false | string {
|
|
|
|
try {
|
|
|
|
if (typeof s !== 'string') { return false }
|
|
|
|
const url = new URL(s)
|
|
|
|
return url.hostname
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
export {
|
2023-05-24 14:09:55 +00:00
|
|
|
sanitizePeertubeLiveChatInfos,
|
|
|
|
sanitizePeertubeLiveChatServerInfos,
|
|
|
|
sanitizeXMPPHost,
|
|
|
|
sanitizeXMPPHostFromInstanceUrl
|
2023-04-20 16:28:08 +00:00
|
|
|
}
|