Keeps the ZaStoGram MTProto layout and its unaligned-access fixes, the Forgejo update checker and the rich-page layout contract; takes the upstream IV editor edge restore, horizontal overflow width, round video seek, tlottie renderer and the lib_ui text shaper.
291 lines
7.9 KiB
C++
291 lines
7.9 KiB
C++
/*
|
|
This file is part of Telegram Desktop,
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
For license and copyright information please follow this link:
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
*/
|
|
#include "mtproto/transport/connection_abstract.h"
|
|
|
|
#include "mtproto/transport/connection_tcp.h"
|
|
#include "mtproto/transport/connection_http.h"
|
|
#include "mtproto/transport/details/mtproto_abstract_socket.h"
|
|
#include "mtproto/protocol/mtproto_binary.h"
|
|
#include "mtproto/proxy/resolving_connection.h"
|
|
#include "mtproto/proxy/diagnostics.h"
|
|
#include "mtproto/runtime/runtime_environment.h"
|
|
#include "mtproto/session/session.h"
|
|
#include "logs.h"
|
|
#include "base/unixtime.h"
|
|
#include "base/random.h"
|
|
|
|
#include <QtCore/QtEndian>
|
|
|
|
namespace MTP {
|
|
namespace details {
|
|
namespace {
|
|
|
|
std::atomic<int> GlobalConnectionCounter/* = 0*/;
|
|
|
|
[[nodiscard]] bool IsMtproxyTransport(AbstractConnection::TransportMode mode) {
|
|
return (mode == AbstractConnection::TransportMode::PlainMtproxy)
|
|
|| (mode == AbstractConnection::TransportMode::FakeTlsMtproxy);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
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::handshakeProgress);
|
|
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();
|
|
}
|
|
|
|
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);
|
|
binary::WriteAt<uint64>(
|
|
bytes::make_span(result),
|
|
kAuthKeyIdPosition * sizeof(mtpPrime),
|
|
keyId);
|
|
binary::WriteAt<MTPint128>(
|
|
bytes::make_span(result),
|
|
kMessageKeyPosition * sizeof(mtpPrime),
|
|
msgKey);
|
|
return result;
|
|
}
|
|
|
|
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
|
|
//|| (base::unixtime::now() - answer[3] > 300) // We didn't sync time yet.
|
|
//|| (answer[3] - base::unixtime::now() > 60)
|
|
|| 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 % sizeof(mtpPrime))
|
|
|| 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 / sizeof(mtpPrime));
|
|
}
|
|
|
|
mtpBuffer AbstractConnection::preparePQFake(const MTPint128 &nonce) const {
|
|
return prepareNotSecurePacket(
|
|
MTPReq_pq(nonce),
|
|
base::unixtime::mtproto_msg_id());
|
|
}
|
|
|
|
std::optional<MTPResPQ> AbstractConnection::readPQFakeReply(
|
|
const mtpBuffer &buffer) const {
|
|
const auto answer = parseNotSecureResponse(buffer);
|
|
if (answer.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
auto from = answer.data();
|
|
MTPResPQ response;
|
|
return response.read(from, from + answer.size())
|
|
? std::make_optional(response)
|
|
: std::nullopt;
|
|
}
|
|
|
|
AbstractConnection::AbstractConnection(
|
|
not_null<RuntimeEnvironment*> runtime,
|
|
QThread *thread,
|
|
const ProxyData &proxy)
|
|
: _runtime(runtime)
|
|
, _proxy(proxy)
|
|
, _debugId(QString::number(++GlobalConnectionCounter)) {
|
|
moveToThread(thread);
|
|
}
|
|
|
|
HandshakePhase AbstractConnection::handshakePhase() const {
|
|
return HandshakePhase::None;
|
|
}
|
|
|
|
ProxyConnectionAttempt AbstractConnection::proxyConnectionAttempt() const {
|
|
return {};
|
|
}
|
|
|
|
ProxyTransportFailure AbstractConnection::proxyTransportFailure() const {
|
|
return {};
|
|
}
|
|
|
|
bool AbstractConnection::serviceRequestNeeded(
|
|
TransportServiceRequest request) const {
|
|
return serviceRequest() == request;
|
|
}
|
|
|
|
ConnectionPointer AbstractConnection::Create(
|
|
not_null<RuntimeEnvironment*> runtime,
|
|
DcOptions::Variants::Protocol protocol,
|
|
QThread *thread,
|
|
const bytes::vector &secret,
|
|
const ProxyData &proxy,
|
|
const ProxyStealthOptions &stealth) {
|
|
auto result = [&] {
|
|
if (protocol == DcOptions::Variants::Tcp) {
|
|
return ConnectionPointer::New<TcpConnection>(
|
|
runtime,
|
|
thread,
|
|
proxy,
|
|
stealth);
|
|
} else {
|
|
return ConnectionPointer::New<HttpConnection>(
|
|
runtime,
|
|
thread,
|
|
proxy);
|
|
}
|
|
}();
|
|
if (proxy.tryCustomResolve()) {
|
|
return ConnectionPointer::New<ResolvingConnection>(
|
|
runtime,
|
|
thread,
|
|
proxy,
|
|
std::move(result));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
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) {
|
|
const auto full = QString("Connection %1 Info: ").arg(_debugId) + message;
|
|
if (IsMtproxyTransport(_transport)) {
|
|
WriteProxyDiagnosticsLine(_runtime, {
|
|
.source = ProxyDiagnosticsSource::MTProxy,
|
|
.phase = ProxyDiagnosticsPhase::None,
|
|
.severity = ProxyDiagnosticsSeverity::Info,
|
|
.proxy = _proxy,
|
|
.transport = transport(),
|
|
.connectionId = _debugId,
|
|
.message = full,
|
|
});
|
|
} else {
|
|
DEBUG_LOG((full));
|
|
}
|
|
}
|
|
|
|
void AbstractConnection::logError(const QString &message) {
|
|
const auto full = QString("Connection %1 Error: ").arg(_debugId) + message;
|
|
if (IsMtproxyTransport(_transport)) {
|
|
WriteProxyDiagnosticsLine(_runtime, {
|
|
.source = ProxyDiagnosticsSource::MTProxy,
|
|
.phase = ProxyDiagnosticsPhase::Failed,
|
|
.severity = ProxyDiagnosticsSeverity::Error,
|
|
.proxy = _proxy,
|
|
.transport = transport(),
|
|
.connectionId = _debugId,
|
|
.message = full,
|
|
});
|
|
} else {
|
|
DEBUG_LOG((full));
|
|
}
|
|
}
|
|
|
|
uint32 AbstractConnection::extendedNotSecurePadding() const {
|
|
return uint32(base::RandomValue<uchar>() & 0x3F);
|
|
}
|
|
|
|
} // namespace details
|
|
} // namespace MTP
|