diff options
| author | Bitflash <admin@bitflash.network> | 2026-08-12 10:18:14 -0300 |
|---|---|---|
| committer | Bitflash <admin@bitflash.network> | 2026-08-12 10:18:14 -0300 |
| commit | c330b8e5e1c057049b5b361908c94f41344dff80 (patch) | |
| tree | 44e70a657e4fa70205d9d318839ac68f175a1fb9 /src/nostr.cpp | |
| parent | 026ec904b7bde4898fbb889b7758dbcb13bf860a (diff) | |
| parent | 066379ebcb1dac720221fe1a1841fa6f59df21d9 (diff) | |
Merge pull request 'net: add optional SOCKS5 outbound proxy' (#27) from socks5-proxy into main
Diffstat (limited to 'src/nostr.cpp')
| -rw-r--r-- | src/nostr.cpp | 50 |
1 files changed, 3 insertions, 47 deletions
diff --git a/src/nostr.cpp b/src/nostr.cpp index 9bc538f..d286767 100644 --- a/src/nostr.cpp +++ b/src/nostr.cpp | |||
| @@ -29,6 +29,7 @@ | |||
| 29 | 29 | ||
| 30 | #include "btfaddr.h" | 30 | #include "btfaddr.h" |
| 31 | #include "btfchan.h" | 31 | #include "btfchan.h" |
| 32 | #include "proxy.h" | ||
| 32 | 33 | ||
| 33 | using json = nlohmann::json; | 34 | using json = nlohmann::json; |
| 34 | 35 | ||
| @@ -540,53 +541,8 @@ public: | |||
| 540 | size_t colon = host.find(':'); | 541 | size_t colon = host.find(':'); |
| 541 | if (colon != string::npos) { port = atoi(host.substr(colon + 1).c_str()); host = host.substr(0, colon); } | 542 | if (colon != string::npos) { port = atoi(host.substr(colon + 1).c_str()); host = host.substr(0, colon); } |
| 542 | 543 | ||
| 543 | // Resolve and connect (TCP). AF_UNSPEC so we get both A and AAAA | 544 | hSocket = BtfConnectSocket(host, (unsigned short)port, SOCK_NOSTR, timeoutSec); |
| 544 | // records back and try them in the order the resolver prefers, | 545 | if (hSocket == INVALID_SOCKET) |
| 545 | // rather than hard-failing whenever the IPv4 path to a relay is | ||
| 546 | // filtered/rate-limited (increasingly common for CDN-fronted | ||
| 547 | // relays) while a working IPv6 route exists right there in the | ||
| 548 | // same getaddrinfo() result. | ||
| 549 | struct addrinfo hints, *res = NULL, *rp = NULL; | ||
| 550 | memset(&hints, 0, sizeof(hints)); | ||
| 551 | hints.ai_family = AF_UNSPEC; | ||
| 552 | hints.ai_socktype = SOCK_STREAM; | ||
| 553 | char portstr[16]; sprintf(portstr, "%d", port); | ||
| 554 | if (getaddrinfo(host.c_str(), portstr, &hints, &res) != 0 || !res) | ||
| 555 | return error("Nostr: getaddrinfo %s failed", host.c_str()); | ||
| 556 | |||
| 557 | bool fConnected = false; | ||
| 558 | for (rp = res; rp != NULL; rp = rp->ai_next) | ||
| 559 | { | ||
| 560 | hSocket = BtfSocketTag(socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol), SOCK_NOSTR); | ||
| 561 | if (hSocket == INVALID_SOCKET) | ||
| 562 | continue; | ||
| 563 | |||
| 564 | // Socket receive/send timeout: Windows takes a DWORD of | ||
| 565 | // milliseconds; POSIX takes a struct timeval. | ||
| 566 | #ifdef _WIN32 | ||
| 567 | DWORD tv = timeoutSec * 1000; | ||
| 568 | setsockopt(hSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)); | ||
| 569 | setsockopt(hSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv)); | ||
| 570 | #else | ||
| 571 | struct timeval tv; | ||
| 572 | tv.tv_sec = timeoutSec; | ||
| 573 | tv.tv_usec = 0; | ||
| 574 | setsockopt(hSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)); | ||
| 575 | setsockopt(hSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv)); | ||
| 576 | #endif | ||
| 577 | |||
| 578 | if (connect(hSocket, rp->ai_addr, (int)rp->ai_addrlen) == 0) | ||
| 579 | { | ||
| 580 | fConnected = true; | ||
| 581 | break; | ||
| 582 | } | ||
| 583 | |||
| 584 | BtfCloseSocket(hSocket); | ||
| 585 | hSocket = INVALID_SOCKET; | ||
| 586 | } | ||
| 587 | freeaddrinfo(res); | ||
| 588 | |||
| 589 | if (!fConnected) | ||
| 590 | return error("Nostr: connect %s:%d failed", host.c_str(), port); | 546 | return error("Nostr: connect %s:%d failed", host.c_str(), port); |
| 591 | 547 | ||
| 592 | // TLS | 548 | // TLS |