2014-05-30 08:53:19 +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.
|
2014-05-30 08:53:19 +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
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2019-12-02 13:10:19 +00:00
|
|
|
#include "mtproto/details/mtproto_dcenter.h"
|
2016-03-23 18:43:12 +00:00
|
|
|
|
|
|
|
#include "mtproto/facade.h"
|
2019-11-18 09:28:14 +00:00
|
|
|
#include "mtproto/mtproto_auth_key.h"
|
2017-02-23 06:57:04 +00:00
|
|
|
#include "mtproto/dc_options.h"
|
2017-02-24 17:15:41 +00:00
|
|
|
#include "mtproto/mtp_instance.h"
|
2017-06-26 17:38:16 +00:00
|
|
|
#include "mtproto/special_config_request.h"
|
2014-11-22 09:45:04 +00:00
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
namespace MTP {
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2016-03-24 12:57:10 +00:00
|
|
|
namespace {
|
2017-02-23 06:57:04 +00:00
|
|
|
|
|
|
|
constexpr auto kEnumerateDcTimeout = 8000; // 8 seconds timeout for help_getConfig to work (then move to other dc)
|
2017-06-26 17:38:16 +00:00
|
|
|
constexpr auto kSpecialRequestTimeoutMs = 6000; // 4 seconds timeout for it to work in a specially requested dc.
|
2017-02-23 06:57:04 +00:00
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
int IndexByType(TemporaryKeyType type) {
|
|
|
|
switch (type) {
|
|
|
|
case TemporaryKeyType::Regular: return 0;
|
|
|
|
case TemporaryKeyType::MediaCluster: return 1;
|
|
|
|
}
|
|
|
|
Unexpected("Type value in IndexByType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
int IndexByType(CreatingKeyType type) {
|
|
|
|
switch (type) {
|
|
|
|
case CreatingKeyType::Persistent:
|
|
|
|
case CreatingKeyType::TemporaryRegular: return 0;
|
|
|
|
case CreatingKeyType::TemporaryMediaCluster: return 1;
|
|
|
|
}
|
|
|
|
Unexpected("Creating type value in IndexByType.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *NameOfType(CreatingKeyType type) {
|
|
|
|
switch (type) {
|
|
|
|
case CreatingKeyType::Persistent: return "persistent";
|
|
|
|
case CreatingKeyType::TemporaryRegular: return "regular";
|
|
|
|
case CreatingKeyType::TemporaryMediaCluster: return "media";
|
|
|
|
}
|
|
|
|
Unexpected("Type value in NameOfType.");
|
|
|
|
}
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-12-28 10:52:29 +00:00
|
|
|
|
|
|
|
TemporaryKeyType TemporaryKeyTypeByDcType(DcType type) {
|
|
|
|
return (type == DcType::MediaCluster)
|
|
|
|
? TemporaryKeyType::MediaCluster
|
|
|
|
: TemporaryKeyType::Regular;
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:04:32 +00:00
|
|
|
Dcenter::Dcenter(DcId dcId, AuthKeyPtr &&key)
|
|
|
|
: _id(dcId)
|
2019-11-19 10:10:51 +00:00
|
|
|
, _persistentKey(std::move(key)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:04:32 +00:00
|
|
|
DcId Dcenter::id() const {
|
|
|
|
return _id;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
AuthKeyPtr Dcenter::getTemporaryKey(TemporaryKeyType type) const {
|
2019-11-15 13:04:32 +00:00
|
|
|
QReadLocker lock(&_mutex);
|
2019-11-21 10:37:39 +00:00
|
|
|
return _temporaryKeys[IndexByType(type)];
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
AuthKeyPtr Dcenter::getPersistentKey() const {
|
|
|
|
QReadLocker lock(&_mutex);
|
|
|
|
return _persistentKey;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
bool Dcenter::destroyTemporaryKey(uint64 keyId) {
|
|
|
|
QWriteLocker lock(&_mutex);
|
2019-11-21 10:37:39 +00:00
|
|
|
for (auto &key : _temporaryKeys) {
|
|
|
|
if (key && key->keyId() == keyId) {
|
|
|
|
key = nullptr;
|
|
|
|
_connectionInited = false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-19 10:10:51 +00:00
|
|
|
}
|
2019-11-21 10:37:39 +00:00
|
|
|
return false;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
bool Dcenter::destroyConfirmedForgottenKey(uint64 keyId) {
|
2019-11-15 13:04:32 +00:00
|
|
|
QWriteLocker lock(&_mutex);
|
2019-11-19 10:10:51 +00:00
|
|
|
if (!_persistentKey || _persistentKey->keyId() != keyId) {
|
2019-11-15 13:04:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-21 10:37:39 +00:00
|
|
|
for (auto &key : _temporaryKeys) {
|
|
|
|
key = nullptr;
|
|
|
|
}
|
2019-11-19 10:10:51 +00:00
|
|
|
_persistentKey = nullptr;
|
2019-11-15 13:04:32 +00:00
|
|
|
_connectionInited = false;
|
|
|
|
return true;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 07:28:33 +00:00
|
|
|
bool Dcenter::connectionInited() const {
|
2019-11-15 13:04:32 +00:00
|
|
|
QReadLocker lock(&_mutex);
|
2019-11-15 07:28:33 +00:00
|
|
|
return _connectionInited;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dcenter::setConnectionInited(bool connectionInited) {
|
2019-11-15 13:04:32 +00:00
|
|
|
QWriteLocker lock(&_mutex);
|
2019-11-15 07:28:33 +00:00
|
|
|
_connectionInited = connectionInited;
|
2019-11-15 13:04:32 +00:00
|
|
|
}
|
2019-11-15 07:28:33 +00:00
|
|
|
|
2019-12-28 10:52:29 +00:00
|
|
|
CreatingKeyType Dcenter::acquireKeyCreation(DcType type) {
|
2019-11-15 13:04:32 +00:00
|
|
|
QReadLocker lock(&_mutex);
|
2019-12-28 10:52:29 +00:00
|
|
|
const auto keyType = TemporaryKeyTypeByDcType(type);
|
|
|
|
const auto index = IndexByType(keyType);
|
2019-11-21 10:37:39 +00:00
|
|
|
auto &key = _temporaryKeys[index];
|
|
|
|
if (key != nullptr) {
|
|
|
|
return CreatingKeyType::None;
|
2019-11-15 07:28:33 +00:00
|
|
|
}
|
2019-11-15 13:04:32 +00:00
|
|
|
auto expected = false;
|
2019-11-21 10:37:39 +00:00
|
|
|
const auto regular = IndexByType(TemporaryKeyType::Regular);
|
2019-12-28 10:52:29 +00:00
|
|
|
if (keyType == TemporaryKeyType::MediaCluster && _temporaryKeys[regular]) {
|
2019-11-21 10:37:39 +00:00
|
|
|
return !_creatingKeys[index].compare_exchange_strong(expected, true)
|
|
|
|
? CreatingKeyType::None
|
|
|
|
: CreatingKeyType::TemporaryMediaCluster;
|
|
|
|
}
|
|
|
|
return !_creatingKeys[regular].compare_exchange_strong(expected, true)
|
|
|
|
? CreatingKeyType::None
|
2019-12-28 10:52:29 +00:00
|
|
|
: (type != DcType::Cdn && !_persistentKey)
|
2019-11-21 10:37:39 +00:00
|
|
|
? CreatingKeyType::Persistent
|
|
|
|
: CreatingKeyType::TemporaryRegular;
|
2019-11-15 13:04:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
bool Dcenter::releaseKeyCreationOnDone(
|
|
|
|
CreatingKeyType type,
|
2019-11-19 10:10:51 +00:00
|
|
|
const AuthKeyPtr &temporaryKey,
|
2019-11-21 10:37:39 +00:00
|
|
|
const AuthKeyPtr &persistentKeyUsedForBind) {
|
|
|
|
Expects(_creatingKeys[IndexByType(type)]);
|
|
|
|
Expects(_temporaryKeys[IndexByType(type)] == nullptr);
|
|
|
|
Expects(temporaryKey != nullptr);
|
2019-11-15 13:04:32 +00:00
|
|
|
|
|
|
|
QWriteLocker lock(&_mutex);
|
2019-11-21 10:37:39 +00:00
|
|
|
if (type == CreatingKeyType::Persistent) {
|
|
|
|
_persistentKey = persistentKeyUsedForBind;
|
|
|
|
} else if (_persistentKey != persistentKeyUsedForBind) {
|
|
|
|
return false;
|
2019-11-19 10:10:51 +00:00
|
|
|
}
|
2019-11-21 10:37:39 +00:00
|
|
|
_temporaryKeys[IndexByType(type)] = temporaryKey;
|
|
|
|
_creatingKeys[IndexByType(type)] = false;
|
2019-11-15 13:04:32 +00:00
|
|
|
_connectionInited = false;
|
2019-11-21 10:37:39 +00:00
|
|
|
|
|
|
|
DEBUG_LOG(("AuthKey Info: Dcenter::releaseKeyCreationOnDone(%1, %2, %3)."
|
|
|
|
).arg(NameOfType(type)
|
|
|
|
).arg(temporaryKey ? temporaryKey->keyId() : 0
|
|
|
|
).arg(persistentKeyUsedForBind
|
|
|
|
? persistentKeyUsedForBind->keyId()
|
|
|
|
: 0));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dcenter::releaseKeyCreationOnFail(CreatingKeyType type) {
|
|
|
|
Expects(_creatingKeys[IndexByType(type)]);
|
|
|
|
Expects(_temporaryKeys[IndexByType(type)] == nullptr);
|
|
|
|
|
|
|
|
_creatingKeys[IndexByType(type)] = false;
|
2019-11-15 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2016-03-24 12:57:10 +00:00
|
|
|
} // namespace MTP
|