2017-02-23 06:57:04 +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-02-23 06:57:04 +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-02-23 06:57:04 +00:00
|
|
|
*/
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_dc_options.h"
|
2017-02-23 06:57:04 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
#include "mtproto/details/mtproto_rsa_public_key.h"
|
2019-11-13 08:31:12 +00:00
|
|
|
#include "mtproto/facade.h"
|
2019-07-11 10:20:37 +00:00
|
|
|
#include "mtproto/connection_tcp.h"
|
2019-12-02 13:10:19 +00:00
|
|
|
#include "storage/serialize_common.h"
|
2020-11-04 15:50:17 +00:00
|
|
|
#include "base/qt_adapters.h"
|
2017-03-23 16:11:35 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
#include <QtCore/QFile>
|
|
|
|
#include <QtCore/QRegularExpression>
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
namespace MTP {
|
2017-12-08 15:24:09 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
using namespace details;
|
|
|
|
|
2020-05-27 14:31:39 +00:00
|
|
|
struct BuiltInDc {
|
|
|
|
int id;
|
|
|
|
const char *ip;
|
|
|
|
int port;
|
|
|
|
};
|
|
|
|
|
|
|
|
const BuiltInDc kBuiltInDcs[] = {
|
|
|
|
{ 1, "149.154.175.50" , 443 },
|
|
|
|
{ 2, "149.154.167.51" , 443 },
|
|
|
|
{ 2, "95.161.76.100" , 443 },
|
|
|
|
{ 3, "149.154.175.100", 443 },
|
|
|
|
{ 4, "149.154.167.91" , 443 },
|
|
|
|
{ 5, "149.154.171.5" , 443 },
|
|
|
|
};
|
|
|
|
|
|
|
|
const BuiltInDc kBuiltInDcsIPv6[] = {
|
|
|
|
{ 1, "2001:0b28:f23d:f001:0000:0000:0000:000a", 443 },
|
|
|
|
{ 2, "2001:067c:04e8:f002:0000:0000:0000:000a", 443 },
|
|
|
|
{ 3, "2001:0b28:f23d:f003:0000:0000:0000:000a", 443 },
|
|
|
|
{ 4, "2001:067c:04e8:f004:0000:0000:0000:000a", 443 },
|
|
|
|
{ 5, "2001:0b28:f23f:f005:0000:0000:0000:000a", 443 },
|
|
|
|
};
|
|
|
|
|
|
|
|
const BuiltInDc kBuiltInDcsTest[] = {
|
|
|
|
{ 1, "149.154.175.10" , 443 },
|
|
|
|
{ 2, "149.154.167.40" , 443 },
|
|
|
|
{ 3, "149.154.175.117", 443 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const BuiltInDc kBuiltInDcsIPv6Test[] = {
|
|
|
|
{ 1, "2001:0b28:f23d:f001:0000:0000:0000:000e", 443 },
|
|
|
|
{ 2, "2001:067c:04e8:f002:0000:0000:0000:000e", 443 },
|
|
|
|
{ 3, "2001:0b28:f23d:f003:0000:0000:0000:000e", 443 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *(kPublicRSAKeys[]) = { "\
|
2017-12-08 15:24:09 +00:00
|
|
|
-----BEGIN RSA PUBLIC KEY-----\n\
|
|
|
|
MIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6\n\
|
|
|
|
lyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS\n\
|
|
|
|
an9tqw3bfUV/nqgbhGX81v/+7RFAEd+RwFnK7a+XYl9sluzHRyVVaTTveB2GazTw\n\
|
|
|
|
Efzk2DWgkBluml8OREmvfraX3bkHZJTKX4EQSjBbbdJ2ZXIsRrYOXfaA+xayEGB+\n\
|
|
|
|
8hdlLmAjbCVfaigxX0CDqWeR1yFL9kwd9P0NsZRPsmoqVwMbMu7mStFai6aIhc3n\n\
|
|
|
|
Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB\n\
|
|
|
|
-----END RSA PUBLIC KEY-----", "\
|
|
|
|
-----BEGIN PUBLIC KEY-----\n\
|
|
|
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAruw2yP/BCcsJliRoW5eB\n\
|
|
|
|
VBVle9dtjJw+OYED160Wybum9SXtBBLXriwt4rROd9csv0t0OHCaTmRqBcQ0J8fx\n\
|
|
|
|
hN6/cpR1GWgOZRUAiQxoMnlt0R93LCX/j1dnVa/gVbCjdSxpbrfY2g2L4frzjJvd\n\
|
|
|
|
l84Kd9ORYjDEAyFnEA7dD556OptgLQQ2e2iVNq8NZLYTzLp5YpOdO1doK+ttrltg\n\
|
|
|
|
gTCy5SrKeLoCPPbOgGsdxJxyz5KKcZnSLj16yE5HvJQn0CNpRdENvRUXe6tBP78O\n\
|
|
|
|
39oJ8BTHp9oIjd6XWXAsp2CvK45Ol8wFXGF710w9lwCGNbmNxNYhtIkdqfsEcwR5\n\
|
|
|
|
JwIDAQAB\n\
|
|
|
|
-----END PUBLIC KEY-----", "\
|
|
|
|
-----BEGIN PUBLIC KEY-----\n\
|
|
|
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvfLHfYH2r9R70w8prHbl\n\
|
|
|
|
Wt/nDkh+XkgpflqQVcnAfSuTtO05lNPspQmL8Y2XjVT4t8cT6xAkdgfmmvnvRPOO\n\
|
|
|
|
KPi0OfJXoRVylFzAQG/j83u5K3kRLbae7fLccVhKZhY46lvsueI1hQdLgNV9n1cQ\n\
|
|
|
|
3TDS2pQOCtovG4eDl9wacrXOJTG2990VjgnIKNA0UMoP+KF03qzryqIt3oTvZq03\n\
|
|
|
|
DyWdGK+AZjgBLaDKSnC6qD2cFY81UryRWOab8zKkWAnhw2kFpcqhI0jdV5QaSCEx\n\
|
|
|
|
vnsjVaX0Y1N0870931/5Jb9ICe4nweZ9kSDF/gip3kWLG0o8XQpChDfyvsqB9OLV\n\
|
|
|
|
/wIDAQAB\n\
|
|
|
|
-----END PUBLIC KEY-----", "\
|
|
|
|
-----BEGIN PUBLIC KEY-----\n\
|
|
|
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs/ditzm+mPND6xkhzwFI\n\
|
|
|
|
z6J/968CtkcSE/7Z2qAJiXbmZ3UDJPGrzqTDHkO30R8VeRM/Kz2f4nR05GIFiITl\n\
|
|
|
|
4bEjvpy7xqRDspJcCFIOcyXm8abVDhF+th6knSU0yLtNKuQVP6voMrnt9MV1X92L\n\
|
|
|
|
GZQLgdHZbPQz0Z5qIpaKhdyA8DEvWWvSUwwc+yi1/gGaybwlzZwqXYoPOhwMebzK\n\
|
|
|
|
Uk0xW14htcJrRrq+PXXQbRzTMynseCoPIoke0dtCodbA3qQxQovE16q9zz4Otv2k\n\
|
|
|
|
4j63cz53J+mhkVWAeWxVGI0lltJmWtEYK6er8VqqWot3nqmWMXogrgRLggv/Nbbo\n\
|
|
|
|
oQIDAQAB\n\
|
|
|
|
-----END PUBLIC KEY-----", "\
|
|
|
|
-----BEGIN PUBLIC KEY-----\n\
|
|
|
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvmpxVY7ld/8DAjz6F6q0\n\
|
|
|
|
5shjg8/4p6047bn6/m8yPy1RBsvIyvuDuGnP/RzPEhzXQ9UJ5Ynmh2XJZgHoE9xb\n\
|
|
|
|
nfxL5BXHplJhMtADXKM9bWB11PU1Eioc3+AXBB8QiNFBn2XI5UkO5hPhbb9mJpjA\n\
|
|
|
|
9Uhw8EdfqJP8QetVsI/xrCEbwEXe0xvifRLJbY08/Gp66KpQvy7g8w7VB8wlgePe\n\
|
|
|
|
xW3pT13Ap6vuC+mQuJPyiHvSxjEKHgqePji9NP3tJUFQjcECqcm0yV7/2d0t/pbC\n\
|
|
|
|
m+ZH1sadZspQCEPPrtbkQBlvHb4OLiIWPGHKSMeRFvp3IWcmdJqXahxLCUS1Eh6M\n\
|
|
|
|
AQIDAQAB\n\
|
|
|
|
-----END PUBLIC KEY-----" };
|
|
|
|
|
|
|
|
} // namespace
|
2017-02-23 06:57:04 +00:00
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
class DcOptions::WriteLocker {
|
|
|
|
public:
|
2018-05-02 19:07:34 +00:00
|
|
|
WriteLocker(not_null<DcOptions*> that)
|
|
|
|
: _that(that)
|
|
|
|
, _lock(&_that->_useThroughLockers) {
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
2019-11-27 14:22:22 +00:00
|
|
|
|
|
|
|
void unlock() {
|
|
|
|
_lock.unlock();
|
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
~WriteLocker() {
|
|
|
|
_that->computeCdnDcIds();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<DcOptions*> _that;
|
2017-03-23 16:11:35 +00:00
|
|
|
QWriteLocker _lock;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class DcOptions::ReadLocker {
|
|
|
|
public:
|
2018-05-02 19:07:34 +00:00
|
|
|
ReadLocker(not_null<const DcOptions*> that)
|
|
|
|
: _lock(&that->_useThroughLockers) {
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 14:22:22 +00:00
|
|
|
void unlock() {
|
|
|
|
_lock.unlock();
|
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
private:
|
|
|
|
QReadLocker _lock;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
DcOptions::DcOptions(Environment environment)
|
|
|
|
: _environment(environment) {
|
2019-07-24 08:46:23 +00:00
|
|
|
constructFromBuiltIn();
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
DcOptions::DcOptions(const DcOptions &other)
|
|
|
|
: _environment(other._environment)
|
|
|
|
, _data(other._data)
|
|
|
|
, _cdnDcIds(other._cdnDcIds)
|
|
|
|
, _publicKeys(other._publicKeys)
|
|
|
|
, _cdnPublicKeys(other._cdnPublicKeys)
|
|
|
|
, _immutable(other._immutable) {
|
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
DcOptions::~DcOptions() = default;
|
|
|
|
|
2019-07-11 10:20:37 +00:00
|
|
|
bool DcOptions::ValidateSecret(bytes::const_span secret) {
|
|
|
|
// See also TcpConnection::Protocol::Create.
|
|
|
|
return (secret.size() >= 21 && secret[0] == bytes::type(0xEE))
|
|
|
|
|| (secret.size() == 17 && secret[0] == bytes::type(0xDD))
|
|
|
|
|| (secret.size() == 16)
|
|
|
|
|| secret.empty();
|
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
void DcOptions::readBuiltInPublicKeys() {
|
2020-05-27 14:31:39 +00:00
|
|
|
for (const auto key : kPublicRSAKeys) {
|
2018-03-27 12:16:00 +00:00
|
|
|
const auto keyBytes = bytes::make_span(key, strlen(key));
|
2019-12-02 13:10:19 +00:00
|
|
|
auto parsed = RSAPublicKey(keyBytes);
|
2019-11-13 14:12:04 +00:00
|
|
|
if (parsed.valid()) {
|
|
|
|
_publicKeys.emplace(parsed.fingerprint(), std::move(parsed));
|
2017-03-23 16:11:35 +00:00
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: could not read this public RSA key:"));
|
2017-12-08 15:24:09 +00:00
|
|
|
LOG((key));
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
Environment DcOptions::environment() const {
|
|
|
|
return _environment;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DcOptions::isTestMode() const {
|
|
|
|
return (_environment != Environment::Production);
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
void DcOptions::constructFromBuiltIn() {
|
2017-03-23 16:11:35 +00:00
|
|
|
WriteLocker lock(this);
|
2017-02-23 06:57:04 +00:00
|
|
|
_data.clear();
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
readBuiltInPublicKeys();
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto list = isTestMode()
|
2020-05-27 14:31:39 +00:00
|
|
|
? gsl::make_span(kBuiltInDcsTest)
|
|
|
|
: gsl::make_span(kBuiltInDcs).subspan(0);
|
|
|
|
for (const auto &entry : list) {
|
2018-05-02 19:07:34 +00:00
|
|
|
const auto flags = Flag::f_static | 0;
|
2020-05-27 14:31:39 +00:00
|
|
|
applyOneGuarded(entry.id, flags, entry.ip, entry.port, {});
|
|
|
|
DEBUG_LOG(("MTP Info: adding built in DC %1 connect option: %2:%3"
|
|
|
|
).arg(entry.id
|
|
|
|
).arg(entry.ip
|
|
|
|
).arg(entry.port));
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto listv6 = isTestMode()
|
2020-05-27 14:31:39 +00:00
|
|
|
? gsl::make_span(kBuiltInDcsIPv6Test)
|
|
|
|
: gsl::make_span(kBuiltInDcsIPv6).subspan(0);
|
|
|
|
for (const auto &entry : listv6) {
|
2018-05-02 19:07:34 +00:00
|
|
|
const auto flags = Flag::f_static | Flag::f_ipv6;
|
2020-05-27 14:31:39 +00:00
|
|
|
applyOneGuarded(entry.id, flags, entry.ip, entry.port, {});
|
2018-04-24 19:09:20 +00:00
|
|
|
DEBUG_LOG(("MTP Info: adding built in DC %1 IPv6 connect option: "
|
2020-05-27 14:31:39 +00:00
|
|
|
"%2:%3"
|
|
|
|
).arg(entry.id
|
|
|
|
).arg(entry.ip
|
|
|
|
).arg(entry.port));
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
void DcOptions::processFromList(
|
|
|
|
const QVector<MTPDcOption> &options,
|
|
|
|
bool overwrite) {
|
2017-03-01 17:22:37 +00:00
|
|
|
if (options.empty() || _immutable) {
|
2017-02-23 06:57:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:07:34 +00:00
|
|
|
auto data = [&] {
|
2017-02-23 06:57:04 +00:00
|
|
|
if (overwrite) {
|
2020-06-17 09:36:25 +00:00
|
|
|
return base::flat_map<DcId, std::vector<Endpoint>>();
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
ReadLocker lock(this);
|
|
|
|
return _data;
|
|
|
|
}();
|
|
|
|
for (auto &mtpOption : options) {
|
|
|
|
if (mtpOption.type() != mtpc_dcOption) {
|
|
|
|
LOG(("Wrong type in DcOptions: %1").arg(mtpOption.type()));
|
|
|
|
continue;
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
|
|
|
|
auto &option = mtpOption.c_dcOption();
|
2019-07-05 13:38:38 +00:00
|
|
|
auto dcId = option.vid().v;
|
|
|
|
auto flags = option.vflags().v;
|
2018-05-02 19:07:34 +00:00
|
|
|
auto ip = std::string(
|
2019-07-05 13:38:38 +00:00
|
|
|
option.vip_address().v.constData(),
|
|
|
|
option.vip_address().v.size());
|
|
|
|
auto port = option.vport().v;
|
|
|
|
auto secret = bytes::make_vector(option.vsecret().value_or_empty());
|
2018-05-02 19:07:34 +00:00
|
|
|
ApplyOneOption(data, dcId, flags, ip, port, secret);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 10:41:17 +00:00
|
|
|
const auto difference = [&] {
|
2018-05-02 19:07:34 +00:00
|
|
|
WriteLocker lock(this);
|
|
|
|
auto result = CountOptionsDifference(_data, data);
|
|
|
|
if (!result.empty()) {
|
|
|
|
_data = std::move(data);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}();
|
2019-11-27 10:41:17 +00:00
|
|
|
for (const auto dcId : difference) {
|
|
|
|
_changed.fire_copy(dcId);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcOptions::setFromList(const MTPVector<MTPDcOption> &options) {
|
2017-03-10 19:46:28 +00:00
|
|
|
processFromList(options.v, true);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DcOptions::addFromList(const MTPVector<MTPDcOption> &options) {
|
2017-03-10 19:46:28 +00:00
|
|
|
processFromList(options.v, false);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
void DcOptions::addFromOther(DcOptions &&options) {
|
2017-03-01 17:22:37 +00:00
|
|
|
if (this == &options || _immutable) {
|
2017-02-23 06:57:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto idsChanged = std::vector<DcId>();
|
|
|
|
{
|
2017-03-23 16:11:35 +00:00
|
|
|
ReadLocker lock(&options);
|
2017-02-23 06:57:04 +00:00
|
|
|
if (options._data.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
idsChanged.reserve(options._data.size());
|
|
|
|
{
|
2017-03-23 16:11:35 +00:00
|
|
|
WriteLocker lock(this);
|
2018-05-02 19:07:34 +00:00
|
|
|
const auto changed = [&](const std::vector<Endpoint> &list) {
|
|
|
|
auto result = false;
|
|
|
|
for (const auto &endpoint : list) {
|
|
|
|
const auto dcId = endpoint.id;
|
|
|
|
const auto flags = endpoint.flags;
|
|
|
|
const auto &ip = endpoint.ip;
|
|
|
|
const auto port = endpoint.port;
|
|
|
|
const auto &secret = endpoint.secret;
|
2018-04-25 09:24:48 +00:00
|
|
|
if (applyOneGuarded(dcId, flags, ip, port, secret)) {
|
2018-05-02 19:07:34 +00:00
|
|
|
result = true;
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
for (const auto &item : base::take(options._data)) {
|
|
|
|
if (changed(item.second)) {
|
|
|
|
idsChanged.push_back(item.first);
|
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
for (auto &item : options._cdnPublicKeys) {
|
|
|
|
for (auto &entry : item.second) {
|
|
|
|
_cdnPublicKeys[item.first].insert(std::move(entry));
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-27 10:41:17 +00:00
|
|
|
for (const auto dcId : idsChanged) {
|
|
|
|
_changed.fire_copy(dcId);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 09:24:48 +00:00
|
|
|
void DcOptions::constructAddOne(
|
|
|
|
int id,
|
2018-05-02 19:07:34 +00:00
|
|
|
Flags flags,
|
2018-04-25 09:24:48 +00:00
|
|
|
const std::string &ip,
|
|
|
|
int port,
|
|
|
|
const bytes::vector &secret) {
|
2017-03-23 16:11:35 +00:00
|
|
|
WriteLocker lock(this);
|
2018-06-11 18:35:27 +00:00
|
|
|
applyOneGuarded(BareDcId(id), flags, ip, port, secret);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
bool DcOptions::applyOneGuarded(
|
|
|
|
DcId dcId,
|
2018-05-02 19:07:34 +00:00
|
|
|
Flags flags,
|
|
|
|
const std::string &ip,
|
|
|
|
int port,
|
|
|
|
const bytes::vector &secret) {
|
|
|
|
return ApplyOneOption(_data, dcId, flags, ip, port, secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DcOptions::ApplyOneOption(
|
2020-06-17 09:36:25 +00:00
|
|
|
base::flat_map<DcId, std::vector<Endpoint>> &data,
|
2018-05-02 19:07:34 +00:00
|
|
|
DcId dcId,
|
|
|
|
Flags flags,
|
2018-04-24 19:09:20 +00:00
|
|
|
const std::string &ip,
|
2018-04-25 09:24:48 +00:00
|
|
|
int port,
|
|
|
|
const bytes::vector &secret) {
|
2018-05-02 19:07:34 +00:00
|
|
|
auto i = data.find(dcId);
|
|
|
|
if (i != data.cend()) {
|
|
|
|
for (auto &endpoint : i->second) {
|
|
|
|
if (endpoint.ip == ip && endpoint.port == port) {
|
2018-04-24 19:09:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
2019-07-08 16:02:45 +00:00
|
|
|
i->second.emplace_back(dcId, flags, ip, port, secret);
|
2017-02-23 06:57:04 +00:00
|
|
|
} else {
|
2018-05-02 19:07:34 +00:00
|
|
|
data.emplace(dcId, std::vector<Endpoint>(
|
2018-04-24 19:09:20 +00:00
|
|
|
1,
|
2018-05-02 19:07:34 +00:00
|
|
|
Endpoint(dcId, flags, ip, port, secret)));
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-27 10:41:17 +00:00
|
|
|
std::vector<DcId> DcOptions::CountOptionsDifference(
|
2020-06-17 09:36:25 +00:00
|
|
|
const base::flat_map<DcId, std::vector<Endpoint>> &a,
|
|
|
|
const base::flat_map<DcId, std::vector<Endpoint>> &b) {
|
2019-11-27 10:41:17 +00:00
|
|
|
auto result = std::vector<DcId>();
|
2018-05-02 19:07:34 +00:00
|
|
|
const auto find = [](
|
|
|
|
const std::vector<Endpoint> &where,
|
|
|
|
const Endpoint &what) {
|
|
|
|
for (const auto &endpoint : where) {
|
|
|
|
if (endpoint.ip == what.ip && endpoint.port == what.port) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
const auto equal = [&](
|
|
|
|
const std::vector<Endpoint> &m,
|
|
|
|
const std::vector<Endpoint> &n) {
|
|
|
|
if (m.size() != n.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto &endpoint : m) {
|
|
|
|
if (!find(n, endpoint)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto i = begin(a);
|
|
|
|
auto j = begin(b);
|
|
|
|
const auto max = std::numeric_limits<DcId>::max();
|
|
|
|
while (i != end(a) || j != end(b)) {
|
|
|
|
const auto aId = (i == end(a)) ? max : i->first;
|
|
|
|
const auto bId = (j == end(b)) ? max : j->first;
|
|
|
|
if (aId < bId) {
|
|
|
|
result.push_back(aId);
|
|
|
|
++i;
|
|
|
|
} else if (bId < aId) {
|
|
|
|
result.push_back(bId);
|
|
|
|
++j;
|
|
|
|
} else {
|
|
|
|
if (!equal(i->second, j->second)) {
|
|
|
|
result.push_back(aId);
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
QByteArray DcOptions::serialize() const {
|
2017-03-01 17:22:37 +00:00
|
|
|
if (_immutable) {
|
|
|
|
// Don't write the overriden options to our settings.
|
2020-06-17 09:36:25 +00:00
|
|
|
return DcOptions(_environment).serialize();
|
2017-03-01 17:22:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
ReadLocker lock(this);
|
2017-02-23 06:57:04 +00:00
|
|
|
|
|
|
|
auto size = sizeof(qint32);
|
2018-04-25 09:24:48 +00:00
|
|
|
|
|
|
|
// Dc options.
|
|
|
|
auto optionsCount = 0;
|
|
|
|
size += sizeof(qint32);
|
2018-04-24 19:09:20 +00:00
|
|
|
for (const auto &item : _data) {
|
2017-06-26 17:38:16 +00:00
|
|
|
if (isTemporaryDcId(item.first)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
for (const auto &endpoint : item.second) {
|
2018-04-25 09:24:48 +00:00
|
|
|
++optionsCount;
|
|
|
|
// id + flags + port
|
|
|
|
size += sizeof(qint32) + sizeof(qint32) + sizeof(qint32);
|
2018-05-02 19:07:34 +00:00
|
|
|
size += sizeof(qint32) + endpoint.ip.size();
|
|
|
|
size += sizeof(qint32) + endpoint.secret.size();
|
2018-04-24 19:09:20 +00:00
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 09:24:48 +00:00
|
|
|
// CDN public keys.
|
2017-03-23 16:11:35 +00:00
|
|
|
auto count = 0;
|
|
|
|
for (auto &keysInDc : _cdnPublicKeys) {
|
|
|
|
count += keysInDc.second.size();
|
|
|
|
}
|
|
|
|
struct SerializedPublicKey {
|
|
|
|
DcId dcId;
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector n;
|
|
|
|
bytes::vector e;
|
2017-03-23 16:11:35 +00:00
|
|
|
};
|
|
|
|
std::vector<SerializedPublicKey> publicKeys;
|
|
|
|
publicKeys.reserve(count);
|
2018-04-25 09:24:48 +00:00
|
|
|
size += sizeof(qint32);
|
2018-04-24 19:09:20 +00:00
|
|
|
for (const auto &keysInDc : _cdnPublicKeys) {
|
|
|
|
for (const auto &entry : keysInDc.second) {
|
2018-04-25 09:24:48 +00:00
|
|
|
publicKeys.push_back({
|
|
|
|
keysInDc.first,
|
|
|
|
entry.second.getN(),
|
|
|
|
entry.second.getE()
|
|
|
|
});
|
|
|
|
size += sizeof(qint32)
|
|
|
|
+ Serialize::bytesSize(publicKeys.back().n)
|
|
|
|
+ Serialize::bytesSize(publicKeys.back().e);
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 09:24:48 +00:00
|
|
|
constexpr auto kVersion = 1;
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
auto result = QByteArray();
|
|
|
|
result.reserve(size);
|
|
|
|
{
|
2017-08-08 18:25:10 +00:00
|
|
|
QDataStream stream(&result, QIODevice::WriteOnly);
|
2017-02-23 06:57:04 +00:00
|
|
|
stream.setVersion(QDataStream::Qt_5_1);
|
2018-04-25 09:24:48 +00:00
|
|
|
stream << qint32(-kVersion);
|
|
|
|
|
|
|
|
// Dc options.
|
|
|
|
stream << qint32(optionsCount);
|
2018-04-24 19:09:20 +00:00
|
|
|
for (const auto &item : _data) {
|
2017-06-26 17:38:16 +00:00
|
|
|
if (isTemporaryDcId(item.first)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
for (const auto &endpoint : item.second) {
|
|
|
|
stream << qint32(endpoint.id)
|
|
|
|
<< qint32(endpoint.flags)
|
|
|
|
<< qint32(endpoint.port)
|
|
|
|
<< qint32(endpoint.ip.size());
|
|
|
|
stream.writeRawData(endpoint.ip.data(), endpoint.ip.size());
|
|
|
|
stream << qint32(endpoint.secret.size());
|
2018-04-25 09:24:48 +00:00
|
|
|
stream.writeRawData(
|
2018-05-02 19:07:34 +00:00
|
|
|
reinterpret_cast<const char*>(endpoint.secret.data()),
|
|
|
|
endpoint.secret.size());
|
2018-04-24 19:09:20 +00:00
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
2018-04-25 09:24:48 +00:00
|
|
|
|
|
|
|
// CDN public keys.
|
2017-03-23 16:11:35 +00:00
|
|
|
stream << qint32(publicKeys.size());
|
|
|
|
for (auto &key : publicKeys) {
|
2018-04-25 09:24:48 +00:00
|
|
|
stream << qint32(key.dcId)
|
|
|
|
<< Serialize::bytes(key.n)
|
|
|
|
<< Serialize::bytes(key.e);
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
bool DcOptions::constructFromSerialized(const QByteArray &serialized) {
|
2017-08-08 18:25:10 +00:00
|
|
|
QDataStream stream(serialized);
|
2017-02-24 17:15:41 +00:00
|
|
|
stream.setVersion(QDataStream::Qt_5_1);
|
2018-04-25 09:24:48 +00:00
|
|
|
|
|
|
|
auto minusVersion = qint32(0);
|
|
|
|
stream >> minusVersion;
|
|
|
|
const auto version = (minusVersion < 0) ? (-minusVersion) : 0;
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
auto count = qint32(0);
|
2018-04-25 09:24:48 +00:00
|
|
|
if (version > 0) {
|
|
|
|
stream >> count;
|
|
|
|
} else {
|
|
|
|
count = minusVersion;
|
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
if (stream.status() != QDataStream::Ok) {
|
2017-02-23 09:32:28 +00:00
|
|
|
LOG(("MTP Error: Bad data for DcOptions::constructFromSerialized()"));
|
2020-06-17 09:36:25 +00:00
|
|
|
return false;
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
WriteLocker lock(this);
|
2017-02-23 06:57:04 +00:00
|
|
|
_data.clear();
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
qint32 id = 0, flags = 0, port = 0, ipSize = 0;
|
|
|
|
stream >> id >> flags >> port >> ipSize;
|
2017-09-04 11:53:14 +00:00
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/1076714/max-length-for-client-ip-address
|
|
|
|
constexpr auto kMaxIpSize = 45;
|
2017-11-30 07:14:31 +00:00
|
|
|
if (ipSize <= 0 || ipSize > kMaxIpSize) {
|
2017-09-04 11:53:14 +00:00
|
|
|
LOG(("MTP Error: Bad data inside DcOptions::constructFromSerialized()"));
|
2020-06-17 09:36:25 +00:00
|
|
|
return false;
|
2017-09-04 11:53:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 19:07:34 +00:00
|
|
|
auto ip = std::string(ipSize, ' ');
|
2018-04-25 09:24:48 +00:00
|
|
|
stream.readRawData(ip.data(), ipSize);
|
|
|
|
|
|
|
|
constexpr auto kMaxSecretSize = 32;
|
|
|
|
auto secret = bytes::vector();
|
|
|
|
if (version > 0) {
|
|
|
|
auto secretSize = qint32(0);
|
|
|
|
stream >> secretSize;
|
|
|
|
if (secretSize < 0 || secretSize > kMaxSecretSize) {
|
|
|
|
LOG(("MTP Error: Bad data inside DcOptions::constructFromSerialized()"));
|
2020-06-17 09:36:25 +00:00
|
|
|
return false;
|
2018-04-25 09:24:48 +00:00
|
|
|
} else if (secretSize > 0) {
|
|
|
|
secret.resize(secretSize);
|
|
|
|
stream.readRawData(
|
|
|
|
reinterpret_cast<char*>(secret.data()),
|
|
|
|
secretSize);
|
|
|
|
}
|
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
2017-02-23 09:32:28 +00:00
|
|
|
LOG(("MTP Error: Bad data inside DcOptions::constructFromSerialized()"));
|
2020-06-17 09:36:25 +00:00
|
|
|
return false;
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 09:24:48 +00:00
|
|
|
applyOneGuarded(
|
|
|
|
DcId(id),
|
2018-05-02 19:07:34 +00:00
|
|
|
Flags::from_raw(flags),
|
2018-04-25 09:24:48 +00:00
|
|
|
ip,
|
|
|
|
port,
|
|
|
|
secret);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
2017-03-23 16:11:35 +00:00
|
|
|
|
|
|
|
// Read CDN config
|
|
|
|
if (!stream.atEnd()) {
|
|
|
|
auto count = qint32(0);
|
|
|
|
stream >> count;
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
LOG(("MTP Error: Bad data for CDN config in DcOptions::constructFromSerialized()"));
|
2020-06-17 09:36:25 +00:00
|
|
|
return false;
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
qint32 dcId = 0;
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector n, e;
|
2017-04-24 12:16:38 +00:00
|
|
|
stream >> dcId >> Serialize::bytes(n) >> Serialize::bytes(e);
|
2017-03-23 16:11:35 +00:00
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
LOG(("MTP Error: Bad data for CDN config inside DcOptions::constructFromSerialized()"));
|
2020-06-17 09:36:25 +00:00
|
|
|
return false;
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
auto key = RSAPublicKey(n, e);
|
2019-11-13 14:12:04 +00:00
|
|
|
if (key.valid()) {
|
|
|
|
_cdnPublicKeys[dcId].emplace(key.fingerprint(), std::move(key));
|
2017-03-23 16:11:35 +00:00
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: Could not read valid CDN public key."));
|
2020-06-17 09:36:25 +00:00
|
|
|
return false;
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
return true;
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 10:41:17 +00:00
|
|
|
rpl::producer<DcId> DcOptions::changed() const {
|
|
|
|
return _changed.events();
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:22:22 +00:00
|
|
|
rpl::producer<> DcOptions::cdnConfigChanged() const {
|
|
|
|
return _cdnConfigChanged.events();
|
|
|
|
}
|
|
|
|
|
2019-11-27 10:41:17 +00:00
|
|
|
std::vector<DcId> DcOptions::configEnumDcIds() const {
|
|
|
|
auto result = std::vector<DcId>();
|
2017-02-23 06:57:04 +00:00
|
|
|
{
|
2017-03-23 16:11:35 +00:00
|
|
|
ReadLocker lock(this);
|
2017-02-23 06:57:04 +00:00
|
|
|
result.reserve(_data.size());
|
|
|
|
for (auto &item : _data) {
|
2018-05-02 19:07:34 +00:00
|
|
|
const auto dcId = item.first;
|
2018-04-24 19:09:20 +00:00
|
|
|
Assert(!item.second.empty());
|
|
|
|
if (!isCdnDc(item.second.front().flags)
|
2018-05-02 19:07:34 +00:00
|
|
|
&& !isTemporaryDcId(dcId)) {
|
2018-04-24 19:09:20 +00:00
|
|
|
result.push_back(dcId);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 12:23:20 +00:00
|
|
|
ranges::sort(result);
|
2017-02-23 06:57:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
DcType DcOptions::dcType(ShiftedDcId shiftedDcId) const {
|
2017-06-26 17:38:16 +00:00
|
|
|
if (isTemporaryDcId(shiftedDcId)) {
|
|
|
|
return DcType::Temporary;
|
|
|
|
}
|
2017-03-23 16:11:35 +00:00
|
|
|
ReadLocker lock(this);
|
2018-06-11 18:35:27 +00:00
|
|
|
if (_cdnDcIds.find(BareDcId(shiftedDcId)) != _cdnDcIds.cend()) {
|
2017-03-23 16:11:35 +00:00
|
|
|
return DcType::Cdn;
|
|
|
|
}
|
2019-11-21 10:37:39 +00:00
|
|
|
const auto dcId = BareDcId(shiftedDcId);
|
|
|
|
if (isDownloadDcId(shiftedDcId) && hasMediaOnlyOptionsFor(dcId)) {
|
|
|
|
return DcType::MediaCluster;
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
return DcType::Regular;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcOptions::setCDNConfig(const MTPDcdnConfig &config) {
|
|
|
|
WriteLocker lock(this);
|
|
|
|
_cdnPublicKeys.clear();
|
2019-11-27 14:22:22 +00:00
|
|
|
for (const auto &key : config.vpublic_keys().v) {
|
|
|
|
key.match([&](const MTPDcdnPublicKey &data) {
|
|
|
|
const auto keyBytes = bytes::make_span(data.vpublic_key().v);
|
2019-12-02 13:10:19 +00:00
|
|
|
auto key = RSAPublicKey(keyBytes);
|
2019-11-27 14:22:22 +00:00
|
|
|
if (key.valid()) {
|
|
|
|
_cdnPublicKeys[data.vdc_id().v].emplace(
|
|
|
|
key.fingerprint(),
|
|
|
|
std::move(key));
|
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: could not read this public RSA key:"));
|
|
|
|
LOG((qs(data.vpublic_key())));
|
|
|
|
}
|
|
|
|
});
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
2019-11-27 14:22:22 +00:00
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
_cdnConfigChanged.fire({});
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DcOptions::hasCDNKeysForDc(DcId dcId) const {
|
|
|
|
ReadLocker lock(this);
|
|
|
|
return _cdnPublicKeys.find(dcId) != _cdnPublicKeys.cend();
|
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
RSAPublicKey DcOptions::getDcRSAKey(
|
2019-11-13 14:12:04 +00:00
|
|
|
DcId dcId,
|
|
|
|
const QVector<MTPlong> &fingerprints) const {
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto findKey = [&](
|
|
|
|
const base::flat_map<uint64, RSAPublicKey> &keys) {
|
2019-11-13 14:12:04 +00:00
|
|
|
for (const auto &fingerprint : fingerprints) {
|
|
|
|
const auto it = keys.find(static_cast<uint64>(fingerprint.v));
|
2017-03-23 16:11:35 +00:00
|
|
|
if (it != keys.cend()) {
|
2019-11-13 14:12:04 +00:00
|
|
|
return it->second;
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 13:10:19 +00:00
|
|
|
return RSAPublicKey();
|
2017-03-23 16:11:35 +00:00
|
|
|
};
|
|
|
|
{
|
|
|
|
ReadLocker lock(this);
|
2019-11-13 14:12:04 +00:00
|
|
|
const auto it = _cdnPublicKeys.find(dcId);
|
2017-03-23 16:11:35 +00:00
|
|
|
if (it != _cdnPublicKeys.cend()) {
|
|
|
|
return findKey(it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return findKey(_publicKeys);
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 19:07:34 +00:00
|
|
|
auto DcOptions::lookup(
|
2018-04-24 19:09:20 +00:00
|
|
|
DcId dcId,
|
|
|
|
DcType type,
|
2018-05-02 19:07:34 +00:00
|
|
|
bool throughProxy) const -> Variants {
|
|
|
|
using Flag = Flag;
|
|
|
|
auto result = Variants();
|
2019-11-21 10:37:39 +00:00
|
|
|
|
|
|
|
ReadLocker lock(this);
|
|
|
|
const auto i = _data.find(dcId);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
for (const auto &endpoint : i->second) {
|
|
|
|
const auto flags = endpoint.flags;
|
|
|
|
if (type == DcType::Cdn && !(flags & Flag::f_cdn)) {
|
|
|
|
continue;
|
|
|
|
} else if (type != DcType::MediaCluster
|
|
|
|
&& (flags & Flag::f_media_only)) {
|
|
|
|
continue;
|
|
|
|
} else if (!ValidateSecret(endpoint.secret)) {
|
|
|
|
continue;
|
2018-05-02 19:07:34 +00:00
|
|
|
}
|
2019-11-21 10:37:39 +00:00
|
|
|
const auto address = (flags & Flag::f_ipv6)
|
|
|
|
? Variants::IPv6
|
|
|
|
: Variants::IPv4;
|
|
|
|
result.data[address][Variants::Tcp].push_back(endpoint);
|
|
|
|
if (!(flags & (Flag::f_tcpo_only | Flag::f_secret))) {
|
|
|
|
result.data[address][Variants::Http].push_back(endpoint);
|
2018-05-02 19:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-21 10:37:39 +00:00
|
|
|
if (type == DcType::MediaCluster) {
|
|
|
|
FilterIfHasWithFlag(result, Flag::f_media_only);
|
|
|
|
}
|
|
|
|
if (throughProxy) {
|
|
|
|
FilterIfHasWithFlag(result, Flag::f_static);
|
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-02-23 06:57:04 +00:00
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
bool DcOptions::hasMediaOnlyOptionsFor(DcId dcId) const {
|
|
|
|
ReadLocker lock(this);
|
|
|
|
const auto i = _data.find(dcId);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto &endpoint : i->second) {
|
|
|
|
const auto flags = endpoint.flags;
|
|
|
|
if (flags & Flag::f_media_only) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:07:34 +00:00
|
|
|
void DcOptions::FilterIfHasWithFlag(Variants &variants, Flag flag) {
|
|
|
|
const auto is = [&](const Endpoint &endpoint) {
|
|
|
|
return (endpoint.flags & flag) != 0;
|
|
|
|
};
|
|
|
|
const auto has = [&](const std::vector<Endpoint> &list) {
|
2020-05-18 19:33:14 +00:00
|
|
|
return ranges::any_of(list, is);
|
2018-05-02 19:07:34 +00:00
|
|
|
};
|
|
|
|
for (auto &byAddress : variants.data) {
|
|
|
|
for (auto &list : byAddress) {
|
|
|
|
if (has(list)) {
|
|
|
|
list = ranges::view::all(
|
|
|
|
list
|
|
|
|
) | ranges::view::filter(
|
|
|
|
is
|
|
|
|
) | ranges::to_vector;
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
void DcOptions::computeCdnDcIds() {
|
|
|
|
_cdnDcIds.clear();
|
|
|
|
for (auto &item : _data) {
|
2018-04-24 19:09:20 +00:00
|
|
|
Assert(!item.second.empty());
|
2018-05-02 19:07:34 +00:00
|
|
|
if (item.second.front().flags & Flag::f_cdn) {
|
2018-06-11 18:35:27 +00:00
|
|
|
_cdnDcIds.insert(BareDcId(item.first));
|
2017-03-23 16:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:22:37 +00:00
|
|
|
bool DcOptions::loadFromFile(const QString &path) {
|
|
|
|
QVector<MTPDcOption> options;
|
|
|
|
|
|
|
|
QFile f(path);
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
LOG(("MTP Error: could not read '%1'").arg(path));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QTextStream stream(&f);
|
|
|
|
stream.setCodec("UTF-8");
|
|
|
|
while (!stream.atEnd()) {
|
|
|
|
auto line = stream.readLine();
|
2020-11-04 15:50:17 +00:00
|
|
|
auto components = line.split(QRegularExpression(R"(\s)"), base::QStringSkipEmptyParts);
|
2017-03-01 17:22:37 +00:00
|
|
|
if (components.isEmpty() || components[0].startsWith('#')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto error = [line] {
|
|
|
|
LOG(("MTP Error: in .tdesktop-endpoints expected 'dcId host port [tcpo_only] [media_only]', got '%1'").arg(line));
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (components.size() < 3) {
|
|
|
|
return error();
|
|
|
|
}
|
|
|
|
auto dcId = components[0].toInt();
|
|
|
|
auto ip = components[1];
|
|
|
|
auto port = components[2].toInt();
|
|
|
|
auto host = QHostAddress();
|
2018-06-11 18:35:27 +00:00
|
|
|
if (dcId <= 0 || dcId >= kDcShift || !host.setAddress(ip) || port <= 0) {
|
2017-03-01 17:22:37 +00:00
|
|
|
return error();
|
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
auto flags = Flags(0);
|
2017-03-01 17:22:37 +00:00
|
|
|
if (host.protocol() == QAbstractSocket::IPv6Protocol) {
|
2018-05-02 19:07:34 +00:00
|
|
|
flags |= Flag::f_ipv6;
|
2017-03-01 17:22:37 +00:00
|
|
|
}
|
|
|
|
for (auto &option : components.mid(3)) {
|
|
|
|
if (option.startsWith('#')) {
|
|
|
|
break;
|
|
|
|
} else if (option == qstr("tcpo_only")) {
|
2018-05-02 19:07:34 +00:00
|
|
|
flags |= Flag::f_tcpo_only;
|
2017-03-01 17:22:37 +00:00
|
|
|
} else if (option == qstr("media_only")) {
|
2018-05-02 19:07:34 +00:00
|
|
|
flags |= Flag::f_media_only;
|
2017-03-01 17:22:37 +00:00
|
|
|
} else {
|
|
|
|
return error();
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 09:24:48 +00:00
|
|
|
options.push_back(MTP_dcOption(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(dcId),
|
|
|
|
MTP_string(ip),
|
|
|
|
MTP_int(port),
|
|
|
|
MTPbytes()));
|
2017-03-01 17:22:37 +00:00
|
|
|
}
|
|
|
|
if (options.isEmpty()) {
|
|
|
|
LOG(("MTP Error: in .tdesktop-endpoints expected at least one endpoint being provided."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_immutable = false;
|
|
|
|
setFromList(MTP_vector<MTPDcOption>(options));
|
|
|
|
_immutable = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DcOptions::writeToFile(const QString &path) const {
|
|
|
|
QFile f(path);
|
|
|
|
if (!f.open(QIODevice::WriteOnly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QTextStream stream(&f);
|
|
|
|
stream.setCodec("UTF-8");
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
ReadLocker lock(this);
|
2018-04-24 19:09:20 +00:00
|
|
|
for (const auto &item : _data) {
|
|
|
|
for (const auto &option : item.second) {
|
|
|
|
stream
|
|
|
|
<< option.id
|
|
|
|
<< ' '
|
|
|
|
<< QString::fromStdString(option.ip)
|
|
|
|
<< ' ' << option.port;
|
2018-05-02 19:07:34 +00:00
|
|
|
if (option.flags & Flag::f_tcpo_only) {
|
2018-04-24 19:09:20 +00:00
|
|
|
stream << " tcpo_only";
|
|
|
|
}
|
2018-05-02 19:07:34 +00:00
|
|
|
if (option.flags & Flag::f_media_only) {
|
2018-04-24 19:09:20 +00:00
|
|
|
stream << " media_only";
|
|
|
|
}
|
|
|
|
stream << '\n';
|
2017-03-01 17:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
} // namespace MTP
|