mirror of https://github.com/procxx/kepka.git
Allow creating temporary keys.
This commit is contained in:
parent
08bfe6f1c1
commit
1524b4a930
|
@ -21,6 +21,8 @@ p_q_inner_data_dc#a9f55f95 pq:string p:string q:string nonce:int128 server_nonce
|
||||||
p_q_inner_data_temp#3c6a84d4 pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 expires_in:int = P_Q_inner_data;
|
p_q_inner_data_temp#3c6a84d4 pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 expires_in:int = P_Q_inner_data;
|
||||||
p_q_inner_data_temp_dc#56fddf88 pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 dc:int expires_in:int = P_Q_inner_data;
|
p_q_inner_data_temp_dc#56fddf88 pq:string p:string q:string nonce:int128 server_nonce:int128 new_nonce:int256 dc:int expires_in:int = P_Q_inner_data;
|
||||||
|
|
||||||
|
bind_auth_key_inner#75a3f765 nonce:long temp_auth_key_id:long perm_auth_key_id:long temp_session_id:long expires_at:int = BindAuthKeyInner;
|
||||||
|
|
||||||
server_DH_params_fail#79cb045d nonce:int128 server_nonce:int128 new_nonce_hash:int128 = Server_DH_Params;
|
server_DH_params_fail#79cb045d nonce:int128 server_nonce:int128 new_nonce_hash:int128 = Server_DH_Params;
|
||||||
server_DH_params_ok#d0e8075c nonce:int128 server_nonce:int128 encrypted_answer:string = Server_DH_Params;
|
server_DH_params_ok#d0e8075c nonce:int128 server_nonce:int128 encrypted_answer:string = Server_DH_Params;
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
namespace MTP {
|
namespace MTP {
|
||||||
namespace details {
|
namespace details {
|
||||||
class DcKeyCreator;
|
class DcKeyCreator;
|
||||||
|
class DcKeyChecker;
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|
||||||
// How much time to wait for some more requests, when sending msg acks.
|
// How much time to wait for some more requests, when sending msg acks.
|
||||||
|
@ -240,9 +241,10 @@ private:
|
||||||
base::Timer _waitForBetterTimer;
|
base::Timer _waitForBetterTimer;
|
||||||
crl::time _waitForReceived = 0;
|
crl::time _waitForReceived = 0;
|
||||||
crl::time _waitForConnected = 0;
|
crl::time _waitForConnected = 0;
|
||||||
crl::time firstSentAt = -1;
|
crl::time _firstSentAt = -1;
|
||||||
|
|
||||||
QVector<MTPlong> ackRequestData, resendRequestData;
|
QVector<MTPlong> _ackRequestData;
|
||||||
|
QVector<MTPlong> _resendRequestData;
|
||||||
|
|
||||||
mtpPingId _pingId = 0;
|
mtpPingId _pingId = 0;
|
||||||
mtpPingId _pingIdToSend = 0;
|
mtpPingId _pingIdToSend = 0;
|
||||||
|
@ -250,15 +252,15 @@ private:
|
||||||
mtpMsgId _pingMsgId = 0;
|
mtpMsgId _pingMsgId = 0;
|
||||||
base::Timer _pingSender;
|
base::Timer _pingSender;
|
||||||
|
|
||||||
bool restarted = false;
|
bool _restarted = false;
|
||||||
bool _finished = false;
|
bool _finished = false;
|
||||||
|
|
||||||
uint64 keyId = 0;
|
uint64 _keyId = 0;
|
||||||
QReadWriteLock sessionDataMutex;
|
QReadWriteLock _sessionDataMutex;
|
||||||
SessionData *sessionData = nullptr;
|
SessionData *_sessionData = nullptr;
|
||||||
std::unique_ptr<ConnectionOptions> _connectionOptions;
|
std::unique_ptr<ConnectionOptions> _connectionOptions;
|
||||||
|
|
||||||
bool myKeyLock = false;
|
bool _myKeyLock = false;
|
||||||
|
|
||||||
std::unique_ptr<details::DcKeyCreator> _keyCreator;
|
std::unique_ptr<details::DcKeyCreator> _keyCreator;
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,7 @@ 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;
|
||||||
|
|
|
@ -69,8 +69,9 @@ struct ParsedPQ {
|
||||||
return { pStr, qStr };
|
return { pStr, qStr };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename PQInnerData>
|
||||||
[[nodiscard]] bytes::vector EncryptPQInnerRSA(
|
[[nodiscard]] bytes::vector EncryptPQInnerRSA(
|
||||||
const MTPP_Q_inner_data &data,
|
const PQInnerData &data,
|
||||||
const RSAPublicKey &key) {
|
const RSAPublicKey &key) {
|
||||||
constexpr auto kSkipPrimes = 6;
|
constexpr auto kSkipPrimes = 6;
|
||||||
constexpr auto kMaxPrimes = 65; // 260 bytes
|
constexpr auto kMaxPrimes = 65; // 260 bytes
|
||||||
|
@ -150,12 +151,15 @@ DcKeyCreator::DcKeyCreator(
|
||||||
int16 protocolDcId,
|
int16 protocolDcId,
|
||||||
not_null<AbstractConnection*> connection,
|
not_null<AbstractConnection*> connection,
|
||||||
not_null<DcOptions*> dcOptions,
|
not_null<DcOptions*> dcOptions,
|
||||||
Delegate delegate)
|
Delegate delegate,
|
||||||
|
TimeId expireIn)
|
||||||
: _connection(connection)
|
: _connection(connection)
|
||||||
, _dcOptions(dcOptions)
|
, _dcOptions(dcOptions)
|
||||||
, _dcId(dcId)
|
, _dcId(dcId)
|
||||||
, _protocolDcId(protocolDcId)
|
, _protocolDcId(protocolDcId)
|
||||||
|
, _expireIn(expireIn)
|
||||||
, _delegate(std::move(delegate)) {
|
, _delegate(std::move(delegate)) {
|
||||||
|
Expects(_expireIn >= 0);
|
||||||
Expects(_delegate.done != nullptr);
|
Expects(_delegate.done != nullptr);
|
||||||
|
|
||||||
_data.nonce = openssl::RandomValue<MTPint128>();
|
_data.nonce = openssl::RandomValue<MTPint128>();
|
||||||
|
@ -219,15 +223,30 @@ void DcKeyCreator::pqAnswered() {
|
||||||
return failed();
|
return failed();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto p_q_inner = MTP_p_q_inner_data_dc(
|
const auto dhEncString = [&] {
|
||||||
|
return (_expireIn == 0)
|
||||||
|
? EncryptPQInnerRSA(
|
||||||
|
MTP_p_q_inner_data_dc(
|
||||||
res_pq_data.vpq(),
|
res_pq_data.vpq(),
|
||||||
MTP_bytes(parsed.p),
|
MTP_bytes(parsed.p),
|
||||||
MTP_bytes(parsed.q),
|
MTP_bytes(parsed.q),
|
||||||
_data.nonce,
|
_data.nonce,
|
||||||
_data.server_nonce,
|
_data.server_nonce,
|
||||||
_data.new_nonce,
|
_data.new_nonce,
|
||||||
MTP_int(_protocolDcId));
|
MTP_int(_protocolDcId)),
|
||||||
const auto dhEncString = EncryptPQInnerRSA(p_q_inner, rsaKey);
|
rsaKey)
|
||||||
|
: EncryptPQInnerRSA(
|
||||||
|
MTP_p_q_inner_data_temp_dc(
|
||||||
|
res_pq_data.vpq(),
|
||||||
|
MTP_bytes(parsed.p),
|
||||||
|
MTP_bytes(parsed.q),
|
||||||
|
_data.nonce,
|
||||||
|
_data.server_nonce,
|
||||||
|
_data.new_nonce,
|
||||||
|
MTP_int(_protocolDcId),
|
||||||
|
MTP_int(_expireIn)),
|
||||||
|
rsaKey);
|
||||||
|
}();
|
||||||
if (dhEncString.empty()) {
|
if (dhEncString.empty()) {
|
||||||
return failed();
|
return failed();
|
||||||
}
|
}
|
||||||
|
@ -241,8 +260,8 @@ void DcKeyCreator::pqAnswered() {
|
||||||
sendNotSecureRequest(MTPReq_DH_params(
|
sendNotSecureRequest(MTPReq_DH_params(
|
||||||
_data.nonce,
|
_data.nonce,
|
||||||
_data.server_nonce,
|
_data.server_nonce,
|
||||||
p_q_inner.c_p_q_inner_data_dc().vp(),
|
MTP_bytes(parsed.p),
|
||||||
p_q_inner.c_p_q_inner_data_dc().vq(),
|
MTP_bytes(parsed.q),
|
||||||
MTP_long(rsaKey.fingerprint()),
|
MTP_long(rsaKey.fingerprint()),
|
||||||
MTP_bytes(dhEncString)));
|
MTP_bytes(dhEncString)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,8 @@ public:
|
||||||
int16 protocolDcId,
|
int16 protocolDcId,
|
||||||
not_null<AbstractConnection*> connection,
|
not_null<AbstractConnection*> connection,
|
||||||
not_null<DcOptions*> dcOptions,
|
not_null<DcOptions*> dcOptions,
|
||||||
Delegate delegate);
|
Delegate delegate,
|
||||||
|
TimeId expireIn = 0); // 0 - persistent, > 0 - temporary
|
||||||
~DcKeyCreator();
|
~DcKeyCreator();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -87,8 +88,9 @@ private:
|
||||||
|
|
||||||
const not_null<AbstractConnection*> _connection;
|
const not_null<AbstractConnection*> _connection;
|
||||||
const not_null<DcOptions*> _dcOptions;
|
const not_null<DcOptions*> _dcOptions;
|
||||||
const DcId _dcId;
|
const DcId _dcId = 0;
|
||||||
const int16 _protocolDcId = 0;
|
const int16 _protocolDcId = 0;
|
||||||
|
const TimeId _expireIn = 0;
|
||||||
Delegate _delegate;
|
Delegate _delegate;
|
||||||
|
|
||||||
Data _data;
|
Data _data;
|
||||||
|
|
|
@ -64,13 +64,13 @@ ConnectionOptions::ConnectionOptions(
|
||||||
|
|
||||||
void SessionData::setKey(const AuthKeyPtr &key) {
|
void SessionData::setKey(const AuthKeyPtr &key) {
|
||||||
if (_authKey != key) {
|
if (_authKey != key) {
|
||||||
uint64 session = rand_value<uint64>();
|
const auto sessionId = rand_value<uint64>();
|
||||||
_authKey = key;
|
_authKey = key;
|
||||||
|
|
||||||
DEBUG_LOG(("MTP Info: new auth key set in SessionData, id %1, setting random server_session %2").arg(key ? key->keyId() : 0).arg(session));
|
DEBUG_LOG(("MTP Info: new auth key set in SessionData, id %1, setting random server_session %2").arg(key ? key->keyId() : 0).arg(sessionId));
|
||||||
QWriteLocker locker(&_lock);
|
QWriteLocker locker(&_lock);
|
||||||
if (_session != session) {
|
if (_sessionId != sessionId) {
|
||||||
_session = session;
|
_sessionId = sessionId;
|
||||||
_messagesSent = 0;
|
_messagesSent = 0;
|
||||||
}
|
}
|
||||||
_layerInited = false;
|
_layerInited = false;
|
||||||
|
@ -135,13 +135,14 @@ void SessionData::clear(Instance *instance) {
|
||||||
instance->clearCallbacksDelayed(std::move(clearCallbacks));
|
instance->clearCallbacksDelayed(std::move(clearCallbacks));
|
||||||
}
|
}
|
||||||
|
|
||||||
Session::Session(not_null<Instance*> instance, ShiftedDcId shiftedDcId) : QObject()
|
Session::Session(not_null<Instance*> instance, ShiftedDcId shiftedDcId)
|
||||||
|
: QObject()
|
||||||
, _instance(instance)
|
, _instance(instance)
|
||||||
, data(this)
|
, _data(this)
|
||||||
, dcWithShift(shiftedDcId)
|
, _shiftedDcId(shiftedDcId)
|
||||||
, sender([=] { needToResumeAndSend(); }) {
|
, _sender([=] { needToResumeAndSend(); }) {
|
||||||
connect(&timeouter, SIGNAL(timeout()), this, SLOT(checkRequestsByTimer()));
|
connect(&_timeouter, SIGNAL(timeout()), this, SLOT(checkRequestsByTimer()));
|
||||||
timeouter.start(1000);
|
_timeouter.start(1000);
|
||||||
|
|
||||||
refreshOptions();
|
refreshOptions();
|
||||||
}
|
}
|
||||||
|
@ -149,26 +150,26 @@ Session::Session(not_null<Instance*> instance, ShiftedDcId shiftedDcId) : QObjec
|
||||||
void Session::start() {
|
void Session::start() {
|
||||||
createDcData();
|
createDcData();
|
||||||
_connection = std::make_unique<Connection>(_instance);
|
_connection = std::make_unique<Connection>(_instance);
|
||||||
_connection->start(&data, dcWithShift);
|
_connection->start(&_data, _shiftedDcId);
|
||||||
if (_instance->isKeysDestroyer()) {
|
if (_instance->isKeysDestroyer()) {
|
||||||
_instance->scheduleKeyDestroy(dcWithShift);
|
_instance->scheduleKeyDestroy(_shiftedDcId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::createDcData() {
|
void Session::createDcData() {
|
||||||
if (dc) {
|
if (_dc) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
dc = _instance->getDcById(dcWithShift);
|
_dc = _instance->getDcById(_shiftedDcId);
|
||||||
|
|
||||||
if (auto lock = ReadLockerAttempt(keyMutex())) {
|
if (auto lock = ReadLockerAttempt(keyMutex())) {
|
||||||
data.setKey(dc->getKey());
|
_data.setKey(_dc->getKey());
|
||||||
if (dc->connectionInited()) {
|
if (_dc->connectionInited()) {
|
||||||
data.setConnectionInited();
|
_data.setConnectionInited();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
connect(dc.get(), SIGNAL(authKeyCreated()), this, SLOT(authKeyCreatedForDC()), Qt::QueuedConnection);
|
connect(_dc.get(), SIGNAL(authKeyCreated()), this, SLOT(authKeyCreatedForDC()), Qt::QueuedConnection);
|
||||||
connect(dc.get(), SIGNAL(connectionWasInited()), this, SLOT(connectionWasInitedForDC()), Qt::QueuedConnection);
|
connect(_dc.get(), SIGNAL(connectionWasInited()), this, SLOT(connectionWasInitedForDC()), Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Session::rpcErrorOccured(
|
bool Session::rpcErrorOccured(
|
||||||
|
@ -197,7 +198,7 @@ void Session::refreshOptions() {
|
||||||
const auto useHttp = (proxyType != ProxyData::Type::Mtproto);
|
const auto useHttp = (proxyType != ProxyData::Type::Mtproto);
|
||||||
const auto useIPv4 = true;
|
const auto useIPv4 = true;
|
||||||
const auto useIPv6 = Global::TryIPv6();
|
const auto useIPv6 = Global::TryIPv6();
|
||||||
data.applyConnectionOptions(ConnectionOptions(
|
_data.applyConnectionOptions(ConnectionOptions(
|
||||||
_instance->systemLangCode(),
|
_instance->systemLangCode(),
|
||||||
_instance->cloudLangCode(),
|
_instance->cloudLangCode(),
|
||||||
_instance->langPackName(),
|
_instance->langPackName(),
|
||||||
|
@ -211,8 +212,8 @@ void Session::refreshOptions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::reInitConnection() {
|
void Session::reInitConnection() {
|
||||||
dc->setConnectionInited(false);
|
_dc->setConnectionInited(false);
|
||||||
data.setConnectionInited(false);
|
_data.setConnectionInited(false);
|
||||||
restart();
|
restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +222,7 @@ void Session::stop() {
|
||||||
DEBUG_LOG(("Session Error: can't kill a killed session"));
|
DEBUG_LOG(("Session Error: can't kill a killed session"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DEBUG_LOG(("Session Info: stopping session dcWithShift %1").arg(dcWithShift));
|
DEBUG_LOG(("Session Info: stopping session dcWithShift %1").arg(_shiftedDcId));
|
||||||
if (_connection) {
|
if (_connection) {
|
||||||
_connection->kill();
|
_connection->kill();
|
||||||
_instance->queueQuittingConnection(std::move(_connection));
|
_instance->queueQuittingConnection(std::move(_connection));
|
||||||
|
@ -231,7 +232,7 @@ void Session::stop() {
|
||||||
void Session::kill() {
|
void Session::kill() {
|
||||||
stop();
|
stop();
|
||||||
_killed = true;
|
_killed = true;
|
||||||
DEBUG_LOG(("Session Info: marked session dcWithShift %1 as killed").arg(dcWithShift));
|
DEBUG_LOG(("Session Info: marked session dcWithShift %1 as killed").arg(_shiftedDcId));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::unpaused() {
|
void Session::unpaused() {
|
||||||
|
@ -246,27 +247,27 @@ void Session::sendAnything(qint64 msCanWait) {
|
||||||
DEBUG_LOG(("Session Error: can't send anything in a killed session"));
|
DEBUG_LOG(("Session Error: can't send anything in a killed session"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto ms = crl::now();
|
const auto ms = crl::now();
|
||||||
if (msSendCall) {
|
if (_msSendCall) {
|
||||||
if (ms > msSendCall + msWait) {
|
if (ms > _msSendCall + _msWait) {
|
||||||
msWait = 0;
|
_msWait = 0;
|
||||||
} else {
|
} else {
|
||||||
msWait = (msSendCall + msWait) - ms;
|
_msWait = (_msSendCall + _msWait) - ms;
|
||||||
if (msWait > msCanWait) {
|
if (_msWait > msCanWait) {
|
||||||
msWait = msCanWait;
|
_msWait = msCanWait;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msWait = msCanWait;
|
_msWait = msCanWait;
|
||||||
}
|
}
|
||||||
if (msWait) {
|
if (_msWait) {
|
||||||
DEBUG_LOG(("MTP Info: dcWithShift %1 can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall));
|
DEBUG_LOG(("MTP Info: dcWithShift %1 can wait for %2ms from current %3").arg(_shiftedDcId).arg(_msWait).arg(_msSendCall));
|
||||||
msSendCall = ms;
|
_msSendCall = ms;
|
||||||
sender.callOnce(msWait);
|
_sender.callOnce(_msWait);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_LOG(("MTP Info: dcWithShift %1 stopped send timer, can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall));
|
DEBUG_LOG(("MTP Info: dcWithShift %1 stopped send timer, can wait for %2ms from current %3").arg(_shiftedDcId).arg(_msWait).arg(_msSendCall));
|
||||||
sender.cancel();
|
_sender.cancel();
|
||||||
msSendCall = 0;
|
_msSendCall = 0;
|
||||||
needToResumeAndSend();
|
needToResumeAndSend();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,10 +278,10 @@ void Session::needToResumeAndSend() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!_connection) {
|
if (!_connection) {
|
||||||
DEBUG_LOG(("Session Info: resuming session dcWithShift %1").arg(dcWithShift));
|
DEBUG_LOG(("Session Info: resuming session dcWithShift %1").arg(_shiftedDcId));
|
||||||
createDcData();
|
createDcData();
|
||||||
_connection = std::make_unique<Connection>(_instance);
|
_connection = std::make_unique<Connection>(_instance);
|
||||||
_connection->start(&data, dcWithShift);
|
_connection->start(&_data, _shiftedDcId);
|
||||||
}
|
}
|
||||||
if (_ping) {
|
if (_ping) {
|
||||||
_ping = false;
|
_ping = false;
|
||||||
|
@ -292,7 +293,7 @@ void Session::needToResumeAndSend() {
|
||||||
|
|
||||||
void Session::sendPong(quint64 msgId, quint64 pingId) {
|
void Session::sendPong(quint64 msgId, quint64 pingId) {
|
||||||
_instance->sendProtocolMessage(
|
_instance->sendProtocolMessage(
|
||||||
dcWithShift,
|
_shiftedDcId,
|
||||||
MTPPong(MTP_pong(MTP_long(msgId), MTP_long(pingId))));
|
MTPPong(MTP_pong(MTP_long(msgId), MTP_long(pingId))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,7 +304,7 @@ void Session::sendMsgsStateInfo(quint64 msgId, QByteArray data) {
|
||||||
bytes::copy(info, bytes::make_span(data));
|
bytes::copy(info, bytes::make_span(data));
|
||||||
}
|
}
|
||||||
_instance->sendProtocolMessage(
|
_instance->sendProtocolMessage(
|
||||||
dcWithShift,
|
_shiftedDcId,
|
||||||
MTPMsgsStateInfo(
|
MTPMsgsStateInfo(
|
||||||
MTP_msgs_state_info(MTP_long(msgId), MTP_bytes(data))));
|
MTP_msgs_state_info(MTP_long(msgId), MTP_bytes(data))));
|
||||||
}
|
}
|
||||||
|
@ -314,8 +315,8 @@ void Session::checkRequestsByTimer() {
|
||||||
QVector<mtpMsgId> stateRequestIds;
|
QVector<mtpMsgId> stateRequestIds;
|
||||||
|
|
||||||
{
|
{
|
||||||
QReadLocker locker(data.haveSentMutex());
|
QReadLocker locker(_data.haveSentMutex());
|
||||||
auto &haveSent = data.haveSentMap();
|
auto &haveSent = _data.haveSentMap();
|
||||||
const auto haveSentCount = haveSent.size();
|
const auto haveSentCount = haveSent.size();
|
||||||
auto ms = crl::now();
|
auto ms = crl::now();
|
||||||
for (auto i = haveSent.begin(), e = haveSent.end(); i != e; ++i) {
|
for (auto i = haveSent.begin(), e = haveSent.end(); i != e; ++i) {
|
||||||
|
@ -342,9 +343,9 @@ void Session::checkRequestsByTimer() {
|
||||||
if (stateRequestIds.size()) {
|
if (stateRequestIds.size()) {
|
||||||
DEBUG_LOG(("MTP Info: requesting state of msgs: %1").arg(LogIds(stateRequestIds)));
|
DEBUG_LOG(("MTP Info: requesting state of msgs: %1").arg(LogIds(stateRequestIds)));
|
||||||
{
|
{
|
||||||
QWriteLocker locker(data.stateRequestMutex());
|
QWriteLocker locker(_data.stateRequestMutex());
|
||||||
for (uint32 i = 0, l = stateRequestIds.size(); i < l; ++i) {
|
for (uint32 i = 0, l = stateRequestIds.size(); i < l; ++i) {
|
||||||
data.stateRequestMap().insert(stateRequestIds[i], true);
|
_data.stateRequestMap().insert(stateRequestIds[i], true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendAnything(kCheckResendWaiting);
|
sendAnything(kCheckResendWaiting);
|
||||||
|
@ -358,8 +359,8 @@ void Session::checkRequestsByTimer() {
|
||||||
if (!removingIds.isEmpty()) {
|
if (!removingIds.isEmpty()) {
|
||||||
auto clearCallbacks = std::vector<RPCCallbackClear>();
|
auto clearCallbacks = std::vector<RPCCallbackClear>();
|
||||||
{
|
{
|
||||||
QWriteLocker locker(data.haveSentMutex());
|
QWriteLocker locker(_data.haveSentMutex());
|
||||||
auto &haveSent = data.haveSentMap();
|
auto &haveSent = _data.haveSentMap();
|
||||||
for (uint32 i = 0, l = removingIds.size(); i < l; ++i) {
|
for (uint32 i = 0, l = removingIds.size(); i < l; ++i) {
|
||||||
auto j = haveSent.find(removingIds[i]);
|
auto j = haveSent.find(removingIds[i]);
|
||||||
if (j != haveSent.cend()) {
|
if (j != haveSent.cend()) {
|
||||||
|
@ -375,21 +376,21 @@ void Session::checkRequestsByTimer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::onConnectionStateChange(qint32 newState) {
|
void Session::onConnectionStateChange(qint32 newState) {
|
||||||
_instance->onStateChange(dcWithShift, newState);
|
_instance->onStateChange(_shiftedDcId, newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::onResetDone() {
|
void Session::onResetDone() {
|
||||||
_instance->onSessionReset(dcWithShift);
|
_instance->onSessionReset(_shiftedDcId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::cancel(mtpRequestId requestId, mtpMsgId msgId) {
|
void Session::cancel(mtpRequestId requestId, mtpMsgId msgId) {
|
||||||
if (requestId) {
|
if (requestId) {
|
||||||
QWriteLocker locker(data.toSendMutex());
|
QWriteLocker locker(_data.toSendMutex());
|
||||||
data.toSendMap().remove(requestId);
|
_data.toSendMap().remove(requestId);
|
||||||
}
|
}
|
||||||
if (msgId) {
|
if (msgId) {
|
||||||
QWriteLocker locker(data.haveSentMutex());
|
QWriteLocker locker(_data.haveSentMutex());
|
||||||
data.haveSentMap().remove(msgId);
|
_data.haveSentMap().remove(msgId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,8 +422,8 @@ int32 Session::requestState(mtpRequestId requestId) const {
|
||||||
}
|
}
|
||||||
if (!requestId) return MTP::RequestSent;
|
if (!requestId) return MTP::RequestSent;
|
||||||
|
|
||||||
QWriteLocker locker(data.toSendMutex());
|
QWriteLocker locker(_data.toSendMutex());
|
||||||
const auto &toSend = data.toSendMap();
|
const auto &toSend = _data.toSendMap();
|
||||||
const auto i = toSend.constFind(requestId);
|
const auto i = toSend.constFind(requestId);
|
||||||
if (i != toSend.cend()) {
|
if (i != toSend.cend()) {
|
||||||
return MTP::RequestSending;
|
return MTP::RequestSending;
|
||||||
|
@ -461,8 +462,8 @@ QString Session::transport() const {
|
||||||
mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContainer, bool sendMsgStateInfo) {
|
mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContainer, bool sendMsgStateInfo) {
|
||||||
SecureRequest request;
|
SecureRequest request;
|
||||||
{
|
{
|
||||||
QWriteLocker locker(data.haveSentMutex());
|
QWriteLocker locker(_data.haveSentMutex());
|
||||||
auto &haveSent = data.haveSentMap();
|
auto &haveSent = _data.haveSentMap();
|
||||||
|
|
||||||
auto i = haveSent.find(msgId);
|
auto i = haveSent.find(msgId);
|
||||||
if (i == haveSent.end()) {
|
if (i == haveSent.end()) {
|
||||||
|
@ -472,7 +473,7 @@ mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContaine
|
||||||
|
|
||||||
auto info = std::string(cantResend, cantResend + 1);
|
auto info = std::string(cantResend, cantResend + 1);
|
||||||
return _instance->sendProtocolMessage(
|
return _instance->sendProtocolMessage(
|
||||||
dcWithShift,
|
_shiftedDcId,
|
||||||
MTPMsgsStateInfo(
|
MTPMsgsStateInfo(
|
||||||
MTP_msgs_state_info(
|
MTP_msgs_state_info(
|
||||||
MTP_long(msgId),
|
MTP_long(msgId),
|
||||||
|
@ -495,8 +496,8 @@ mtpRequestId Session::resend(quint64 msgId, qint64 msCanWait, bool forceContaine
|
||||||
request->msDate = forceContainer ? 0 : crl::now();
|
request->msDate = forceContainer ? 0 : crl::now();
|
||||||
sendPrepared(request, msCanWait, false);
|
sendPrepared(request, msCanWait, false);
|
||||||
{
|
{
|
||||||
QWriteLocker locker(data.toResendMutex());
|
QWriteLocker locker(_data.toResendMutex());
|
||||||
data.toResendMap().insert(msgId, request->requestId);
|
_data.toResendMap().insert(msgId, request->requestId);
|
||||||
}
|
}
|
||||||
return request->requestId;
|
return request->requestId;
|
||||||
} else {
|
} else {
|
||||||
|
@ -513,8 +514,8 @@ void Session::resendMany(QVector<quint64> msgIds, qint64 msCanWait, bool forceCo
|
||||||
void Session::resendAll() {
|
void Session::resendAll() {
|
||||||
QVector<mtpMsgId> toResend;
|
QVector<mtpMsgId> toResend;
|
||||||
{
|
{
|
||||||
QReadLocker locker(data.haveSentMutex());
|
QReadLocker locker(_data.haveSentMutex());
|
||||||
const auto &haveSent = data.haveSentMap();
|
const auto &haveSent = _data.haveSentMap();
|
||||||
toResend.reserve(haveSent.size());
|
toResend.reserve(haveSent.size());
|
||||||
for (auto i = haveSent.cbegin(), e = haveSent.cend(); i != e; ++i) {
|
for (auto i = haveSent.cbegin(), e = haveSent.cend(); i != e; ++i) {
|
||||||
if (i.value()->requestId) {
|
if (i.value()->requestId) {
|
||||||
|
@ -534,8 +535,8 @@ void Session::sendPrepared(
|
||||||
DEBUG_LOG(("MTP Info: adding request to toSendMap, msCanWait %1"
|
DEBUG_LOG(("MTP Info: adding request to toSendMap, msCanWait %1"
|
||||||
).arg(msCanWait));
|
).arg(msCanWait));
|
||||||
{
|
{
|
||||||
QWriteLocker locker(data.toSendMutex());
|
QWriteLocker locker(_data.toSendMutex());
|
||||||
data.toSendMap().insert(request->requestId, request);
|
_data.toSendMap().insert(request->requestId, request);
|
||||||
|
|
||||||
if (newRequest) {
|
if (newRequest) {
|
||||||
*(mtpMsgId*)(request->data() + 4) = 0;
|
*(mtpMsgId*)(request->data() + 4) = 0;
|
||||||
|
@ -549,45 +550,47 @@ void Session::sendPrepared(
|
||||||
}
|
}
|
||||||
|
|
||||||
QReadWriteLock *Session::keyMutex() const {
|
QReadWriteLock *Session::keyMutex() const {
|
||||||
return dc->keyMutex();
|
return _dc->keyMutex();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::authKeyCreatedForDC() {
|
void Session::authKeyCreatedForDC() {
|
||||||
DEBUG_LOG(("AuthKey Info: Session::authKeyCreatedForDC slot, emitting authKeyCreated(), dcWithShift %1").arg(dcWithShift));
|
DEBUG_LOG(("AuthKey Info: Session::authKeyCreatedForDC slot, emitting authKeyCreated(), dcWithShift %1").arg(_shiftedDcId));
|
||||||
data.setKey(dc->getKey());
|
_data.setKey(_dc->getKey());
|
||||||
emit authKeyCreated();
|
emit authKeyCreated();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::notifyKeyCreated(AuthKeyPtr &&key) {
|
void Session::notifyKeyCreated(AuthKeyPtr &&key) {
|
||||||
DEBUG_LOG(("AuthKey Info: Session::keyCreated(), setting, dcWithShift %1").arg(dcWithShift));
|
DEBUG_LOG(("AuthKey Info: Session::keyCreated(), setting, dcWithShift %1").arg(_shiftedDcId));
|
||||||
dc->setKey(std::move(key));
|
_dc->setKey(std::move(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::connectionWasInitedForDC() {
|
void Session::connectionWasInitedForDC() {
|
||||||
DEBUG_LOG(("MTP Info: Session::connectionWasInitedForDC slot, dcWithShift %1").arg(dcWithShift));
|
DEBUG_LOG(("MTP Info: Session::connectionWasInitedForDC slot, dcWithShift %1").arg(_shiftedDcId));
|
||||||
data.setConnectionInited();
|
_data.setConnectionInited();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::notifyDcConnectionInited() {
|
void Session::notifyDcConnectionInited() {
|
||||||
DEBUG_LOG(("MTP Info: emitting MTProtoDC::connectionWasInited(), dcWithShift %1").arg(dcWithShift));
|
DEBUG_LOG(("MTP Info: emitting MTProtoDC::connectionWasInited(), dcWithShift %1").arg(_shiftedDcId));
|
||||||
dc->setConnectionInited();
|
_dc->setConnectionInited();
|
||||||
emit dc->connectionWasInited();
|
emit _dc->connectionWasInited();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::destroyKey() {
|
void Session::destroyKey() {
|
||||||
if (!dc) return;
|
if (!_dc) {
|
||||||
|
return;
|
||||||
if (data.getKey()) {
|
|
||||||
DEBUG_LOG(("MTP Info: destroying auth_key for dcWithShift %1").arg(dcWithShift));
|
|
||||||
if (data.getKey() == dc->getKey()) {
|
|
||||||
dc->destroyKey();
|
|
||||||
}
|
}
|
||||||
data.setKey(AuthKeyPtr());
|
|
||||||
|
if (_data.getKey()) {
|
||||||
|
DEBUG_LOG(("MTP Info: destroying auth_key for dcWithShift %1").arg(_shiftedDcId));
|
||||||
|
if (_data.getKey() == _dc->getKey()) {
|
||||||
|
_dc->destroyKey();
|
||||||
|
}
|
||||||
|
_data.setKey(AuthKeyPtr());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int32 Session::getDcWithShift() const {
|
int32 Session::getDcWithShift() const {
|
||||||
return dcWithShift;
|
return _shiftedDcId;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::tryToReceive() {
|
void Session::tryToReceive() {
|
||||||
|
@ -604,11 +607,11 @@ void Session::tryToReceive() {
|
||||||
auto isUpdate = false;
|
auto isUpdate = false;
|
||||||
auto message = SerializedMessage();
|
auto message = SerializedMessage();
|
||||||
{
|
{
|
||||||
QWriteLocker locker(data.haveReceivedMutex());
|
QWriteLocker locker(_data.haveReceivedMutex());
|
||||||
auto &responses = data.haveReceivedResponses();
|
auto &responses = _data.haveReceivedResponses();
|
||||||
auto response = responses.begin();
|
auto response = responses.begin();
|
||||||
if (response == responses.cend()) {
|
if (response == responses.cend()) {
|
||||||
auto &updates = data.haveReceivedUpdates();
|
auto &updates = _data.haveReceivedUpdates();
|
||||||
auto update = updates.begin();
|
auto update = updates.begin();
|
||||||
if (update == updates.cend()) {
|
if (update == updates.cend()) {
|
||||||
return;
|
return;
|
||||||
|
@ -624,7 +627,7 @@ void Session::tryToReceive() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isUpdate) {
|
if (isUpdate) {
|
||||||
if (dcWithShift == BareDcId(dcWithShift)) { // call globalCallback only in main session
|
if (_shiftedDcId == BareDcId(_shiftedDcId)) { // call globalCallback only in main session
|
||||||
_instance->globalCallback(message.constData(), message.constData() + message.size());
|
_instance->globalCallback(message.constData(), message.constData() + message.size());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -141,18 +141,18 @@ public:
|
||||||
SessionData(not_null<Session*> creator) : _owner(creator) {
|
SessionData(not_null<Session*> creator) : _owner(creator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSession(uint64 session) {
|
void setSessionId(uint64 sessionId) {
|
||||||
DEBUG_LOG(("MTP Info: setting server_session: %1").arg(session));
|
DEBUG_LOG(("MTP Info: setting server_session: %1").arg(sessionId));
|
||||||
|
|
||||||
QWriteLocker locker(&_lock);
|
QWriteLocker locker(&_lock);
|
||||||
if (_session != session) {
|
if (_sessionId != sessionId) {
|
||||||
_session = session;
|
_sessionId = sessionId;
|
||||||
_messagesSent = 0;
|
_messagesSent = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
uint64 getSession() const {
|
uint64 getSessionId() const {
|
||||||
QReadLocker locker(&_lock);
|
QReadLocker locker(&_lock);
|
||||||
return _session;
|
return _sessionId;
|
||||||
}
|
}
|
||||||
void setConnectionInited(bool inited = true) {
|
void setConnectionInited(bool inited = true) {
|
||||||
QWriteLocker locker(&_lock);
|
QWriteLocker locker(&_lock);
|
||||||
|
@ -283,7 +283,7 @@ public:
|
||||||
void clear(Instance *instance);
|
void clear(Instance *instance);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64 _session = 0;
|
uint64 _sessionId = 0;
|
||||||
uint64 _salt = 0;
|
uint64 _salt = 0;
|
||||||
|
|
||||||
uint32 _messagesSent = 0;
|
uint32 _messagesSent = 0;
|
||||||
|
@ -389,18 +389,18 @@ private:
|
||||||
bool _killed = false;
|
bool _killed = false;
|
||||||
bool _needToReceive = false;
|
bool _needToReceive = false;
|
||||||
|
|
||||||
SessionData data;
|
SessionData _data;
|
||||||
|
|
||||||
ShiftedDcId dcWithShift = 0;
|
ShiftedDcId _shiftedDcId = 0;
|
||||||
std::shared_ptr<Dcenter> dc;
|
std::shared_ptr<Dcenter> _dc;
|
||||||
|
|
||||||
crl::time msSendCall = 0;
|
crl::time _msSendCall = 0;
|
||||||
crl::time msWait = 0;
|
crl::time _msWait = 0;
|
||||||
|
|
||||||
bool _ping = false;
|
bool _ping = false;
|
||||||
|
|
||||||
QTimer timeouter;
|
QTimer _timeouter;
|
||||||
base::Timer sender;
|
base::Timer _sender;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue