From 3b3b692b55c7f434a4f4f893a390a51ff9007601 Mon Sep 17 00:00:00 2001 From: Jakub Filo Date: Sun, 30 Apr 2023 16:50:04 +0000 Subject: [PATCH] Implemented basic compliance with XEP-0156 to discover the websocket connection on a properly configured XMPP server --- src/client.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/client.ts b/src/client.ts index 0b79aab..88de1ae 100644 --- a/src/client.ts +++ b/src/client.ts @@ -15,6 +15,7 @@ import { Log, IRemoteRoom } from "mx-puppet-bridge"; import { EventEmitter } from "events"; import { client, xml } from "@xmpp/client"; import { Client as XmppClient } from "@xmpp/client-core"; +import * as Parser from "node-html-parser"; const log = new Log("XmppPuppet:client"); @@ -51,21 +52,32 @@ export class Client extends EventEmitter { return this.loginUsername.split("@")[0].trim(); } - public get host(): string { + public get domain(): string { return this.loginUsername.split("@")[1].trim(); } + public async getWebsocket(): Promise { + const response = await fetch(`https://${this.domain}/.well-known/host-meta`); + const xmlData = await response.text(); + const document = Parser.parse(xmlData) as unknown as HTMLElement; + const relValue = "urn:xmpp:alt-connections:websocket" + const line = document.querySelectorAll(`[rel="${relValue}"]`); + return line[0].getAttribute('href') as string; + } + public get getState() { return {}; } public async connect() { - log.info("Connecting to ", this.host); + const websocketUrl = await this.getWebsocket(); + log.info("Connecting to ", websocketUrl); log.info(this.username); + log.info(this.domain); this.api = client({ - service: "ws://"+this.host+":5280/xmpp-websocket", - domain: this.host, + service: websocketUrl, + domain: this.domain, resource: "mx_bridge", username: this.username, password: this.password,