Implemented basic compliance with XEP-0156 to discover the websocket connection on a properly configured XMPP server

This commit is contained in:
Jakub Filo 2023-04-30 16:50:04 +00:00
parent b04ba0b100
commit 3b3b692b55

View File

@ -15,6 +15,7 @@ import { Log, IRemoteRoom } from "mx-puppet-bridge";
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { client, xml } from "@xmpp/client"; import { client, xml } from "@xmpp/client";
import { Client as XmppClient } from "@xmpp/client-core"; import { Client as XmppClient } from "@xmpp/client-core";
import * as Parser from "node-html-parser";
const log = new Log("XmppPuppet:client"); const log = new Log("XmppPuppet:client");
@ -51,21 +52,32 @@ export class Client extends EventEmitter {
return this.loginUsername.split("@")[0].trim(); return this.loginUsername.split("@")[0].trim();
} }
public get host(): string { public get domain(): string {
return this.loginUsername.split("@")[1].trim(); return this.loginUsername.split("@")[1].trim();
} }
public async getWebsocket(): Promise<string> {
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() { public get getState() {
return {}; return {};
} }
public async connect() { 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.username);
log.info(this.domain);
this.api = client({ this.api = client({
service: "ws://"+this.host+":5280/xmpp-websocket", service: websocketUrl,
domain: this.host, domain: this.domain,
resource: "mx_bridge", resource: "mx_bridge",
username: this.username, username: this.username,
password: this.password, password: this.password,