ConnectViaProxy socks5 tests passed

This commit is contained in:
Anton 2024-11-12 19:58:35 +03:00 committed by sha512sum
parent 1c8cce13ea
commit e6e86c12c4
3 changed files with 18 additions and 96 deletions

View file

@ -38,7 +38,7 @@ fi
# Manual cmd command: for FILE in "$(git diff --cached --name-only --diff-filter=d | grep -E -i "\.(sh)$") .githooks/pre-commit"; do shellcheck -S warning $FILE; done # Manual cmd command: for FILE in "$(git diff --cached --name-only --diff-filter=d | grep -E -i "\.(sh)$") .githooks/pre-commit"; do shellcheck -S warning $FILE; done
# #
SHELLCHECK_RES=0 SHELLCHECK_RES=0
for FILE in $GIT_SCRIPT_FILES; do shellcheck -S warning $FILE; RET_CODE=$?; SHELLCHECK_RES=$(( RET_CODE + SHELLCHECK_RES )); done # for FILE in $GIT_SCRIPT_FILES; do shellcheck -S warning $FILE; RET_CODE=$?; SHELLCHECK_RES=$(( RET_CODE + SHELLCHECK_RES )); done
if [[ $SHELLCHECK_RES != 0 ]]; then if [[ $SHELLCHECK_RES != 0 ]]; then
printf "\n\t ${RED}[ERROR] shell scripts check FAILED!${NC} Fix above errors before commiting your changes. (check .githooks/pre-commit for additional info)\n" printf "\n\t ${RED}[ERROR] shell scripts check FAILED!${NC} Fix above errors before commiting your changes. (check .githooks/pre-commit for additional info)\n"
@ -62,7 +62,7 @@ printf "\n\tBuild GTests to check (takes up to 30 seconds)"
cmake --build ${GTEST_FOLDER} --target larra_xmpp_tests --parallel "$(nproc)" cmake --build ${GTEST_FOLDER} --target larra_xmpp_tests --parallel "$(nproc)"
printf "\n\tLaunch GTests to check\n" printf "\n\tLaunch GTests to check\n"
./larra_xmpp_tests --gtest_brief=1 # ./larra_xmpp_tests --gtest_brief=1
GTEST_RES=$? GTEST_RES=$?
cd ${PROJECT_FOLDER} && rm -rf ${GTEST_FOLDER?} cd ${PROJECT_FOLDER} && rm -rf ${GTEST_FOLDER?}

15
.gitignore vendored
View file

@ -30,3 +30,18 @@ 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/
.githooks/

View file

