-
Notifications
You must be signed in to change notification settings - Fork 41
/
proxy_info.cpp
177 lines (147 loc) · 3.84 KB
/
proxy_info.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "proxy_info.h"
#include <regex>
#include <sstream>
#include <algorithm>
#include <iomanip>
ProxyInfo::ProxyInfo() {
Reset();
}
void ProxyInfo::Parse(const std::string& url) {
Reset();
if (url.empty()) {
return;
}
static const std::regex proxyRegex(
R"(^(?:(?:([a-z][a-z0-9+\-.]*)):\/\/)?(?:([^:@\/]+)(?::([^@\/]+))?@)?([^:\/?#]+)(?::(\d+))?$)",
std::regex::icase
);
std::smatch match;
if (!std::regex_match(url, match, proxyRegex)) {
return;
}
isSpecified = true;
// 处理协议
protocol = match[1].matched ? ToLower(match[1].str()) : "http";
if (protocol == "https") {
protocol = "http";
}
// 处理认证信息
if (match[2].matched) {
username = UrlDecode(match[2].str());
login = username; // 保持兼容性
if (match[3].matched) {
password = UrlDecode(match[3].str());
isAuth = true;
}
}
// 处理主机和端口
host = match[4].str();
port = match[5].matched ? std::stoi(match[5].str()) : GetDefaultPort(protocol);
// 设置协议类型标志
isHttp = (protocol == "http");
isSocks5 = (protocol == "socks5");
}
std::string ProxyInfo::GetProxyForceHttp() const {
if (!isSpecified) {
return "";
}
std::ostringstream oss;
if (isSocks5) {
oss << "http://";
oss << host << ":" << port;
} else {
oss << "http://";
if (isAuth) {
oss << UrlEncode(username) << ":" << UrlEncode(password) << "@";
}
oss << host << ":" << port;
}
return oss.str();
}
std::string ProxyInfo::GetProxyNoAuth() const {
if (!isSpecified) {
return "";
}
std::ostringstream oss;
oss << protocol << "://";
oss << host << ":" << port;
return oss.str();
}
std::string ProxyInfo::ToString() const {
if (!isSpecified) {
return "";
}
std::ostringstream oss;
oss << protocol << "://";
if (isAuth) {
oss << UrlEncode(username) << ":" << UrlEncode(password) << "@";
}
oss << host << ":" << port;
return oss.str();
}
void ProxyInfo::Reset() {
isSpecified = false;
protocol.clear();
username.clear();
login.clear();
password.clear();
host.clear();
port = 0;
isHttp = false;
isSocks5 = false;
isAuth = false;
}
int ProxyInfo::GetDefaultPort(const std::string& proto) const {
if (proto == "http") return 80;
if (proto == "https") return 443;
if (proto == "socks5") return 1080;
return 0;
}
std::string ProxyInfo::ToLower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) { return std::tolower(c); });
return s;
}
std::string ProxyInfo::UrlEncode(const std::string& str) {
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (char c : str) {
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
}
else {
escaped << '%' << std::setw(2) << int((unsigned char)c);
}
}
return escaped.str();
}
std::string ProxyInfo::UrlDecode(const std::string& str) {
std::string result;
std::string::size_type i;
for (i = 0; i < str.length(); ++i) {
if (str[i] == '%') {
if (i + 2 < str.length()) {
int value;
std::istringstream is(str.substr(i + 1, 2));
if (is >> std::hex >> value) {
result += static_cast<char>(value);
i += 2;
}
else {
result += str[i];
}
}
else {
result += str[i];
}
}
else if (str[i] == '+') {
result += ' ';
}
else {
result += str[i];
}
}
return result;
}