Kurlyk
Loading...
Searching...
No Matches
ProxyConfig.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _CRYPTOX_DATA_CONNECTION_PROXY_CONFIG_HPP_INCLUDED
3#define _CRYPTOX_DATA_CONNECTION_PROXY_CONFIG_HPP_INCLUDED
4
7
8namespace cryptox {
9
12 struct ProxyConfig {
13 std::string proxy_server;
14 std::string proxy_auth;
15 ProxyType proxy_type;
16 bool use = false;
17
20 : proxy_type(ProxyType::HTTP) {}
21
26 ProxyConfig(std::string server, std::string auth, ProxyType type)
27 : proxy_server(std::move(server)), proxy_auth(std::move(auth)), proxy_type(type) {}
28
33 void set_proxy(const std::string& ip, int port, ProxyType type) {
34 proxy_server = ip + ":" + std::to_string(port);
35 proxy_type = type;
36 }
37
45 const std::string& ip,
46 int port,
47 const std::string& username,
48 const std::string& password,
49 ProxyType type = ProxyType::HTTP) {
50 set_proxy(ip, port);
51 set_proxy_auth(username, password);
52 proxy_type = type;
53 }
54
58 void set_proxy_auth(const std::string& username, const std::string& password) {
59 proxy_auth = username + ":" + password;
60 }
61
64 std::string get_ip() const {
65 auto pos = proxy_server.find(':');
66 if (pos == std::string::npos) return {};
67 return proxy_server.substr(0, pos);
68 }
69
72 int get_port() const {
73 auto pos = proxy_server.find(':');
74 if (pos == std::string::npos) return 0;
75 try {
76 return std::stoi(proxy_server.substr(pos + 1));
77 } catch (...) {
78 return 0;
79 }
80 }
81
84 std::string get_username() const {
85 auto pos = proxy_auth.find(':');
86 if (pos == std::string::npos) return {};
87 return proxy_auth.substr(0, pos);
88 }
89
92 std::string get_password() const {
93 auto pos = proxy_auth.find(':');
94 if (pos == std::string::npos || pos + 1 >= proxy_auth.size()) return {};
95 return proxy_auth.substr(pos + 1);
96 }
97
100 bool is_valid() const {
101 return !proxy_server.empty() && !proxy.get_ip().empty() && proxy.get_port() != 0;
102 }
103 };
104
106 inline void to_json(nlohmann::json& j, const ProxyConfig& config) {
107 j = nlohmann::json{
108 {"proxy_server", config.proxy_server},
109 {"proxy_auth", config.proxy_auth},
110 {"proxy_type", config.proxy_type},
111 {"use", config.use}
112 };
113 }
114
116 inline void from_json(const nlohmann::json& j, ProxyConfig& config) {
117 if (j.contains("proxy_server")) config.proxy_server = j.at("proxy_server").get<std::string>();
118 if (j.contains("proxy_auth")) config.proxy_auth = j.at("proxy_auth").get<std::string>();
119 if (j.contains("proxy_type")) config.proxy_type = j.at("proxy_type").get<ProxyType>();
120 if (j.contains("use")) config.use = j.at("use").get<bool>();
121 }
122
123} // namespace cryptox
124
125#endif // _CRYPTOX_DATA_CONNECTION_PROXY_CONFIG_HPP_INCLUDED
void to_json(nlohmann::json &j, const ProxyConfig &config)
Serializes ProxyConfig to JSON.
void from_json(const nlohmann::json &j, ProxyConfig &config)
Deserializes ProxyConfig from JSON.
Enables use of ClientError with std::error_code.
Configuration structure for connecting through a proxy server.
std::string get_username() const
Returns the proxy username.
std::string proxy_auth
Proxy authentication in <username:password> format.
int get_port() const
Returns the proxy port.
std::string get_password() const
Returns the proxy password.
ProxyConfig(std::string server, std::string auth, ProxyType type)
Constructs ProxyConfig with server and authentication.
ProxyType proxy_type
Proxy type (e.g., HTTP, SOCKS5).
bool use
Whether to use proxy for connection.
ProxyConfig()
Default constructor.
void set_proxy_auth(const std::string &username, const std::string &password)
Sets proxy authentication credentials.
std::string proxy_server
Proxy address in <ip:port> format.
std::string get_ip() const
Returns the proxy IP address (host part).
bool is_valid() const
Checks if the proxy configuration is valid (server field must not be empty).
void set_proxy(const std::string &ip, int port, ProxyType type)
Sets the proxy server address.
void set_proxy(const std::string &ip, int port, const std::string &username, const std::string &password, ProxyType type=ProxyType::HTTP)
Sets the proxy server address with authentication details and proxy type.