diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-03-23 10:22:26 +0100 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2020-03-25 22:35:23 +0100 |
commit | b4748f1a31808d21aeb359b21aedd3b483b535d1 (patch) | |
tree | f8f92b2cab7154c6f42d0cf53d643fdecf29d455 /lib | |
parent | dc53839b2afd2e1ee5af882924a1c0a5b553da4e (diff) | |
download | libquotient-b4748f1a31808d21aeb359b21aedd3b483b535d1.tar.gz libquotient-b4748f1a31808d21aeb359b21aedd3b483b535d1.zip |
.well-known support
Squashed cherry-pick of #330.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/connection.cpp | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index ac69228b..ddeaa883 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -32,12 +32,13 @@ #include "csapi/joining.h" #include "csapi/to_device.h" #include "csapi/room_send.h" +#include "csapi/wellknown.h" +#include "csapi/versions.h" #include "jobs/syncjob.h" #include "jobs/mediathumbnailjob.h" #include "jobs/downloadfilejob.h" #include "csapi/voip.h" -#include <QtNetwork/QDnsLookup> #include <QtCore/QFile> #include <QtCore/QDir> #include <QtCore/QStandardPaths> @@ -177,37 +178,45 @@ void Connection::resolveServer(const QString& mxidOrDomain) } setHomeserver(maybeBaseUrl); - emit resolved(); - return; - // FIXME, #178: The below code is incorrect and is no more executed. The - // correct server resolution should be done from .well-known/matrix/client auto domain = maybeBaseUrl.host(); qCDebug(MAIN) << "Finding the server" << domain; - // Check if the Matrix server has a dedicated service record. - auto* dns = new QDnsLookup(); - dns->setType(QDnsLookup::SRV); - dns->setName("_matrix._tcp." + domain); - - connect(dns, &QDnsLookup::finished, [this,dns,maybeBaseUrl]() { - QUrl baseUrl { maybeBaseUrl }; - if (dns->error() == QDnsLookup::NoError && - dns->serviceRecords().isEmpty()) - { - auto record = dns->serviceRecords().front(); - baseUrl.setHost(record.target()); - baseUrl.setPort(record.port()); - qCDebug(MAIN) << "SRV record for" << maybeBaseUrl.host() - << "is" << baseUrl.authority(); + + auto getWellKnownJob = callApi<GetWellknownJob>(); + connect(getWellKnownJob, &BaseJob::finished, [this, getWellKnownJob, maybeBaseUrl] { + if (getWellKnownJob->status() == BaseJob::NotFoundError) { + qCDebug(MAIN) << "No .well-known file, IGNORE"; + } else if (getWellKnownJob->status() != BaseJob::Success) { + qCDebug(MAIN) << "Fetching .well-known file failed, FAIL_PROMPT"; + emit resolveError(tr("Fetching .well-known file failed")); + return; + } else if (getWellKnownJob->data().homeserver.baseUrl.isEmpty()) { + qCDebug(MAIN) << "base_url not provided, FAIL_PROMPT"; + emit resolveError(tr("base_url not provided")); + return; + } else if (!QUrl(getWellKnownJob->data().homeserver.baseUrl).isValid()) { + qCDebug(MAIN) << "base_url invalid, FAIL_ERROR"; + emit resolveError(tr("base_url invalid")); + return; } else { - qCDebug(MAIN) << baseUrl.host() << "doesn't have SRV record" - << dns->name() << "- using the hostname as is"; + QUrl baseUrl(getWellKnownJob->data().homeserver.baseUrl); + + qCDebug(MAIN) << ".well-known for" << maybeBaseUrl.host() << "is" << baseUrl.authority(); + setHomeserver(baseUrl); } - setHomeserver(baseUrl); - emit resolved(); - dns->deleteLater(); + + auto getVersionsJob = callApi<GetVersionsJob>(); + + connect(getVersionsJob, &BaseJob::finished, [this, getVersionsJob] { + if (getVersionsJob->status() == BaseJob::Success) { + qCDebug(MAIN) << "homeserver url is valid"; + emit resolved(); + } else { + qCDebug(MAIN) << "homeserver url invalid"; + emit resolveError(tr("homeserver url invalid")); + } + }); }); - dns->lookup(); } void Connection::connectToServer(const QString& user, const QString& password, |