-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.cppm
More file actions
155 lines (135 loc) · 4.15 KB
/
proxy.cppm
File metadata and controls
155 lines (135 loc) · 4.15 KB
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
export module mcpplibs.tinyhttps:proxy;
import :socket;
import std;
namespace mcpplibs::tinyhttps {
export struct ProxyConfig {
std::string host;
int port { 8080 };
};
// Parse "http://host:port" proxy URL
export ProxyConfig parse_proxy_url(std::string_view url) {
ProxyConfig config;
// Strip scheme if present
auto schemeEnd = url.find("://");
if (schemeEnd != std::string_view::npos) {
url = url.substr(schemeEnd + 3);
}
// Strip trailing path if present
auto pathStart = url.find('/');
if (pathStart != std::string_view::npos) {
url = url.substr(0, pathStart);
}
// Check for port
auto colonPos = url.find(':');
if (colonPos != std::string_view::npos) {
config.host = std::string(url.substr(0, colonPos));
auto portStr = url.substr(colonPos + 1);
config.port = 0;
for (char c : portStr) {
if (c >= '0' && c <= '9') {
config.port = config.port * 10 + (c - '0');
}
}
} else {
config.host = std::string(url);
config.port = 8080;
}
return config;
}
// Read a line (ending with \r\n) from a plain Socket
static std::string read_line_plain(Socket& sock, int timeoutMs) {
std::string line;
char c;
while (true) {
if (!sock.wait_readable(timeoutMs)) {
break;
}
int ret = sock.read(&c, 1);
if (ret < 0) break;
if (ret == 0) {
if (!sock.wait_readable(timeoutMs)) break;
ret = sock.read(&c, 1);
if (ret <= 0) break;
}
line += c;
if (line.size() >= 2 && line[line.size() - 2] == '\r' && line[line.size() - 1] == '\n') {
line.resize(line.size() - 2);
break;
}
}
return line;
}
// Write all data to a plain Socket
static bool write_all_plain(Socket& sock, const std::string& data) {
int total = 0;
int len = static_cast<int>(data.size());
while (total < len) {
int ret = sock.write(data.c_str() + total, len - total);
if (ret < 0) return false;
if (ret == 0) {
ret = sock.write(data.c_str() + total, len - total);
if (ret <= 0) return false;
}
total += ret;
}
return true;
}
// Connect through HTTP CONNECT proxy, returning a Socket connected to target through tunnel.
// On failure, returns an invalid (closed) Socket.
export Socket proxy_connect(std::string_view proxyHost, int proxyPort,
std::string_view targetHost, int targetPort,
int timeoutMs) {
Socket sock;
// Step 1: Connect to proxy
std::string proxyHostStr(proxyHost);
if (!sock.connect(proxyHostStr.c_str(), proxyPort, timeoutMs)) {
return sock;
}
// Step 2: Send CONNECT request
std::string request = "CONNECT ";
request += targetHost;
request += ":";
request += std::to_string(targetPort);
request += " HTTP/1.1\r\nHost: ";
request += targetHost;
request += ":";
request += std::to_string(targetPort);
request += "\r\n\r\n";
if (!write_all_plain(sock, request)) {
sock.close();
return sock;
}
// Step 3: Read response status line
std::string statusLine = read_line_plain(sock, timeoutMs);
if (statusLine.empty()) {
sock.close();
return sock;
}
// Parse status code from "HTTP/1.x 200 ..."
int statusCode = 0;
auto spacePos = statusLine.find(' ');
if (spacePos != std::string::npos) {
auto rest = std::string_view(statusLine).substr(spacePos + 1);
for (char c : rest) {
if (c >= '0' && c <= '9') {
statusCode = statusCode * 10 + (c - '0');
} else {
break;
}
}
}
if (statusCode != 200) {
sock.close();
return sock;
}
// Step 4: Read remaining response headers until empty line
while (true) {
std::string headerLine = read_line_plain(sock, timeoutMs);
if (headerLine.empty()) {
break;
}
}
// Socket is now tunneled to the target
return sock;
}
} // namespace mcpplibs::tinyhttps