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
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_dc_options.h"
|
2019-11-13 14:12:04 +00:00
|
|
|
#include "mtproto/mtproto_proxy_data.h"
|
2018-04-24 19:09:20 +00:00
|
|
|
#include "base/bytes.h"
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
#include <QtCore/QObject>
|
|
|
|
#include <QtCore/QThread>
|
|
|
|
|
2022-11-01 16:57:36 +00:00
|
|
|
#include <deque>
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
namespace MTP {
|
2019-11-13 08:31:12 +00:00
|
|
|
|
|
|
|
class Instance;
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-04-24 08:46:27 +00:00
|
|
|
struct ConnectionOptions;
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
class AbstractConnection;
|
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
inline constexpr auto kTestModeDcIdShift = 10000;
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
class ConnectionPointer {
|
|
|
|
public:
|
|
|
|
ConnectionPointer();
|
|
|
|
ConnectionPointer(std::nullptr_t);
|
|
|
|
ConnectionPointer(ConnectionPointer &&other);
|
|
|
|
ConnectionPointer &operator=(ConnectionPointer &&other);
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
template <typename ConnectionType, typename ...Args>
|
|
|
|
static ConnectionPointer New(Args &&...args) {
|
|
|
|
return ConnectionPointer(new ConnectionType(
|
|
|
|
std::forward<Args>(args)...
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
AbstractConnection *get() const;
|
|
|
|
void reset(AbstractConnection *value = nullptr);
|
|
|
|
operator AbstractConnection*() const;
|
|
|
|
AbstractConnection *operator->() const;
|
|
|
|
AbstractConnection &operator*() const;
|
|
|
|
explicit operator bool() const;
|
|
|
|
|
|
|
|
~ConnectionPointer();
|
|
|
|
|
|
|
|
private:
|
2018-05-17 19:58:00 +00:00
|
|
|
explicit ConnectionPointer(AbstractConnection *value);
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
AbstractConnection *_value = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
class AbstractConnection : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2019-07-05 07:54:53 +00:00
|
|
|
AbstractConnection(QThread *thread, const ProxyData &proxy);
|
2016-03-24 12:57:10 +00:00
|
|
|
AbstractConnection(const AbstractConnection &other) = delete;
|
|
|
|
AbstractConnection &operator=(const AbstractConnection &other) = delete;
|
2018-06-25 18:55:27 +00:00
|
|
|
virtual ~AbstractConnection() = default;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
// virtual constructor
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] static ConnectionPointer Create(
|
2018-05-17 19:58:00 +00:00
|
|
|
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);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] virtual ConnectionPointer clone(const ProxyData &proxy) = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] virtual crl::time pingTime() const = 0;
|
|
|
|
[[nodiscard]] virtual crl::time fullConnectTimeout() const = 0;
|
2018-06-25 18:55:27 +00:00
|
|
|
virtual void sendData(mtpBuffer &&buffer) = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
virtual void disconnectFromServer() = 0;
|
2018-04-24 19:09:20 +00:00
|
|
|
virtual void connectToServer(
|
|
|
|
const QString &ip,
|
|
|
|
int port,
|
|
|
|
const bytes::vector &protocolSecret,
|
2021-06-15 12:45:48 +00:00
|
|
|
int16 protocolDcId,
|
|
|
|
bool protocolForFiles) = 0;
|
2019-07-10 15:03:48 +00:00
|
|
|
virtual void timedOut() {
|
|
|
|
}
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] virtual bool isConnected() const = 0;
|
|
|
|
[[nodiscard]] virtual bool usingHttpWait() {
|
2016-03-24 12:57:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] virtual bool needHttpWait() {
|
2016-03-24 12:57:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] virtual int32 debugState() const = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] virtual QString transport() const = 0;
|
|
|
|
[[nodiscard]] virtual QString tag() const = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-11-15 13:04:32 +00:00
|
|
|
void setSentEncryptedWithKeyId(uint64 keyId) {
|
|
|
|
_sentEncryptedWithKeyId = keyId;
|
|
|
|
}
|
|
|
|
[[nodiscard]] uint64 sentEncryptedWithKeyId() const {
|
|
|
|
return _sentEncryptedWithKeyId;
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 09:51:03 +00:00
|
|
|
using BuffersQueue = std::deque<mtpBuffer>;
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] BuffersQueue &received() {
|
2017-02-27 09:51:03 +00:00
|
|
|
return _receivedQueue;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
template <typename Request>
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] mtpBuffer prepareNotSecurePacket(
|
2019-07-10 17:28:33 +00:00
|
|
|
const Request &request,
|
|
|
|
mtpMsgId newId) const;
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] mtpBuffer prepareSecurePacket(
|
2018-06-25 18:55:27 +00:00
|
|
|
uint64 keyId,
|
|
|
|
MTPint128 msgKey,
|
|
|
|
uint32 size) const;
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] gsl::span<const mtpPrime> parseNotSecureResponse(
|
2018-06-25 21:18:27 +00:00
|
|
|
const mtpBuffer &buffer) const;
|
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
[[nodiscard]] static QString ProtocolDcDebugId(int16 protocolDcId);
|
|
|
|
[[nodiscard]] QString debugId() const {
|
|
|
|
return _debugId;
|
|
|
|
}
|
|
|
|
void logInfo(const QString &message);
|
|
|
|
void logError(const QString &message);
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// Used to emit error(...) with no real code from the server.
|
|
|
|
static constexpr auto kErrorCodeOther = -499;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2021-03-03 18:22:42 +00:00
|
|
|
Q_SIGNALS:
|
2016-03-24 12:57:10 +00:00
|
|
|
void receivedData();
|
|
|
|
void receivedSome(); // to stop restart timer
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void error(qint32 errorCodebool);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
void connected();
|
|
|
|
void disconnected();
|
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
void syncTimeRequest();
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
protected:
|
2017-02-27 09:51:03 +00:00
|
|
|
BuffersQueue _receivedQueue; // list of received packets, not processed yet
|
2018-04-30 15:49:03 +00:00
|
|
|
int _pingTime = 0;
|
2018-05-17 19:58:00 +00:00
|
|
|
ProxyData _proxy;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
QString _debugId;
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
// first we always send fake MTPReq_pq to see if connection works at all
|
|
|
|
// we send them simultaneously through TCP/HTTP/IPv4/IPv6 to choose the working one
|
2019-11-13 14:12:04 +00:00
|
|
|
[[nodiscard]] mtpBuffer preparePQFake(const MTPint128 &nonce) const;
|
|
|
|
[[nodiscard]] std::optional<MTPResPQ> readPQFakeReply(
|
|
|
|
const mtpBuffer &buffer) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
[[nodiscard]] uint32 extendedNotSecurePadding() const;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-11-15 13:04:32 +00:00
|
|
|
uint64 _sentEncryptedWithKeyId = 0;
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
};
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
template <typename Request>
|
2019-07-10 17:28:33 +00:00
|
|
|
mtpBuffer AbstractConnection::prepareNotSecurePacket(
|
|
|
|
const Request &request,
|
|
|
|
mtpMsgId newId) const {
|
2019-11-05 10:23:09 +00:00
|
|
|
const auto intsSize = tl::count_length(request) >> 2;
|
2019-11-13 14:12:04 +00:00
|
|
|
const auto intsPadding = extendedNotSecurePadding();
|
2018-06-25 18:55:27 +00:00
|
|
|
|
|
|
|
auto result = mtpBuffer();
|
|
|
|
constexpr auto kTcpPrefixInts = 2;
|
|
|
|
constexpr auto kAuthKeyIdInts = 2;
|
|
|
|
constexpr auto kMessageIdInts = 2;
|
|
|
|
constexpr auto kMessageLengthInts = 1;
|
|
|
|
constexpr auto kPrefixInts = kTcpPrefixInts
|
|
|
|
+ kAuthKeyIdInts
|
|
|
|
+ kMessageIdInts
|
|
|
|
+ kMessageLengthInts;
|
|
|
|
constexpr auto kTcpPostfixInts = 4;
|
|
|
|
|
|
|
|
result.reserve(kPrefixInts + intsSize + intsPadding + kTcpPostfixInts);
|
|
|
|
result.resize(kPrefixInts);
|
|
|
|
|
|
|
|
const auto messageId = &result[kTcpPrefixInts + kAuthKeyIdInts];
|
2019-07-10 17:28:33 +00:00
|
|
|
*reinterpret_cast<mtpMsgId*>(messageId) = newId;
|
2018-06-25 18:55:27 +00:00
|
|
|
|
|
|
|
request.write(result);
|
|
|
|
|
|
|
|
const auto messageLength = messageId + kMessageIdInts;
|
|
|
|
*messageLength = (result.size() - kPrefixInts + intsPadding) << 2;
|
|
|
|
|
|
|
|
if (intsPadding > 0) {
|
2019-11-13 14:12:04 +00:00
|
|
|
const auto skipPrimes = result.size();
|
|
|
|
result.resize(skipPrimes + intsPadding);
|
|
|
|
const auto skipBytes = skipPrimes * sizeof(mtpPrime);
|
|
|
|
bytes::set_random(bytes::make_span(result).subspan(skipBytes));
|
2018-06-25 18:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-07 11:24:02 +00:00
|
|
|
#define CONNECTION_LOG_INFO(x) if (Logs::DebugEnabled()) { logInfo(x); }
|
|
|
|
#define CONNECTION_LOG_ERROR(x) if (Logs::DebugEnabled()) { logError(x); }
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2016-03-24 12:57:10 +00:00
|
|
|
} // namespace MTP
|