2016-03-24 15:57:10 +03:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-03-24 15:57:10 +03:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-03-24 15:57:10 +03:00
|
|
|
*/
|
|
|
|
#include "mtproto/connection_abstract.h"
|
|
|
|
|
|
|
|
#include "mtproto/connection_tcp.h"
|
|
|
|
#include "mtproto/connection_http.h"
|
2018-05-17 22:58:00 +03:00
|
|
|
#include "mtproto/connection_resolving.h"
|
2018-04-24 12:46:27 +04:00
|
|
|
#include "mtproto/session.h"
|
2016-03-24 15:57:10 +03:00
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
|
2018-04-24 23:09:20 +04: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 19:55:27 +01: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 16:27:34 +03:00
|
|
|
}
|
|
|
|
|
2018-06-25 19:55:27 +01:00
|
|
|
mtpBuffer AbstractConnection::preparePQFake(const MTPint128 &nonce) const {
|
|
|
|
return prepareNotSecurePacket(MTPReq_pq(nonce));
|
2016-03-24 15:57:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-25 19:55:27 +01:00
|
|
|
MTPResPQ AbstractConnection::readPQFakeReply(
|
|
|
|
const mtpBuffer &buffer) const {
|
2016-03-24 15:57:10 +03:00
|
|
|
const mtpPrime *answer(buffer.constData());
|
|
|
|
uint32 len = buffer.size();
|
|
|
|
if (len < 5) {
|
|
|
|
LOG(("Fake PQ Error: bad request answer, len = %1").arg(len * sizeof(mtpPrime)));
|
|
|
|
DEBUG_LOG(("Fake PQ Error: answer bytes %1").arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
|
|
|
|
throw Exception("bad pq reply");
|
|
|
|
}
|
|
|
|
if (answer[0] != 0 || answer[1] != 0 || (((uint32)answer[2]) & 0x03) != 1/* || (unixtime() - answer[3] > 300) || (answer[3] - unixtime() > 60)*/) { // didnt sync time yet
|
|
|
|
LOG(("Fake PQ Error: bad request answer start (%1 %2 %3)").arg(answer[0]).arg(answer[1]).arg(answer[2]));
|
|
|
|
DEBUG_LOG(("Fake PQ Error: answer bytes %1").arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
|
|
|
|
throw Exception("bad pq reply");
|
|
|
|
}
|
|
|
|
uint32 answerLen = (uint32)answer[4];
|
|
|
|
if (answerLen != (len - 5) * sizeof(mtpPrime)) {
|
|
|
|
LOG(("Fake PQ Error: bad request answer %1 <> %2").arg(answerLen).arg((len - 5) * sizeof(mtpPrime)));
|
|
|
|
DEBUG_LOG(("Fake PQ Error: answer bytes %1").arg(Logs::mb(answer, len * sizeof(mtpPrime)).str()));
|
|
|
|
throw Exception("bad pq reply");
|
|
|
|
}
|
|
|
|
const mtpPrime *from(answer + 5), *end(from + len - 5);
|
|
|
|
MTPResPQ response;
|
|
|
|
response.read(from, end);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-05-17 22:58:00 +03:00
|
|
|
AbstractConnection::AbstractConnection(
|
|
|
|
QThread *thread,
|
|
|
|
const ProxyData &proxy)
|
|
|
|
: _proxy(proxy) {
|
2018-04-30 19:49:03 +04:00
|
|
|
moveToThread(thread);
|
|
|
|
}
|
|
|
|
|
2018-05-17 22:58:00 +03:00
|
|
|
ConnectionPointer AbstractConnection::Create(
|
|
|
|
not_null<Instance*> instance,
|
2018-04-24 23:09:20 +04:00
|
|
|
DcOptions::Variants::Protocol protocol,
|
2018-05-17 22:58:00 +03:00
|
|
|
QThread *thread,
|
|
|
|
const ProxyData &proxy) {
|
|
|
|
auto result = [&] {
|
|
|
|
if (protocol == DcOptions::Variants::Tcp) {
|
|
|
|
return ConnectionPointer::New<TcpConnection>(thread, proxy);
|
|
|
|
} else {
|
|
|
|
return ConnectionPointer::New<HttpConnection>(thread, proxy);
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (proxy.tryCustomResolve()) {
|
|
|
|
return ConnectionPointer::New<ResolvingConnection>(
|
|
|
|
instance,
|
|
|
|
thread,
|
|
|
|
proxy,
|
|
|
|
std::move(result));
|
2016-03-24 15:57:10 +03:00
|
|
|
}
|
2018-05-17 22:58:00 +03:00
|
|
|
return result;
|
2016-03-24 15:57:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace MTP
|