From 8299d01fe52c29d5474d040741a1d92fad60967b Mon Sep 17 00:00:00 2001 From: sectapunterx Date: Thu, 21 Nov 2024 20:36:13 +0300 Subject: [PATCH] Added Proxy in ClientCreateVisitor --- .gitignore | 9 ------ library/include/larra/client/client.hpp | 37 +++++++++++++++++-------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 6c2ba0b..3f7db93 100644 --- a/.gitignore +++ b/.gitignore @@ -30,15 +30,6 @@ libxmlplusplus-prefix/ spdlog.pc build* temp* -#/.idea/codeStyles/codeStyleConfig.xml -#/.idea/discord.xml -#/.idea/editor.xml -#/.idea/larra.iml -#/.idea/material_theme_project_new.xml -#/.idea/misc.xml -#/.idea/modules.xml -#/.idea/codeStyles/Project.xml -#/.idea/vcs.xml /.idea/ diff --git a/library/include/larra/client/client.hpp b/library/include/larra/client/client.hpp index cf65660..15f9884 100644 --- a/library/include/larra/client/client.hpp +++ b/library/include/larra/client/client.hpp @@ -164,17 +164,17 @@ auto ConnectViaProxy(Socket& socket, const HttpProxy& param_proxy, std::string_v constexpr unsigned int kSuccessStatusCode = 200; constexpr std::string_view kEndOfHeaders = "\r\n\r\n"; constexpr int kEndOfHttpSubstring = 5; - // HTTP CONNECT запрос - std::string request = std::format("CONNECT {}:{} {}\r\nHost: {}:{}\r\n\r\n", host, port, kHttpVersion, host, port); - co_await boost::asio::async_write(socket, boost::asio::buffer(request), boost::asio::transfer_all(), boost::asio::use_awaitable); + std::string httpConnectRequest = std::format("CONNECT {}:{} {}\r\nHost: {}:{}\r\n\r\n", host, port, kHttpVersion, host, port); - // ответ от прокси-сервера - boost::asio::streambuf response; - std::size_t bytesTransferred = co_await boost::asio::async_read_until(socket, response, kEndOfHeaders, boost::asio::use_awaitable); + co_await boost::asio::async_write( + socket, boost::asio::buffer(httpConnectRequest), boost::asio::transfer_all(), boost::asio::use_awaitable); - // статус ответа - std::istream responseStream(&response); + boost::asio::streambuf proxyServerResponse; + std::size_t bytesTransferred = + co_await boost::asio::async_read_until(socket, proxyServerResponse, kEndOfHeaders, boost::asio::use_awaitable); + + std::istream responseStream(&proxyServerResponse); std::string httpVersion; unsigned int statusCode = 0; std::string statusMessage; @@ -263,9 +263,9 @@ inline auto GetSystemProxySettings() -> Proxy { constexpr std::uint16_t kHttpPort = 8080; return GetProxySettings(proxyEnv, kHttpPort); } - if(const char* proxy_env = std::getenv("socks_proxy")) { + if(const char* proxyEnv = std::getenv("socks_proxy")) { constexpr std::uint16_t kSocksPort = 1080; - return GetProxySettings(proxy_env, kSocksPort); + return GetProxySettings(proxyEnv, kSocksPort); } return NoProxy{}; @@ -274,13 +274,13 @@ inline auto GetSystemProxySettings() -> Proxy { template auto ConnectViaProxy(Socket& socket, const SystemConfiguredProxy&, std::string_view host, std::uint16_t port) -> boost::asio::awaitable { - auto proxy_opt = GetSystemProxySettings(); + auto proxyOpt = GetSystemProxySettings(); co_await std::visit( [&](auto&& proxy_variant) -> boost::asio::awaitable { co_await ConnectViaProxy(socket, proxy_variant, host, port); }, - proxy_opt); + proxyOpt); } template @@ -418,6 +418,19 @@ struct ClientCreateVisitor { co_await boost::asio::async_connect(socket, resolveResults, boost::asio::use_awaitable); } + template + auto Connect(Socket& socket) -> boost::asio::awaitable { + if(!std::holds_alternative(this->options.proxy)) { + auto host = this->options.hostname.value_or(account.Jid().server); + auto port = this->options.port.value_or(kDefaultXmppPort); + + co_await ConnectWithProxy(socket, this->options.proxy, host, port); + } else { + auto resolveResults = co_await this->Resolve(); + co_await boost::asio::async_connect(socket, resolveResults, boost::asio::use_awaitable); + } + } + template auto ProcessTls(XmlStream>& stream) -> boost::asio::awaitable { const StartTlsRequest request;