New transport improvements.

GitOrigin-RevId: cc7b8aafe6a30009fd4d549ef969c373ce2b9d9a
This commit is contained in:
levlam 2018-06-16 03:03:14 +03:00
parent 70be2b6f85
commit 8a6b550a86
5 changed files with 30 additions and 13 deletions

View File

@ -18,6 +18,7 @@
namespace td { namespace td {
namespace mtproto { namespace mtproto {
template <class Object, class ObjectStorer> template <class Object, class ObjectStorer>
class ObjectImpl { class ObjectImpl {
public: public:
@ -329,5 +330,6 @@ class CryptoImpl {
uint64 message_id_; uint64 message_id_;
int32 seq_no_; int32 seq_no_;
}; };
} // namespace mtproto } // namespace mtproto
} // namespace td } // namespace td

View File

@ -11,12 +11,12 @@
namespace td { namespace td {
namespace mtproto { namespace mtproto {
class NoCryptoImpl { class NoCryptoImpl {
public: public:
NoCryptoImpl(uint64 message_id, const Storer &data, bool need_pad = true) : message_id(message_id), data(data) { NoCryptoImpl(uint64 message_id, const Storer &data, bool need_pad = true) : message_id_(message_id), data_(data) {
if (need_pad) { if (need_pad) {
auto data_size = data.size(); auto pad_size = -static_cast<int>(data_.size()) & 15;
auto pad_size = (data_size + 15) / 16 * 16 - data_size;
pad_size += 16 * (static_cast<size_t>(Random::secure_int32()) % 16); pad_size += 16 * (static_cast<size_t>(Random::secure_int32()) % 16);
pad_.resize(pad_size); pad_.resize(pad_size);
Random::secure_bytes(pad_); Random::secure_bytes(pad_);
@ -24,16 +24,17 @@ class NoCryptoImpl {
} }
template <class T> template <class T>
void do_store(T &storer) const { void do_store(T &storer) const {
storer.store_binary(message_id); storer.store_binary(message_id_);
storer.store_binary(static_cast<int32>(data.size() + pad_.size())); storer.store_binary(static_cast<int32>(data_.size() + pad_.size()));
storer.store_storer(data); storer.store_storer(data_);
storer.store_slice(pad_); storer.store_slice(pad_);
} }
private: private:
uint64 message_id; uint64 message_id_;
const Storer &data; const Storer &data_;
std::string pad_; std::string pad_;
}; };
} // namespace mtproto } // namespace mtproto
} // namespace td } // namespace td

View File

@ -151,7 +151,7 @@ void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *outp
} }
auto first_int = as<uint32>(header.data()); auto first_int = as<uint32>(header.data());
if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f || if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f ||
first_int == 0xeeeeeeee) { first_int == 0xdddddddd || first_int == 0xeeeeeeee) {
continue; continue;
} }
auto second_int = as<uint32>(header.data() + sizeof(uint32)); auto second_int = as<uint32>(header.data() + sizeof(uint32));

View File

@ -119,7 +119,8 @@ class OldTransport : public IStreamTransport {
class ObfuscatedTransport : public IStreamTransport { class ObfuscatedTransport : public IStreamTransport {
public: public:
ObfuscatedTransport(int16 dc_id, std::string secret) : dc_id_(dc_id), secret_(std::move(secret)) { ObfuscatedTransport(int16 dc_id, std::string secret)
: dc_id_(dc_id), secret_(std::move(secret)), impl_(secret_.size() >= 17) {
} }
Result<size_t> read_next(BufferSlice *message, uint32 *quick_ack) override TD_WARN_UNUSED_RESULT { Result<size_t> read_next(BufferSlice *message, uint32 *quick_ack) override TD_WARN_UNUSED_RESULT {
aes_ctr_byte_flow_.wakeup(); aes_ctr_byte_flow_.wakeup();
@ -162,7 +163,7 @@ class ObfuscatedTransport : public IStreamTransport {
private: private:
int16 dc_id_; int16 dc_id_;
std::string secret_; std::string secret_;
TransportImpl impl_{secret_.size() >= 17}; TransportImpl impl_;
AesCtrByteFlow aes_ctr_byte_flow_; AesCtrByteFlow aes_ctr_byte_flow_;
ByteFlowSink byte_flow_sink_; ByteFlowSink byte_flow_sink_;
ChainBufferReader *input_; ChainBufferReader *input_;

View File

@ -279,6 +279,16 @@ void ConnectionCreator::add_proxy(string server, int32 port, bool enable,
return promise.set_error(Status::Error(400, "Wrong port number")); return promise.set_error(Status::Error(400, "Wrong port number"));
} }
auto is_secret_supported = [](Slice secret) {
if (secret.size() == 32) {
return true;
}
if (secret.size() == 34) {
return begins_with(secret, "dd");
}
return false;
};
Proxy new_proxy; Proxy new_proxy;
switch (proxy_type->get_id()) { switch (proxy_type->get_id()) {
case td_api::proxyTypeSocks5::ID: { case td_api::proxyTypeSocks5::ID: {
@ -288,8 +298,11 @@ void ConnectionCreator::add_proxy(string server, int32 port, bool enable,
} }
case td_api::proxyTypeMtproto::ID: { case td_api::proxyTypeMtproto::ID: {
auto type = td_api::move_object_as<td_api::proxyTypeMtproto>(proxy_type); auto type = td_api::move_object_as<td_api::proxyTypeMtproto>(proxy_type);
if ((type->secret_.size() != 32 && type->secret_.size() != 34) || hex_decode(type->secret_).is_error()) { if (hex_decode(type->secret_).is_error()) {
return promise.set_error(Status::Error(400, "Wrong server secret")); return promise.set_error(Status::Error(400, "Wrong secret"));
}
if (!is_secret_supported(type->secret_)) {
return promise.set_error(Status::Error(400, "Unsupported secret"));
} }
new_proxy = Proxy::mtproto(server, port, type->secret_); new_proxy = Proxy::mtproto(server, port, type->secret_);
break; break;