2016-03-24 12:57:10 +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.
|
2016-03-24 12:57:10 +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
|
2016-03-24 12:57:10 +00:00
|
|
|
*/
|
|
|
|
#include "mtproto/connection_abstract.h"
|
|
|
|
|
|
|
|
#include "mtproto/connection_tcp.h"
|
|
|
|
#include "mtproto/connection_http.h"
|
2018-05-17 19:58:00 +00:00
|
|
|
#include "mtproto/connection_resolving.h"
|
2018-04-24 08:46:27 +00:00
|
|
|
#include "mtproto/session.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2021-09-15 10:21:45 +00:00
|
|
|
#include "base/random.h"
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
namespace MTP {
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2022-02-07 11:24:02 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::atomic<int> GlobalConnectionCounter/* = 0*/;
|
|
|
|
|
|
|
|
} // namespace
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
ConnectionPointer::ConnectionPointer() = default;
|
|
|
|
|
|
|
|
ConnectionPointer::ConnectionPointer(std::nullptr_t) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionPointer::ConnectionPointer(AbstractConnection *value)
|
|
|
|
: _value(value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionPointer::ConnectionPointer(ConnectionPointer &&other)
|
|
|
|
: _value(base::take(other._value)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionPointer &ConnectionPointer::operator=(ConnectionPointer &&other) {
|
|
|
|
reset(base::take(other._value));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractConnection *ConnectionPointer::get() const {
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionPointer::reset(AbstractConnection *value) {
|
|
|
|
if (_value == value) {
|
|
|
|
return;
|
|
|
|
} else if (const auto old = base::take(_value)) {
|
|
|
|
const auto disconnect = [&](auto signal) {
|
|
|
|
old->disconnect(old, signal, nullptr, nullptr);
|
|
|
|
};
|
|
|
|
disconnect(&AbstractConnection::receivedData);
|
|
|
|
disconnect(&AbstractConnection::receivedSome);
|
|
|
|
disconnect(&AbstractConnection::error);
|
|
|
|
disconnect(&AbstractConnection::connected);
|
|
|
|
disconnect(&AbstractConnection::disconnected);
|
|
|
|
old->disconnectFromServer();
|
|
|
|
old->deleteLater();
|
|
|
|
}
|
|
|
|
_value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionPointer::operator AbstractConnection*() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractConnection *ConnectionPointer::operator->() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractConnection &ConnectionPointer::operator*() const {
|
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionPointer::operator bool() const {
|
|
|
|
return get() != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionPointer::~ConnectionPointer() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
mtpBuffer AbstractConnection::prepareSecurePacket(
|
|
|
|
uint64 keyId,
|
|
|
|
MTPint128 msgKey,
|
|
|
|
uint32 size) const {
|
|
|
|
auto result = mtpBuffer();
|
|
|
|
constexpr auto kTcpPrefixInts = 2;
|
|
|
|
constexpr auto kAuthKeyIdPosition = kTcpPrefixInts;
|
|
|
|
constexpr auto kAuthKeyIdInts = 2;
|
|
|
|
constexpr auto kMessageKeyPosition = kAuthKeyIdPosition
|
|
|
|
+ kAuthKeyIdInts;
|
|
|
|
constexpr auto kMessageKeyInts = 4;
|
|
|
|
constexpr auto kPrefixInts = kTcpPrefixInts
|
|
|
|
+ kAuthKeyIdInts
|
|
|
|
+ kMessageKeyInts;
|
|
|
|
constexpr auto kTcpPostfixInts = 4;
|
|
|
|
result.reserve(kPrefixInts + size + kTcpPostfixInts);
|
|
|
|
result.resize(kPrefixInts);
|
|
|
|
*reinterpret_cast<uint64*>(&result[kAuthKeyIdPosition]) = keyId;
|
|
|
|
*reinterpret_cast<MTPint128*>(&result[kMessageKeyPosition]) = msgKey;
|
|
|
|
return result;
|
2016-03-24 13:27:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 21:18:27 +00:00
|
|
|
gsl::span<const mtpPrime> AbstractConnection::parseNotSecureResponse(
|
|
|
|
const mtpBuffer &buffer) const {
|
|
|
|
const auto answer = buffer.data();
|
|
|
|
const auto len = buffer.size();
|
|
|
|
if (len < 6) {
|
|
|
|
LOG(("Not Secure Error: bad request answer, len = %1"
|
|
|
|
).arg(len * sizeof(mtpPrime)));
|
|
|
|
DEBUG_LOG(("Not Secure Error: answer bytes %1"
|
|
|
|
).arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (answer[0] != 0
|
|
|
|
|| answer[1] != 0
|
|
|
|
|| (((uint32)answer[2]) & 0x03) != 1
|
2019-07-10 17:28:33 +00:00
|
|
|
//|| (base::unixtime::now() - answer[3] > 300) // We didn't sync time yet.
|
|
|
|
//|| (answer[3] - base::unixtime::now() > 60)
|
2018-06-25 21:18:27 +00:00
|
|
|
|| false) {
|
|
|
|
LOG(("Not Secure Error: bad request answer start (%1 %2 %3)"
|
|
|
|
).arg(answer[0]
|
|
|
|
).arg(answer[1]
|
|
|
|
).arg(answer[2]));
|
|
|
|
DEBUG_LOG(("Not Secure Error: answer bytes %1"
|
|
|
|
).arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto answerLen = (uint32)answer[4];
|
|
|
|
if (answerLen < 1 || answerLen > (len - 5) * sizeof(mtpPrime)) {
|
|
|
|
LOG(("Not Secure Error: bad request answer 1 <= %1 <= %2"
|
|
|
|
).arg(answerLen
|
|
|
|
).arg((len - 5) * sizeof(mtpPrime)));
|
|
|
|
DEBUG_LOG(("Not Secure Error: answer bytes %1"
|
|
|
|
).arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return gsl::make_span(answer + 5, answerLen);
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
mtpBuffer AbstractConnection::preparePQFake(const MTPint128 &nonce) const {
|
2019-07-10 17:28:33 +00:00
|
|
|
return prepareNotSecurePacket(
|
|
|
|
MTPReq_pq(nonce),
|
|
|
|
base::unixtime::mtproto_msg_id());
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 14:06:38 +00:00
|
|
|
std::optional<MTPResPQ> AbstractConnection::readPQFakeReply(
|
2018-06-25 18:55:27 +00:00
|
|
|
const mtpBuffer &buffer) const {
|
2018-06-25 21:18:27 +00:00
|
|
|
const auto answer = parseNotSecureResponse(buffer);
|
|
|
|
if (answer.empty()) {
|
2019-07-18 14:06:38 +00:00
|
|
|
return std::nullopt;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
2018-06-25 21:18:27 +00:00
|
|
|
auto from = answer.data();
|
2016-03-24 12:57:10 +00:00
|
|
|
MTPResPQ response;
|
2019-07-18 14:06:38 +00:00
|
|
|
return response.read(from, from + answer.size())
|
|
|
|
? std::make_optional(response)
|
|
|
|
: std::nullopt;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
AbstractConnection::AbstractConnection(
|
|
|
|
QThread *thread,
|
|
|
|
const ProxyData &proxy)
|
2022-02-07 11:24:02 +00:00
|
|
|
: _proxy(proxy)
|
|
|
|
, _debugId(QString::number(++GlobalConnectionCounter)) {
|
2018-04-30 15:49:03 +00:00
|
|
|
moveToThread(thread);
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
ConnectionPointer AbstractConnection::Create(
|
|
|
|
not_null<Instance*> instance,
|
2018-04-24 19:09:20 +00:00
|
|
|
DcOptions::Variants::Protocol protocol,
|
2018-05-17 19:58:00 +00:00
|
|
|
QThread *thread,
|
2019-07-05 07:54:53 +00:00
|
|
|
const bytes::vector &secret,
|
2018-05-17 19:58:00 +00:00
|
|
|
const ProxyData &proxy) {
|
|
|
|
auto result = [&] {
|
|
|
|
if (protocol == DcOptions::Variants::Tcp) {
|
2019-07-10 15:03:48 +00:00
|
|
|
return ConnectionPointer::New<TcpConnection>(
|
|
|
|
instance,
|
|
|
|
thread,
|
|
|
|
proxy);
|
2018-05-17 19:58:00 +00:00
|
|
|
} else {
|
|
|
|
return ConnectionPointer::New<HttpConnection>(thread, proxy);
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (proxy.tryCustomResolve()) {
|
|
|
|
return ConnectionPointer::New<ResolvingConnection>(
|
|
|
|
instance,
|
|
|
|
thread,
|
|
|
|
proxy,
|
|
|
|
std::move(result));
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
2018-05-17 19:58:00 +00:00
|
|
|
return result;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
QString AbstractConnection::ProtocolDcDebugId(int16 protocolDcId) {
|
|
|
|
const auto postfix = (protocolDcId < 0) ? "_media" : "";
|
|
|
|
protocolDcId = (protocolDcId < 0) ? (-protocolDcId) : protocolDcId;
|
|
|
|
const auto prefix = (protocolDcId > kTestModeDcIdShift) ? "test_" : "";
|
|
|
|
protocolDcId = (protocolDcId > kTestModeDcIdShift)
|
|
|
|
? (protocolDcId - kTestModeDcIdShift)
|
|
|
|
: protocolDcId;
|
|
|
|
return prefix + QString::number(protocolDcId) + postfix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractConnection::logInfo(const QString &message) {
|
|
|
|
DEBUG_LOG(("Connection %1 Info: ").arg(_debugId) + message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractConnection::logError(const QString &message) {
|
|
|
|
DEBUG_LOG(("Connection %1 Error: ").arg(_debugId) + message);
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
uint32 AbstractConnection::extendedNotSecurePadding() const {
|
2021-09-15 10:21:45 +00:00
|
|
|
return uint32(base::RandomValue<uchar>() & 0x3F);
|
2019-11-13 14:12:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2016-03-24 12:57:10 +00:00
|
|
|
} // namespace MTP
|