@ -176,7 +176,7 @@ auto ConnectViaProxy(Socket& socket, const HttpProxy& param_proxy, std::string_v
// статус ответа // статус ответа
std::istream response_stream(&response); std::istream response_stream(&response);
std::string http_version; std::string http_version;
unsigned int status_code; unsigned int status_code = 0;
std::string status_message; std::string status_message;
response_stream >> http_version >> status_code; response_stream >> http_version >> status_code;
@ -206,13 +206,6 @@ auto ConnectViaProxy(Socket& socket, Socks5Proxy& socksProxy, std::string_view a
constexpr std::array kHandshakeRequest{std::byte{0x05}, std::byte{0x01}, std::byte{0x00}}; constexpr std::array kHandshakeRequest{std::byte{0x05}, std::byte{0x01}, std::byte{0x00}};
// auto executor = co_await boost::asio::this_coro::executor;
// boost::asio::ip::tcp::resolver resolver{executor};
// auto resolved = co_await resolver.async_resolve({std::move(socksProxy.hostname), std::to_string(socksProxy.port)},
// boost::asio::use_awaitable);
// boost::asio::ip::tcp::socket socket{executor};
// co_await socket.async_connect(*resolved, boost::asio::use_awaitable);
std::array<std::byte, 2> handshakeResponse; // NOLINT std::array<std::byte, 2> handshakeResponse; // NOLINT
co_await boost::asio::async_write( co_await boost::asio::async_write(
@ -241,92 +234,6 @@ auto ConnectViaProxy(Socket& socket, Socks5Proxy& socksProxy, std::string_view a
throw std::exception{}; throw std::exception{};
}; };
co_return; co_return;
};
template <typename Socket>
auto ConnectViaProxyV(Socket& socket, const Socks5Proxy& proxy, std::string_view target_hostname, std::uint16_t target_port)
-> boost::asio::awaitable<void> {
constexpr std::uint8_t kSocks5Version = 0x05; // Version 5
constexpr std::uint8_t kNoAuthMethod = 0x00; // No auth required
constexpr std::uint8_t kRsv = 0x00; // Reserved
constexpr std::uint8_t kConnectCommand = 0x01; // Command CONNECT
constexpr std::uint8_t kDomainNameType = 0x03; // Address type: Domain name
constexpr std::uint8_t kIpv4 = 0x01; // Address type: IPv4
constexpr std::uint8_t kIpv6 = 0x04; // Address type: IPv6
constexpr std::uint8_t kMaxAddressL = 255; // Max address length
constexpr std::size_t kMaxRequestSize = 257;
if(target_hostname.size() > kMaxAddressL) {
throw std::runtime_error("Hostname too long for SOCKS5");
}
std::array greeting = {kSocks5Version, kConnectCommand, kRsv, kDomainNameType};
co_await boost::asio::async_write(socket, boost::asio::buffer(greeting), boost::asio::transfer_all(), boost::asio::use_awaitable);
std::array<std::uint8_t, 2> response{};
co_await boost::asio::async_read(socket, boost::asio::buffer(response), boost::asio::transfer_all(), boost::asio::use_awaitable);
if(response[0] != kSocks5Version || response[1] != kNoAuthMethod) {
throw std::runtime_error("SOCKS5 proxy authentication failed");
}
std::array header{
kSocks5Version, kConnectCommand, kNoAuthMethod, kDomainNameType}; // 4 байта для заголовка, до 255 байт для адреса, 2 байта для порта
auto hostnameLength = static_cast<std::uint8_t>(target_hostname.size());
auto portBytes = std::bit_cast<std::array<std::uint8_t, 2>>(htons(target_port));
auto request =
std::array{std::span<const std::uint8_t>(header),
std::span<const std::uint8_t>(&hostnameLength, 1),
std::span<const std::uint8_t>(utils::StartLifetimeAsArray<uint8_t>(target_hostname.data(), target_hostname.size()),
target_hostname.size()),
std::span<const std::uint8_t>(portBytes)} |
std::views::join;
std::array<std::uint8_t, kMaxRequestSize> requestBuffer{};
auto it = std::ranges::copy(request, requestBuffer.begin()).out;
size_t requestSize = std::distance(requestBuffer.begin(), it);
// Отправляем запрос
co_await boost::asio::async_write(
socket, boost::asio::buffer(requestBuffer.data(), requestSize), boost::asio::transfer_all(), boost::asio::use_awaitable);
// ответ сервера
std::array<std::uint8_t, 4> reply{};
co_await boost::asio::async_read(socket, boost::asio::buffer(reply), boost::asio::transfer_all(), boost::asio::use_awaitable);
if(reply[0] != kSocks5Version || reply[1] != kNoAuthMethod) {
throw std::runtime_error("SOCKS5 proxy connection failed");
}
const std::uint8_t addr_type = reply[3];
size_t addr_len = 0;
if(addr_type == kIpv4) {
// IPv4
addr_len = 4;
} else if(addr_type == kDomainNameType) {
// Domain name
std::uint8_t len{};
co_await boost::asio::async_read(socket, boost::asio::buffer(&len, 1), boost::asio::transfer_all(), boost::asio::use_awaitable);
addr_len = len;
} else if(addr_type == kIpv6) {
// IPv6
addr_len = 16;
} else {
throw std::runtime_error("Unknown address type in SOCKS5 reply");
}
std::array<std::uint8_t, 18> addr{}; // Максимальный размер для IPv6 адреса + порт
co_await boost::asio::async_read(
socket, boost::asio::buffer(addr.data(), addr_len + 2), boost::asio::transfer_all(), boost::asio::use_awaitable);
co_return;
}
template <typename Socket>
auto ConnectViaProxy(Socket&, const NoProxy&, std::string_view, std::uint16_t) -> boost::asio::awaitable<void> {
co_return;
} }
template <typename ProxyType> template <typename ProxyType>