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)
|
|
|
|
//
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2024-01-22 00:15:26 +01:00
|
|
|
#include "td/telegram/net/PublicRsaKeySharedMain.h"
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "td/telegram/NotificationManager.h"
|
2024-01-22 11:11:04 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/mtproto/AuthData.h"
|
2021-07-05 20:20:07 +02:00
|
|
|
#include "td/mtproto/DhCallback.h"
|
2019-01-31 12:26:06 +01:00
|
|
|
#include "td/mtproto/DhHandshake.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/Handshake.h"
|
|
|
|
#include "td/mtproto/HandshakeActor.h"
|
2019-05-06 18:59:49 +02:00
|
|
|
#include "td/mtproto/Ping.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/mtproto/PingConnection.h"
|
2019-07-09 04:01:12 +02:00
|
|
|
#include "td/mtproto/ProxySecret.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/RawConnection.h"
|
2020-05-17 16:07:16 +02:00
|
|
|
#include "td/mtproto/RSA.h"
|
2019-06-26 16:13:07 +02:00
|
|
|
#include "td/mtproto/TlsInit.h"
|
2019-01-31 03:13:59 +01:00
|
|
|
#include "td/mtproto/TransportType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
#include "td/net/GetHostByNameActor.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/net/Socks5.h"
|
2018-07-26 02:28:02 +02:00
|
|
|
#include "td/net/TransparentProxy.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "td/actor/actor.h"
|
2020-11-23 01:24:36 +01:00
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-29 16:16:52 +01:00
|
|
|
#include "td/utils/base64.h"
|
2021-11-04 10:46:08 +01:00
|
|
|
#include "td/utils/BufferedFd.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2021-07-08 15:39:34 +02:00
|
|
|
#include "td/utils/crypto.h"
|
2024-05-20 12:46:32 +02:00
|
|
|
#include "td/utils/HttpDate.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/port/Clocks.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/port/SocketFd.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/utils/Random.h"
|
2020-12-31 23:52:01 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "td/utils/tests.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/utils/Time.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-01-22 00:52:58 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2019-02-04 02:32:32 +01:00
|
|
|
TEST(Mtproto, GetHostByNameActor) {
|
|
|
|
int threads_n = 1;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2019-01-24 10:47:54 +01:00
|
|
|
|
|
|
|
int cnt = 1;
|
2022-01-09 10:04:08 +01:00
|
|
|
td::vector<td::ActorOwn<td::GetHostByNameActor>> actors;
|
2019-01-24 10:47:54 +01:00
|
|
|
{
|
|
|
|
auto guard = sched.get_main_guard();
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
auto run = [&](td::ActorId<td::GetHostByNameActor> actor_id, td::string host, bool prefer_ipv6, bool allow_ok,
|
2019-02-04 03:46:17 +01:00
|
|
|
bool allow_error) {
|
2022-01-09 10:04:08 +01:00
|
|
|
auto promise = td::PromiseCreator::lambda([&cnt, &actors, num = cnt, host, allow_ok,
|
|
|
|
allow_error](td::Result<td::IPAddress> r_ip_address) {
|
2019-02-04 03:46:17 +01:00
|
|
|
if (r_ip_address.is_error() && !allow_error) {
|
2019-02-04 02:32:32 +01:00
|
|
|
LOG(ERROR) << num << " \"" << host << "\" " << r_ip_address.error();
|
2019-01-24 10:47:54 +01:00
|
|
|
}
|
2019-02-04 03:46:17 +01:00
|
|
|
if (r_ip_address.is_ok() && !allow_ok && (r_ip_address.ok().is_ipv6() || r_ip_address.ok().get_ipv4() != 0)) {
|
|
|
|
LOG(ERROR) << num << " \"" << host << "\" " << r_ip_address.ok();
|
|
|
|
}
|
2019-01-24 10:47:54 +01:00
|
|
|
if (--cnt == 0) {
|
2019-02-04 02:32:32 +01:00
|
|
|
actors.clear();
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2019-01-24 10:47:54 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
cnt++;
|
2022-01-09 10:04:08 +01:00
|
|
|
td::send_closure_later(actor_id, &td::GetHostByNameActor::run, host, 443, prefer_ipv6, std::move(promise));
|
2019-01-24 10:47:54 +01:00
|
|
|
};
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
td::vector<td::string> hosts = {"127.0.0.2",
|
|
|
|
"1.1.1.1",
|
|
|
|
"localhost",
|
|
|
|
"web.telegram.org",
|
|
|
|
"web.telegram.org.",
|
|
|
|
"москва.рф",
|
|
|
|
"",
|
|
|
|
"%",
|
|
|
|
" ",
|
|
|
|
"a",
|
|
|
|
"\x80",
|
|
|
|
"[]",
|
|
|
|
"127.0.0.1.",
|
|
|
|
"0x12.0x34.0x56.0x78",
|
|
|
|
"0x7f.001",
|
|
|
|
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
|
|
|
|
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
|
|
|
|
"[[2001:0db8:85a3:0000:0000:8a2e:0370:7334]]"};
|
|
|
|
for (const auto &types :
|
|
|
|
{td::vector<td::GetHostByNameActor::ResolverType>{td::GetHostByNameActor::ResolverType::Native},
|
|
|
|
td::vector<td::GetHostByNameActor::ResolverType>{td::GetHostByNameActor::ResolverType::Google},
|
|
|
|
td::vector<td::GetHostByNameActor::ResolverType>{td::GetHostByNameActor::ResolverType::Google,
|
|
|
|
td::GetHostByNameActor::ResolverType::Google,
|
|
|
|
td::GetHostByNameActor::ResolverType::Native}}) {
|
|
|
|
td::GetHostByNameActor::Options options;
|
2019-02-04 02:36:13 +01:00
|
|
|
options.resolver_types = types;
|
2019-02-04 02:32:32 +01:00
|
|
|
options.scheduler_id = threads_n;
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
auto actor = td::create_actor<td::GetHostByNameActor>("GetHostByNameActor", std::move(options));
|
2019-02-04 02:32:32 +01:00
|
|
|
auto actor_id = actor.get();
|
|
|
|
actors.push_back(std::move(actor));
|
|
|
|
|
2019-02-04 00:29:37 +01:00
|
|
|
for (auto host : hosts) {
|
2019-01-24 18:07:24 +01:00
|
|
|
for (auto prefer_ipv6 : {false, true}) {
|
2020-05-16 22:54:40 +02:00
|
|
|
bool allow_ok = host.size() > 2 && host[1] != '[';
|
2019-02-04 05:24:05 +01:00
|
|
|
bool allow_both = host == "127.0.0.1." || host == "localhost" || (host == "москва.рф" && prefer_ipv6);
|
|
|
|
bool allow_error = !allow_ok || allow_both;
|
2019-02-04 03:46:17 +01:00
|
|
|
run(actor_id, host, prefer_ipv6, allow_ok, allow_error);
|
2019-01-24 18:07:24 +01:00
|
|
|
}
|
2019-01-24 14:07:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cnt--;
|
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
2019-01-24 18:08:29 +01:00
|
|
|
// empty
|
2019-01-24 14:07:11 +01:00
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
2019-01-24 18:08:29 +01:00
|
|
|
|
2019-07-10 20:11:16 +02:00
|
|
|
TEST(Time, to_unix_time) {
|
2022-01-09 10:04:08 +01:00
|
|
|
ASSERT_EQ(0, td::HttpDate::to_unix_time(1970, 1, 1, 0, 0, 0).move_as_ok());
|
|
|
|
ASSERT_EQ(60 * 60 + 60 + 1, td::HttpDate::to_unix_time(1970, 1, 1, 1, 1, 1).move_as_ok());
|
|
|
|
ASSERT_EQ(24 * 60 * 60, td::HttpDate::to_unix_time(1970, 1, 2, 0, 0, 0).move_as_ok());
|
|
|
|
ASSERT_EQ(31 * 24 * 60 * 60, td::HttpDate::to_unix_time(1970, 2, 1, 0, 0, 0).move_as_ok());
|
|
|
|
ASSERT_EQ(365 * 24 * 60 * 60, td::HttpDate::to_unix_time(1971, 1, 1, 0, 0, 0).move_as_ok());
|
|
|
|
ASSERT_EQ(1562780559, td::HttpDate::to_unix_time(2019, 7, 10, 17, 42, 39).move_as_ok());
|
2019-07-10 20:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Time, parse_http_date) {
|
2022-01-09 10:04:08 +01:00
|
|
|
ASSERT_EQ(784887151, td::HttpDate::parse_http_date("Tue, 15 Nov 1994 08:12:31 GMT").move_as_ok());
|
2019-07-10 20:11:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
TEST(Mtproto, config) {
|
|
|
|
int threads_n = 0;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-24 17:09:27 +02:00
|
|
|
int cnt = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
{
|
2018-09-18 15:43:16 +02:00
|
|
|
auto guard = sched.get_main_guard();
|
2018-03-12 19:04:03 +01:00
|
|
|
|
2018-05-24 17:09:27 +02:00
|
|
|
auto run = [&](auto &func, bool is_test) {
|
2022-01-09 10:04:08 +01:00
|
|
|
auto promise =
|
|
|
|
td::PromiseCreator::lambda([&, num = cnt](td::Result<td::SimpleConfigResult> r_simple_config_result) {
|
|
|
|
if (r_simple_config_result.is_ok()) {
|
|
|
|
auto simple_config_result = r_simple_config_result.move_as_ok();
|
|
|
|
auto date = simple_config_result.r_http_date.is_ok()
|
|
|
|
? td::to_string(simple_config_result.r_http_date.ok())
|
|
|
|
: (PSTRING() << simple_config_result.r_http_date.error());
|
|
|
|
auto config = simple_config_result.r_config.is_ok()
|
|
|
|
? to_string(simple_config_result.r_config.ok())
|
|
|
|
: (PSTRING() << simple_config_result.r_config.error());
|
|
|
|
LOG(ERROR) << num << " " << date << " " << config;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << num << " " << r_simple_config_result.error();
|
|
|
|
}
|
|
|
|
if (--cnt == 0) {
|
|
|
|
td::Scheduler::instance()->finish();
|
|
|
|
}
|
|
|
|
});
|
2018-05-24 18:27:56 +02:00
|
|
|
cnt++;
|
2022-08-18 20:45:29 +02:00
|
|
|
func(std::move(promise), false, td::Slice(), is_test, -1).release();
|
2018-05-24 17:09:27 +02:00
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
run(td::get_simple_config_azure, false);
|
|
|
|
run(td::get_simple_config_google_dns, false);
|
|
|
|
run(td::get_simple_config_mozilla_dns, false);
|
|
|
|
run(td::get_simple_config_azure, true);
|
|
|
|
run(td::get_simple_config_google_dns, true);
|
|
|
|
run(td::get_simple_config_mozilla_dns, true);
|
|
|
|
run(td::get_simple_config_firebase_remote_config, false);
|
|
|
|
run(td::get_simple_config_firebase_realtime, false);
|
|
|
|
run(td::get_simple_config_firebase_firestore, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-24 17:09:27 +02:00
|
|
|
cnt--;
|
2020-11-21 13:34:51 +01:00
|
|
|
if (cnt != 0) {
|
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty;
|
|
|
|
}
|
|
|
|
sched.finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Mtproto, encrypted_config) {
|
2022-01-09 10:04:08 +01:00
|
|
|
td::string data =
|
2018-05-24 17:09:27 +02:00
|
|
|
" hO//tt \b\n\tiwPVovorKtIYtQ8y2ik7CqfJiJ4pJOCLRa4fBmNPixuRPXnBFF/3mTAAZoSyHq4SNylGHz0Cv1/"
|
|
|
|
"FnWWdEV+BPJeOTk+ARHcNkuJBt0CqnfcVCoDOpKqGyq0U31s2MOpQvHgAG+Tlpg02syuH0E4dCGRw5CbJPARiynteb9y5fT5x/"
|
|
|
|
"kmdp6BMR5tWQSQF0liH16zLh8BDSIdiMsikdcwnAvBwdNhRqQBqGx9MTh62MDmlebjtczE9Gz0z5cscUO2yhzGdphgIy6SP+"
|
|
|
|
"bwaqLWYF0XdPGjKLMUEJW+rou6fbL1t/EUXPtU0XmQAnO0Fh86h+AqDMOe30N4qKrPQ== ";
|
2024-01-22 11:11:04 +01:00
|
|
|
td::telegram_api::object_ptr<td::telegram_api::help_configSimple> config = td::decode_config(data).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class TestPingActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-01-09 10:04:08 +01:00
|
|
|
TestPingActor(td::IPAddress ip_address, td::Status *result) : ip_address_(ip_address), result_(result) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-09 10:04:08 +01:00
|
|
|
td::IPAddress ip_address_;
|
|
|
|
td::unique_ptr<td::mtproto::PingConnection> ping_connection_;
|
|
|
|
td::Status *result_;
|
2020-05-30 02:10:35 +02:00
|
|
|
bool is_inited_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2022-01-09 10:04:08 +01:00
|
|
|
auto r_socket = td::SocketFd::open(ip_address_);
|
2020-05-30 02:10:35 +02:00
|
|
|
if (r_socket.is_error()) {
|
|
|
|
LOG(ERROR) << "Failed to open socket: " << r_socket.error();
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
ping_connection_ = td::mtproto::PingConnection::create_req_pq(
|
|
|
|
td::mtproto::RawConnection::create(
|
|
|
|
ip_address_, td::BufferedFd<td::SocketFd>(r_socket.move_as_ok()),
|
|
|
|
td::mtproto::TransportType{td::mtproto::TransportType::Tcp, 0, td::mtproto::ProxySecret()}, nullptr),
|
2018-06-04 18:58:54 +02:00
|
|
|
3);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::subscribe(ping_connection_->get_poll_info().extract_pollable_fd(this));
|
2020-05-30 02:10:35 +02:00
|
|
|
is_inited_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
set_timeout_in(10);
|
|
|
|
yield();
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2020-05-30 02:10:35 +02:00
|
|
|
if (is_inited_) {
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::unsubscribe_before_close(ping_connection_->get_poll_info().get_pollable_fd_ref());
|
2020-05-30 02:10:35 +02:00
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto status = ping_connection_->flush();
|
|
|
|
if (status.is_error()) {
|
|
|
|
*result_ = std::move(status);
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
if (ping_connection_->was_pong()) {
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(INFO) << "Receive pong";
|
2018-12-31 20:04:05 +01:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2022-01-09 10:04:08 +01:00
|
|
|
*result_ = td::Status::Error("Timeout expired");
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
static td::IPAddress get_default_ip_address() {
|
|
|
|
td::IPAddress ip_address;
|
2018-09-04 17:51:28 +02:00
|
|
|
#if TD_EMSCRIPTEN
|
|
|
|
ip_address.init_host_port("venus.web.telegram.org/apiws", 443).ensure();
|
|
|
|
#else
|
2018-12-31 20:04:05 +01:00
|
|
|
ip_address.init_ipv4_port("149.154.167.40", 80).ensure();
|
2018-09-04 17:51:28 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
return ip_address;
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
static td::int32 get_default_dc_id() {
|
2018-05-23 18:49:05 +02:00
|
|
|
return 10002;
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class Mtproto_ping final : public td::Test {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
using Test::Test;
|
|
|
|
bool step() final {
|
|
|
|
if (!is_inited_) {
|
|
|
|
sched_.create_actor_unsafe<TestPingActor>(0, "Pinger", get_default_ip_address(), &result_).release();
|
|
|
|
sched_.start();
|
|
|
|
is_inited_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = sched_.run_main(10);
|
|
|
|
if (ret) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sched_.finish();
|
|
|
|
if (result_.is_error()) {
|
|
|
|
LOG(ERROR) << result_;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_inited_ = false;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched_{0, 0};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Status result_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::RegisterTest<Mtproto_ping> mtproto_ping("Mtproto_ping");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class HandshakeContext final : public td::mtproto::AuthKeyHandshakeContext {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-01-09 10:04:08 +01:00
|
|
|
td::mtproto::DhCallback *get_dh_callback() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
td::mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
|
2024-01-22 00:52:58 +01:00
|
|
|
return public_rsa_key_.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-01-22 00:52:58 +01:00
|
|
|
std::shared_ptr<td::mtproto::PublicRsaKeyInterface> public_rsa_key_ = td::PublicRsaKeySharedMain::create(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class HandshakeTestActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-01-09 10:04:08 +01:00
|
|
|
HandshakeTestActor(td::int32 dc_id, td::Status *result) : dc_id_(dc_id), result_(result) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-09 10:04:08 +01:00
|
|
|
td::int32 dc_id_ = 0;
|
|
|
|
td::Status *result_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool wait_for_raw_connection_ = false;
|
2022-01-09 10:04:08 +01:00
|
|
|
td::unique_ptr<td::mtproto::RawConnection> raw_connection_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool wait_for_handshake_ = false;
|
2022-01-09 10:04:08 +01:00
|
|
|
td::unique_ptr<td::mtproto::AuthKeyHandshake> handshake_;
|
|
|
|
td::Status status_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool wait_for_result_ = false;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (raw_connection_) {
|
|
|
|
raw_connection_->close();
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
finish(td::Status::Error("Interrupted"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!wait_for_raw_connection_ && !raw_connection_) {
|
2021-03-26 16:04:01 +01:00
|
|
|
auto ip_address = get_default_ip_address();
|
2022-01-09 10:04:08 +01:00
|
|
|
auto r_socket = td::SocketFd::open(ip_address);
|
2020-05-30 02:10:35 +02:00
|
|
|
if (r_socket.is_error()) {
|
2022-01-09 10:04:08 +01:00
|
|
|
finish(td::Status::Error(PSTRING() << "Failed to open socket: " << r_socket.error()));
|
2020-05-30 02:10:35 +02:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
raw_connection_ = td::mtproto::RawConnection::create(
|
|
|
|
ip_address, td::BufferedFd<td::SocketFd>(r_socket.move_as_ok()),
|
|
|
|
td::mtproto::TransportType{td::mtproto::TransportType::Tcp, 0, td::mtproto::ProxySecret()}, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!wait_for_handshake_ && !handshake_) {
|
2022-01-09 10:04:08 +01:00
|
|
|
handshake_ = td::make_unique<td::mtproto::AuthKeyHandshake>(dc_id_, 3600);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (raw_connection_ && handshake_) {
|
|
|
|
if (wait_for_result_) {
|
|
|
|
wait_for_result_ = false;
|
|
|
|
if (status_.is_error()) {
|
|
|
|
finish(std::move(status_));
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
if (!handshake_->is_ready_for_finish()) {
|
2022-01-09 10:04:08 +01:00
|
|
|
finish(td::Status::Error("Key is not ready.."));
|
2018-12-31 20:04:05 +01:00
|
|
|
return stop();
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
finish(td::Status::OK());
|
2018-12-31 20:04:05 +01:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_result_ = true;
|
2022-01-09 10:04:08 +01:00
|
|
|
td::create_actor<td::mtproto::HandshakeActor>(
|
|
|
|
"HandshakeActor", std::move(handshake_), std::move(raw_connection_), td::make_unique<HandshakeContext>(),
|
|
|
|
10.0,
|
|
|
|
td::PromiseCreator::lambda(
|
2022-03-05 12:35:19 +01:00
|
|
|
[actor_id = actor_id(this)](td::Result<td::unique_ptr<td::mtproto::RawConnection>> raw_connection) {
|
|
|
|
td::send_closure(actor_id, &HandshakeTestActor::got_connection, std::move(raw_connection), 1);
|
2022-01-09 10:04:08 +01:00
|
|
|
}),
|
|
|
|
td::PromiseCreator::lambda(
|
2022-03-05 12:35:19 +01:00
|
|
|
[actor_id = actor_id(this)](td::Result<td::unique_ptr<td::mtproto::AuthKeyHandshake>> handshake) {
|
|
|
|
td::send_closure(actor_id, &HandshakeTestActor::got_handshake, std::move(handshake), 1);
|
2022-01-09 10:04:08 +01:00
|
|
|
}))
|
2018-12-31 20:04:05 +01:00
|
|
|
.release();
|
|
|
|
wait_for_raw_connection_ = true;
|
|
|
|
wait_for_handshake_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
void got_connection(td::Result<td::unique_ptr<td::mtproto::RawConnection>> r_raw_connection, bool dummy) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(wait_for_raw_connection_);
|
|
|
|
wait_for_raw_connection_ = false;
|
|
|
|
if (r_raw_connection.is_ok()) {
|
|
|
|
raw_connection_ = r_raw_connection.move_as_ok();
|
2022-01-09 10:04:08 +01:00
|
|
|
status_ = td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
status_ = r_raw_connection.move_as_error();
|
|
|
|
}
|
|
|
|
// TODO: save error
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
void got_handshake(td::Result<td::unique_ptr<td::mtproto::AuthKeyHandshake>> r_handshake, bool dummy) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(wait_for_handshake_);
|
|
|
|
wait_for_handshake_ = false;
|
|
|
|
CHECK(r_handshake.is_ok());
|
|
|
|
handshake_ = r_handshake.move_as_ok();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
void finish(td::Status status) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!result_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*result_ = std::move(status);
|
|
|
|
result_ = nullptr;
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class Mtproto_handshake final : public td::Test {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
using Test::Test;
|
|
|
|
bool step() final {
|
|
|
|
if (!is_inited_) {
|
2018-05-23 18:49:05 +02:00
|
|
|
sched_.create_actor_unsafe<HandshakeTestActor>(0, "HandshakeTestActor", get_default_dc_id(), &result_).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
sched_.start();
|
|
|
|
is_inited_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = sched_.run_main(10);
|
|
|
|
if (ret) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sched_.finish();
|
|
|
|
if (result_.is_error()) {
|
|
|
|
LOG(ERROR) << result_;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_inited_ = false;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched_{0, 0};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Status result_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::RegisterTest<Mtproto_handshake> mtproto_handshake("Mtproto_handshake");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class Socks5TestActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2022-01-09 10:04:08 +01:00
|
|
|
auto promise =
|
|
|
|
td::PromiseCreator::lambda([actor_id = actor_id(this)](td::Result<td::BufferedFd<td::SocketFd>> res) {
|
|
|
|
td::send_closure(actor_id, &Socks5TestActor::on_result, std::move(res), false);
|
|
|
|
});
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class Callback final : public td::TransparentProxy::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-01-09 10:04:08 +01:00
|
|
|
explicit Callback(td::Promise<td::BufferedFd<td::SocketFd>> promise) : promise_(std::move(promise)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
void set_result(td::Result<td::BufferedFd<td::SocketFd>> result) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_result(std::move(result));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_connected() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Promise<td::BufferedFd<td::SocketFd>> promise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
td::IPAddress socks5_ip;
|
2018-12-31 20:04:05 +01:00
|
|
|
socks5_ip.init_ipv4_port("131.191.89.104", 43077).ensure();
|
2022-01-09 10:04:08 +01:00
|
|
|
td::IPAddress mtproto_ip_address = get_default_ip_address();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
auto r_socket = td::SocketFd::open(socks5_ip);
|
2020-05-30 02:10:35 +02:00
|
|
|
if (r_socket.is_error()) {
|
2022-01-09 10:04:08 +01:00
|
|
|
return promise.set_error(td::Status::Error(PSTRING() << "Failed to open socket: " << r_socket.error()));
|
2020-05-30 02:10:35 +02:00
|
|
|
}
|
2022-06-14 15:11:38 +02:00
|
|
|
td::create_actor<td::Socks5>("Socks5", r_socket.move_as_ok(), mtproto_ip_address, "", "",
|
2022-01-09 10:04:08 +01:00
|
|
|
td::make_unique<Callback>(std::move(promise)), actor_shared(this))
|
2018-12-31 20:04:05 +01:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-09 10:04:08 +01:00
|
|
|
void on_result(td::Result<td::BufferedFd<td::SocketFd>> res, bool dummy) {
|
2018-12-31 20:04:05 +01:00
|
|
|
res.ensure();
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Mtproto, socks5) {
|
|
|
|
return;
|
|
|
|
int threads_n = 0;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-27 02:03:15 +02:00
|
|
|
sched.create_actor_unsafe<Socks5TestActor>(0, "Socks5TestActor").release();
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty;
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
2019-01-29 16:16:52 +01:00
|
|
|
|
|
|
|
TEST(Mtproto, notifications) {
|
2022-01-09 10:04:08 +01:00
|
|
|
td::vector<td::string> pushes = {
|
2019-01-29 16:16:52 +01:00
|
|
|
"eyJwIjoiSkRnQ3NMRWxEaWhyVWRRN1pYM3J1WVU4TlRBMFhMb0N6UWRNdzJ1cWlqMkdRbVR1WXVvYXhUeFJHaG1QQm8yVElYZFBzX2N3b2RIb3lY"
|
|
|
|
"b2drVjM1dVl0UzdWeElNX1FNMDRKMG1mV3ZZWm4zbEtaVlJ0aFVBNGhYUWlaN0pfWDMyZDBLQUlEOWgzRnZwRjNXUFRHQWRaVkdFYzg3bnFPZ3hD"
|
|
|
|
"NUNMRkM2SU9fZmVqcEpaV2RDRlhBWWpwc1k2aktrbVNRdFZ1MzE5ZW04UFVieXZudFpfdTNud2hjQ0czMk96TGp4S1kyS1lzU21JZm1GMzRmTmw1"
|
|
|
|
"QUxaa2JvY2s2cE5rZEdrak9qYmRLckJyU0ZtWU8tQ0FsRE10dEplZFFnY1U5bVJQdU80b1d2NG5sb1VXS19zSlNTaXdIWEZyb1pWTnZTeFJ0Z1dN"
|
2019-03-25 01:12:06 +01:00
|
|
|
"ZyJ9",
|
|
|
|
"eyJwIjoiSkRnQ3NMRWxEaWlZby1GRWJndk9WaTFkUFdPVmZndzBBWHYwTWNzWDFhWEtNZC03T1Q2WWNfT0taRURHZDJsZ0h0WkhMSllyVG50RE95"
|
|
|
|
"TkY1aXJRQlZ4UUFLQlRBekhPTGZIS3BhQXdoaWd5b3NQd0piWnJVV2xRWmh4eEozUFUzZjBNRTEwX0xNT0pFN0xsVUFaY2dabUNaX2V1QmNPZWNK"
|
|
|
|
"VERxRkpIRGZjN2pBOWNrcFkyNmJRT2dPUEhCeHlEMUVrNVdQcFpLTnlBODVuYzQ1eHFPdERqcU5aVmFLU3pKb2VIcXBQMnJqR29kN2M5YkxsdGd5"
|
|
|
|
"Q0NGd2NBU3dJeDc3QWNWVXY1UnVZIn0"};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::string key =
|
2019-01-29 16:16:52 +01:00
|
|
|
"uBa5yu01a-nJJeqsR3yeqMs6fJLYXjecYzFcvS6jIwS3nefBIr95LWrTm-IbRBNDLrkISz1Sv0KYpDzhU8WFRk1D0V_"
|
|
|
|
"qyO7XsbDPyrYxRBpGxofJUINSjb1uCxoSdoh1_F0UXEA2fWWKKVxL0DKUQssZfbVj3AbRglsWpH-jDK1oc6eBydRiS3i4j-"
|
|
|
|
"H0yJkEMoKRgaF9NaYI4u26oIQ-Ez46kTVU-R7e3acdofOJKm7HIKan_5ZMg82Dvec2M6vc_"
|
|
|
|
"I54Vs28iBx8IbBO1y5z9WSScgW3JCvFFKP2MXIu7Jow5-cpUx6jXdzwRUb9RDApwAFKi45zpv8eb3uPCDAmIQ";
|
2022-01-09 10:04:08 +01:00
|
|
|
td::vector<td::string> decrypted_payloads = {
|
2019-03-25 09:08:48 +01:00
|
|
|
"eyJsb2Nfa2V5IjoiTUVTU0FHRV9URVhUIiwibG9jX2FyZ3MiOlsiQXJzZW55IFNtaXJub3YiLCJhYmNkZWZnIl0sImN1c3RvbSI6eyJtc2dfaWQi"
|
|
|
|
"OiI1OTAwNDciLCJmcm9tX2lkIjoiNjI4MTQifSwiYmFkZ2UiOiI0MDkifQ",
|
2019-03-25 01:12:06 +01:00
|
|
|
"eyJsb2Nfa2V5IjoiIiwibG9jX2FyZ3MiOltdLCJjdXN0b20iOnsiY2hhbm5lbF9pZCI6IjExNzY4OTU0OTciLCJtYXhfaWQiOiIxMzU5In0sImJh"
|
|
|
|
"ZGdlIjoiMCJ9"};
|
2022-01-09 10:04:08 +01:00
|
|
|
key = td::base64url_decode(key).move_as_ok();
|
2019-01-29 16:16:52 +01:00
|
|
|
|
2019-03-25 01:12:06 +01:00
|
|
|
for (size_t i = 0; i < pushes.size(); i++) {
|
2022-01-09 10:04:08 +01:00
|
|
|
auto push = td::base64url_decode(pushes[i]).move_as_ok();
|
|
|
|
auto decrypted_payload = td::base64url_decode(decrypted_payloads[i]).move_as_ok();
|
2019-03-25 01:12:06 +01:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
auto key_id = td::mtproto::DhHandshake::calc_key_id(key);
|
|
|
|
ASSERT_EQ(key_id, td::NotificationManager::get_push_receiver_id(push).ok());
|
|
|
|
ASSERT_EQ(decrypted_payload, td::NotificationManager::decrypt_push(key_id, key, push).ok());
|
2019-03-25 01:12:06 +01:00
|
|
|
}
|
2019-01-29 16:16:52 +01:00
|
|
|
}
|
2019-05-06 18:59:49 +02:00
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class FastPingTestActor final : public td::Actor {
|
2019-05-06 18:59:49 +02:00
|
|
|
public:
|
2022-01-09 10:04:08 +01:00
|
|
|
explicit FastPingTestActor(td::Status *result) : result_(result) {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Status *result_;
|
|
|
|
td::unique_ptr<td::mtproto::RawConnection> connection_;
|
|
|
|
td::unique_ptr<td::mtproto::AuthKeyHandshake> handshake_;
|
|
|
|
td::ActorOwn<> fast_ping_;
|
2019-05-06 18:59:49 +02:00
|
|
|
int iteration_{0};
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
// Run handshake to create key and salt
|
2021-03-26 16:04:01 +01:00
|
|
|
auto ip_address = get_default_ip_address();
|
2022-01-09 10:04:08 +01:00
|
|
|
auto r_socket = td::SocketFd::open(ip_address);
|
2020-05-30 02:10:35 +02:00
|
|
|
if (r_socket.is_error()) {
|
2022-01-09 10:04:08 +01:00
|
|
|
*result_ = td::Status::Error(PSTRING() << "Failed to open socket: " << r_socket.error());
|
2020-05-30 02:10:35 +02:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
auto raw_connection = td::mtproto::RawConnection::create(
|
|
|
|
ip_address, td::BufferedFd<td::SocketFd>(r_socket.move_as_ok()),
|
|
|
|
td::mtproto::TransportType{td::mtproto::TransportType::Tcp, 0, td::mtproto::ProxySecret()}, nullptr);
|
|
|
|
auto handshake = td::make_unique<td::mtproto::AuthKeyHandshake>(get_default_dc_id(), 60 * 100 /*temp*/);
|
|
|
|
td::create_actor<td::mtproto::HandshakeActor>(
|
|
|
|
"HandshakeActor", std::move(handshake), std::move(raw_connection), td::make_unique<HandshakeContext>(), 10.0,
|
|
|
|
td::PromiseCreator::lambda(
|
2022-03-05 12:35:19 +01:00
|
|
|
[actor_id = actor_id(this)](td::Result<td::unique_ptr<td::mtproto::RawConnection>> raw_connection) {
|
|
|
|
td::send_closure(actor_id, &FastPingTestActor::got_connection, std::move(raw_connection), 1);
|
2022-01-09 10:04:08 +01:00
|
|
|
}),
|
|
|
|
td::PromiseCreator::lambda(
|
2022-03-05 12:35:19 +01:00
|
|
|
[actor_id = actor_id(this)](td::Result<td::unique_ptr<td::mtproto::AuthKeyHandshake>> handshake) {
|
|
|
|
td::send_closure(actor_id, &FastPingTestActor::got_handshake, std::move(handshake), 1);
|
2022-01-09 10:04:08 +01:00
|
|
|
}))
|
2019-05-06 18:59:49 +02:00
|
|
|
.release();
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
|
|
|
|
void got_connection(td::Result<td::unique_ptr<td::mtproto::RawConnection>> r_raw_connection, bool dummy) {
|
2019-05-06 18:59:49 +02:00
|
|
|
if (r_raw_connection.is_error()) {
|
|
|
|
*result_ = r_raw_connection.move_as_error();
|
2020-01-30 13:14:02 +01:00
|
|
|
LOG(INFO) << "Receive " << *result_ << " instead of a connection";
|
2019-05-06 18:59:49 +02:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
connection_ = r_raw_connection.move_as_ok();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
void got_handshake(td::Result<td::unique_ptr<td::mtproto::AuthKeyHandshake>> r_handshake, bool dummy) {
|
2019-05-06 18:59:49 +02:00
|
|
|
if (r_handshake.is_error()) {
|
|
|
|
*result_ = r_handshake.move_as_error();
|
2020-01-30 13:14:02 +01:00
|
|
|
LOG(INFO) << "Receive " << *result_ << " instead of a handshake";
|
2019-05-06 18:59:49 +02:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
handshake_ = r_handshake.move_as_ok();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
void got_raw_connection(td::Result<td::unique_ptr<td::mtproto::RawConnection>> r_connection) {
|
2019-05-06 18:59:49 +02:00
|
|
|
if (r_connection.is_error()) {
|
|
|
|
*result_ = r_connection.move_as_error();
|
2020-01-30 13:14:02 +01:00
|
|
|
LOG(INFO) << "Receive " << *result_ << " instead of a handshake";
|
2019-05-06 18:59:49 +02:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
connection_ = r_connection.move_as_ok();
|
2021-03-26 16:04:01 +01:00
|
|
|
LOG(INFO) << "RTT: " << connection_->extra().rtt;
|
|
|
|
connection_->extra().rtt = 0;
|
2019-05-06 18:59:49 +02:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
if (handshake_ && connection_) {
|
2019-06-02 02:17:30 +02:00
|
|
|
LOG(INFO) << "Iteration " << iteration_;
|
2019-05-06 18:59:49 +02:00
|
|
|
if (iteration_ == 6) {
|
|
|
|
return stop();
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
td::unique_ptr<td::mtproto::AuthData> auth_data;
|
2019-05-06 18:59:49 +02:00
|
|
|
if (iteration_ % 2 == 0) {
|
2022-01-09 10:04:08 +01:00
|
|
|
auth_data = td::make_unique<td::mtproto::AuthData>();
|
2020-01-30 02:06:15 +01:00
|
|
|
auth_data->set_tmp_auth_key(handshake_->get_auth_key());
|
2023-05-14 20:58:54 +02:00
|
|
|
auth_data->reset_server_time_difference(handshake_->get_server_time_diff());
|
2022-01-09 10:04:08 +01:00
|
|
|
auth_data->set_server_salt(handshake_->get_server_salt(), td::Time::now());
|
|
|
|
auth_data->set_future_salts({td::mtproto::ServerSalt{0u, 1e20, 1e30}}, td::Time::now());
|
2019-05-06 18:59:49 +02:00
|
|
|
auth_data->set_use_pfs(true);
|
2022-01-09 10:04:08 +01:00
|
|
|
td::uint64 session_id = 0;
|
2019-05-06 18:59:49 +02:00
|
|
|
do {
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Random::secure_bytes(reinterpret_cast<td::uint8 *>(&session_id), sizeof(session_id));
|
2019-05-06 18:59:49 +02:00
|
|
|
} while (session_id == 0);
|
|
|
|
auth_data->set_session_id(session_id);
|
|
|
|
}
|
|
|
|
iteration_++;
|
|
|
|
fast_ping_ = create_ping_actor(
|
2021-10-18 18:26:14 +02:00
|
|
|
td::Slice(), std::move(connection_), std::move(auth_data),
|
2022-01-09 10:04:08 +01:00
|
|
|
td::PromiseCreator::lambda(
|
2022-03-05 12:35:19 +01:00
|
|
|
[actor_id = actor_id(this)](td::Result<td::unique_ptr<td::mtproto::RawConnection>> r_raw_connection) {
|
|
|
|
td::send_closure(actor_id, &FastPingTestActor::got_raw_connection, std::move(r_raw_connection));
|
2022-01-09 10:04:08 +01:00
|
|
|
}),
|
|
|
|
td::ActorShared<>());
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 13:14:02 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2020-01-30 13:14:02 +01:00
|
|
|
}
|
2019-05-06 18:59:49 +02:00
|
|
|
};
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
class Mtproto_FastPing final : public td::Test {
|
2019-05-06 18:59:49 +02:00
|
|
|
public:
|
|
|
|
using Test::Test;
|
|
|
|
bool step() final {
|
|
|
|
if (!is_inited_) {
|
|
|
|
sched_.create_actor_unsafe<FastPingTestActor>(0, "FastPingTestActor", &result_).release();
|
|
|
|
sched_.start();
|
|
|
|
is_inited_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = sched_.run_main(10);
|
|
|
|
if (ret) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sched_.finish();
|
|
|
|
if (result_.is_error()) {
|
|
|
|
LOG(ERROR) << result_;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_inited_ = false;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched_{0, 0};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Status result_;
|
2019-05-06 18:59:49 +02:00
|
|
|
};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::RegisterTest<Mtproto_FastPing> mtproto_fastping("Mtproto_FastPing");
|
2019-06-25 23:06:27 +02:00
|
|
|
|
2019-07-01 15:18:28 +02:00
|
|
|
TEST(Mtproto, Grease) {
|
2022-01-09 10:04:08 +01:00
|
|
|
td::string s(10000, '0');
|
|
|
|
td::mtproto::Grease::init(s);
|
2019-06-25 23:06:27 +02:00
|
|
|
for (auto c : s) {
|
2019-07-01 15:18:28 +02:00
|
|
|
CHECK((c & 0xF) == 0xA);
|
2019-06-25 23:06:27 +02:00
|
|
|
}
|
|
|
|
for (size_t i = 1; i < s.size(); i += 2) {
|
|
|
|
CHECK(s[i] != s[i - 1]);
|
|
|
|
}
|
2019-07-01 15:18:28 +02:00
|
|
|
}
|
|
|
|
|
2019-07-07 02:35:30 +02:00
|
|
|
TEST(Mtproto, TlsTransport) {
|
2019-06-25 23:06:27 +02:00
|
|
|
int threads_n = 1;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2019-06-25 23:06:27 +02:00
|
|
|
{
|
|
|
|
auto guard = sched.get_main_guard();
|
2022-01-09 10:04:08 +01:00
|
|
|
class RunTest final : public td::Actor {
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2022-01-09 10:04:08 +01:00
|
|
|
class Callback final : public td::TransparentProxy::Callback {
|
2019-07-07 02:35:30 +02:00
|
|
|
public:
|
2022-01-09 10:04:08 +01:00
|
|
|
void set_result(td::Result<td::BufferedFd<td::SocketFd>> result) final {
|
2020-07-15 15:38:04 +02:00
|
|
|
if (result.is_ok()) {
|
|
|
|
LOG(ERROR) << "Unexpectedly succeeded to connect to MTProto proxy";
|
|
|
|
} else if (result.error().message() != "Response hash mismatch") {
|
|
|
|
LOG(ERROR) << "Receive unexpected result " << result.error();
|
2020-05-30 02:10:35 +02:00
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2019-07-07 02:35:30 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_connected() final {
|
2019-07-07 02:35:30 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-09 10:04:08 +01:00
|
|
|
const td::string domain = "www.google.com";
|
|
|
|
td::IPAddress ip_address;
|
2020-01-30 13:14:02 +01:00
|
|
|
auto resolve_status = ip_address.init_host_port(domain, 443);
|
|
|
|
if (resolve_status.is_error()) {
|
|
|
|
LOG(ERROR) << resolve_status;
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2020-01-30 13:14:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
auto r_socket = td::SocketFd::open(ip_address);
|
2020-05-30 02:10:35 +02:00
|
|
|
if (r_socket.is_error()) {
|
|
|
|
LOG(ERROR) << "Failed to open socket: " << r_socket.error();
|
2022-01-09 10:04:08 +01:00
|
|
|
td::Scheduler::instance()->finish();
|
2020-05-30 02:10:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-01-09 10:04:08 +01:00
|
|
|
td::create_actor<td::mtproto::TlsInit>("TlsInit", r_socket.move_as_ok(), domain, "0123456789secret",
|
|
|
|
td::make_unique<Callback>(), td::ActorShared<>(),
|
|
|
|
td::Clocks::system() - td::Time::now())
|
2019-07-07 02:35:30 +02:00
|
|
|
.release();
|
2019-06-25 23:06:27 +02:00
|
|
|
}
|
|
|
|
};
|
2022-01-09 10:04:08 +01:00
|
|
|
td::create_actor<RunTest>("RunTest").release();
|
2019-06-25 23:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
2020-12-22 20:28:04 +01:00
|
|
|
|
|
|
|
TEST(Mtproto, RSA) {
|
|
|
|
auto pem = td::Slice(
|
|
|
|
"-----BEGIN RSA PUBLIC KEY-----\n"
|
|
|
|
"MIIBCgKCAQEAr4v4wxMDXIaMOh8bayF/NyoYdpcysn5EbjTIOZC0RkgzsRj3SGlu\n"
|
|
|
|
"52QSz+ysO41dQAjpFLgxPVJoOlxXokaOq827IfW0bGCm0doT5hxtedu9UCQKbE8j\n"
|
|
|
|
"lDOk+kWMXHPZFJKWRgKgTu9hcB3y3Vk+JFfLpq3d5ZB48B4bcwrRQnzkx5GhWOFX\n"
|
|
|
|
"x73ZgjO93eoQ2b/lDyXxK4B4IS+hZhjzezPZTI5upTRbs5ljlApsddsHrKk6jJNj\n"
|
|
|
|
"8Ygs/ps8e6ct82jLXbnndC9s8HjEvDvBPH9IPjv5JUlmHMBFZ5vFQIfbpo0u0+1P\n"
|
|
|
|
"n6bkEi5o7/ifoyVv2pAZTRwppTz0EuXD8QIDAQAB\n"
|
|
|
|
"-----END RSA PUBLIC KEY-----");
|
2021-07-08 15:39:34 +02:00
|
|
|
auto rsa = td::mtproto::RSA::from_pem_public_key(pem).move_as_ok();
|
2020-12-22 20:28:04 +01:00
|
|
|
ASSERT_EQ(-7596991558377038078, rsa.get_fingerprint());
|
|
|
|
ASSERT_EQ(256u, rsa.size());
|
|
|
|
|
2021-07-08 15:39:34 +02:00
|
|
|
td::string to(256, '\0');
|
|
|
|
rsa.encrypt(pem.substr(0, 256), to);
|
|
|
|
ASSERT_EQ("U2nJEtB2AgpHrm3HB0yhpTQgb0wbesi9Pv/W1v/vULU=", td::base64_encode(td::sha256(to)));
|
2020-12-22 20:28:04 +01:00
|
|
|
}
|