Kurlyk
Toggle main menu visibility
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
8
namespace
cryptox
{
9
12
struct
ProxyConfig
{
13
std::string
proxy_server
;
14
std::string
proxy_auth
;
15
ProxyType
proxy_type
;
16
bool
use
=
false
;
17
19
ProxyConfig
()
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
44
void
set_proxy
(
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
cryptox
Definition
ProxyConfig.hpp:8
cryptox::to_json
void to_json(nlohmann::json &j, const ProxyConfig &config)
Serializes ProxyConfig to JSON.
Definition
ProxyConfig.hpp:106
cryptox::from_json
void from_json(const nlohmann::json &j, ProxyConfig &config)
Deserializes ProxyConfig from JSON.
Definition
ProxyConfig.hpp:116
std
Enables use of ClientError with std::error_code.
Definition
ClientErrorCategory.hpp:69
cryptox::ProxyConfig
Configuration structure for connecting through a proxy server.
Definition
ProxyConfig.hpp:12
cryptox::ProxyConfig::get_username
std::string get_username() const
Returns the proxy username.
Definition
ProxyConfig.hpp:84
cryptox::ProxyConfig::proxy_auth
std::string proxy_auth
Proxy authentication in <username:password> format.
Definition
ProxyConfig.hpp:14
cryptox::ProxyConfig::get_port
int get_port() const
Returns the proxy port.
Definition
ProxyConfig.hpp:72
cryptox::ProxyConfig::get_password
std::string get_password() const
Returns the proxy password.
Definition
ProxyConfig.hpp:92
cryptox::ProxyConfig::ProxyConfig
ProxyConfig(std::string server, std::string auth, ProxyType type)
Constructs ProxyConfig with server and authentication.
Definition
ProxyConfig.hpp:26
cryptox::ProxyConfig::proxy_type
ProxyType proxy_type
Proxy type (e.g., HTTP, SOCKS5).
Definition
ProxyConfig.hpp:15
cryptox::ProxyConfig::use
bool use
Whether to use proxy for connection.
Definition
ProxyConfig.hpp:16
cryptox::ProxyConfig::ProxyConfig
ProxyConfig()
Default constructor.
Definition
ProxyConfig.hpp:19
cryptox::ProxyConfig::set_proxy_auth
void set_proxy_auth(const std::string &username, const std::string &password)
Sets proxy authentication credentials.
Definition
ProxyConfig.hpp:58
cryptox::ProxyConfig::proxy_server
std::string proxy_server
Proxy address in <ip:port> format.
Definition
ProxyConfig.hpp:13
cryptox::ProxyConfig::get_ip
std::string get_ip() const
Returns the proxy IP address (host part).
Definition
ProxyConfig.hpp:64
cryptox::ProxyConfig::is_valid
bool is_valid() const
Checks if the proxy configuration is valid (server field must not be empty).
Definition
ProxyConfig.hpp:100
cryptox::ProxyConfig::set_proxy
void set_proxy(const std::string &ip, int port, ProxyType type)
Sets the proxy server address.
Definition
ProxyConfig.hpp:33
cryptox::ProxyConfig::set_proxy
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.
Definition
ProxyConfig.hpp:44
include
kurlyk
types
ProxyConfig.hpp
Generated by
1.17.0