Lookup SRV records when trying to get open ID information

Fixes #58
This commit is contained in:
turt2live 2017-08-26 23:49:27 -06:00
parent 2f41826fd5
commit 7cee8c99c4
4 changed files with 54 additions and 16 deletions

23
package-lock.json generated
View File

@ -1968,6 +1968,14 @@
"safe-buffer": "5.1.1"
}
},
"dns-then": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/dns-then/-/dns-then-0.1.0.tgz",
"integrity": "sha1-xXOo7RA8p0kuBwUHy/qyexXqsfQ=",
"requires": {
"node-then": "0.1.0"
}
},
"dns-txt": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/dns-txt/-/dns-txt-2.0.2.tgz",
@ -4616,6 +4624,21 @@
}
}
},
"node-then": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/node-then/-/node-then-0.1.0.tgz",
"integrity": "sha1-rxqJUSyuZwCCI48fktbLgLdflNI=",
"requires": {
"when": "2.2.1"
},
"dependencies": {
"when": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/when/-/when-2.2.1.tgz",
"integrity": "sha1-sd75lAFzULgIf26adZarKDO9xxI="
}
}
},
"nopt": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz",

View File

@ -19,11 +19,13 @@
},
"author": "Travis Ralston",
"dependencies": {
"bluebird": "^3.5.0",
"body-parser": "^1.17.2",
"chalk": "^2.1.0",
"config": "^1.26.2",
"db-migrate": "^0.10.0-beta.23",
"db-migrate-sqlite3": "^0.2.1",
"dns-then": "^0.1.0",
"express": "^4.15.4",
"js-yaml": "^3.9.1",
"lodash": "^4.17.4",

View File

@ -3,6 +3,7 @@ var randomString = require("random-string");
var ScalarClient = require("./scalar/ScalarClient.js");
var _ = require("lodash");
var log = require("./util/LogService");
var Promise = require("bluebird");
/**
* API handler for the Scalar API, as required by Riot

View File

@ -1,5 +1,7 @@
var request = require('request');
var log = require("../util/LogService");
var dns = require("dns-then");
var Promise = require("bluebird");
/**
* Represents a lightweight matrix client with minimal functionality
@ -26,26 +28,36 @@ class MatrixLiteClient {
}
_do(method, endpoint, qs = null, body = null) {
var url = "http://" + this._openId.matrix_server_name + endpoint;
// HACK: We have to wrap the dns promise in a Bluebird promise just to make sure it works
var dnsPromise = dns.resolveSrv("_matrix._tcp." + this._openId.matrix_server_name);
return Promise.resolve(dnsPromise).then(records => {
if (records && records.length > 0)
this._openId.matrix_server_name = records[0].name + ":" + records[0].port;
}, err => {
log.warn("MatrixLiteClient", "Failed to lookup SRV for " + this._openId.matrix_server_name + " - assuming none available.");
log.warn("MatrixLiteClient", err);
}).then(() => {
var url = "http://" + this._openId.matrix_server_name + endpoint;
log.verbose("MatrixLiteClient", "Performing request: " + url);
log.verbose("MatrixLiteClient", "Performing request: " + url);
if (!qs) qs = {};
qs['access_token'] = this._openId.access_token;
if (!qs) qs = {};
qs['access_token'] = this._openId.access_token;
var params = {
url: url,
method: method,
form: body,
qs: qs
};
var params = {
url: url,
method: method,
form: body,
qs: qs
};
return new Promise((resolve, reject) => {
request(params, (err, response, body) => {
if (err) {
log.error("MatrixLiteClient", err);
reject(err);
} else resolve(response, body);
return new Promise((resolve, reject) => {
request(params, (err, response, body) => {
if (err) {
log.error("MatrixLiteClient", err);
reject(err);
} else resolve(response, body);
});
});
});
}