Move ObfuscatedTransport::init implementation to cpp.

GitOrigin-RevId: 7e69d147dffce3ac1cd1254959b9e52ece5d50ca
This commit is contained in:
levlam 2018-02-12 12:01:11 +03:00
parent 389ff96082
commit b2d9b5738c
4 changed files with 55 additions and 45 deletions

View File

@ -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 <class StorerT>
void store(const ServerSalt &salt, StorerT &storer) {
storer.template store_binary<int64>(salt.salt);
storer.template store_binary<double>(salt.valid_since);
storer.template store_binary<double>(salt.valid_until);
}
template <class ParserT>
void parse(ServerSalt &salt, ParserT &parser) {
salt.salt = parser.fetch_long();

View File

@ -129,12 +129,15 @@ class SessionConnection
int rtt() const {
return std::max(2, static_cast<int>(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;
}

View File

@ -6,6 +6,10 @@
//
#include "td/mtproto/TcpTransport.h"
#include "td/utils/Random.h"
#include <algorithm>
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<uint8>(header.data()) == 0xef) {
continue;
}
auto first_int = as<uint32>(header.data());
if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f ||
first_int == 0xeeeeeeee) {
continue;
}
auto second_int = as<uint32>(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<uint32>(header_slice.begin() + 56) = 0xeeeeeeee;
string rheader = header;
std::reverse(rheader.begin(), rheader.end());
aes_ctr_byte_flow_.init(as<UInt256>(rheader.data() + 8), as<UInt128>(rheader.data() + 8 + 32));
aes_ctr_byte_flow_.set_input(input_);
aes_ctr_byte_flow_ >> byte_flow_sink_;
output_key_ = as<UInt256>(header.data() + 8);
output_state_.init(output_key_, as<UInt128>(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

View File

@ -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 <algorithm>
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<uint8>(header.data()) == 0xef) {
continue;
}
auto first_int = as<uint32>(header.data());
if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f ||
first_int == 0xeeeeeeee) {
continue;
}
auto second_int = as<uint32>(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<uint32>(header_slice.begin() + 56) = 0xeeeeeeee;
string rheader = header;
std::reverse(rheader.begin(), rheader.end());
aes_ctr_byte_flow_.init(as<UInt256>(rheader.data() + 8), as<UInt128>(rheader.data() + 8 + 32));
aes_ctr_byte_flow_.set_input(input_);
aes_ctr_byte_flow_ >> byte_flow_sink_;
output_key_ = as<UInt256>(header.data() + 8);
output_state_.init(output_key_, as<UInt128>(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;