diff options
| author | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-13 12:20:16 -0300 |
|---|---|---|
| committer | Nsable-sh <nsable-sh@noreply.git.bitflash.network> | 2026-08-13 12:20:16 -0300 |
| commit | 20243de8fcf6dc30d7f0b1dbbc2f9db22683ae24 (patch) | |
| tree | d9d3f3378949e08dfd0e4ece9be36736a4a64e51 /src/tor.cpp | |
| parent | cd4ab7f6b4a2b08ec820a414fcdbede55674bab1 (diff) | |
net: fix managed Tor Windows startup
Diffstat (limited to 'src/tor.cpp')
| -rw-r--r-- | src/tor.cpp | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/tor.cpp b/src/tor.cpp index 0a1f48e..180cd2b 100644 --- a/src/tor.cpp +++ b/src/tor.cpp | |||
| @@ -177,38 +177,52 @@ static bool ReadOneLine(const std::string& path, std::string& out) | |||
| 177 | static bool PickLocalPort(unsigned short& portOut, std::string& errOut) | 177 | static bool PickLocalPort(unsigned short& portOut, std::string& errOut) |
| 178 | { | 178 | { |
| 179 | portOut = 0; | 179 | portOut = 0; |
| 180 | bool ok = false; | ||
| 181 | #ifdef _WIN32 | ||
| 182 | WSADATA wsadata; | ||
| 183 | int ret = WSAStartup(MAKEWORD(2,2), &wsadata); | ||
| 184 | if (ret != NO_ERROR) | ||
| 185 | { | ||
| 186 | errOut = strprintf("could not start Winsock for local port probe (WSAStartup returned %d)", ret); | ||
| 187 | return false; | ||
| 188 | } | ||
| 189 | #endif | ||
| 190 | struct sockaddr_in addr; | ||
| 191 | socklen_t len = sizeof(addr); | ||
| 180 | SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 192 | SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 181 | if (s == INVALID_SOCKET) | 193 | if (s == INVALID_SOCKET) |
| 182 | { | 194 | { |
| 183 | errOut = "could not create local port probe socket"; | 195 | errOut = "could not create local port probe socket"; |
| 184 | return false; | 196 | goto done; |
| 185 | } | 197 | } |
| 186 | struct sockaddr_in addr; | ||
| 187 | memset(&addr, 0, sizeof(addr)); | 198 | memset(&addr, 0, sizeof(addr)); |
| 188 | addr.sin_family = AF_INET; | 199 | addr.sin_family = AF_INET; |
| 189 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | 200 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 190 | addr.sin_port = 0; | 201 | addr.sin_port = 0; |
| 191 | if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) != 0) | 202 | if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) != 0) |
| 192 | { | 203 | { |
| 193 | closesocket(s); | ||
| 194 | errOut = "could not bind local port probe socket"; | 204 | errOut = "could not bind local port probe socket"; |
| 195 | return false; | 205 | goto done; |
| 196 | } | 206 | } |
| 197 | socklen_t len = sizeof(addr); | ||
| 198 | if (getsockname(s, (struct sockaddr*)&addr, &len) != 0) | 207 | if (getsockname(s, (struct sockaddr*)&addr, &len) != 0) |
| 199 | { | 208 | { |
| 200 | closesocket(s); | ||
| 201 | errOut = "could not read local port probe socket"; | 209 | errOut = "could not read local port probe socket"; |
| 202 | return false; | 210 | goto done; |
| 203 | } | 211 | } |
| 204 | portOut = ntohs(addr.sin_port); | 212 | portOut = ntohs(addr.sin_port); |
| 205 | closesocket(s); | ||
| 206 | if (portOut == 0) | 213 | if (portOut == 0) |
| 207 | { | 214 | { |
| 208 | errOut = "local port probe returned port zero"; | 215 | errOut = "local port probe returned port zero"; |
| 209 | return false; | 216 | goto done; |
| 210 | } | 217 | } |
| 211 | return true; | 218 | ok = true; |
| 219 | done: | ||
| 220 | if (s != INVALID_SOCKET) | ||
| 221 | closesocket(s); | ||
| 222 | #ifdef _WIN32 | ||
| 223 | WSACleanup(); | ||
| 224 | #endif | ||
| 225 | return ok; | ||
| 212 | } | 226 | } |
| 213 | 227 | ||
| 214 | static std::string ExecutableDir() | 228 | static std::string ExecutableDir() |
| @@ -285,8 +299,6 @@ std::string BtfBuildManagedTorrcForTest(const std::string& dataDir, | |||
| 285 | { | 299 | { |
| 286 | std::string torData = NormalizeTorrcPath(dataDir); | 300 | std::string torData = NormalizeTorrcPath(dataDir); |
| 287 | std::string hsDir = NormalizeTorrcPath(hiddenServiceDir); | 301 | std::string hsDir = NormalizeTorrcPath(hiddenServiceDir); |
| 288 | std::string logFile = NormalizeTorrcPath(PathJoin(dataDir, "tor.log")); | ||
| 289 | |||
| 290 | std::string s; | 302 | std::string s; |
| 291 | s += "# Generated by Bitflash. Edit only while Bitflash is not managing Tor.\n"; | 303 | s += "# Generated by Bitflash. Edit only while Bitflash is not managing Tor.\n"; |
| 292 | s += "DataDirectory " + QuoteTorrcPath(torData) + "\n"; | 304 | s += "DataDirectory " + QuoteTorrcPath(torData) + "\n"; |
| @@ -296,7 +308,6 @@ std::string BtfBuildManagedTorrcForTest(const std::string& dataDir, | |||
| 296 | s += "HiddenServiceVersion 3\n"; | 308 | s += "HiddenServiceVersion 3\n"; |
| 297 | s += strprintf("HiddenServicePort %u 127.0.0.1:%u\n", | 309 | s += strprintf("HiddenServicePort %u 127.0.0.1:%u\n", |
| 298 | (unsigned)p2pPort, (unsigned)p2pPort); | 310 | (unsigned)p2pPort, (unsigned)p2pPort); |
| 299 | s += "Log notice file " + QuoteTorrcPath(logFile) + "\n"; | ||
| 300 | return s; | 311 | return s; |
| 301 | } | 312 | } |
| 302 | 313 | ||