diff --git a/td/mtproto/AuthData.h b/td/mtproto/AuthData.h index 646211c3..47e1a5cc 100644 --- a/td/mtproto/AuthData.h +++ b/td/mtproto/AuthData.h @@ -5,6 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once + #include "td/mtproto/AuthKey.h" #include "td/utils/format.h" @@ -25,12 +26,14 @@ struct ServerSalt { double valid_since; double valid_until; }; + template void store(const ServerSalt &salt, StorerT &storer) { storer.template store_binary(salt.salt); storer.template store_binary(salt.valid_since); storer.template store_binary(salt.valid_until); } + template void parse(ServerSalt &salt, ParserT &parser) { salt.salt = parser.fetch_long(); diff --git a/td/mtproto/SessionConnection.h b/td/mtproto/SessionConnection.h index 2f2c2ccc..eacbea66 100644 --- a/td/mtproto/SessionConnection.h +++ b/td/mtproto/SessionConnection.h @@ -129,12 +129,15 @@ class SessionConnection int rtt() const { return std::max(2, static_cast(raw_connection_->rtt_ * 1.5)); } + int32 ping_disconnect_delay() const { return online_flag_ ? rtt() * 5 / 2 : 135; } + int32 ping_may_delay() const { return online_flag_ ? rtt() / 2 : 30; } + int32 ping_must_delay() const { return online_flag_ ? rtt() : 60; } diff --git a/td/mtproto/TcpTransport.cpp b/td/mtproto/TcpTransport.cpp index 8207828f..ed266cb5 100644 --- a/td/mtproto/TcpTransport.cpp +++ b/td/mtproto/TcpTransport.cpp @@ -6,6 +6,10 @@ // #include "td/mtproto/TcpTransport.h" +#include "td/utils/Random.h" + +#include + namespace td { namespace mtproto { namespace tcp { @@ -118,6 +122,50 @@ void AbridgedTransport::init_output_stream(ChainBufferWriter *stream) { const uint8 magic = 0xef; stream->append(Slice(&magic, 1)); } + +void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *output) { + input_ = input; + output_ = output; + + const size_t header_size = 64; + string header(header_size, '\0'); + MutableSlice header_slice = header; + int32 try_cnt = 0; + while (true) { + try_cnt++; + CHECK(try_cnt < 10); + Random::secure_bytes(header_slice.ubegin(), header.size()); + if (as(header.data()) == 0xef) { + continue; + } + auto first_int = as(header.data()); + if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f || + first_int == 0xeeeeeeee) { + continue; + } + auto second_int = as(header.data() + sizeof(uint32)); + if (second_int == 0) { + continue; + } + break; + } + // TODO: It is actually IntermediateTransport::init_output_stream, so it will work only with + // TransportImpl==IntermediateTransport + as(header_slice.begin() + 56) = 0xeeeeeeee; + + string rheader = header; + std::reverse(rheader.begin(), rheader.end()); + aes_ctr_byte_flow_.init(as(rheader.data() + 8), as(rheader.data() + 8 + 32)); + aes_ctr_byte_flow_.set_input(input_); + aes_ctr_byte_flow_ >> byte_flow_sink_; + + output_key_ = as(header.data() + 8); + output_state_.init(output_key_, as(header.data() + 8 + 32)); + output_->append(header_slice.substr(0, 56)); + output_state_.encrypt(header_slice, header_slice); + output_->append(header_slice.substr(56, 8)); +} + } // namespace tcp } // namespace mtproto } // namespace td diff --git a/td/mtproto/TcpTransport.h b/td/mtproto/TcpTransport.h index 1e0204c1..e5e217cd 100644 --- a/td/mtproto/TcpTransport.h +++ b/td/mtproto/TcpTransport.h @@ -15,12 +15,9 @@ #include "td/utils/crypto.h" #include "td/utils/logging.h" #include "td/utils/port/Fd.h" -#include "td/utils/Random.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" -#include - namespace td { namespace mtproto { namespace tcp { @@ -128,48 +125,7 @@ class ObfuscatedTransport : public IStreamTransport { output_->append(std::move(slice)); } - void init(ChainBufferReader *input, ChainBufferWriter *output) override { - input_ = input; - output_ = output; - - const size_t header_size = 64; - string header(header_size, '\0'); - MutableSlice header_slice = header; - int32 try_cnt = 0; - while (true) { - try_cnt++; - CHECK(try_cnt < 10); - Random::secure_bytes(header_slice.ubegin(), header.size()); - if (as(header.data()) == 0xef) { - continue; - } - auto first_int = as(header.data()); - if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f || - first_int == 0xeeeeeeee) { - continue; - } - auto second_int = as(header.data() + sizeof(uint32)); - if (second_int == 0) { - continue; - } - break; - } - // TODO: It is actually IntermediateTransport::init_output_stream, so it will work only with - // TransportImpl==IntermediateTransport - as(header_slice.begin() + 56) = 0xeeeeeeee; - - string rheader = header; - std::reverse(rheader.begin(), rheader.end()); - aes_ctr_byte_flow_.init(as(rheader.data() + 8), as(rheader.data() + 8 + 32)); - aes_ctr_byte_flow_.set_input(input_); - aes_ctr_byte_flow_ >> byte_flow_sink_; - - output_key_ = as(header.data() + 8); - output_state_.init(output_key_, as(header.data() + 8 + 32)); - output_->append(header_slice.substr(0, 56)); - output_state_.encrypt(header_slice, header_slice); - output_->append(header_slice.substr(56, 8)); - } + void init(ChainBufferReader *input, ChainBufferWriter *output) override; bool can_read() const override { return true;