-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunway_manager.cpp
More file actions
309 lines (261 loc) · 10.9 KB
/
runway_manager.cpp
File metadata and controls
309 lines (261 loc) · 10.9 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "runway_manager.h"
#include "network.h"
#include "utils.h"
#include <sstream>
#include <ctime>
#include <algorithm>
#ifdef _WIN32
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
#else
#include <ifaddrs.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
RunwayManager::RunwayManager(
const std::vector<std::string>& interfaces,
const std::vector<UpstreamProxyConfig>& upstream_proxies,
const std::vector<DNSServerConfig>& dns_servers,
std::shared_ptr<DNSResolver> dns_resolver)
: interfaces_(interfaces)
, dns_resolver_(dns_resolver) {
// Convert configs to runtime objects
for (const auto& proxy_cfg : upstream_proxies) {
upstream_proxies_.push_back(std::make_shared<UpstreamProxy>(proxy_cfg));
}
for (const auto& dns_cfg : dns_servers) {
dns_servers_.push_back(std::make_shared<DNSServer>(dns_cfg));
}
discover_interfaces();
}
RunwayManager::~RunwayManager() {
}
uint64_t RunwayManager::get_current_time() const {
#ifdef _WIN32
return static_cast<uint64_t>(time(nullptr));
#else
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return static_cast<uint64_t>(ts.tv_sec);
#endif
}
void RunwayManager::discover_interfaces() {
std::lock_guard<std::mutex> lock(mutex_);
#ifdef _WIN32
// Windows: Use GetAdaptersAddresses
ULONG buffer_size = 15000;
std::vector<uint8_t> buffer(buffer_size);
PIP_ADAPTER_ADDRESSES adapters = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buffer.data());
ULONG result = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, adapters, &buffer_size);
if (result == ERROR_BUFFER_OVERFLOW) {
buffer.resize(buffer_size);
adapters = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buffer.data());
result = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, adapters, &buffer_size);
}
if (result == NO_ERROR) {
std::map<std::string, InterfaceInfo> current_interfaces;
for (PIP_ADAPTER_ADDRESSES adapter = adapters; adapter != nullptr; adapter = adapter->Next) {
if (adapter->IfType == IF_TYPE_ETHERNET_CSMACD || adapter->IfType == IF_TYPE_IEEE80211) {
for (PIP_ADAPTER_UNICAST_ADDRESS addr = adapter->FirstUnicastAddress;
addr != nullptr; addr = addr->Next) {
if (addr->Address.lpSockaddr->sa_family == AF_INET) {
struct sockaddr_in* sin = reinterpret_cast<struct sockaddr_in*>(addr->Address.lpSockaddr);
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &sin->sin_addr, ip_str, INET_ADDRSTRLEN);
InterfaceInfo info;
info.name = adapter->AdapterName;
info.ip = ip_str;
info.last_seen = get_current_time();
current_interfaces[info.name] = info;
}
}
}
}
interface_info_ = current_interfaces;
}
#else
// POSIX: Use getifaddrs
struct ifaddrs* ifaddr = nullptr;
if (getifaddrs(&ifaddr) == -1) {
return;
}
std::map<std::string, InterfaceInfo> current_interfaces;
for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) continue;
if (ifa->ifa_addr->sa_family != AF_INET) continue;
struct sockaddr_in* sin = reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr);
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &sin->sin_addr, ip_str, INET_ADDRSTRLEN);
InterfaceInfo info;
info.name = ifa->ifa_name;
info.ip = ip_str;
if (ifa->ifa_netmask) {
struct sockaddr_in* mask = reinterpret_cast<struct sockaddr_in*>(ifa->ifa_netmask);
char mask_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &mask->sin_addr, mask_str, INET_ADDRSTRLEN);
info.netmask = mask_str;
}
info.last_seen = get_current_time();
current_interfaces[info.name] = info;
}
freeifaddrs(ifaddr);
interface_info_ = current_interfaces;
#endif
}
void RunwayManager::refresh_interfaces() {
std::map<std::string, InterfaceInfo> old_interfaces = interface_info_;
discover_interfaces();
// Log changes (defensive: check terminal before logging)
for (const auto& pair : interface_info_) {
if (old_interfaces.find(pair.first) == old_interfaces.end()) {
// New interface
}
}
for (const auto& pair : old_interfaces) {
if (interface_info_.find(pair.first) == interface_info_.end()) {
// Removed interface
}
}
}
std::vector<std::shared_ptr<Runway>> RunwayManager::discover_runways() {
std::lock_guard<std::mutex> lock(mutex_);
std::vector<std::string> interfaces_to_use;
if (std::find(interfaces_.begin(), interfaces_.end(), std::string("auto")) != interfaces_.end()) {
for (const auto& pair : interface_info_) {
interfaces_to_use.push_back(pair.first);
}
} else {
for (const auto& iface : interfaces_) {
if (interface_info_.find(iface) != interface_info_.end()) {
interfaces_to_use.push_back(iface);
}
}
}
std::vector<std::shared_ptr<Runway>> runways;
size_t runway_id_counter = 0;
// Create direct runways (no upstream proxy)
for (const auto& iface : interfaces_to_use) {
const auto& info = interface_info_[iface];
for (const auto& dns_server : dns_servers_) {
std::ostringstream oss;
oss << "direct_" << iface << "_" << dns_server->config.host << "_" << runway_id_counter++;
std::string runway_id = oss.str();
auto runway = std::make_shared<Runway>(
runway_id, iface, info.ip, nullptr, dns_server);
runways.push_back(runway);
runways_[runway_id] = runway;
}
}
// Create proxy runways (with upstream proxy)
for (const auto& iface : interfaces_to_use) {
const auto& info = interface_info_[iface];
for (const auto& proxy : upstream_proxies_) {
for (const auto& dns_server : dns_servers_) {
std::ostringstream oss;
oss << "proxy_" << iface << "_" << proxy->config.proxy_type
<< "_" << proxy->config.host << "_" << dns_server->config.host
<< "_" << runway_id_counter++;
std::string runway_id = oss.str();
auto runway = std::make_shared<Runway>(
runway_id, iface, info.ip, proxy, dns_server);
runways.push_back(runway);
runways_[runway_id] = runway;
}
}
}
return runways;
}
std::shared_ptr<Runway> RunwayManager::get_runway(const std::string& runway_id) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = runways_.find(runway_id);
if (it != runways_.end()) {
return it->second;
}
return nullptr;
}
std::vector<std::shared_ptr<Runway>> RunwayManager::get_all_runways() {
std::lock_guard<std::mutex> lock(mutex_);
std::vector<std::shared_ptr<Runway>> result;
for (const auto& pair : runways_) {
result.push_back(pair.second);
}
return result;
}
std::tuple<bool, bool, double> RunwayManager::test_runway_accessibility(
const std::string& target, std::shared_ptr<Runway> runway, double timeout_secs) {
// Resolve target if needed
std::string resolved_ip;
if (dns_resolver_->is_ip_address(target) || dns_resolver_->is_private_ip(target)) {
resolved_ip = target;
} else {
auto result = dns_resolver_->resolve(target);
if (result.first.empty()) {
return std::make_tuple(false, false, 0.0);
}
resolved_ip = result.first;
}
// Test connection
bool network_success = false;
if (runway->upstream_proxy && runway->upstream_proxy->accessible) {
network_success = test_proxy_connection(runway, resolved_ip, timeout_secs);
} else {
network_success = test_direct_connection(runway, resolved_ip, timeout_secs);
}
double response_time = 0.0; // Simplified
bool user_success = network_success; // Simplified for now
return std::make_tuple(network_success, user_success, response_time);
}
bool RunwayManager::test_direct_connection(
std::shared_ptr<Runway> runway, const std::string& target_ip, double timeout_secs) {
std::lock_guard<std::mutex> lock(mutex_);
if (interface_info_.find(runway->interface_name) == interface_info_.end()) {
return false;
}
socket_t sock = network::create_tcp_socket();
if (sock == network::INVALID_SOCKET_VALUE) {
return false;
}
// Set timeout
struct timeval timeout;
timeout.tv_sec = static_cast<long>(timeout_secs);
timeout.tv_usec = static_cast<long>((timeout_secs - timeout.tv_sec) * 1000000);
#ifdef _WIN32
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&timeout), sizeof(timeout));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&timeout), sizeof(timeout));
#else
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
#endif
bool success = network::connect_socket(sock, target_ip, 80);
network::close_socket(sock);
return success;
}
bool RunwayManager::test_proxy_connection(
std::shared_ptr<Runway> runway, const std::string& /*target_ip*/, double timeout_secs) {
if (!runway->upstream_proxy || !runway->upstream_proxy->accessible) {
return false;
}
// Simplified proxy test - would need full HTTP CONNECT or proxy protocol
// For now, just test if we can connect to the proxy
socket_t sock = network::create_tcp_socket();
if (sock == network::INVALID_SOCKET_VALUE) {
return false;
}
struct timeval timeout;
timeout.tv_sec = static_cast<long>(timeout_secs);
timeout.tv_usec = static_cast<long>((timeout_secs - timeout.tv_sec) * 1000000);
#ifdef _WIN32
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&timeout), sizeof(timeout));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&timeout), sizeof(timeout));
#else
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
#endif
bool success = network::connect_socket(sock,
runway->upstream_proxy->config.host,
runway->upstream_proxy->config.port);
network::close_socket(sock);
return success;
}