From b38d9a64269d6ac976fe9f35e1d3ca0c6109219a Mon Sep 17 00:00:00 2001 From: John Livingston Date: Tue, 23 May 2023 17:54:01 +0200 Subject: [PATCH] Cleaning an aborted PoC --- server/lib/routers/webchat.ts | 12 --- server/lib/xmpp-ws-proxy/check-remote.ts | 73 -------------- server/lib/xmpp-ws-proxy/server.ts | 119 ----------------------- 3 files changed, 204 deletions(-) delete mode 100644 server/lib/xmpp-ws-proxy/check-remote.ts delete mode 100644 server/lib/xmpp-ws-proxy/server.ts diff --git a/server/lib/routers/webchat.ts b/server/lib/routers/webchat.ts index dae3a2c8..4727a5fe 100644 --- a/server/lib/routers/webchat.ts +++ b/server/lib/routers/webchat.ts @@ -16,8 +16,6 @@ import { getBoshUri, getWSUri } from '../uri/webchat' import { getVideoLiveChatInfos } from '../federation/storage' import { LiveChatJSONLDAttribute } from '../federation/types' import { anonymousConnectionInfos, remoteAuthenticatedConnectionEnabled } from '../federation/connection-infos' -// import { XMPPWsProxyServer } from '../xmpp-ws-proxy/server' -// import { checkRemote } from '../xmpp-ws-proxy/check-remote' import * as path from 'path' const got = require('got') @@ -282,16 +280,6 @@ async function initWebchatRouter (options: RegisterServerOptionsV5): Promise { - // const remoteInstanceUrl = request.headers['peertube-livechat-ws-proxy-instance-url'] - // if (!await checkRemote(options, remoteInstanceUrl)) { - // return - // } - // XMPPWsProxyServer.singleton(options).handleUpgrade(request, socket, head) - // } - // }) } router.get('/prosody-list-rooms', asyncMiddleware( diff --git a/server/lib/xmpp-ws-proxy/check-remote.ts b/server/lib/xmpp-ws-proxy/check-remote.ts deleted file mode 100644 index 27f14279..00000000 --- a/server/lib/xmpp-ws-proxy/check-remote.ts +++ /dev/null @@ -1,73 +0,0 @@ -import type { RegisterServerOptions } from '@peertube/peertube-types' -// import { getBaseRouterRoute } from '../helpers' -// import { canonicalizePluginUri } from '../uri/canonicalize' -// import { URL } from 'url' -// const got = require('got') - -/** - * FIXME: this method should not be necessary anymore, it was a proof of concept. - * - * This function checks that there is a valid Peertube instance behind - * the remote url, to avoid spoofing. - * It also ensure that we have needed serverInfos for the federation - * (so we can also open outgoing proxyfied connection to that instance) - * @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 - */ -async function checkRemote ( - _options: RegisterServerOptions, - _remoteInstanceUrl: any -): Promise { - throw new Error('Not Implemented Yet') - - // const logger = options.peertubeHelpers.logger - // if (typeof remoteInstanceUrl !== 'string') { - // logger.info('WS-Proxy-Check: Received invalid request on xmpp-websocket-proxy: invalid remoteInstanceUrl header') - // return false - // } - // logger.debug( - // `WS-Proxy-Check: Receiving request on xmpp-websocket-proxy for host ${remoteInstanceUrl}, ` + - // 'checking the host is a valid Peertube server' - // ) - // 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('WS-Proxy-Check: Invalid remote instance url provided: ' + remoteInstanceUrl) - // return false - // } - - // try { - // logger.debug('WS-Proxy-Check: We must check remote server infos using url: ' + url) - // const response = await got(url, { - // method: 'GET', - // headers: {}, - // responseType: 'json' - // }).json() - - // if (!response) { - // logger.info('WS-Proxy-Check: Invalid remote server options') - // return false - // } - - // // FIXME/TODO - - // return true - // } catch (_err) { - // logger.info('WS-Proxy-Check: Can\'t get remote instance informations using url ' + url) - // return false - // } -} - -export { - checkRemote -} diff --git a/server/lib/xmpp-ws-proxy/server.ts b/server/lib/xmpp-ws-proxy/server.ts deleted file mode 100644 index f29130dd..00000000 --- a/server/lib/xmpp-ws-proxy/server.ts +++ /dev/null @@ -1,119 +0,0 @@ -// import type { RegisterServerOptions } from '@peertube/peertube-types' -// import type { IncomingMessage } from 'http' -// import type { Duplex } from 'stream' -// import { WebSocketServer, WebSocket } from 'ws' -// import { Socket } from 'net' - -// FIXME: this method should not be necessary anymore, it was a proof of concept. - -// interface ProxyLogger { -// debug: (s: string) => void -// info: (s: string) => void -// error: (s: string) => void -// } - -// let xmppWsProxyServer: XMPPWsProxyServer | undefined -// class XMPPWsProxyServer { -// private readonly logger: ProxyLogger -// private readonly options: RegisterServerOptions -// private readonly wsProxyServer: WebSocketServer -// private prosodyPort: number | undefined -// private readonly connections: Map = new Map() - -// constructor (options: RegisterServerOptions) { -// const logger = options.peertubeHelpers.logger -// this.logger = { -// debug: s => logger.debug('XMPP-WS-PROXY: ' + s), -// info: s => logger.info('XMPP-WS-PROXY: ' + s), -// error: s => logger.error('XMPP-WS-PROXY: ' + s) -// } -// this.options = options - -// this.wsProxyServer = new WebSocketServer({ noServer: true, perMessageDeflate: false }) -// } - -// public handleUpgrade (request: IncomingMessage, incomingSocket: Duplex, head: Buffer): void { -// this.wsProxyServer.handleUpgrade(request, incomingSocket, head, ws => { -// this.handleUpgradeCallback(ws).then(() => {}, () => {}) -// }) -// } - -// public async handleUpgradeCallback (ws: WebSocket): Promise { -// this.logger.debug('Opening a Websocket Proxy connection') - -// const port = await this.getProsodyPort() -// if (!port) { -// this.logger.error('No port configured for Prosody, closing the websocket stream') -// ws.close() // FIXME: add a code and error message -// return -// } - -// // Opening a tcp connection to local Prosody: -// const prosodySocket = new Socket() -// this.connections.set(ws, prosodySocket) -// prosodySocket.connect(port, 'localhost', () => { -// // TODO: write the remote IP in the header line. -// prosodySocket.write('LIVECHAT-WS-PROXY\n') -// }) -// prosodySocket.on('close', () => { -// ws.close() -// }) -// prosodySocket.on('data', (data) => { -// ws.send(data) -// }) - -// ws.on('message', (data) => { -// // TODO: remove this log -// this.logger.debug('Receiving raw data') -// if (Array.isArray(data)) { -// data.forEach(chunck => { -// prosodySocket.write(chunck) -// }) -// } else if (data instanceof ArrayBuffer) { -// prosodySocket.write(Buffer.from(data)) -// } else { -// prosodySocket.write(data) -// } -// }) -// ws.on('close', () => { -// this.logger.debug('Websocket connection is closed, closing socket') -// prosodySocket.end() -// }) -// } - -// private async getProsodyPort (): Promise { -// if (this.prosodyPort) { -// return this.prosodyPort -// } -// const port = await this.options.settingsManager.getSetting('prosody-port') as string -// this.prosodyPort = parseInt(port) -// return this.prosodyPort -// } - -// private async closeConnections (): Promise { -// this.logger.debug('Closing XMPPWsProxyServer connections...') -// this.connections.forEach((socket, _ws) => { -// socket.end() -// // ws.terminate() // not necessary, socket close event should be called -// }) -// // FIXME: wait for all connections to be closed... -// } - -// static singleton (options: RegisterServerOptions): XMPPWsProxyServer { -// if (!xmppWsProxyServer) { -// xmppWsProxyServer = new XMPPWsProxyServer(options) -// } -// return xmppWsProxyServer -// } - -// static async destroySingleton (): Promise { -// if (!xmppWsProxyServer) { return } -// const server = xmppWsProxyServer -// xmppWsProxyServer = undefined -// await server.closeConnections() -// } -// } - -// export { -// XMPPWsProxyServer -// }