2017-06-26 17:38:16 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-06-26 17:38:16 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-06-26 17:38:16 +00:00
|
|
|
*/
|
|
|
|
#include "mtproto/special_config_request.h"
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
#include "mtproto/details/mtproto_rsa_public_key.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_dc_options.h"
|
2019-11-18 09:28:14 +00:00
|
|
|
#include "mtproto/mtproto_auth_key.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2017-06-26 17:38:16 +00:00
|
|
|
#include "base/openssl_help.h"
|
2019-09-26 10:55:35 +00:00
|
|
|
#include "base/call_delayed.h"
|
2018-06-06 12:16:21 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QJsonDocument>
|
|
|
|
#include <QtCore/QJsonArray>
|
|
|
|
#include <QtCore/QJsonObject>
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace MTP::details {
|
2017-06-26 17:38:16 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-10-01 17:42:18 +00:00
|
|
|
constexpr auto kSendNextTimeout = crl::time(800);
|
2018-05-03 08:11:07 +00:00
|
|
|
|
2020-01-29 09:44:37 +00:00
|
|
|
constexpr auto kPublicKey = "\
|
2017-06-26 17:38:16 +00:00
|
|
|
-----BEGIN RSA PUBLIC KEY-----\n\
|
|
|
|
MIIBCgKCAQEAyr+18Rex2ohtVy8sroGPBwXD3DOoKCSpjDqYoXgCqB7ioln4eDCF\n\
|
|
|
|
fOBUlfXUEvM/fnKCpF46VkAftlb4VuPDeQSS/ZxZYEGqHaywlroVnXHIjgqoxiAd\n\
|
|
|
|
192xRGreuXIaUKmkwlM9JID9WS2jUsTpzQ91L8MEPLJ/4zrBwZua8W5fECwCCh2c\n\
|
|
|
|
9G5IzzBm+otMS/YKwmR1olzRCyEkyAEjXWqBI9Ftv5eG8m0VkBzOG655WIYdyV0H\n\
|
|
|
|
fDK/NWcvGqa0w/nriMD6mDjKOryamw0OP9QuYgMN0C9xMW9y8SmP4h92OAWodTYg\n\
|
|
|
|
Y1hZCxdv6cs5UnW9+PWvS+WIbkh+GaWYxwIDAQAB\n\
|
|
|
|
-----END RSA PUBLIC KEY-----\
|
2020-01-29 09:44:37 +00:00
|
|
|
"_cs;
|
2017-06-26 17:38:16 +00:00
|
|
|
|
2019-10-01 17:42:18 +00:00
|
|
|
const auto kRemoteProject = "peak-vista-421";
|
|
|
|
const auto kFireProject = "reserve-5a846";
|
|
|
|
const auto kConfigKey = "ipconfig";
|
|
|
|
const auto kConfigSubKey = "v3";
|
|
|
|
const auto kApiKey = "AIzaSyC2-kAkpDsroixRXw-sTw-Wfqo4NxjMwwM";
|
|
|
|
const auto kAppId = "1:560508485281:web:4ee13a6af4e84d49e67ae0";
|
|
|
|
|
|
|
|
QString ApiDomain(const QString &service) {
|
|
|
|
return service + ".googleapis.com";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GenerateInstanceId() {
|
|
|
|
auto fid = bytes::array<17>();
|
|
|
|
bytes::set_random(fid);
|
|
|
|
fid[0] = (bytes::type(0xF0) & fid[0]) | bytes::type(0x07);
|
|
|
|
return QString::fromLatin1(
|
|
|
|
QByteArray::fromRawData(
|
|
|
|
reinterpret_cast<const char*>(fid.data()),
|
|
|
|
fid.size()
|
|
|
|
).toBase64(QByteArray::Base64UrlEncoding).mid(0, 22));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString InstanceId() {
|
|
|
|
static const auto result = GenerateInstanceId();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:27:03 +00:00
|
|
|
bool CheckPhoneByPrefixesRules(const QString &phone, const QString &rules) {
|
2018-05-05 18:54:54 +00:00
|
|
|
const auto check = QString(phone).replace(
|
|
|
|
QRegularExpression("[^0-9]"),
|
|
|
|
QString());
|
2018-05-02 19:27:03 +00:00
|
|
|
auto result = false;
|
|
|
|
for (const auto &prefix : rules.split(',')) {
|
|
|
|
if (prefix.isEmpty()) {
|
|
|
|
result = true;
|
2018-05-05 18:54:54 +00:00
|
|
|
} else if (prefix[0] == '+' && check.startsWith(prefix.mid(1))) {
|
2018-05-02 19:27:03 +00:00
|
|
|
result = true;
|
2018-05-05 18:54:54 +00:00
|
|
|
} else if (prefix[0] == '-' && check.startsWith(prefix.mid(1))) {
|
2018-05-02 19:27:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
QByteArray ConcatenateDnsTxtFields(const std::vector<DnsEntry> &response) {
|
2020-11-04 15:50:17 +00:00
|
|
|
auto entries = QMultiMap<int, QString>();
|
2018-05-17 19:58:00 +00:00
|
|
|
for (const auto &entry : response) {
|
2020-11-04 15:50:17 +00:00
|
|
|
entries.insert(INT_MAX - entry.data.size(), entry.data);
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
2018-05-03 08:11:07 +00:00
|
|
|
return QStringList(entries.values()).join(QString()).toLatin1();
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:42:18 +00:00
|
|
|
QByteArray ParseRemoteConfigResponse(const QByteArray &bytes) {
|
|
|
|
auto error = QJsonParseError{ 0, QJsonParseError::NoError };
|
|
|
|
const auto document = QJsonDocument::fromJson(bytes, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
LOG(("Config Error: Failed to parse fire response JSON, error: %1"
|
|
|
|
).arg(error.errorString()));
|
|
|
|
return {};
|
|
|
|
} else if (!document.isObject()) {
|
|
|
|
LOG(("Config Error: Not an object received in fire response JSON."));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return document.object().value(
|
|
|
|
"entries"
|
|
|
|
).toObject().value(
|
2022-11-29 21:46:36 +00:00
|
|
|
u"%1%2"_q.arg(kConfigKey, kConfigSubKey)
|
2019-10-01 17:42:18 +00:00
|
|
|
).toString().toLatin1();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray ParseFireStoreResponse(const QByteArray &bytes) {
|
|
|
|
auto error = QJsonParseError{ 0, QJsonParseError::NoError };
|
|
|
|
const auto document = QJsonDocument::fromJson(bytes, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
LOG(("Config Error: Failed to parse fire response JSON, error: %1"
|
|
|
|
).arg(error.errorString()));
|
|
|
|
return {};
|
|
|
|
} else if (!document.isObject()) {
|
|
|
|
LOG(("Config Error: Not an object received in fire response JSON."));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return document.object().value(
|
|
|
|
"fields"
|
|
|
|
).toObject().value(
|
|
|
|
"data"
|
|
|
|
).toObject().value(
|
|
|
|
"stringValue"
|
|
|
|
).toString().toLatin1();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray ParseRealtimeResponse(const QByteArray &bytes) {
|
|
|
|
if (bytes.size() < 2
|
|
|
|
|| bytes[0] != '"'
|
|
|
|
|| bytes[bytes.size() - 1] != '"') {
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
return bytes.mid(1, bytes.size() - 2);
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
[[nodiscard]] QDateTime ParseHttpDate(const QString &date) {
|
|
|
|
// Wed, 10 Jul 2019 14:33:38 GMT
|
|
|
|
static const auto expression = QRegularExpression(
|
|
|
|
R"(\w\w\w, (\d\d) (\w\w\w) (\d\d\d\d) (\d\d):(\d\d):(\d\d) GMT)");
|
|
|
|
const auto match = expression.match(date);
|
|
|
|
if (!match.hasMatch()) {
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto number = [&](int index) {
|
2021-10-19 13:00:21 +00:00
|
|
|
return match.capturedView(index).toInt();
|
2019-07-10 15:03:48 +00:00
|
|
|
};
|
|
|
|
const auto day = number(1);
|
|
|
|
const auto month = [&] {
|
|
|
|
static const auto months = {
|
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec"
|
|
|
|
};
|
2021-10-19 13:00:21 +00:00
|
|
|
const auto captured = match.capturedView(2);
|
2019-07-10 15:03:48 +00:00
|
|
|
for (auto i = begin(months); i != end(months); ++i) {
|
2021-10-19 13:00:21 +00:00
|
|
|
if (captured == QString(*i)) {
|
2019-07-10 15:03:48 +00:00
|
|
|
return 1 + int(i - begin(months));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}();
|
|
|
|
const auto year = number(3);
|
|
|
|
const auto hour = number(4);
|
|
|
|
const auto minute = number(5);
|
|
|
|
const auto second = number(6);
|
|
|
|
return QDateTime(
|
|
|
|
QDate(year, month, day),
|
|
|
|
QTime(hour, minute, second),
|
|
|
|
Qt::UTC);
|
|
|
|
}
|
|
|
|
|
2018-05-03 08:11:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SpecialConfigRequest::SpecialConfigRequest(
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(
|
2018-05-03 08:11:07 +00:00
|
|
|
DcId dcId,
|
|
|
|
const std::string &ip,
|
|
|
|
int port,
|
|
|
|
bytes::const_span secret)> callback,
|
2019-07-10 17:28:33 +00:00
|
|
|
Fn<void()> timeDoneCallback,
|
2020-06-17 09:36:25 +00:00
|
|
|
const QString &domainString,
|
2018-05-03 08:11:07 +00:00
|
|
|
const QString &phone)
|
|
|
|
: _callback(std::move(callback))
|
2019-07-10 17:28:33 +00:00
|
|
|
, _timeDoneCallback(std::move(timeDoneCallback))
|
2020-06-17 09:36:25 +00:00
|
|
|
, _domainString(domainString)
|
2018-05-03 08:11:07 +00:00
|
|
|
, _phone(phone) {
|
2019-07-10 17:28:33 +00:00
|
|
|
Expects((_callback == nullptr) != (_timeDoneCallback == nullptr));
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
_manager.setProxy(QNetworkProxy::NoProxy);
|
2019-10-01 17:42:18 +00:00
|
|
|
|
|
|
|
auto domains = DnsDomains();
|
|
|
|
const auto domainsCount = domains.size();
|
|
|
|
|
|
|
|
std::random_device rd;
|
|
|
|
ranges::shuffle(domains, std::mt19937(rd()));
|
|
|
|
const auto takeDomain = [&] {
|
|
|
|
const auto result = domains.back();
|
|
|
|
domains.pop_back();
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const auto shuffle = [&](int from, int till) {
|
|
|
|
Expects(till > from);
|
|
|
|
|
|
|
|
ranges::shuffle(
|
|
|
|
begin(_attempts) + from,
|
|
|
|
begin(_attempts) + till,
|
|
|
|
std::mt19937(rd()));
|
2018-05-03 08:11:07 +00:00
|
|
|
};
|
2019-10-01 17:42:18 +00:00
|
|
|
|
|
|
|
_attempts = {};
|
|
|
|
_attempts.push_back({ Type::Google, "dns.google.com" });
|
|
|
|
_attempts.push_back({ Type::Google, takeDomain(), "dns" });
|
|
|
|
_attempts.push_back({ Type::Mozilla, "mozilla.cloudflare-dns.com" });
|
|
|
|
_attempts.push_back({ Type::RemoteConfig, "firebaseremoteconfig" });
|
|
|
|
while (!domains.empty()) {
|
|
|
|
_attempts.push_back({ Type::Google, takeDomain(), "dns" });
|
|
|
|
}
|
2019-10-02 08:14:38 +00:00
|
|
|
if (!_timeDoneCallback) {
|
|
|
|
_attempts.push_back({ Type::Realtime, "firebaseio.com" });
|
|
|
|
_attempts.push_back({ Type::FireStore, "firestore" });
|
|
|
|
for (const auto &domain : DnsDomains()) {
|
|
|
|
_attempts.push_back({ Type::FireStore, domain, "firestore" });
|
|
|
|
}
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
2019-10-01 17:42:18 +00:00
|
|
|
|
|
|
|
shuffle(0, 2);
|
|
|
|
shuffle(2, 4);
|
2019-10-02 08:14:38 +00:00
|
|
|
if (!_timeDoneCallback) {
|
|
|
|
shuffle(
|
|
|
|
_attempts.size() - (2 + domainsCount),
|
|
|
|
_attempts.size() - domainsCount);
|
|
|
|
shuffle(_attempts.size() - domainsCount, _attempts.size());
|
|
|
|
}
|
2019-10-01 17:42:18 +00:00
|
|
|
ranges::reverse(_attempts); // We go from last to first.
|
|
|
|
|
2018-05-03 08:11:07 +00:00
|
|
|
sendNextRequest();
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:28:33 +00:00
|
|
|
SpecialConfigRequest::SpecialConfigRequest(
|
|
|
|
Fn<void(
|
|
|
|
DcId dcId,
|
|
|
|
const std::string &ip,
|
|
|
|
int port,
|
|
|
|
bytes::const_span secret)> callback,
|
2020-06-17 09:36:25 +00:00
|
|
|
const QString &domainString,
|
2019-07-10 17:28:33 +00:00
|
|
|
const QString &phone)
|
2020-06-17 09:36:25 +00:00
|
|
|
: SpecialConfigRequest(std::move(callback), nullptr, domainString, phone) {
|
2019-07-10 17:28:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
SpecialConfigRequest::SpecialConfigRequest(
|
|
|
|
Fn<void()> timeDoneCallback,
|
|
|
|
const QString &domainString)
|
|
|
|
: SpecialConfigRequest(
|
|
|
|
nullptr,
|
|
|
|
std::move(timeDoneCallback),
|
|
|
|
domainString,
|
|
|
|
QString()) {
|
2019-07-10 15:03:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 08:11:07 +00:00
|
|
|
void SpecialConfigRequest::sendNextRequest() {
|
|
|
|
Expects(!_attempts.empty());
|
|
|
|
|
|
|
|
const auto attempt = _attempts.back();
|
|
|
|
_attempts.pop_back();
|
|
|
|
if (!_attempts.empty()) {
|
2019-09-26 10:55:35 +00:00
|
|
|
base::call_delayed(kSendNextTimeout, this, [=] {
|
2018-05-03 08:11:07 +00:00
|
|
|
sendNextRequest();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
performRequest(attempt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialConfigRequest::performRequest(const Attempt &attempt) {
|
|
|
|
const auto type = attempt.type;
|
|
|
|
auto url = QUrl();
|
2022-11-29 21:46:36 +00:00
|
|
|
url.setScheme(u"https"_q);
|
2018-05-03 08:11:07 +00:00
|
|
|
auto request = QNetworkRequest();
|
2019-10-01 17:42:18 +00:00
|
|
|
auto payload = QByteArray();
|
2018-05-03 08:11:07 +00:00
|
|
|
switch (type) {
|
2019-10-01 17:42:18 +00:00
|
|
|
case Type::Mozilla: {
|
|
|
|
url.setHost(attempt.data);
|
2022-11-29 21:46:36 +00:00
|
|
|
url.setPath(u"/dns-query"_q);
|
|
|
|
url.setQuery(u"name=%1&type=16&random_padding=%2"_q.arg(
|
2021-03-13 11:50:34 +00:00
|
|
|
_domainString,
|
|
|
|
GenerateDnsRandomPadding()));
|
2019-10-01 17:42:18 +00:00
|
|
|
request.setRawHeader("accept", "application/dns-json");
|
|
|
|
} break;
|
|
|
|
case Type::Google: {
|
|
|
|
url.setHost(attempt.data);
|
2022-11-29 21:46:36 +00:00
|
|
|
url.setPath(u"/resolve"_q);
|
|
|
|
url.setQuery(u"name=%1&type=ANY&random_padding=%2"_q.arg(
|
2021-03-13 11:50:34 +00:00
|
|
|
_domainString,
|
|
|
|
GenerateDnsRandomPadding()));
|
2019-10-01 17:42:18 +00:00
|
|
|
if (!attempt.host.isEmpty()) {
|
|
|
|
const auto host = attempt.host + ".google.com";
|
|
|
|
request.setRawHeader("Host", host.toLatin1());
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Type::RemoteConfig: {
|
|
|
|
url.setHost(ApiDomain(attempt.data));
|
2022-11-29 21:46:36 +00:00
|
|
|
url.setPath((u"/v1/projects/%1/namespaces/firebase:fetch"_q
|
2019-10-01 17:42:18 +00:00
|
|
|
).arg(kRemoteProject));
|
2022-11-29 21:46:36 +00:00
|
|
|
url.setQuery(u"key=%1"_q.arg(kApiKey));
|
|
|
|
payload = u"{\"app_id\":\"%1\",\"app_instance_id\":\"%2\"}"_q.arg(
|
2021-03-13 11:50:34 +00:00
|
|
|
kAppId,
|
|
|
|
InstanceId()).toLatin1();
|
2019-10-01 17:42:18 +00:00
|
|
|
request.setRawHeader("Content-Type", "application/json");
|
|
|
|
} break;
|
|
|
|
case Type::Realtime: {
|
2022-11-29 21:46:36 +00:00
|
|
|
url.setHost(kFireProject + u".%1"_q.arg(attempt.data));
|
|
|
|
url.setPath(u"/%1%2.json"_q.arg(kConfigKey, kConfigSubKey));
|
2019-10-01 17:42:18 +00:00
|
|
|
} break;
|
|
|
|
case Type::FireStore: {
|
|
|
|
url.setHost(attempt.host.isEmpty()
|
|
|
|
? ApiDomain(attempt.data)
|
|
|
|
: attempt.data);
|
2022-11-29 21:46:36 +00:00
|
|
|
url.setPath((u"/v1/projects/%1/databases/(default)/documents/%2/%3"_q
|
2021-03-13 11:50:34 +00:00
|
|
|
).arg(
|
|
|
|
kFireProject,
|
|
|
|
kConfigKey,
|
|
|
|
kConfigSubKey));
|
2019-10-01 17:42:18 +00:00
|
|
|
if (!attempt.host.isEmpty()) {
|
|
|
|
const auto host = ApiDomain(attempt.host);
|
|
|
|
request.setRawHeader("Host", host.toLatin1());
|
|
|
|
}
|
2018-05-03 08:11:07 +00:00
|
|
|
} break;
|
|
|
|
default: Unexpected("Type in SpecialConfigRequest::performRequest.");
|
|
|
|
}
|
|
|
|
request.setUrl(url);
|
2019-12-02 13:10:19 +00:00
|
|
|
request.setRawHeader("User-Agent", DnsUserAgent());
|
2019-10-01 17:42:18 +00:00
|
|
|
const auto reply = _requests.emplace_back(payload.isEmpty()
|
|
|
|
? _manager.get(request)
|
|
|
|
: _manager.post(request, payload)
|
2018-05-03 08:11:07 +00:00
|
|
|
).reply;
|
|
|
|
connect(reply, &QNetworkReply::finished, this, [=] {
|
|
|
|
requestFinished(type, reply);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
void SpecialConfigRequest::handleHeaderUnixtime(
|
|
|
|
not_null<QNetworkReply*> reply) {
|
2019-07-10 17:28:33 +00:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2019-07-10 15:03:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto date = QString::fromLatin1([&] {
|
|
|
|
for (const auto &pair : reply->rawHeaderPairs()) {
|
|
|
|
if (pair.first == "Date") {
|
|
|
|
return pair.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QByteArray();
|
|
|
|
}());
|
|
|
|
if (date.isEmpty()) {
|
|
|
|
LOG(("Config Error: No 'Date' header received."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto parsed = ParseHttpDate(date);
|
|
|
|
if (!parsed.isValid()) {
|
|
|
|
LOG(("Config Error: Bad 'Date' header received: %1").arg(date));
|
|
|
|
return;
|
|
|
|
}
|
2021-10-19 13:00:21 +00:00
|
|
|
base::unixtime::http_update(parsed.toSecsSinceEpoch());
|
2019-07-10 17:28:33 +00:00
|
|
|
if (_timeDoneCallback) {
|
|
|
|
_timeDoneCallback();
|
|
|
|
}
|
2019-07-10 15:03:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 08:11:07 +00:00
|
|
|
void SpecialConfigRequest::requestFinished(
|
|
|
|
Type type,
|
|
|
|
not_null<QNetworkReply*> reply) {
|
2019-07-10 15:03:48 +00:00
|
|
|
handleHeaderUnixtime(reply);
|
2018-05-03 08:11:07 +00:00
|
|
|
const auto result = finalizeRequest(reply);
|
2021-05-28 11:21:15 +00:00
|
|
|
if (!_callback || result.isEmpty()) {
|
2019-07-10 15:03:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-03 08:11:07 +00:00
|
|
|
switch (type) {
|
2019-10-01 17:42:18 +00:00
|
|
|
case Type::Mozilla:
|
|
|
|
case Type::Google: {
|
2019-01-30 18:10:53 +00:00
|
|
|
constexpr auto kTypeRestriction = 16; // TXT
|
|
|
|
handleResponse(ConcatenateDnsTxtFields(
|
|
|
|
ParseDnsResponse(result, kTypeRestriction)));
|
|
|
|
} break;
|
2019-10-01 17:42:18 +00:00
|
|
|
case Type::RemoteConfig: {
|
|
|
|
handleResponse(ParseRemoteConfigResponse(result));
|
|
|
|
} break;
|
|
|
|
case Type::Realtime: {
|
|
|
|
handleResponse(ParseRealtimeResponse(result));
|
|
|
|
} break;
|
|
|
|
case Type::FireStore: {
|
|
|
|
handleResponse(ParseFireStoreResponse(result));
|
|
|
|
} break;
|
2018-05-03 08:11:07 +00:00
|
|
|
default: Unexpected("Type in SpecialConfigRequest::requestFinished.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray SpecialConfigRequest::finalizeRequest(
|
|
|
|
not_null<QNetworkReply*> reply) {
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2021-05-28 11:21:15 +00:00
|
|
|
DEBUG_LOG(("Config Error: Failed to get response, error: %2 (%3)"
|
2018-05-03 08:11:07 +00:00
|
|
|
).arg(reply->errorString()
|
|
|
|
).arg(reply->error()));
|
|
|
|
}
|
|
|
|
const auto result = reply->readAll();
|
|
|
|
const auto from = ranges::remove(
|
|
|
|
_requests,
|
|
|
|
reply,
|
2018-05-17 19:58:00 +00:00
|
|
|
[](const ServiceWebRequest &request) { return request.reply; });
|
2018-05-03 08:11:07 +00:00
|
|
|
_requests.erase(from, end(_requests));
|
|
|
|
return result;
|
2017-06-26 17:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpecialConfigRequest::decryptSimpleConfig(const QByteArray &bytes) {
|
|
|
|
auto cleanBytes = bytes;
|
|
|
|
auto removeFrom = std::remove_if(cleanBytes.begin(), cleanBytes.end(), [](char ch) {
|
|
|
|
auto isGoodBase64 = (ch == '+') || (ch == '=') || (ch == '/')
|
|
|
|
|| (ch >= 'a' && ch <= 'z')
|
|
|
|
|| (ch >= 'A' && ch <= 'Z')
|
|
|
|
|| (ch >= '0' && ch <= '9');
|
|
|
|
return !isGoodBase64;
|
|
|
|
});
|
|
|
|
if (removeFrom != cleanBytes.end()) {
|
|
|
|
cleanBytes.remove(removeFrom - cleanBytes.begin(), cleanBytes.end() - removeFrom);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto kGoodSizeBase64 = 344;
|
|
|
|
if (cleanBytes.size() != kGoodSizeBase64) {
|
|
|
|
LOG(("Config Error: Bad data size %1 required %2").arg(cleanBytes.size()).arg(kGoodSizeBase64));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
constexpr auto kGoodSizeData = 256;
|
|
|
|
auto decodedBytes = QByteArray::fromBase64(cleanBytes, QByteArray::Base64Encoding);
|
|
|
|
if (decodedBytes.size() != kGoodSizeData) {
|
|
|
|
LOG(("Config Error: Bad data size %1 required %2").arg(decodedBytes.size()).arg(kGoodSizeData));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-29 09:44:37 +00:00
|
|
|
auto publicKey = details::RSAPublicKey(bytes::make_span(kPublicKey));
|
2018-03-27 12:16:00 +00:00
|
|
|
auto decrypted = publicKey.decrypt(bytes::make_span(decodedBytes));
|
2017-06-26 17:38:16 +00:00
|
|
|
auto decryptedBytes = gsl::make_span(decrypted);
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
auto aesEncryptedBytes = decryptedBytes.subspan(CTRState::KeySize);
|
|
|
|
auto aesivec = bytes::make_vector(decryptedBytes.subspan(CTRState::KeySize - CTRState::IvecSize, CTRState::IvecSize));
|
2017-06-26 17:38:16 +00:00
|
|
|
AES_KEY aeskey;
|
2018-03-27 12:16:00 +00:00
|
|
|
AES_set_decrypt_key(reinterpret_cast<const unsigned char*>(decryptedBytes.data()), CTRState::KeySize * CHAR_BIT, &aeskey);
|
2017-06-26 17:38:16 +00:00
|
|
|
AES_cbc_encrypt(reinterpret_cast<const unsigned char*>(aesEncryptedBytes.data()), reinterpret_cast<unsigned char*>(aesEncryptedBytes.data()), aesEncryptedBytes.size(), &aeskey, reinterpret_cast<unsigned char*>(aesivec.data()), AES_DECRYPT);
|
|
|
|
|
|
|
|
constexpr auto kDigestSize = 16;
|
|
|
|
auto dataSize = aesEncryptedBytes.size() - kDigestSize;
|
|
|
|
auto data = aesEncryptedBytes.subspan(0, dataSize);
|
|
|
|
auto hash = openssl::Sha256(data);
|
2018-03-27 12:16:00 +00:00
|
|
|
if (bytes::compare(gsl::make_span(hash).subspan(0, kDigestSize), aesEncryptedBytes.subspan(dataSize)) != 0) {
|
2017-06-26 17:38:16 +00:00
|
|
|
LOG(("Config Error: Bad digest."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpBuffer buffer;
|
|
|
|
buffer.resize(data.size() / sizeof(mtpPrime));
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::copy(bytes::make_span(buffer), data);
|
2017-06-26 17:38:16 +00:00
|
|
|
auto from = &*buffer.cbegin();
|
|
|
|
auto end = from + buffer.size();
|
|
|
|
auto realLength = *from++;
|
|
|
|
if (realLength <= 0 || realLength > dataSize || (realLength & 0x03)) {
|
|
|
|
LOG(("Config Error: Bad length %1.").arg(realLength));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:06:38 +00:00
|
|
|
if (!_simpleConfig.read(from, end)) {
|
2017-06-26 17:38:16 +00:00
|
|
|
LOG(("Config Error: Could not read configSimple."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((end - from) * sizeof(mtpPrime) != (dataSize - realLength)) {
|
|
|
|
LOG(("Config Error: Bad read length %1, should be %2.").arg((end - from) * sizeof(mtpPrime)).arg(dataSize - realLength));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialConfigRequest::handleResponse(const QByteArray &bytes) {
|
|
|
|
if (!decryptSimpleConfig(bytes)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_simpleConfig.type() == mtpc_help_configSimple);
|
2019-07-10 17:28:33 +00:00
|
|
|
const auto &config = _simpleConfig.c_help_configSimple();
|
2019-07-15 08:30:38 +00:00
|
|
|
const auto now = base::unixtime::http_now();
|
2019-07-10 17:28:33 +00:00
|
|
|
if (now > config.vexpires().v) {
|
2019-07-15 08:30:38 +00:00
|
|
|
LOG(("Config Error: "
|
|
|
|
"Bad date frame for simple config: %1-%2, our time is %3."
|
|
|
|
).arg(config.vdate().v
|
|
|
|
).arg(config.vexpires().v
|
|
|
|
).arg(now));
|
2017-06-26 17:38:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
if (config.vrules().v.empty()) {
|
2017-06-26 17:38:16 +00:00
|
|
|
LOG(("Config Error: Empty simple config received."));
|
|
|
|
return;
|
|
|
|
}
|
2019-07-15 08:30:38 +00:00
|
|
|
for (const auto &rule : config.vrules().v) {
|
2018-05-02 19:27:03 +00:00
|
|
|
Assert(rule.type() == mtpc_accessPointRule);
|
2019-07-15 08:30:38 +00:00
|
|
|
const auto &data = rule.c_accessPointRule();
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto phoneRules = qs(data.vphone_prefix_rules());
|
2018-05-02 19:27:03 +00:00
|
|
|
if (!CheckPhoneByPrefixesRules(_phone, phoneRules)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto dcId = data.vdc_id().v;
|
|
|
|
for (const auto &address : data.vips().v) {
|
2018-05-02 19:27:03 +00:00
|
|
|
const auto parseIp = [](const MTPint &ipv4) {
|
|
|
|
const auto ip = *reinterpret_cast<const uint32*>(&ipv4.v);
|
2022-11-29 21:46:36 +00:00
|
|
|
return (u"%1.%2.%3.%4"_q
|
2018-05-02 19:27:03 +00:00
|
|
|
).arg((ip >> 24) & 0xFF
|
|
|
|
).arg((ip >> 16) & 0xFF
|
|
|
|
).arg((ip >> 8) & 0xFF
|
|
|
|
).arg(ip & 0xFF).toStdString();
|
|
|
|
};
|
|
|
|
switch (address.type()) {
|
|
|
|
case mtpc_ipPort: {
|
|
|
|
const auto &fields = address.c_ipPort();
|
2019-10-02 08:14:38 +00:00
|
|
|
const auto ip = parseIp(fields.vipv4());
|
|
|
|
if (!ip.empty()) {
|
|
|
|
_callback(dcId, ip, fields.vport().v, {});
|
|
|
|
}
|
2018-05-02 19:27:03 +00:00
|
|
|
} break;
|
|
|
|
case mtpc_ipPortSecret: {
|
|
|
|
const auto &fields = address.c_ipPortSecret();
|
2019-10-02 08:14:38 +00:00
|
|
|
const auto ip = parseIp(fields.vipv4());
|
|
|
|
if (!ip.empty()) {
|
|
|
|
_callback(
|
|
|
|
dcId,
|
|
|
|
ip,
|
|
|
|
fields.vport().v,
|
|
|
|
bytes::make_span(fields.vsecret().v));
|
|
|
|
}
|
2018-05-02 19:27:03 +00:00
|
|
|
} break;
|
|
|
|
default: Unexpected("Type in simpleConfig ips.");
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 17:38:16 +00:00
|
|
|
}
|
2019-10-02 08:14:38 +00:00
|
|
|
_callback(0, std::string(), 0, {});
|
2017-06-26 17:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace MTP::details
|