diff options
| author | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-13 15:24:10 -0300 |
|---|---|---|
| committer | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-13 15:58:42 -0300 |
| commit | 5309170e567f4a8655b3b622e7b26b6d8b382942 (patch) | |
| tree | 3d7fa8c459fb3b493b878f68b85cb53b7c16659e /src/proxy.cpp | |
| parent | 8e21e8ca0b8d50c29376bc2f0a89575baa477213 (diff) | |
net: add onion-only btf dial mode
Diffstat (limited to 'src/proxy.cpp')
| -rw-r--r-- | src/proxy.cpp | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/src/proxy.cpp b/src/proxy.cpp index 7d30911..eed054c 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp | |||
| @@ -13,6 +13,7 @@ static bool g_fTorProxy = false; | |||
| 13 | static std::string g_socks5Host; | 13 | static std::string g_socks5Host; |
| 14 | static unsigned short g_socks5Port = 0; | 14 | static unsigned short g_socks5Port = 0; |
| 15 | static const char* DEFAULT_TOR_SOCKS5_PROXY = "127.0.0.1:9050"; | 15 | static const char* DEFAULT_TOR_SOCKS5_PROXY = "127.0.0.1:9050"; |
| 16 | static const int TOR_CONNECT_TIMEOUT_SECS = 120; | ||
| 16 | 17 | ||
| 17 | static void SetSocketTimeout(SOCKET s, int nTimeoutSecs) | 18 | static void SetSocketTimeout(SOCKET s, int nTimeoutSecs) |
| 18 | { | 19 | { |
| @@ -297,10 +298,25 @@ static bool Socks5Connect(SOCKET s, const std::string& destHost, | |||
| 297 | 298 | ||
| 298 | unsigned char hello[3] = { 0x05, 0x01, 0x00 }; // SOCKS5, one method, no auth | 299 | unsigned char hello[3] = { 0x05, 0x01, 0x00 }; // SOCKS5, one method, no auth |
| 299 | if (!WriteN(s, hello, sizeof(hello))) | 300 | if (!WriteN(s, hello, sizeof(hello))) |
| 301 | { | ||
| 302 | error("SOCKS5 hello write failed for %s:%u\n", | ||
| 303 | destHost.c_str(), (unsigned)destPort); | ||
| 300 | return false; | 304 | return false; |
| 305 | } | ||
| 301 | unsigned char choice[2] = { 0, 0 }; | 306 | unsigned char choice[2] = { 0, 0 }; |
| 302 | if (!ReadN(s, choice, sizeof(choice)) || choice[0] != 0x05 || choice[1] != 0x00) | 307 | if (!ReadN(s, choice, sizeof(choice))) |
| 308 | { | ||
| 309 | error("SOCKS5 method selection read failed for %s:%u\n", | ||
| 310 | destHost.c_str(), (unsigned)destPort); | ||
| 311 | return false; | ||
| 312 | } | ||
| 313 | if (choice[0] != 0x05 || choice[1] != 0x00) | ||
| 314 | { | ||
| 315 | error("SOCKS5 proxy rejected no-auth method for %s:%u (version=0x%02x method=0x%02x)\n", | ||
| 316 | destHost.c_str(), (unsigned)destPort, | ||
| 317 | (unsigned)choice[0], (unsigned)choice[1]); | ||
| 303 | return false; | 318 | return false; |
| 319 | } | ||
| 304 | 320 | ||
| 305 | std::vector<unsigned char> req; | 321 | std::vector<unsigned char> req; |
| 306 | req.reserve(7 + destHost.size()); | 322 | req.reserve(7 + destHost.size()); |
| @@ -313,11 +329,25 @@ static bool Socks5Connect(SOCKET s, const std::string& destHost, | |||
| 313 | req.push_back((unsigned char)(destPort >> 8)); | 329 | req.push_back((unsigned char)(destPort >> 8)); |
| 314 | req.push_back((unsigned char)(destPort & 0xff)); | 330 | req.push_back((unsigned char)(destPort & 0xff)); |
| 315 | if (!WriteN(s, &req[0], (int)req.size())) | 331 | if (!WriteN(s, &req[0], (int)req.size())) |
| 332 | { | ||
| 333 | error("SOCKS5 CONNECT request write failed for %s:%u\n", | ||
| 334 | destHost.c_str(), (unsigned)destPort); | ||
| 316 | return false; | 335 | return false; |
| 336 | } | ||
| 317 | 337 | ||
| 318 | unsigned char rep[4] = {0,0,0,0}; | 338 | unsigned char rep[4] = {0,0,0,0}; |
| 319 | if (!ReadN(s, rep, sizeof(rep)) || rep[0] != 0x05) | 339 | if (!ReadN(s, rep, sizeof(rep))) |
| 340 | { | ||
| 341 | error("SOCKS5 CONNECT reply read failed for %s:%u\n", | ||
| 342 | destHost.c_str(), (unsigned)destPort); | ||
| 320 | return false; | 343 | return false; |
| 344 | } | ||
| 345 | if (rep[0] != 0x05) | ||
| 346 | { | ||
| 347 | error("SOCKS5 CONNECT reply has invalid version 0x%02x for %s:%u\n", | ||
| 348 | (unsigned)rep[0], destHost.c_str(), (unsigned)destPort); | ||
| 349 | return false; | ||
| 350 | } | ||
| 321 | if (rep[1] != 0x00) | 351 | if (rep[1] != 0x00) |
| 322 | { | 352 | { |
| 323 | error("SOCKS5 CONNECT to %s:%u failed with reply 0x%02x\n", | 353 | error("SOCKS5 CONNECT to %s:%u failed with reply 0x%02x\n", |
| @@ -335,10 +365,20 @@ static bool Socks5Connect(SOCKET s, const std::string& destHost, | |||
| 335 | nAddr = len; | 365 | nAddr = len; |
| 336 | } | 366 | } |
| 337 | else | 367 | else |
| 368 | { | ||
| 369 | error("SOCKS5 CONNECT reply has invalid address type 0x%02x for %s:%u\n", | ||
| 370 | (unsigned)rep[3], destHost.c_str(), (unsigned)destPort); | ||
| 338 | return false; | 371 | return false; |
| 372 | } | ||
| 339 | 373 | ||
| 340 | std::vector<unsigned char> discard(nAddr + 2); | 374 | std::vector<unsigned char> discard(nAddr + 2); |
| 341 | return ReadN(s, &discard[0], (int)discard.size()); | 375 | if (!ReadN(s, &discard[0], (int)discard.size())) |
| 376 | { | ||
| 377 | error("SOCKS5 CONNECT bind address read failed for %s:%u\n", | ||
| 378 | destHost.c_str(), (unsigned)destPort); | ||
| 379 | return false; | ||
| 380 | } | ||
| 381 | return true; | ||
| 342 | } | 382 | } |
| 343 | 383 | ||
| 344 | SOCKET BtfConnectSocket(const std::string& destHost, unsigned short destPort, | 384 | SOCKET BtfConnectSocket(const std::string& destHost, unsigned short destPort, |
| @@ -347,9 +387,11 @@ SOCKET BtfConnectSocket(const std::string& destHost, unsigned short destPort, | |||
| 347 | std::string proxyHost; | 387 | std::string proxyHost; |
| 348 | unsigned short proxyPort = 0; | 388 | unsigned short proxyPort = 0; |
| 349 | bool fProxy = false; | 389 | bool fProxy = false; |
| 390 | bool fTorProxy = false; | ||
| 350 | CRITICAL_BLOCK(cs_socks5) | 391 | CRITICAL_BLOCK(cs_socks5) |
| 351 | { | 392 | { |
| 352 | fProxy = g_fSocks5Proxy; | 393 | fProxy = g_fSocks5Proxy; |
| 394 | fTorProxy = g_fTorProxy; | ||
| 353 | proxyHost = g_socks5Host; | 395 | proxyHost = g_socks5Host; |
| 354 | proxyPort = g_socks5Port; | 396 | proxyPort = g_socks5Port; |
| 355 | } | 397 | } |
| @@ -365,6 +407,9 @@ SOCKET BtfConnectSocket(const std::string& destHost, unsigned short destPort, | |||
| 365 | return ConnectDirect(destHost, destPort, nSockSite, nTimeoutSecs); | 407 | return ConnectDirect(destHost, destPort, nSockSite, nTimeoutSecs); |
| 366 | } | 408 | } |
| 367 | 409 | ||
| 410 | if (fTorProxy && nTimeoutSecs > 0 && nTimeoutSecs < TOR_CONNECT_TIMEOUT_SECS) | ||
| 411 | nTimeoutSecs = TOR_CONNECT_TIMEOUT_SECS; | ||
| 412 | |||
| 368 | SOCKET s = ConnectDirect(proxyHost, proxyPort, nSockSite, nTimeoutSecs); | 413 | SOCKET s = ConnectDirect(proxyHost, proxyPort, nSockSite, nTimeoutSecs); |
| 369 | if (s == INVALID_SOCKET) | 414 | if (s == INVALID_SOCKET) |
| 370 | return INVALID_SOCKET; | 415 | return INVALID_SOCKET; |