Compare commits

..

2 commits

Author SHA1 Message Date
d4e78e63b2 Successful connection using Connect() overload for proxy
Some checks failed
PR Check / on-push-commit-check (push) Failing after 12m30s
2024-11-23 14:32:40 +03:00
8299d01fe5 Added Proxy in ClientCreateVisitor 2024-11-21 20:36:13 +03:00
2 changed files with 43 additions and 28 deletions

9
.gitignore vendored
View file

@ -30,15 +30,6 @@ libxmlplusplus-prefix/
spdlog.pc spdlog.pc
build* build*
temp* 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/ /.idea/

View file

@ -157,6 +157,15 @@ struct StartTlsRequest {
} }
}; };
template <typename Socket>
auto ConnectViaProxy(Socket& socket, const NoProxy&, std::string_view host, std::uint16_t port) -> boost::asio::awaitable<void> {
auto executor = co_await boost::asio::this_coro::executor;
boost::asio::ip::tcp::resolver resolver(executor);
auto endpoints = co_await resolver.async_resolve(host, std::to_string(port), boost::asio::use_awaitable);
co_await boost::asio::async_connect(socket, endpoints, boost::asio::use_awaitable);
co_return;
}
template <typename Socket> template <typename Socket>
auto ConnectViaProxy(Socket& socket, const HttpProxy& param_proxy, std::string_view host, std::uint16_t port) auto ConnectViaProxy(Socket& socket, const HttpProxy& param_proxy, std::string_view host, std::uint16_t port)
-> boost::asio::awaitable<void> { -> boost::asio::awaitable<void> {
@ -164,17 +173,17 @@ auto ConnectViaProxy(Socket& socket, const HttpProxy& param_proxy, std::string_v
constexpr unsigned int kSuccessStatusCode = 200; constexpr unsigned int kSuccessStatusCode = 200;
constexpr std::string_view kEndOfHeaders = "\r\n\r\n"; constexpr std::string_view kEndOfHeaders = "\r\n\r\n";
constexpr int kEndOfHttpSubstring = 5; 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);
// ответ от прокси-сервера co_await boost::asio::async_write(
boost::asio::streambuf response; socket, boost::asio::buffer(httpConnectRequest), boost::asio::transfer_all(), boost::asio::use_awaitable);
std::size_t bytesTransferred = co_await boost::asio::async_read_until(socket, response, kEndOfHeaders, boost::asio::use_awaitable);
// статус ответа boost::asio::streambuf proxyServerResponse;
std::istream responseStream(&response); 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; std::string httpVersion;
unsigned int statusCode = 0; unsigned int statusCode = 0;
std::string statusMessage; std::string statusMessage;
@ -196,7 +205,7 @@ auto ConnectViaProxy(Socket& socket, const HttpProxy& param_proxy, std::string_v
} }
template <typename Socket> template <typename Socket>
auto ConnectViaProxy(Socket& socket, Socks5Proxy& socksProxy, std::string_view address, std::uint16_t port) auto ConnectViaProxy(Socket& socket, const Socks5Proxy& socksProxy, std::string_view address, std::uint16_t port)
-> boost::asio::awaitable<void> { -> boost::asio::awaitable<void> {
constexpr std::array kSocks5RequestStart = {std::byte{0x05}, std::byte{0x01}, std::byte{0x00}, std::byte{0x03}}; constexpr std::array kSocks5RequestStart = {std::byte{0x05}, std::byte{0x01}, std::byte{0x00}, std::byte{0x03}};
@ -263,9 +272,9 @@ inline auto GetSystemProxySettings() -> Proxy {
constexpr std::uint16_t kHttpPort = 8080; constexpr std::uint16_t kHttpPort = 8080;
return GetProxySettings<HttpProxy>(proxyEnv, kHttpPort); return GetProxySettings<HttpProxy>(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; constexpr std::uint16_t kSocksPort = 1080;
return GetProxySettings<Socks5Proxy>(proxy_env, kSocksPort); return GetProxySettings<Socks5Proxy>(proxyEnv, kSocksPort);
} }
return NoProxy{}; return NoProxy{};
@ -274,23 +283,25 @@ inline auto GetSystemProxySettings() -> Proxy {
template <typename Socket> template <typename Socket>
auto ConnectViaProxy(Socket& socket, const SystemConfiguredProxy&, std::string_view host, std::uint16_t port) auto ConnectViaProxy(Socket& socket, const SystemConfiguredProxy&, std::string_view host, std::uint16_t port)
-> boost::asio::awaitable<void> { -> boost::asio::awaitable<void> {
auto proxy_opt = GetSystemProxySettings(); auto proxyOpt = GetSystemProxySettings();
co_await std::visit( co_await std::visit(
[&](auto&& proxy_variant) -> boost::asio::awaitable<void> { [&](auto&& proxy_variant) -> boost::asio::awaitable<void> {
co_await ConnectViaProxy(socket, proxy_variant, host, port); co_await ConnectViaProxy(socket, proxy_variant, host, port);
}, },
proxy_opt); proxyOpt);
} }
template <typename Socket, typename ProxyType> template <typename Socket, typename ProxyType>
auto ConnectToServer(Socket& socket, const ProxyType& proxy, std::string_view host, std::uint16_t port) -> boost::asio::awaitable<void> { auto ConnectToServer(Socket& socket, const ProxyType& proxy, std::string_view host, std::uint16_t port) -> boost::asio::awaitable<void> {
auto executor = co_await boost::asio::this_coro::executor; auto executor = co_await boost::asio::this_coro::executor;
boost::asio::ip::tcp::resolver resolver(executor); boost::asio::ip::tcp::resolver resolver(executor);
auto endpoints = co_await resolver.async_resolve(proxy.hostname, std::to_string(proxy.port), boost::asio::use_awaitable); // auto endpoints = co_await resolver.async_resolve(proxy.hostname, std::to_string(proxy.port), boost::asio::use_awaitable);
co_await boost::asio::async_connect(socket, endpoints, boost::asio::use_awaitable); // co_await boost::asio::async_connect(socket, endpoints, boost::asio::use_awaitable);
if constexpr(!std::same_as<ProxyType, NoProxy>) { if constexpr(!std::same_as<ProxyType, NoProxy>) {
auto endpoints = co_await resolver.async_resolve(proxy.hostname, std::to_string(proxy.port), boost::asio::use_awaitable);
co_await boost::asio::async_connect(socket, endpoints, boost::asio::use_awaitable);
co_await ConnectViaProxy(socket, proxy, host, port); co_await ConnectViaProxy(socket, proxy, host, port);
} }
} }
@ -308,12 +319,12 @@ auto ConnectToServer(Socket& socket, const SystemConfiguredProxy& proxy, std::st
template <typename Socket> template <typename Socket>
auto ConnectWithProxy(Socket& socket, const Proxy& proxy, std::string_view host, std::uint16_t port) -> boost::asio::awaitable<void> { auto ConnectWithProxy(Socket& socket, const Proxy& proxy, std::string_view host, std::uint16_t port) -> boost::asio::awaitable<void> {
auto awaitable = std::visit( co_await std::visit(
[&socket, host, port](const auto& proxy_variant) { [&socket, host, port](const auto& proxy_variant) {
return ConnectToServer(socket, proxy_variant, host, port); return ConnectToServer(socket, proxy_variant, host, port);
}, },
proxy); proxy);
co_await awaitable; // co_await awaitable;
} }
inline auto GetAuthData(const PlainUserAccount& account) -> std::string { inline auto GetAuthData(const PlainUserAccount& account) -> std::string {
@ -418,6 +429,19 @@ struct ClientCreateVisitor {
co_await boost::asio::async_connect(socket, resolveResults, boost::asio::use_awaitable); co_await boost::asio::async_connect(socket, resolveResults, boost::asio::use_awaitable);
} }
template <typename Socket>
auto Connect(Socket& socket) -> boost::asio::awaitable<void> {
if(!std::holds_alternative<NoProxy>(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 <typename Socket> template <typename Socket>
auto ProcessTls(XmlStream<boost::asio::ssl::stream<Socket>>& stream) -> boost::asio::awaitable<void> { auto ProcessTls(XmlStream<boost::asio::ssl::stream<Socket>>& stream) -> boost::asio::awaitable<void> {
const StartTlsRequest request; const StartTlsRequest request;
@ -446,7 +470,7 @@ struct ClientCreateVisitor {
template <typename Socket> template <typename Socket>
inline auto operator()(XmlStream<Socket> stream) inline auto operator()(XmlStream<Socket> stream)
-> boost::asio::awaitable<std::variant<Client<Socket>, Client<boost::asio::ssl::stream<Socket>>>> { -> boost::asio::awaitable<std::variant<Client<Socket>, Client<boost::asio::ssl::stream<Socket>>>> {
co_await this->Connect(stream.next_layer(), co_await this->Resolve()); co_await this->Connect(stream.next_layer());
co_await stream.Send(UserStream{.from = account.Jid(), .to = account.Jid().server, .version = "1.0", .xmlLang = "en"}); co_await stream.Send(UserStream{.from = account.Jid(), .to = account.Jid().server, .version = "1.0", .xmlLang = "en"});
SPDLOG_DEBUG("UserStream sended"); SPDLOG_DEBUG("UserStream sended");
@ -469,7 +493,7 @@ struct ClientCreateVisitor {
inline auto operator()(XmlStream<boost::asio::ssl::stream<Socket>> stream) inline auto operator()(XmlStream<boost::asio::ssl::stream<Socket>> stream)
-> boost::asio::awaitable<std::variant<Client<Socket>, Client<boost::asio::ssl::stream<Socket>>>> { -> boost::asio::awaitable<std::variant<Client<Socket>, Client<boost::asio::ssl::stream<Socket>>>> {
auto& socket = stream.next_layer(); auto& socket = stream.next_layer();
co_await this->Connect(socket.next_layer(), co_await this->Resolve()); co_await this->Connect(socket.next_layer());
co_await stream.Send( co_await stream.Send(
UserStream{.from = account.Jid().Username("anonymous"), .to = account.Jid().server, .version = "1.0", .xmlLang = "en"}, UserStream{.from = account.Jid().Username("anonymous"), .to = account.Jid().server, .version = "1.0", .xmlLang = "en"},
socket.next_layer()); socket.next_layer());