2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/mtproto/IStreamTransport.h"
|
2019-07-09 04:01:12 +02:00
|
|
|
#include "td/mtproto/ProxySecret.h"
|
2019-06-26 15:51:48 +02:00
|
|
|
#include "td/mtproto/TlsReaderByteFlow.h"
|
2019-07-01 15:18:28 +02:00
|
|
|
#include "td/mtproto/TransportType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/AesCtrByteFlow.h"
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/ByteFlow.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/crypto.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
|
|
|
namespace tcp {
|
2018-07-27 02:54:25 +02:00
|
|
|
|
2023-01-10 20:03:17 +01:00
|
|
|
class IntermediateTransport {
|
|
|
|
public:
|
|
|
|
explicit IntermediateTransport(bool with_padding) : with_padding_(with_padding) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes a packet into message.
|
2018-12-31 20:04:05 +01:00
|
|
|
// Returns 0 if everything is ok, and [expected_size] otherwise.
|
|
|
|
// There is no sense to call this function when [stream->size > expected_size]
|
|
|
|
//
|
2023-01-10 20:03:17 +01:00
|
|
|
// (TCP is a stream-oriented protocol, so the input message is a stream, not a slice)
|
|
|
|
size_t read_from_stream(ChainBufferReader *stream, BufferSlice *message, uint32 *quick_ack);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Writes header inplace.
|
2023-01-10 20:03:17 +01:00
|
|
|
void write_prepare_inplace(BufferWriter *message, bool quick_ack);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Writes first several bytes into output stream.
|
2023-01-10 20:03:17 +01:00
|
|
|
void init_output_stream(ChainBufferWriter *stream);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-06-15 20:54:22 +02:00
|
|
|
bool with_padding() const {
|
|
|
|
return with_padding_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool with_padding_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class OldTransport final : public IStreamTransport {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
OldTransport() = default;
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<size_t> read_next(BufferSlice *message, uint32 *quick_ack) final TD_WARN_UNUSED_RESULT {
|
2018-12-31 20:04:05 +01:00
|
|
|
return impl_.read_from_stream(input_, message, quick_ack);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool support_quick_ack() const final {
|
2023-01-10 20:03:17 +01:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void write(BufferWriter &&message, bool quick_ack) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
impl_.write_prepare_inplace(&message, quick_ack);
|
|
|
|
output_->append(message.as_buffer_slice());
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void init(ChainBufferReader *input, ChainBufferWriter *output) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
input_ = input;
|
|
|
|
output_ = output;
|
|
|
|
impl_.init_output_stream(output_);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool can_read() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool can_write() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t max_prepend_size() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return 4;
|
|
|
|
}
|
2018-06-15 20:54:22 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t max_append_size() const final {
|
2018-06-15 20:54:22 +02:00
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
TransportType get_type() const final {
|
2019-07-09 04:01:12 +02:00
|
|
|
return TransportType{TransportType::Tcp, 0, ProxySecret()};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
bool use_random_padding() const final {
|
2019-07-04 12:56:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
2023-01-10 20:03:17 +01:00
|
|
|
IntermediateTransport impl_{false};
|
2021-11-11 15:39:09 +01:00
|
|
|
ChainBufferReader *input_{nullptr};
|
|
|
|
ChainBufferWriter *output_{nullptr};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ObfuscatedTransport final : public IStreamTransport {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-18 18:26:14 +02:00
|
|
|
ObfuscatedTransport(int16 dc_id, ProxySecret secret)
|
|
|
|
: dc_id_(dc_id), secret_(std::move(secret)), impl_(secret_.use_random_padding()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<size_t> read_next(BufferSlice *message, uint32 *quick_ack) final TD_WARN_UNUSED_RESULT;
|
2019-06-26 15:51:48 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
bool support_quick_ack() const final {
|
2023-01-10 20:03:17 +01:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void write(BufferWriter &&message, bool quick_ack) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void init(ChainBufferReader *input, ChainBufferWriter *output) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
bool can_read() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
bool can_write() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t max_prepend_size() const final {
|
2019-06-26 15:51:48 +02:00
|
|
|
size_t res = 4;
|
2019-07-09 04:01:12 +02:00
|
|
|
if (secret_.emulate_tls()) {
|
2019-06-26 15:51:48 +02:00
|
|
|
res += 5;
|
|
|
|
if (is_first_tls_packet_) {
|
|
|
|
res += 6;
|
|
|
|
}
|
|
|
|
}
|
2019-07-01 15:34:39 +02:00
|
|
|
res += header_.size();
|
2019-07-01 15:18:28 +02:00
|
|
|
if (res & 3) {
|
|
|
|
res += 4 - (res & 3);
|
|
|
|
}
|
2019-06-26 15:51:48 +02:00
|
|
|
return res;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t max_append_size() const final {
|
2018-06-15 20:54:22 +02:00
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
TransportType get_type() const final {
|
2018-04-30 19:01:18 +02:00
|
|
|
return TransportType{TransportType::ObfuscatedTcp, dc_id_, secret_};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-18 18:26:14 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
bool use_random_padding() const final {
|
2019-07-09 04:01:12 +02:00
|
|
|
return secret_.use_random_padding();
|
2019-07-04 12:56:10 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2018-04-30 19:01:18 +02:00
|
|
|
int16 dc_id_;
|
2019-06-26 15:51:48 +02:00
|
|
|
bool is_first_tls_packet_{true};
|
2019-07-09 04:01:12 +02:00
|
|
|
ProxySecret secret_;
|
2019-07-01 15:34:39 +02:00
|
|
|
std::string header_;
|
2023-01-10 20:03:17 +01:00
|
|
|
IntermediateTransport impl_;
|
2019-06-26 15:51:48 +02:00
|
|
|
TlsReaderByteFlow tls_reader_byte_flow_;
|
2018-12-31 20:04:05 +01:00
|
|
|
AesCtrByteFlow aes_ctr_byte_flow_;
|
|
|
|
ByteFlowSink byte_flow_sink_;
|
2019-07-09 04:01:12 +02:00
|
|
|
ChainBufferReader *input_ = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-02 00:58:01 +02:00
|
|
|
static constexpr int32 MAX_TLS_PACKET_LENGTH = 2878;
|
2019-07-01 15:18:28 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: use ByteFlow?
|
|
|
|
// One problem is that BufferedFd owns output_buffer_
|
|
|
|
// The other problem is that first 56 bytes must be sent unencrypted.
|
2021-12-01 14:01:27 +01:00
|
|
|
UInt256 output_key_;
|
2018-12-31 20:04:05 +01:00
|
|
|
AesCtrState output_state_;
|
2021-11-11 15:39:09 +01:00
|
|
|
ChainBufferWriter *output_ = nullptr;
|
2019-06-26 15:51:48 +02:00
|
|
|
|
|
|
|
void do_write_tls(BufferWriter &&message);
|
|
|
|
void do_write_tls(BufferBuilder &&builder);
|
2019-07-01 15:34:39 +02:00
|
|
|
void do_write_main(BufferWriter &&message);
|
2019-06-26 15:51:48 +02:00
|
|
|
void do_write(BufferSlice &&message);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using Transport = ObfuscatedTransport;
|
|
|
|
|
|
|
|
} // namespace tcp
|
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|