| 390 | } |
| 391 | |
| 392 | bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& sock) |
| 393 | { |
| 394 | try { |
| 395 | IntrRecvError recvr; |
| 396 | LogDebug(BCLog::NET, "SOCKS5 connecting %s\n", strDest); |
| 397 | if (strDest.size() > 255) { |
| 398 | LogError("Hostname too long\n"); |
| 399 | return false; |
| 400 | } |
| 401 | // Construct the version identifier/method selection message |
| 402 | std::vector<uint8_t> vSocks5Init; |
| 403 | vSocks5Init.push_back(SOCKSVersion::SOCKS5); // We want the SOCK5 protocol |
| 404 | if (auth) { |
| 405 | vSocks5Init.push_back(0x02); // 2 method identifiers follow... |
| 406 | vSocks5Init.push_back(SOCKS5Method::NOAUTH); |
| 407 | vSocks5Init.push_back(SOCKS5Method::USER_PASS); |
| 408 | } else { |
| 409 | vSocks5Init.push_back(0x01); // 1 method identifier follows... |
| 410 | vSocks5Init.push_back(SOCKS5Method::NOAUTH); |
| 411 | } |
| 412 | sock.SendComplete(vSocks5Init, g_socks5_recv_timeout, g_socks5_interrupt); |
| 413 | uint8_t pchRet1[2]; |
| 414 | if (InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) { |
| 415 | LogInfo("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port); |
| 416 | return false; |
| 417 | } |
| 418 | if (pchRet1[0] != SOCKSVersion::SOCKS5) { |
| 419 | LogError("Proxy failed to initialize\n"); |
| 420 | return false; |
| 421 | } |
| 422 | if (pchRet1[1] == SOCKS5Method::USER_PASS && auth) { |
| 423 | // Perform username/password authentication (as described in RFC1929) |
| 424 | std::vector<uint8_t> vAuth; |
| 425 | vAuth.push_back(0x01); // Current (and only) version of user/pass subnegotiation |
| 426 | if (auth->username.size() > 255 || auth->password.size() > 255) { |
| 427 | LogError("Proxy username or password too long\n"); |
| 428 | return false; |
| 429 | } |
| 430 | vAuth.push_back(auth->username.size()); |
| 431 | vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end()); |
| 432 | vAuth.push_back(auth->password.size()); |
| 433 | vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end()); |
| 434 | LogDebug(BCLog::PROXY, "SOCKS5 sending username/password authentication\n"); |
| 435 | sock.SendComplete(vAuth, g_socks5_recv_timeout, g_socks5_interrupt); |
| 436 | uint8_t pchRetA[2]; |
| 437 | if (InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) { |
| 438 | LogError("Error reading proxy authentication response\n"); |
| 439 | return false; |
| 440 | } |
| 441 | if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) { |
| 442 | LogError("Proxy authentication unsuccessful\n"); |
| 443 | return false; |
| 444 | } |
| 445 | } else if (pchRet1[1] == SOCKS5Method::NOAUTH) { |
| 446 | // Perform no authentication |
| 447 | } else { |
| 448 | LogError("Proxy requested wrong authentication method %02x\n", pchRet1[1]); |
| 449 | return false; |
no test coverage detected