diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 962471afb..f5ff9dda7 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -581,9 +581,9 @@ class TestProxyRequest : public RequestOnceActor { send_closure(actor_id, &TestProxyRequest::on_connection_data, std::move(r_data)); }); - child_ = ConnectionCreator::prepare_connection(r_socket_fd.move_as_ok(), ConnectionCreator::ProxyInfo{&proxy_}, - mtproto_ip, get_transport(), "Test", "TestPingDC2", nullptr, {}, - false, std::move(connection_promise)); + child_ = + ConnectionCreator::prepare_connection(r_socket_fd.move_as_ok(), proxy_, mtproto_ip, get_transport(), "Test", + "TestPingDC2", nullptr, {}, false, std::move(connection_promise)); } void on_connection_data(Result r_data) { diff --git a/td/telegram/net/ConnectionCreator.cpp b/td/telegram/net/ConnectionCreator.cpp index 1486c3e30..3d661caac 100644 --- a/td/telegram/net/ConnectionCreator.cpp +++ b/td/telegram/net/ConnectionCreator.cpp @@ -295,7 +295,6 @@ ActorId ConnectionCreator::get_dns_resolver() { void ConnectionCreator::ping_proxy(int32 proxy_id, Promise promise) { if (proxy_id == 0) { - ProxyInfo proxy{nullptr}; auto main_dc_id = G()->net_query_dispatcher().main_dc_id(); bool prefer_ipv6 = G()->shared_config().get_option_boolean("prefer_ipv6"); auto infos = dc_options_set_.find_all_connections(main_dc_id, false, false, prefer_ipv6, false); @@ -314,7 +313,7 @@ void ConnectionCreator::ping_proxy(int32 proxy_id, Promise promise) { request.result = Status::Error(400, "Failed to ping"); for (auto &info : infos) { - auto r_transport_type = get_transport_type(ProxyInfo{nullptr}, info); + auto r_transport_type = get_transport_type(Proxy(), info); if (r_transport_type.is_error()) { LOG(ERROR) << r_transport_type.error(); on_ping_main_dc_result(token, r_transport_type.move_as_error()); @@ -359,7 +358,7 @@ void ConnectionCreator::ping_proxy_resolved(int32 proxy_id, IPAddress ip_address if (it == proxies_.end()) { return promise.set_error(Status::Error(400, "Unknown proxy identifier")); } - ProxyInfo proxy(&it->second); + const Proxy &proxy = it->second; auto main_dc_id = G()->net_query_dispatcher().main_dc_id(); FindConnectionExtra extra; auto r_socket_fd = find_connection(proxy, ip_address, main_dc_id, false, extra); @@ -639,7 +638,7 @@ void ConnectionCreator::request_raw_connection_by_ip(IPAddress ip_address, mtpro }); auto token = next_token(); - auto ref = prepare_connection(std::move(socket_fd), ProxyInfo{nullptr}, IPAddress(), transport_type, "Raw", + auto ref = prepare_connection(std::move(socket_fd), Proxy(), IPAddress(), transport_type, "Raw", PSTRING() << "to IP address " << ip_address, nullptr, create_reference(token), false, std::move(connection_promise)); if (!ref.empty()) { @@ -647,7 +646,7 @@ void ConnectionCreator::request_raw_connection_by_ip(IPAddress ip_address, mtpro } } -Result ConnectionCreator::get_transport_type(const ProxyInfo &proxy, +Result ConnectionCreator::get_transport_type(const Proxy &proxy, const DcOptionsSet::ConnectionInfo &info) { int32 int_dc_id = info.option->get_dc_id().get_raw_id(); if (G()->is_test_dc()) { @@ -656,14 +655,13 @@ Result ConnectionCreator::get_transport_type(const Proxy int16 raw_dc_id = narrow_cast(info.option->is_media_only() ? -int_dc_id : int_dc_id); if (proxy.use_mtproto_proxy()) { - return mtproto::TransportType{mtproto::TransportType::ObfuscatedTcp, raw_dc_id, proxy.proxy().secret()}; + return mtproto::TransportType{mtproto::TransportType::ObfuscatedTcp, raw_dc_id, proxy.secret()}; } if (proxy.use_http_caching_proxy()) { CHECK(info.option != nullptr); string proxy_authorization; - if (!proxy.proxy().user().empty() || !proxy.proxy().password().empty()) { - proxy_authorization = - "|basic " + td::base64_encode(PSLICE() << proxy.proxy().user() << ':' << proxy.proxy().password()); + if (!proxy.user().empty() || !proxy.password().empty()) { + proxy_authorization = "|basic " + td::base64_encode(PSLICE() << proxy.user() << ':' << proxy.password()); } return mtproto::TransportType{ mtproto::TransportType::Http, 0, @@ -677,8 +675,8 @@ Result ConnectionCreator::get_transport_type(const Proxy } } -Result ConnectionCreator::find_connection(const ProxyInfo &proxy, const IPAddress &proxy_ip_address, - DcId dc_id, bool allow_media_only, FindConnectionExtra &extra) { +Result ConnectionCreator::find_connection(const Proxy &proxy, const IPAddress &proxy_ip_address, DcId dc_id, + bool allow_media_only, FindConnectionExtra &extra) { extra.debug_str = PSTRING() << "Failed to find valid IP for " << dc_id; bool prefer_ipv6 = G()->shared_config().get_option_boolean("prefer_ipv6") || (proxy.use_proxy() && proxy_ip_address.is_ipv6()); @@ -714,9 +712,9 @@ Result ConnectionCreator::find_connection(const ProxyInfo &proxy, cons } } -ActorOwn<> ConnectionCreator::prepare_connection(SocketFd socket_fd, const ProxyInfo &proxy, - const IPAddress &mtproto_ip, mtproto::TransportType transport_type, - Slice actor_name_prefix, Slice debug_str, +ActorOwn<> ConnectionCreator::prepare_connection(SocketFd socket_fd, const Proxy &proxy, const IPAddress &mtproto_ip, + mtproto::TransportType transport_type, Slice actor_name_prefix, + Slice debug_str, unique_ptr stats_callback, ActorShared<> parent, bool use_connection_token, Promise promise) { @@ -766,11 +764,11 @@ ActorOwn<> ConnectionCreator::prepare_connection(SocketFd socket_fd, const Proxy auto callback = make_unique(std::move(promise), std::move(stats_callback), use_connection_token); if (proxy.use_socks5_proxy()) { return ActorOwn<>(create_actor(PSLICE() << actor_name_prefix << "Socks5", std::move(socket_fd), - mtproto_ip, proxy.proxy().user().str(), proxy.proxy().password().str(), + mtproto_ip, proxy.user().str(), proxy.password().str(), std::move(callback), std::move(parent))); } else if (proxy.use_http_tcp_proxy()) { return ActorOwn<>(create_actor(PSLICE() << actor_name_prefix << "HttpProxy", std::move(socket_fd), - mtproto_ip, proxy.proxy().user().str(), proxy.proxy().password().str(), + mtproto_ip, proxy.user().str(), proxy.password().str(), std::move(callback), std::move(parent))); } else if (transport_type.secret.emulate_tls()) { return ActorOwn<>(create_actor( @@ -802,7 +800,8 @@ void ConnectionCreator::client_loop(ClientInfo &client) { return; } - ProxyInfo proxy{active_proxy_id_ == 0 ? nullptr : &proxies_[active_proxy_id_]}; + Proxy proxy = active_proxy_id_ == 0 ? Proxy() : proxies_[active_proxy_id_]; + if (proxy.use_proxy() && !proxy_ip_address_.is_valid()) { VLOG(connections) << "Exit client_loop, because there is no valid IP address for proxy: " << proxy_ip_address_; return; diff --git a/td/telegram/net/ConnectionCreator.h b/td/telegram/net/ConnectionCreator.h index 84c0b0d08..752dfc148 100644 --- a/td/telegram/net/ConnectionCreator.h +++ b/td/telegram/net/ConnectionCreator.h @@ -89,40 +89,10 @@ class ConnectionCreator : public NetQueryCallback { StateManager::ConnectionToken connection_token; unique_ptr stats_callback; }; - class ProxyInfo { - public: - explicit ProxyInfo(const Proxy *proxy) : proxy_(proxy) { - } - bool use_proxy() const { - return proxy_ != nullptr; - } - Proxy::Type proxy_type() const { - return proxy_ == nullptr ? Proxy::Type::None : proxy_->type(); - } - bool use_socks5_proxy() const { - return proxy_type() == Proxy::Type::Socks5; - } - bool use_http_tcp_proxy() const { - return proxy_type() == Proxy::Type::HttpTcp; - } - bool use_http_caching_proxy() const { - return proxy_type() == Proxy::Type::HttpCaching; - } - bool use_mtproto_proxy() const { - return proxy_type() == Proxy::Type::Mtproto; - } - const Proxy &proxy() const { - CHECK(use_proxy()); - return *proxy_; - } - - private: - const Proxy *proxy_; - }; static DcOptions get_default_dc_options(bool is_test); - static ActorOwn<> prepare_connection(SocketFd socket_fd, const ProxyInfo &proxy, const IPAddress &mtproto_ip, + static ActorOwn<> prepare_connection(SocketFd socket_fd, const Proxy &proxy, const IPAddress &mtproto_ip, mtproto::TransportType transport_type, Slice actor_name_prefix, Slice debug_str, unique_ptr stats_callback, ActorShared<> parent, bool use_connection_token, @@ -273,10 +243,10 @@ class ConnectionCreator : public NetQueryCallback { bool check_mode{false}; }; - static Result get_transport_type(const ProxyInfo &proxy, + static Result get_transport_type(const Proxy &proxy, const DcOptionsSet::ConnectionInfo &info); - Result find_connection(const ProxyInfo &proxy, const IPAddress &proxy_ip_address, DcId dc_id, + Result find_connection(const Proxy &proxy, const IPAddress &proxy_ip_address, DcId dc_id, bool allow_media_only, FindConnectionExtra &extra); ActorId get_dns_resolver(); diff --git a/td/telegram/net/Proxy.h b/td/telegram/net/Proxy.h index d9abcf568..55937626c 100644 --- a/td/telegram/net/Proxy.h +++ b/td/telegram/net/Proxy.h @@ -88,6 +88,22 @@ class Proxy { return type_; } + bool use_proxy() const { + return type() != Proxy::Type::None; + } + bool use_socks5_proxy() const { + return type() == Proxy::Type::Socks5; + } + bool use_mtproto_proxy() const { + return type() == Proxy::Type::Mtproto; + } + bool use_http_tcp_proxy() const { + return type() == Proxy::Type::HttpTcp; + } + bool use_http_caching_proxy() const { + return type() == Proxy::Type::HttpCaching; + } + template void store(StorerT &storer) const { using td::store;