connection_abstract.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "mtproto/connection_abstract.h"
  8. #include "mtproto/connection_tcp.h"
  9. #include "mtproto/connection_http.h"
  10. #include "mtproto/connection_resolving.h"
  11. #include "mtproto/session.h"
  12. #include "base/unixtime.h"
  13. #include "base/random.h"
  14. namespace MTP {
  15. namespace details {
  16. namespace {
  17. std::atomic<int> GlobalConnectionCounter/* = 0*/;
  18. } // namespace
  19. ConnectionPointer::ConnectionPointer() = default;
  20. ConnectionPointer::ConnectionPointer(std::nullptr_t) {
  21. }
  22. ConnectionPointer::ConnectionPointer(AbstractConnection *value)
  23. : _value(value) {
  24. }
  25. ConnectionPointer::ConnectionPointer(ConnectionPointer &&other)
  26. : _value(base::take(other._value)) {
  27. }
  28. ConnectionPointer &ConnectionPointer::operator=(ConnectionPointer &&other) {
  29. reset(base::take(other._value));
  30. return *this;
  31. }
  32. AbstractConnection *ConnectionPointer::get() const {
  33. return _value;
  34. }
  35. void ConnectionPointer::reset(AbstractConnection *value) {
  36. if (_value == value) {
  37. return;
  38. } else if (const auto old = base::take(_value)) {
  39. const auto disconnect = [&](auto signal) {
  40. old->disconnect(old, signal, nullptr, nullptr);
  41. };
  42. disconnect(&AbstractConnection::receivedData);
  43. disconnect(&AbstractConnection::receivedSome);
  44. disconnect(&AbstractConnection::error);
  45. disconnect(&AbstractConnection::connected);
  46. disconnect(&AbstractConnection::disconnected);
  47. old->disconnectFromServer();
  48. old->deleteLater();
  49. }
  50. _value = value;
  51. }
  52. ConnectionPointer::operator AbstractConnection*() const {
  53. return get();
  54. }
  55. AbstractConnection *ConnectionPointer::operator->() const {
  56. return get();
  57. }
  58. AbstractConnection &ConnectionPointer::operator*() const {
  59. return *get();
  60. }
  61. ConnectionPointer::operator bool() const {
  62. return get() != nullptr;
  63. }
  64. ConnectionPointer::~ConnectionPointer() {
  65. reset();
  66. }
  67. mtpBuffer AbstractConnection::prepareSecurePacket(
  68. uint64 keyId,
  69. MTPint128 msgKey,
  70. uint32 size) const {
  71. auto result = mtpBuffer();
  72. constexpr auto kTcpPrefixInts = 2;
  73. constexpr auto kAuthKeyIdPosition = kTcpPrefixInts;
  74. constexpr auto kAuthKeyIdInts = 2;
  75. constexpr auto kMessageKeyPosition = kAuthKeyIdPosition
  76. + kAuthKeyIdInts;
  77. constexpr auto kMessageKeyInts = 4;
  78. constexpr auto kPrefixInts = kTcpPrefixInts
  79. + kAuthKeyIdInts
  80. + kMessageKeyInts;
  81. constexpr auto kTcpPostfixInts = 4;
  82. result.reserve(kPrefixInts + size + kTcpPostfixInts);
  83. result.resize(kPrefixInts);
  84. *reinterpret_cast<uint64*>(&result[kAuthKeyIdPosition]) = keyId;
  85. *reinterpret_cast<MTPint128*>(&result[kMessageKeyPosition]) = msgKey;
  86. return result;
  87. }
  88. gsl::span<const mtpPrime> AbstractConnection::parseNotSecureResponse(
  89. const mtpBuffer &buffer) const {
  90. const auto answer = buffer.data();
  91. const auto len = buffer.size();
  92. if (len < 6) {
  93. LOG(("Not Secure Error: bad request answer, len = %1"
  94. ).arg(len * sizeof(mtpPrime)));
  95. DEBUG_LOG(("Not Secure Error: answer bytes %1"
  96. ).arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
  97. return {};
  98. }
  99. if (answer[0] != 0
  100. || answer[1] != 0
  101. || (((uint32)answer[2]) & 0x03) != 1
  102. //|| (base::unixtime::now() - answer[3] > 300) // We didn't sync time yet.
  103. //|| (answer[3] - base::unixtime::now() > 60)
  104. || false) {
  105. LOG(("Not Secure Error: bad request answer start (%1 %2 %3)"
  106. ).arg(answer[0]
  107. ).arg(answer[1]
  108. ).arg(answer[2]));
  109. DEBUG_LOG(("Not Secure Error: answer bytes %1"
  110. ).arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
  111. return {};
  112. }
  113. const auto answerLen = (uint32)answer[4];
  114. if (answerLen < 1 || answerLen > (len - 5) * sizeof(mtpPrime)) {
  115. LOG(("Not Secure Error: bad request answer 1 <= %1 <= %2"
  116. ).arg(answerLen
  117. ).arg((len - 5) * sizeof(mtpPrime)));
  118. DEBUG_LOG(("Not Secure Error: answer bytes %1"
  119. ).arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
  120. return {};
  121. }
  122. return gsl::make_span(answer + 5, answerLen);
  123. }
  124. mtpBuffer AbstractConnection::preparePQFake(const MTPint128 &nonce) const {
  125. return prepareNotSecurePacket(
  126. MTPReq_pq(nonce),
  127. base::unixtime::mtproto_msg_id());
  128. }
  129. std::optional<MTPResPQ> AbstractConnection::readPQFakeReply(
  130. const mtpBuffer &buffer) const {
  131. const auto answer = parseNotSecureResponse(buffer);
  132. if (answer.empty()) {
  133. return std::nullopt;
  134. }
  135. auto from = answer.data();
  136. MTPResPQ response;
  137. return response.read(from, from + answer.size())
  138. ? std::make_optional(response)
  139. : std::nullopt;
  140. }
  141. AbstractConnection::AbstractConnection(
  142. QThread *thread,
  143. const ProxyData &proxy)
  144. : _proxy(proxy)
  145. , _debugId(QString::number(++GlobalConnectionCounter)) {
  146. moveToThread(thread);
  147. }
  148. ConnectionPointer AbstractConnection::Create(
  149. not_null<Instance*> instance,
  150. DcOptions::Variants::Protocol protocol,
  151. QThread *thread,
  152. const bytes::vector &secret,
  153. const ProxyData &proxy) {
  154. auto result = [&] {
  155. if (protocol == DcOptions::Variants::Tcp) {
  156. return ConnectionPointer::New<TcpConnection>(
  157. instance,
  158. thread,
  159. proxy);
  160. } else {
  161. return ConnectionPointer::New<HttpConnection>(thread, proxy);
  162. }
  163. }();
  164. if (proxy.tryCustomResolve()) {
  165. return ConnectionPointer::New<ResolvingConnection>(
  166. instance,
  167. thread,
  168. proxy,
  169. std::move(result));
  170. }
  171. return result;
  172. }
  173. QString AbstractConnection::ProtocolDcDebugId(int16 protocolDcId) {
  174. const auto postfix = (protocolDcId < 0) ? "_media" : "";
  175. protocolDcId = (protocolDcId < 0) ? (-protocolDcId) : protocolDcId;
  176. const auto prefix = (protocolDcId > kTestModeDcIdShift) ? "test_" : "";
  177. protocolDcId = (protocolDcId > kTestModeDcIdShift)
  178. ? (protocolDcId - kTestModeDcIdShift)
  179. : protocolDcId;
  180. return prefix + QString::number(protocolDcId) + postfix;
  181. }
  182. void AbstractConnection::logInfo(const QString &message) {
  183. DEBUG_LOG(("Connection %1 Info: ").arg(_debugId) + message);
  184. }
  185. void AbstractConnection::logError(const QString &message) {
  186. DEBUG_LOG(("Connection %1 Error: ").arg(_debugId) + message);
  187. }
  188. uint32 AbstractConnection::extendedNotSecurePadding() const {
  189. return uint32(base::RandomValue<uchar>() & 0x3F);
  190. }
  191. } // namespace details
  192. } // namespace MTP