Skip to content

Commit 18436d8

Browse files
committed
Merge #8033: Fix Socks5() connect failures to be less noisy and less unnecessarily scary
bf9266e Use Socks5ErrorString() to decode error responses from socks proxy. (Warren Togami) 94fd1d8 Make Socks5() InterruptibleRecv() timeout/failures informative. (Warren Togami) 0d9af79 SOCKS5 connecting and connected messages with -debug=net. (Warren Togami) 00678bd Make failures to connect via Socks5() more informative and less unnecessarily scary. (Warren Togami)
2 parents 239d419 + bf9266e commit 18436d8

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/netbase.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,25 @@ struct ProxyCredentials
291291
std::string password;
292292
};
293293

294+
std::string Socks5ErrorString(int err)
295+
{
296+
switch(err) {
297+
case 0x01: return "general failure";
298+
case 0x02: return "connection not allowed";
299+
case 0x03: return "network unreachable";
300+
case 0x04: return "host unreachable";
301+
case 0x05: return "connection refused";
302+
case 0x06: return "TTL expired";
303+
case 0x07: return "protocol error";
304+
case 0x08: return "address type not supported";
305+
default: return "unknown";
306+
}
307+
}
308+
294309
/** Connect using SOCKS5 (as described in RFC1928) */
295310
static bool Socks5(const std::string& strDest, int port, const ProxyCredentials *auth, SOCKET& hSocket)
296311
{
297-
LogPrintf("SOCKS5 connecting %s\n", strDest);
312+
LogPrint("net", "SOCKS5 connecting %s\n", strDest);
298313
if (strDest.size() > 255) {
299314
CloseSocket(hSocket);
300315
return error("Hostname too long");
@@ -318,7 +333,8 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
318333
char pchRet1[2];
319334
if (!InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
320335
CloseSocket(hSocket);
321-
return error("Error reading proxy response");
336+
LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
337+
return false;
322338
}
323339
if (pchRet1[0] != 0x05) {
324340
CloseSocket(hSocket);
@@ -379,19 +395,10 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
379395
return error("Proxy failed to accept request");
380396
}
381397
if (pchRet2[1] != 0x00) {
398+
// Failures to connect to a peer that are not proxy errors
382399
CloseSocket(hSocket);
383-
switch (pchRet2[1])
384-
{
385-
case 0x01: return error("Proxy error: general failure");
386-
case 0x02: return error("Proxy error: connection not allowed");
387-
case 0x03: return error("Proxy error: network unreachable");
388-
case 0x04: return error("Proxy error: host unreachable");
389-
case 0x05: return error("Proxy error: connection refused");
390-
case 0x06: return error("Proxy error: TTL expired");
391-
case 0x07: return error("Proxy error: protocol error");
392-
case 0x08: return error("Proxy error: address type not supported");
393-
default: return error("Proxy error: unknown");
394-
}
400+
LogPrintf("Socks5() connect to %s:%d failed: %s\n", strDest, port, Socks5ErrorString(pchRet2[1]));
401+
return false;
395402
}
396403
if (pchRet2[2] != 0x00) {
397404
CloseSocket(hSocket);
@@ -423,7 +430,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
423430
CloseSocket(hSocket);
424431
return error("Error reading from proxy");
425432
}
426-
LogPrintf("SOCKS5 connected %s\n", strDest);
433+
LogPrint("net", "SOCKS5 connected %s\n", strDest);
427434
return true;
428435
}
429436

0 commit comments

Comments
 (0)