2#ifndef KURLYK_HEADER_KURLYK_TYPES_PROXY_CONFIG_HPP_INCLUDED
3#define KURLYK_HEADER_KURLYK_TYPES_PROXY_CONFIG_HPP_INCLUDED
15#if defined(KURLYK_JSON_SUPPORT) && KURLYK_JSON_SUPPORT
16#include <nlohmann/json.hpp>
59 const std::string& ip,
61 const std::string& username,
62 const std::string& password,
71 void set_proxy_auth(
const std::string& username,
const std::string& password) {
78 const std::string::size_type pos =
proxy_server.rfind(
':');
79 if (pos == std::string::npos || pos == 0)
return std::string();
86 const std::string::size_type pos =
proxy_server.rfind(
':');
87 if (pos == std::string::npos || pos + 1 >=
proxy_server.size())
return 0;
89 const int port = std::stoi(
proxy_server.substr(pos + 1));
90 return port > 0 && port <= 65535 ? port : 0;
99 const std::string::size_type pos =
proxy_auth.find(
':');
100 if (pos == std::string::npos)
return std::string();
107 const std::string::size_type pos =
proxy_auth.find(
':');
108 if (pos == std::string::npos || pos + 1 >=
proxy_auth.size())
return std::string();
119#if defined(KURLYK_JSON_SUPPORT) && KURLYK_JSON_SUPPORT
122 inline const char* proxy_type_name(ProxyType type) {
124 case ProxyType::PROXY_HTTP:
return "PROXY_HTTP";
125 case ProxyType::PROXY_HTTPS:
return "PROXY_HTTPS";
126 case ProxyType::PROXY_HTTP_1_0:
return "PROXY_HTTP_1_0";
127 case ProxyType::PROXY_SOCKS4:
return "PROXY_SOCKS4";
128 case ProxyType::PROXY_SOCKS4A:
return "PROXY_SOCKS4A";
129 case ProxyType::PROXY_SOCKS5:
return "PROXY_SOCKS5";
130 case ProxyType::PROXY_SOCKS5_HOSTNAME:
return "PROXY_SOCKS5_HOSTNAME";
135 inline ProxyType proxy_type_from_json(
const nlohmann::json& value) {
136 if (value.is_number_integer()) {
137 const int raw = value.get<
int>();
138 if (raw >=
static_cast<int>(ProxyType::PROXY_HTTP) &&
139 raw <=
static_cast<int>(ProxyType::PROXY_SOCKS5_HOSTNAME)) {
144 const std::string name = utils::to_upper_case(value.get<std::string>());
145 if (name ==
"PROXY_HTTP")
return ProxyType::PROXY_HTTP;
146 if (name ==
"PROXY_HTTPS")
return ProxyType::PROXY_HTTPS;
147 if (name ==
"PROXY_HTTP_1_0")
return ProxyType::PROXY_HTTP_1_0;
148 if (name ==
"PROXY_SOCKS4")
return ProxyType::PROXY_SOCKS4;
149 if (name ==
"PROXY_SOCKS4A")
return ProxyType::PROXY_SOCKS4A;
150 if (name ==
"PROXY_SOCKS5")
return ProxyType::PROXY_SOCKS5;
151 if (name ==
"PROXY_SOCKS5_HOSTNAME")
return ProxyType::PROXY_SOCKS5_HOSTNAME;
152 throw std::invalid_argument(
"Invalid ProxyType: " + name);
159 json = nlohmann::json{
160 {
"proxy_server", config.proxy_server},
161 {
"proxy_auth", config.proxy_auth},
162 {
"proxy_type", detail::proxy_type_name(config.proxy_type)},
168 inline void from_json(
const nlohmann::json&
json,
ProxyConfig& config) {
169 if (
json.contains(
"proxy_server")) config.proxy_server =
json.at(
"proxy_server").get<std::string>();
170 if (
json.contains(
"proxy_auth")) config.proxy_auth =
json.at(
"proxy_auth").get<std::string>();
171 if (
json.contains(
"proxy_type")) config.proxy_type = detail::proxy_type_from_json(
json.at(
"proxy_type"));
172 if (
json.contains(
"use")) config.use =
json.at(
"use").get<
bool>();
Defines enums used across the Kurlyk library, including proxy types, rate limits, and WebSocket event...
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
ProxyType
Enumeration of supported proxy types compatible with libcurl.
Enables use of ClientError with std::error_code.
Provides basic string manipulation utilities.
Stores proxy server, authentication, and transport settings.
ProxyType proxy_type
Proxy transport type.
void set_proxy(const std::string &ip, int port, const std::string &username, const std::string &password, ProxyType type=ProxyType::PROXY_HTTP)
Sets the proxy server and authentication values.
ProxyConfig(std::string server, std::string auth, ProxyType type=ProxyType::PROXY_HTTP)
Constructs a proxy configuration with server and authentication values.
std::string proxy_auth
Proxy authentication in username:password format.
void set_proxy(const std::string &ip, int port, ProxyType type=ProxyType::PROXY_HTTP)
Sets the proxy server address and transport type.
std::string proxy_server
Proxy address in host:port format.
bool is_valid() const
Checks whether the proxy server address is valid.
std::string get_username() const
Returns the proxy username.
ProxyConfig()
Constructs an empty HTTP proxy configuration.
int get_port() const
Returns the proxy port.
bool use
Indicates whether the proxy is enabled.
std::string get_password() const
Returns the proxy password.
void set_proxy_auth(const std::string &username, const std::string &password)
Sets proxy authentication credentials.
std::string get_ip() const
Returns the host part of the proxy server address.