-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
126 lines (98 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const dns = require('dns');
const axios = require('axios').default;
const parseXmlString = require('xml2js').parseString;
const XRD_NAMESPACE = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
/**
* Query SRV records according to {@link https://tools.ietf.org/html/rfc6120#section-3.2.1}.
*/
function querySRV(domain) {
return new Promise((resolve) => {
dns.resolveSrv(`_xmpp-client._tcp.${domain}`, (err, records) => {
if (err || !records) return resolve([]);
let services = records.map((record) => {
return {
server: record.name,
port: record.port,
protocol: 'tcp'
};
});
return resolve(services);
});
});
}
/**
* Query TXT records according to {@link https://xmpp.org/extensions/xep-0156.html#dns}.
*/
function queryTXT(domain) {
return new Promise((resolve) => {
dns.resolveTxt(`_xmppconnect.${domain}`, (err, records) => {
if (err || !records) return resolve([]);
let services = [];
for (let record of records) {
for (let entry of record) {
let matches = entry.match(/^_xmpp-client-(xbosh|websocket)=((wss|https):\/\/.+)/);
if (matches !== null && matches.length > 2) {
services.push({
server: matches[2],
protocol: matches[1]
});
}
}
}
return resolve(services);
});
});
}
/**
* Query Web Host Metadata according to {@link https://xmpp.org/extensions/xep-0156.html#http}.
*/
async function queryHostMeta(domain) {
let response;
try {
response = await axios(`https://${domain}/.well-known/host-meta`);
} catch(err) {
return [];
}
if (!response || response.status !== 200 || !response.data) {
return [];
}
return new Promise(resolve => {
parseXmlString(response.data, (err, result) => {
if (!result) return resolve([]);
let XRD = result.XRD;
if (!XRD || !XRD.$ || XRD.$.xmlns !== XRD_NAMESPACE) return resolve([]);
if (!XRD.Link) return resolve([]);
let services = [];
for (let link of XRD.Link) {
if (!link.$ || !link.$.href || !link.$.rel) continue;
let href = link.$.href;
let rel = link.$.rel;
let matches = rel.match(/^urn:xmpp:alt-connections:(xbosh|websocket)$/);
if (!matches) continue;
services.push({
server: href,
protocol: matches[1]
});
}
return resolve(services);
});
});
}
function discoverServices(domain) {
return Promise.all([
querySRV(domain),
queryTXT(domain),
queryHostMeta(domain)
]).then((results) => {
let services = results.reduce((acc, curr) => (acc || []).concat(curr));
let indexed = {};
for (let service of services) {
let protocol = service.protocol;
delete service.protocol;
if (!indexed[protocol]) indexed[protocol] = [];
indexed[protocol].push(service);
}
return indexed;
});
}
module.exports = discoverServices;