Kurlyk
Loading...
Searching...
No Matches
ProxyConfig.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef KURLYK_HEADER_KURLYK_TYPES_PROXY_CONFIG_HPP_INCLUDED
3#define KURLYK_HEADER_KURLYK_TYPES_PROXY_CONFIG_HPP_INCLUDED
4
7
8#include <string>
9#include <stdexcept>
10#include <utility>
11
12#include "enums.hpp"
14
15#if defined(KURLYK_JSON_SUPPORT) && KURLYK_JSON_SUPPORT
16#include <nlohmann/json.hpp>
17#endif
18
19namespace kurlyk {
20
23 struct ProxyConfig {
24 std::string proxy_server;
25 std::string proxy_auth;
27 bool use;
28
32
37 ProxyConfig(std::string server, std::string auth, ProxyType type = ProxyType::PROXY_HTTP)
38 : proxy_server(std::move(server)),
39 proxy_auth(std::move(auth)),
40 proxy_type(type),
41 use(false) {}
42
47 void set_proxy(const std::string& ip, int port, ProxyType type = ProxyType::PROXY_HTTP) {
48 proxy_server = ip + ":" + std::to_string(port);
49 proxy_type = type;
50 }
51
59 const std::string& ip,
60 int port,
61 const std::string& username,
62 const std::string& password,
64 set_proxy(ip, port, type);
65 set_proxy_auth(username, password);
66 }
67
71 void set_proxy_auth(const std::string& username, const std::string& password) {
72 proxy_auth = username + ":" + password;
73 }
74
77 std::string get_ip() const {
78 const std::string::size_type pos = proxy_server.rfind(':');
79 if (pos == std::string::npos || pos == 0) return std::string();
80 return proxy_server.substr(0, pos);
81 }
82
85 int get_port() const {
86 const std::string::size_type pos = proxy_server.rfind(':');
87 if (pos == std::string::npos || pos + 1 >= proxy_server.size()) return 0;
88 try {
89 const int port = std::stoi(proxy_server.substr(pos + 1));
90 return port > 0 && port <= 65535 ? port : 0;
91 } catch (...) {
92 return 0;
93 }
94 }
95
98 std::string get_username() const {
99 const std::string::size_type pos = proxy_auth.find(':');
100 if (pos == std::string::npos) return std::string();
101 return proxy_auth.substr(0, pos);
102 }
103
106 std::string get_password() const {
107 const std::string::size_type pos = proxy_auth.find(':');
108 if (pos == std::string::npos || pos + 1 >= proxy_auth.size()) return std::string();
109 return proxy_auth.substr(pos + 1);
110 }
111
114 bool is_valid() const {
115 return !get_ip().empty() && get_port() != 0;
116 }
117 };
118
119#if defined(KURLYK_JSON_SUPPORT) && KURLYK_JSON_SUPPORT
120 namespace detail {
121
122 inline const char* proxy_type_name(ProxyType type) {
123 switch (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";
131 }
132 return "PROXY_HTTP";
133 }
134
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)) {
140 return static_cast<ProxyType>(raw);
141 }
142 }
143
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);
153 }
154
155 } // namespace detail
156
158 inline void to_json(nlohmann::json& json, const ProxyConfig& config) {
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)},
163 {"use", config.use}
164 };
165 }
166
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>();
173 }
174#endif
175
176} // namespace kurlyk
177
178#endif // KURLYK_HEADER_KURLYK_TYPES_PROXY_CONFIG_HPP_INCLUDED
nlohmann::json json
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.
Definition enums.hpp:12
@ PROXY_HTTP
HTTP proxy.
Definition enums.hpp:13
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.