mirror of https://github.com/procxx/kepka.git
Fix destroyed key clearing.
This commit is contained in:
parent
4c24ec7725
commit
4edfd4804f
|
@ -378,13 +378,12 @@ void Account::startMtp() {
|
||||||
|
|
||||||
|
|
||||||
void Account::logOut() {
|
void Account::logOut() {
|
||||||
|
if (_loggingOut) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_loggingOut = true;
|
||||||
if (_mtp) {
|
if (_mtp) {
|
||||||
_mtp->logout(::rpcDone([=] {
|
_mtp->logout([=] { loggedOut(); });
|
||||||
loggedOut();
|
|
||||||
}), ::rpcFail([=] {
|
|
||||||
loggedOut();
|
|
||||||
return true;
|
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
// We log out because we've forgotten passcode.
|
// We log out because we've forgotten passcode.
|
||||||
loggedOut();
|
loggedOut();
|
||||||
|
@ -399,6 +398,7 @@ void Account::forcedLogOut() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Account::loggedOut() {
|
void Account::loggedOut() {
|
||||||
|
_loggingOut = false;
|
||||||
if (Global::LocalPasscode()) {
|
if (Global::LocalPasscode()) {
|
||||||
Global::SetLocalPasscode(false);
|
Global::SetLocalPasscode(false);
|
||||||
Global::RefLocalPasscodeChanged().notify();
|
Global::RefLocalPasscodeChanged().notify();
|
||||||
|
|
|
@ -97,6 +97,7 @@ private:
|
||||||
std::unique_ptr<Settings> _storedSettings;
|
std::unique_ptr<Settings> _storedSettings;
|
||||||
MTP::Instance::Config _mtpConfig;
|
MTP::Instance::Config _mtpConfig;
|
||||||
MTP::AuthKeysList _mtpKeysToDestroy;
|
MTP::AuthKeysList _mtpKeysToDestroy;
|
||||||
|
bool _loggingOut = false;
|
||||||
|
|
||||||
rpl::lifetime _lifetime;
|
rpl::lifetime _lifetime;
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ constexpr auto kTemporaryExpiresIn = TimeId(10);
|
||||||
constexpr auto kBindKeyAdditionalExpiresTimeout = TimeId(30);
|
constexpr auto kBindKeyAdditionalExpiresTimeout = TimeId(30);
|
||||||
constexpr auto kTestModeDcIdShift = 10000;
|
constexpr auto kTestModeDcIdShift = 10000;
|
||||||
constexpr auto kCheckSentRequestsEach = 1 * crl::time(1000);
|
constexpr auto kCheckSentRequestsEach = 1 * crl::time(1000);
|
||||||
|
constexpr auto kKeyOldEnoughForDestroy = 60 * crl::time(1000);
|
||||||
|
|
||||||
// If we can't connect for this time we will ask _instance to update config.
|
// If we can't connect for this time we will ask _instance to update config.
|
||||||
constexpr auto kRequestConfigTimeout = 8 * crl::time(1000);
|
constexpr auto kRequestConfigTimeout = 8 * crl::time(1000);
|
||||||
|
@ -997,7 +998,7 @@ void ConnectionPrivate::connectToServer(bool afterConfig) {
|
||||||
if (_testConnections.empty()) {
|
if (_testConnections.empty()) {
|
||||||
if (_instance->isKeysDestroyer()) {
|
if (_instance->isKeysDestroyer()) {
|
||||||
LOG(("MTP Error: DC %1 options for not found for auth key destruction!").arg(_shiftedDcId));
|
LOG(("MTP Error: DC %1 options for not found for auth key destruction!").arg(_shiftedDcId));
|
||||||
_instance->checkIfKeyWasDestroyed(_shiftedDcId);
|
_instance->keyWasPossiblyDestroyed(_shiftedDcId);
|
||||||
return;
|
return;
|
||||||
} else if (afterConfig) {
|
} else if (afterConfig) {
|
||||||
LOG(("MTP Error: DC %1 options for not found right after config load!").arg(_shiftedDcId));
|
LOG(("MTP Error: DC %1 options for not found right after config load!").arg(_shiftedDcId));
|
||||||
|
@ -1857,19 +1858,19 @@ ConnectionPrivate::HandleResult ConnectionPrivate::handleOneReceived(const mtpPr
|
||||||
const auto result = _keyCreator->handleBindResponse(
|
const auto result = _keyCreator->handleBindResponse(
|
||||||
reqMsgId,
|
reqMsgId,
|
||||||
response);
|
response);
|
||||||
if (result == DcKeyBindState::Success) {
|
switch (result) {
|
||||||
|
case DcKeyBindState::Success:
|
||||||
_sessionData->releaseKeyCreationOnDone(
|
_sessionData->releaseKeyCreationOnDone(
|
||||||
_encryptionKey,
|
_encryptionKey,
|
||||||
base::take(_keyCreator)->bindPersistentKey());
|
base::take(_keyCreator)->bindPersistentKey());
|
||||||
_sessionData->queueNeedToResumeAndSend();
|
_sessionData->queueNeedToResumeAndSend();
|
||||||
return HandleResult::Success;
|
return HandleResult::Success;
|
||||||
} else if (result == DcKeyBindState::Failed
|
case DcKeyBindState::DefinitelyDestroyed:
|
||||||
|| result == DcKeyBindState::DefinitelyDestroyed) {
|
if (destroyOldEnoughPersistentKey()) {
|
||||||
// #TODO maybe destroy persistent key
|
return HandleResult::DestroyTemporaryKey;
|
||||||
// crl::on_main(
|
}
|
||||||
// _keyCreator->bindPersistentKey()->setLastCheckTime(crl::now());
|
[[fallthrough]];
|
||||||
// instance->keyDestroyedOnServer(BareDcId(shiftedDcId), base::take(_keyCreator)->bindPersistentKey()->keyId());
|
case DcKeyBindState::Failed:
|
||||||
// )
|
|
||||||
_sessionData->queueNeedToResumeAndSend();
|
_sessionData->queueNeedToResumeAndSend();
|
||||||
return HandleResult::Success;
|
return HandleResult::Success;
|
||||||
}
|
}
|
||||||
|
@ -2362,7 +2363,7 @@ void ConnectionPrivate::applyAuthKey(AuthKeyPtr &&encryptionKey) {
|
||||||
if (_instance->isKeysDestroyer()) {
|
if (_instance->isKeysDestroyer()) {
|
||||||
// We are here to destroy an old key, so we're done.
|
// We are here to destroy an old key, so we're done.
|
||||||
LOG(("MTP Error: No key %1 in updateAuthKey() for destroying.").arg(_shiftedDcId));
|
LOG(("MTP Error: No key %1 in updateAuthKey() for destroying.").arg(_shiftedDcId));
|
||||||
_instance->checkIfKeyWasDestroyed(_shiftedDcId);
|
_instance->keyWasPossiblyDestroyed(_shiftedDcId);
|
||||||
} else if (_keyCreator) {
|
} else if (_keyCreator) {
|
||||||
DEBUG_LOG(("AuthKey Info: No key in updateAuthKey(), creating."));
|
DEBUG_LOG(("AuthKey Info: No key in updateAuthKey(), creating."));
|
||||||
_keyCreator->start(
|
_keyCreator->start(
|
||||||
|
@ -2375,6 +2376,25 @@ void ConnectionPrivate::applyAuthKey(AuthKeyPtr &&encryptionKey) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ConnectionPrivate::destroyOldEnoughPersistentKey() {
|
||||||
|
Expects(_keyCreator != nullptr);
|
||||||
|
|
||||||
|
const auto key = _keyCreator->bindPersistentKey();
|
||||||
|
Assert(key != nullptr);
|
||||||
|
|
||||||
|
const auto created = key->creationTime();
|
||||||
|
if (created > 0 && crl::now() - created < kKeyOldEnoughForDestroy) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto instance = _instance;
|
||||||
|
const auto shiftedDcId = _shiftedDcId;
|
||||||
|
const auto keyId = key->keyId();
|
||||||
|
InvokeQueued(instance, [=] {
|
||||||
|
instance->keyDestroyedOnServer(shiftedDcId, keyId);
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::tryAcquireKeyCreation() {
|
void ConnectionPrivate::tryAcquireKeyCreation() {
|
||||||
if (_instance->isKeysDestroyer()
|
if (_instance->isKeysDestroyer()
|
||||||
|| _keyCreator
|
|| _keyCreator
|
||||||
|
@ -2436,7 +2456,6 @@ void ConnectionPrivate::tryAcquireKeyCreation() {
|
||||||
onReceivedSome();
|
onReceivedSome();
|
||||||
};
|
};
|
||||||
|
|
||||||
// const auto check = (GetDcIdShift(_shiftedDcId) == kCheckKeyDcShift); // #TODO remove kCheckKeyDcShift
|
|
||||||
auto request = DcKeyRequest();
|
auto request = DcKeyRequest();
|
||||||
request.persistentNeeded = !_sessionData->getPersistentKey();
|
request.persistentNeeded = !_sessionData->getPersistentKey();
|
||||||
request.temporaryExpiresIn = kTemporaryExpiresIn;
|
request.temporaryExpiresIn = kTemporaryExpiresIn;
|
||||||
|
@ -2493,7 +2512,7 @@ void ConnectionPrivate::handleError(int errorCode) {
|
||||||
void ConnectionPrivate::destroyTemporaryKey() {
|
void ConnectionPrivate::destroyTemporaryKey() {
|
||||||
if (_instance->isKeysDestroyer()) {
|
if (_instance->isKeysDestroyer()) {
|
||||||
LOG(("MTP Info: -404 error received in destroyer %1, assuming key was destroyed.").arg(_shiftedDcId));
|
LOG(("MTP Info: -404 error received in destroyer %1, assuming key was destroyed.").arg(_shiftedDcId));
|
||||||
_instance->checkIfKeyWasDestroyed(_shiftedDcId);
|
_instance->keyWasPossiblyDestroyed(_shiftedDcId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG(("MTP Info: -404 error received in %1 with temporary key, assuming it was destroyed.").arg(_shiftedDcId));
|
LOG(("MTP Info: -404 error received in %1 with temporary key, assuming it was destroyed.").arg(_shiftedDcId));
|
||||||
|
|
|
@ -189,6 +189,7 @@ private:
|
||||||
void clearUnboundKeyCreator();
|
void clearUnboundKeyCreator();
|
||||||
void releaseKeyCreationOnFail();
|
void releaseKeyCreationOnFail();
|
||||||
void applyAuthKey(AuthKeyPtr &&encryptionKey);
|
void applyAuthKey(AuthKeyPtr &&encryptionKey);
|
||||||
|
bool destroyOldEnoughPersistentKey();
|
||||||
|
|
||||||
void setCurrentKeyId(uint64 newKeyId);
|
void setCurrentKeyId(uint64 newKeyId);
|
||||||
void changeSessionId();
|
void changeSessionId();
|
||||||
|
|
|
@ -42,7 +42,6 @@ constexpr auto kLogoutDcShift = 0x02;
|
||||||
constexpr auto kUpdaterDcShift = 0x03;
|
constexpr auto kUpdaterDcShift = 0x03;
|
||||||
constexpr auto kExportDcShift = 0x04;
|
constexpr auto kExportDcShift = 0x04;
|
||||||
constexpr auto kExportMediaDcShift = 0x05;
|
constexpr auto kExportMediaDcShift = 0x05;
|
||||||
constexpr auto kCheckKeyDcShift = 0x06;
|
|
||||||
constexpr auto kMaxMediaDcCount = 0x10;
|
constexpr auto kMaxMediaDcCount = 0x10;
|
||||||
constexpr auto kBaseDownloadDcShift = 0x10;
|
constexpr auto kBaseDownloadDcShift = 0x10;
|
||||||
constexpr auto kBaseUploadDcShift = 0x20;
|
constexpr auto kBaseUploadDcShift = 0x20;
|
||||||
|
|
|
@ -95,7 +95,7 @@ public:
|
||||||
void killSession(ShiftedDcId shiftedDcId);
|
void killSession(ShiftedDcId shiftedDcId);
|
||||||
void stopSession(ShiftedDcId shiftedDcId);
|
void stopSession(ShiftedDcId shiftedDcId);
|
||||||
void reInitConnection(DcId dcId);
|
void reInitConnection(DcId dcId);
|
||||||
void logout(RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail);
|
void logout(Fn<void()> done);
|
||||||
|
|
||||||
not_null<Dcenter*> getDcById(ShiftedDcId shiftedDcId);
|
not_null<Dcenter*> getDcById(ShiftedDcId shiftedDcId);
|
||||||
Dcenter *findDc(ShiftedDcId shiftedDcId);
|
Dcenter *findDc(ShiftedDcId shiftedDcId);
|
||||||
|
@ -154,11 +154,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void scheduleKeyDestroy(ShiftedDcId shiftedDcId);
|
void scheduleKeyDestroy(ShiftedDcId shiftedDcId);
|
||||||
void checkIfKeyWasDestroyed(ShiftedDcId shiftedDcId);
|
void keyWasPossiblyDestroyed(ShiftedDcId shiftedDcId);
|
||||||
void performKeyDestroy(ShiftedDcId shiftedDcId);
|
void performKeyDestroy(ShiftedDcId shiftedDcId);
|
||||||
void completedKeyDestroy(ShiftedDcId shiftedDcId);
|
void completedKeyDestroy(ShiftedDcId shiftedDcId);
|
||||||
void checkMainDcKey();
|
void keyDestroyedOnServer(ShiftedDcId shiftedDcId, uint64 keyId);
|
||||||
void keyDestroyedOnServer(DcId dcId, uint64 keyId);
|
|
||||||
|
|
||||||
void prepareToDestroy();
|
void prepareToDestroy();
|
||||||
|
|
||||||
|
@ -609,10 +608,13 @@ void Instance::Private::reInitConnection(DcId dcId) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Private::logout(
|
void Instance::Private::logout(Fn<void()> done) {
|
||||||
RPCDoneHandlerPtr onDone,
|
_instance->send(MTPauth_LogOut(), rpcDone([=] {
|
||||||
RPCFailHandlerPtr onFail) {
|
done();
|
||||||
_instance->send(MTPauth_LogOut(), std::move(onDone), std::move(onFail));
|
}), rpcFail([=] {
|
||||||
|
done();
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
logoutGuestDcs();
|
logoutGuestDcs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1502,14 +1504,16 @@ Session *Instance::Private::findSession(ShiftedDcId shiftedDcId) {
|
||||||
not_null<Session*> Instance::Private::startSession(ShiftedDcId shiftedDcId) {
|
not_null<Session*> Instance::Private::startSession(ShiftedDcId shiftedDcId) {
|
||||||
Expects(BareDcId(shiftedDcId) != 0);
|
Expects(BareDcId(shiftedDcId) != 0);
|
||||||
|
|
||||||
const auto dc = (GetDcIdShift(shiftedDcId) != kCheckKeyDcShift)
|
const auto dc = getDcById(shiftedDcId);
|
||||||
? getDcById(shiftedDcId).get()
|
|
||||||
: nullptr;
|
|
||||||
const auto result = _sessions.emplace(
|
const auto result = _sessions.emplace(
|
||||||
shiftedDcId,
|
shiftedDcId,
|
||||||
std::make_unique<Session>(_instance, shiftedDcId, dc)
|
std::make_unique<Session>(_instance, shiftedDcId, dc)
|
||||||
).first->second.get();
|
).first->second.get();
|
||||||
result->start();
|
result->start();
|
||||||
|
if (isKeysDestroyer()) {
|
||||||
|
scheduleKeyDestroy(shiftedDcId);
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1542,24 +1546,20 @@ void Instance::Private::scheduleKeyDestroy(ShiftedDcId shiftedDcId) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Private::checkIfKeyWasDestroyed(ShiftedDcId shiftedDcId) {
|
void Instance::Private::keyWasPossiblyDestroyed(ShiftedDcId shiftedDcId) {
|
||||||
|
Expects(isKeysDestroyer());
|
||||||
|
|
||||||
InvokeQueued(_instance, [=] {
|
InvokeQueued(_instance, [=] {
|
||||||
if (isKeysDestroyer()) {
|
|
||||||
LOG(("MTP Info: checkIfKeyWasDestroyed on destroying key %1, "
|
LOG(("MTP Info: checkIfKeyWasDestroyed on destroying key %1, "
|
||||||
"assuming it is destroyed.").arg(shiftedDcId));
|
"assuming it is destroyed.").arg(shiftedDcId));
|
||||||
completedKeyDestroy(shiftedDcId);
|
completedKeyDestroy(shiftedDcId);
|
||||||
} else if (BareDcId(shiftedDcId) == mainDcId()) {
|
|
||||||
LOG(("MTP Info: checkIfKeyWasDestroyed for main dc %1, "
|
|
||||||
"checking.").arg(shiftedDcId));
|
|
||||||
checkMainDcKey();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Private::performKeyDestroy(ShiftedDcId shiftedDcId) {
|
void Instance::Private::performKeyDestroy(ShiftedDcId shiftedDcId) {
|
||||||
Expects(isKeysDestroyer());
|
Expects(isKeysDestroyer());
|
||||||
|
|
||||||
_instance->send(MTPDestroy_auth_key(), rpcDone([this, shiftedDcId](const MTPDestroyAuthKeyRes &result) {
|
_instance->send(MTPDestroy_auth_key(), rpcDone([=](const MTPDestroyAuthKeyRes &result) {
|
||||||
switch (result.type()) {
|
switch (result.type()) {
|
||||||
case mtpc_destroy_auth_key_ok: LOG(("MTP Info: key %1 destroyed.").arg(shiftedDcId)); break;
|
case mtpc_destroy_auth_key_ok: LOG(("MTP Info: key %1 destroyed.").arg(shiftedDcId)); break;
|
||||||
case mtpc_destroy_auth_key_fail: {
|
case mtpc_destroy_auth_key_fail: {
|
||||||
|
@ -1568,10 +1568,10 @@ void Instance::Private::performKeyDestroy(ShiftedDcId shiftedDcId) {
|
||||||
} break;
|
} break;
|
||||||
case mtpc_destroy_auth_key_none: LOG(("MTP Info: key %1 already destroyed.").arg(shiftedDcId)); break;
|
case mtpc_destroy_auth_key_none: LOG(("MTP Info: key %1 already destroyed.").arg(shiftedDcId)); break;
|
||||||
}
|
}
|
||||||
_instance->checkIfKeyWasDestroyed(shiftedDcId);
|
_instance->keyWasPossiblyDestroyed(shiftedDcId);
|
||||||
}), rpcFail([this, shiftedDcId](const RPCError &error) {
|
}), rpcFail([=](const RPCError &error) {
|
||||||
LOG(("MTP Error: key %1 destruction resulted in error: %2").arg(shiftedDcId).arg(error.type()));
|
LOG(("MTP Error: key %1 destruction resulted in error: %2").arg(shiftedDcId).arg(error.type()));
|
||||||
_instance->checkIfKeyWasDestroyed(shiftedDcId);
|
_instance->keyWasPossiblyDestroyed(shiftedDcId);
|
||||||
return true;
|
return true;
|
||||||
}), shiftedDcId);
|
}), shiftedDcId);
|
||||||
}
|
}
|
||||||
|
@ -1587,34 +1587,19 @@ void Instance::Private::completedKeyDestroy(ShiftedDcId shiftedDcId) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Private::checkMainDcKey() {
|
void Instance::Private::keyDestroyedOnServer(
|
||||||
const auto id = mainDcId();
|
ShiftedDcId shiftedDcId,
|
||||||
const auto shiftedDcId = ShiftDcId(id, kCheckKeyDcShift);
|
uint64 keyId) {
|
||||||
if (findSession(shiftedDcId)) {
|
LOG(("Destroying key for dc: %1").arg(shiftedDcId));
|
||||||
return;
|
if (const auto dc = findDc(BareDcId(shiftedDcId))) {
|
||||||
}
|
|
||||||
const auto i = _keysForWrite.find(id);
|
|
||||||
if (i == end(_keysForWrite)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const auto lastCheckTime = i->second->lastCheckTime();
|
|
||||||
if (lastCheckTime > 0 && lastCheckTime + kCheckKeyEach >= crl::now()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_instance->sendDcKeyCheck(shiftedDcId, i->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Instance::Private::keyDestroyedOnServer(DcId dcId, uint64 keyId) {
|
|
||||||
LOG(("Destroying key for dc: %1").arg(dcId));
|
|
||||||
if (const auto dc = findDc(dcId)) {
|
|
||||||
if (dc->destroyConfirmedForgottenKey(keyId)) {
|
if (dc->destroyConfirmedForgottenKey(keyId)) {
|
||||||
LOG(("Key destroyed!"));
|
LOG(("Key destroyed!"));
|
||||||
dcPersistentKeyChanged(dcId, nullptr);
|
dcPersistentKeyChanged(BareDcId(shiftedDcId), nullptr);
|
||||||
} else {
|
} else {
|
||||||
LOG(("Key already is different."));
|
LOG(("Key already is different."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
restart(dcId);
|
restart(shiftedDcId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::Private::setUpdatesHandler(RPCDoneHandlerPtr onDone) {
|
void Instance::Private::setUpdatesHandler(RPCDoneHandlerPtr onDone) {
|
||||||
|
@ -1758,8 +1743,8 @@ void Instance::reInitConnection(DcId dcId) {
|
||||||
_private->reInitConnection(dcId);
|
_private->reInitConnection(dcId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::logout(RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail) {
|
void Instance::logout(Fn<void()> done) {
|
||||||
_private->logout(onDone, onFail);
|
_private->logout(std::move(done));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::dcPersistentKeyChanged(
|
void Instance::dcPersistentKeyChanged(
|
||||||
|
@ -1857,16 +1842,12 @@ bool Instance::isKeysDestroyer() const {
|
||||||
return _private->isKeysDestroyer();
|
return _private->isKeysDestroyer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::scheduleKeyDestroy(ShiftedDcId shiftedDcId) {
|
void Instance::keyWasPossiblyDestroyed(ShiftedDcId shiftedDcId) {
|
||||||
_private->scheduleKeyDestroy(shiftedDcId);
|
_private->keyWasPossiblyDestroyed(shiftedDcId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::checkIfKeyWasDestroyed(ShiftedDcId shiftedDcId) {
|
void Instance::keyDestroyedOnServer(ShiftedDcId shiftedDcId, uint64 keyId) {
|
||||||
_private->checkIfKeyWasDestroyed(shiftedDcId);
|
_private->keyDestroyedOnServer(shiftedDcId, keyId);
|
||||||
}
|
|
||||||
|
|
||||||
void Instance::keyDestroyedOnServer(DcId dcId, uint64 keyId) {
|
|
||||||
_private->keyDestroyedOnServer(dcId, keyId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::sendRequest(
|
void Instance::sendRequest(
|
||||||
|
@ -1891,10 +1872,6 @@ void Instance::sendAnything(ShiftedDcId shiftedDcId, crl::time msCanWait) {
|
||||||
_private->getSession(shiftedDcId)->sendAnything(msCanWait);
|
_private->getSession(shiftedDcId)->sendAnything(msCanWait);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instance::sendDcKeyCheck(ShiftedDcId shiftedDcId, const AuthKeyPtr &key) {
|
|
||||||
_private->getSession(shiftedDcId)->sendDcKeyCheck(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
Instance::~Instance() {
|
Instance::~Instance() {
|
||||||
_private->prepareToDestroy();
|
_private->prepareToDestroy();
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,7 +85,7 @@ public:
|
||||||
void killSession(ShiftedDcId shiftedDcId);
|
void killSession(ShiftedDcId shiftedDcId);
|
||||||
void stopSession(ShiftedDcId shiftedDcId);
|
void stopSession(ShiftedDcId shiftedDcId);
|
||||||
void reInitConnection(DcId dcId);
|
void reInitConnection(DcId dcId);
|
||||||
void logout(RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail);
|
void logout(Fn<void()> done);
|
||||||
|
|
||||||
void unpaused();
|
void unpaused();
|
||||||
|
|
||||||
|
@ -110,12 +110,12 @@ public:
|
||||||
// return true if need to clean request data
|
// return true if need to clean request data
|
||||||
bool rpcErrorOccured(mtpRequestId requestId, const RPCFailHandlerPtr &onFail, const RPCError &err);
|
bool rpcErrorOccured(mtpRequestId requestId, const RPCFailHandlerPtr &onFail, const RPCError &err);
|
||||||
|
|
||||||
|
// Thread-safe.
|
||||||
bool isKeysDestroyer() const;
|
bool isKeysDestroyer() const;
|
||||||
void scheduleKeyDestroy(ShiftedDcId shiftedDcId);
|
void keyWasPossiblyDestroyed(ShiftedDcId shiftedDcId);
|
||||||
void checkIfKeyWasDestroyed(ShiftedDcId shiftedDcId);
|
|
||||||
|
|
||||||
// Main thread.
|
// Main thread.
|
||||||
void keyDestroyedOnServer(DcId dcId, uint64 keyId);
|
void keyDestroyedOnServer(ShiftedDcId shiftedDcId, uint64 keyId);
|
||||||
|
|
||||||
void requestConfig();
|
void requestConfig();
|
||||||
void requestConfigIfOld();
|
void requestConfigIfOld();
|
||||||
|
@ -128,7 +128,6 @@ public:
|
||||||
void connectionFinished(not_null<internal::Connection*> connection);
|
void connectionFinished(not_null<internal::Connection*> connection);
|
||||||
|
|
||||||
void sendAnything(ShiftedDcId shiftedDcId = 0, crl::time msCanWait = 0);
|
void sendAnything(ShiftedDcId shiftedDcId = 0, crl::time msCanWait = 0);
|
||||||
void sendDcKeyCheck(ShiftedDcId shiftedDcId, const AuthKeyPtr &key);
|
|
||||||
|
|
||||||
template <typename Request>
|
template <typename Request>
|
||||||
mtpRequestId send(
|
mtpRequestId send(
|
||||||
|
|
|
@ -19,7 +19,7 @@ AuthKey::AuthKey(Type type, DcId dcId, const Data &data)
|
||||||
, _key(data) {
|
, _key(data) {
|
||||||
countKeyId();
|
countKeyId();
|
||||||
if (type == Type::Generated || type == Type::Temporary) {
|
if (type == Type::Generated || type == Type::Temporary) {
|
||||||
_lastCheckTime = crl::now();
|
_creationTime = crl::now();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,12 +115,8 @@ bool AuthKey::equals(const std::shared_ptr<AuthKey> &other) const {
|
||||||
return other ? (_key == other->_key) : false;
|
return other ? (_key == other->_key) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
crl::time AuthKey::lastCheckTime() const {
|
crl::time AuthKey::creationTime() const {
|
||||||
return _lastCheckTime;
|
return _creationTime;
|
||||||
}
|
|
||||||
|
|
||||||
void AuthKey::setLastCheckTime(crl::time time) {
|
|
||||||
_lastCheckTime = time;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeId AuthKey::expiresAt() const {
|
TimeId AuthKey::expiresAt() const {
|
||||||
|
|
|
@ -44,8 +44,7 @@ public:
|
||||||
[[nodiscard]] bytes::const_span data() const;
|
[[nodiscard]] bytes::const_span data() const;
|
||||||
[[nodiscard]] bool equals(const std::shared_ptr<AuthKey> &other) const;
|
[[nodiscard]] bool equals(const std::shared_ptr<AuthKey> &other) const;
|
||||||
|
|
||||||
[[nodiscard]] crl::time lastCheckTime() const;
|
[[nodiscard]] crl::time creationTime() const; // > 0 if known.
|
||||||
void setLastCheckTime(crl::time time);
|
|
||||||
[[nodiscard]] TimeId expiresAt() const;
|
[[nodiscard]] TimeId expiresAt() const;
|
||||||
void setExpiresAt(TimeId expiresAt);
|
void setExpiresAt(TimeId expiresAt);
|
||||||
|
|
||||||
|
@ -58,7 +57,7 @@ private:
|
||||||
DcId _dcId = 0;
|
DcId _dcId = 0;
|
||||||
Data _key = { { gsl::byte{} } };
|
Data _key = { { gsl::byte{} } };
|
||||||
KeyId _keyId = 0;
|
KeyId _keyId = 0;
|
||||||
crl::time _lastCheckTime = 0;
|
crl::time _creationTime = 0;
|
||||||
TimeId _expiresAt = 0;
|
TimeId _expiresAt = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,10 +50,6 @@ void SessionData::withSession(Callback &&callback) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SessionData::setKeyForCheck(const AuthKeyPtr &key) {
|
|
||||||
_dcKeyForCheck = key;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SessionData::notifyConnectionInited(const ConnectionOptions &options) {
|
void SessionData::notifyConnectionInited(const ConnectionOptions &options) {
|
||||||
// #TODO race
|
// #TODO race
|
||||||
const auto current = connectionOptions();
|
const auto current = connectionOptions();
|
||||||
|
@ -61,7 +57,10 @@ void SessionData::notifyConnectionInited(const ConnectionOptions &options) {
|
||||||
&& current.systemLangCode == _options.systemLangCode
|
&& current.systemLangCode == _options.systemLangCode
|
||||||
&& current.langPackName == _options.langPackName
|
&& current.langPackName == _options.langPackName
|
||||||
&& current.proxy == _options.proxy) {
|
&& current.proxy == _options.proxy) {
|
||||||
owner()->notifyDcConnectionInited();
|
QMutexLocker lock(&_ownerMutex);
|
||||||
|
if (_owner) {
|
||||||
|
_owner->notifyDcConnectionInited();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,20 +210,17 @@ void SessionData::detach() {
|
||||||
Session::Session(
|
Session::Session(
|
||||||
not_null<Instance*> instance,
|
not_null<Instance*> instance,
|
||||||
ShiftedDcId shiftedDcId,
|
ShiftedDcId shiftedDcId,
|
||||||
Dcenter *dc)
|
not_null<Dcenter*> dc)
|
||||||
: QObject()
|
: QObject()
|
||||||
, _instance(instance)
|
, _instance(instance)
|
||||||
, _shiftedDcId(shiftedDcId)
|
, _shiftedDcId(shiftedDcId)
|
||||||
, _ownedDc(dc ? nullptr : std::make_unique<Dcenter>(shiftedDcId, nullptr))
|
, _dc(dc)
|
||||||
, _dc(dc ? dc : _ownedDc.get())
|
|
||||||
, _data(std::make_shared<SessionData>(this))
|
, _data(std::make_shared<SessionData>(this))
|
||||||
, _sender([=] { needToResumeAndSend(); }) {
|
, _sender([=] { needToResumeAndSend(); }) {
|
||||||
_timeouter.callEach(1000);
|
_timeouter.callEach(1000);
|
||||||
refreshOptions();
|
refreshOptions();
|
||||||
if (sharedDc()) {
|
|
||||||
watchDcKeyChanges();
|
watchDcKeyChanges();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Session::watchDcKeyChanges() {
|
void Session::watchDcKeyChanges() {
|
||||||
_instance->dcTemporaryKeyChanged(
|
_instance->dcTemporaryKeyChanged(
|
||||||
|
@ -240,9 +236,6 @@ void Session::watchDcKeyChanges() {
|
||||||
void Session::start() {
|
void Session::start() {
|
||||||
_connection = std::make_unique<Connection>(_instance);
|
_connection = std::make_unique<Connection>(_instance);
|
||||||
_connection->start(_data, _shiftedDcId);
|
_connection->start(_data, _shiftedDcId);
|
||||||
if (_instance->isKeysDestroyer()) {
|
|
||||||
_instance->scheduleKeyDestroy(_shiftedDcId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Session::rpcErrorOccured(
|
bool Session::rpcErrorOccured(
|
||||||
|
@ -317,11 +310,6 @@ void Session::unpaused() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::sendDcKeyCheck(const AuthKeyPtr &key) {
|
|
||||||
_data->setKeyForCheck(key);
|
|
||||||
needToResumeAndSend();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Session::sendAnything(crl::time msCanWait) {
|
void Session::sendAnything(crl::time msCanWait) {
|
||||||
if (_killed) {
|
if (_killed) {
|
||||||
DEBUG_LOG(("Session Error: can't send anything in a killed session"));
|
DEBUG_LOG(("Session Error: can't send anything in a killed session"));
|
||||||
|
@ -381,10 +369,6 @@ void Session::sendMsgsStateInfo(quint64 msgId, QByteArray data) {
|
||||||
MTP_msgs_state_info(MTP_long(msgId), MTP_bytes(data))));
|
MTP_msgs_state_info(MTP_long(msgId), MTP_bytes(data))));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Session::sharedDc() const {
|
|
||||||
return (_ownedDc == nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Session::connectionStateChange(int newState) {
|
void Session::connectionStateChange(int newState) {
|
||||||
_instance->onStateChange(_shiftedDcId, newState);
|
_instance->onStateChange(_shiftedDcId, newState);
|
||||||
}
|
}
|
||||||
|
@ -565,7 +549,6 @@ void Session::releaseKeyCreationOnDone(
|
||||||
_dc->releaseKeyCreationOnDone(temporaryKey, persistentKey);
|
_dc->releaseKeyCreationOnDone(temporaryKey, persistentKey);
|
||||||
_myKeyCreation = false;
|
_myKeyCreation = false;
|
||||||
|
|
||||||
if (sharedDc()) {
|
|
||||||
const auto dcId = _dc->id();
|
const auto dcId = _dc->id();
|
||||||
const auto instance = _instance;
|
const auto instance = _instance;
|
||||||
InvokeQueued(instance, [=] {
|
InvokeQueued(instance, [=] {
|
||||||
|
@ -576,7 +559,6 @@ void Session::releaseKeyCreationOnDone(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Session::releaseKeyCreationOnFail() {
|
void Session::releaseKeyCreationOnFail() {
|
||||||
Expects(_myKeyCreation);
|
Expects(_myKeyCreation);
|
||||||
|
@ -594,14 +576,12 @@ void Session::destroyTemporaryKey(uint64 keyId) {
|
||||||
if (!_dc->destroyTemporaryKey(keyId)) {
|
if (!_dc->destroyTemporaryKey(keyId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (sharedDc()) {
|
|
||||||
const auto dcId = _dc->id();
|
const auto dcId = _dc->id();
|
||||||
const auto instance = _instance;
|
const auto instance = _instance;
|
||||||
InvokeQueued(instance, [=] {
|
InvokeQueued(instance, [=] {
|
||||||
instance->dcTemporaryKeyChanged(dcId);
|
instance->dcTemporaryKeyChanged(dcId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int32 Session::getDcWithShift() const {
|
int32 Session::getDcWithShift() const {
|
||||||
return _shiftedDcId;
|
return _shiftedDcId;
|
||||||
|
|
|
@ -93,11 +93,6 @@ public:
|
||||||
return _options;
|
return _options;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const AuthKeyPtr &getKeyForCheck() const {
|
|
||||||
return _dcKeyForCheck;
|
|
||||||
}
|
|
||||||
void setKeyForCheck(const AuthKeyPtr &key);
|
|
||||||
|
|
||||||
not_null<QReadWriteLock*> toSendMutex() const {
|
not_null<QReadWriteLock*> toSendMutex() const {
|
||||||
return &_toSendLock;
|
return &_toSendLock;
|
||||||
}
|
}
|
||||||
|
@ -190,7 +185,6 @@ private:
|
||||||
Session *_owner = nullptr;
|
Session *_owner = nullptr;
|
||||||
mutable QMutex _ownerMutex;
|
mutable QMutex _ownerMutex;
|
||||||
|
|
||||||
AuthKeyPtr _dcKeyForCheck;
|
|
||||||
ConnectionOptions _options;
|
ConnectionOptions _options;
|
||||||
|
|
||||||
PreRequestMap _toSend; // map of request_id -> request, that is waiting to be sent
|
PreRequestMap _toSend; // map of request_id -> request, that is waiting to be sent
|
||||||
|
@ -219,7 +213,7 @@ public:
|
||||||
Session(
|
Session(
|
||||||
not_null<Instance*> instance,
|
not_null<Instance*> instance,
|
||||||
ShiftedDcId shiftedDcId,
|
ShiftedDcId shiftedDcId,
|
||||||
Dcenter *dc);
|
not_null<Dcenter*> dc);
|
||||||
~Session();
|
~Session();
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
|
@ -266,8 +260,6 @@ public:
|
||||||
int32 getState() const;
|
int32 getState() const;
|
||||||
QString transport() const;
|
QString transport() const;
|
||||||
|
|
||||||
void sendDcKeyCheck(const AuthKeyPtr &key);
|
|
||||||
|
|
||||||
void tryToReceive();
|
void tryToReceive();
|
||||||
void needToResumeAndSend();
|
void needToResumeAndSend();
|
||||||
void connectionStateChange(int newState);
|
void connectionStateChange(int newState);
|
||||||
|
@ -282,14 +274,12 @@ signals:
|
||||||
void needToRestart();
|
void needToRestart();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
[[nodiscard]] bool sharedDc() const;
|
|
||||||
void watchDcKeyChanges();
|
void watchDcKeyChanges();
|
||||||
|
|
||||||
bool rpcErrorOccured(mtpRequestId requestId, const RPCFailHandlerPtr &onFail, const RPCError &err);
|
bool rpcErrorOccured(mtpRequestId requestId, const RPCFailHandlerPtr &onFail, const RPCError &err);
|
||||||
|
|
||||||
const not_null<Instance*> _instance;
|
const not_null<Instance*> _instance;
|
||||||
const ShiftedDcId _shiftedDcId = 0;
|
const ShiftedDcId _shiftedDcId = 0;
|
||||||
const std::unique_ptr<Dcenter> _ownedDc;
|
|
||||||
const not_null<Dcenter*> _dc;
|
const not_null<Dcenter*> _dc;
|
||||||
const std::shared_ptr<SessionData> _data;
|
const std::shared_ptr<SessionData> _data;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue