Connect using SOCKS5 (as described in RFC1928) */
| 317 | |
| 318 | /** Connect using SOCKS5 (as described in RFC1928) */ |
| 319 | static bool Socks5(const std::string& strDest, int port, const ProxyCredentials *auth, const SOCKET& hSocket) |
| 320 | { |
| 321 | IntrRecvError recvr; |
| 322 | LogPrint(BCLog::NET, "SOCKS5 connecting %s\n", strDest); |
| 323 | if (strDest.size() > 255) { |
| 324 | return error("Hostname too long"); |
| 325 | } |
| 326 | // Accepted authentication methods |
| 327 | std::vector<uint8_t> vSocks5Init; |
| 328 | vSocks5Init.push_back(SOCKSVersion::SOCKS5); |
| 329 | if (auth) { |
| 330 | vSocks5Init.push_back(0x02); // Number of methods |
| 331 | vSocks5Init.push_back(SOCKS5Method::NOAUTH); |
| 332 | vSocks5Init.push_back(SOCKS5Method::USER_PASS); |
| 333 | } else { |
| 334 | vSocks5Init.push_back(0x01); // Number of methods |
| 335 | vSocks5Init.push_back(SOCKS5Method::NOAUTH); |
| 336 | } |
| 337 | ssize_t ret = send(hSocket, (const char*)vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL); |
| 338 | if (ret != (ssize_t)vSocks5Init.size()) { |
| 339 | return error("Error sending to proxy"); |
| 340 | } |
| 341 | uint8_t pchRet1[2]; |
| 342 | if ((recvr = InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) { |
| 343 | LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port); |
| 344 | return false; |
| 345 | } |
| 346 | if (pchRet1[0] != SOCKSVersion::SOCKS5) { |
| 347 | return error("Proxy failed to initialize"); |
| 348 | } |
| 349 | if (pchRet1[1] == SOCKS5Method::USER_PASS && auth) { |
| 350 | // Perform username/password authentication (as described in RFC1929) |
| 351 | std::vector<uint8_t> vAuth; |
| 352 | vAuth.push_back(0x01); // Current (and only) version of user/pass subnegotiation |
| 353 | if (auth->username.size() > 255 || auth->password.size() > 255) |
| 354 | return error("Proxy username or password too long"); |
| 355 | vAuth.push_back(auth->username.size()); |
| 356 | vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end()); |
| 357 | vAuth.push_back(auth->password.size()); |
| 358 | vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end()); |
| 359 | ret = send(hSocket, (const char*)vAuth.data(), vAuth.size(), MSG_NOSIGNAL); |
| 360 | if (ret != (ssize_t)vAuth.size()) { |
| 361 | return error("Error sending authentication to proxy"); |
| 362 | } |
| 363 | LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password); |
| 364 | uint8_t pchRetA[2]; |
| 365 | if ((recvr = InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) { |
| 366 | return error("Error reading proxy authentication response"); |
| 367 | } |
| 368 | if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) { |
| 369 | return error("Proxy authentication unsuccessful"); |
| 370 | } |
| 371 | } else if (pchRet1[1] == SOCKS5Method::NOAUTH) { |
| 372 | // Perform no authentication |
| 373 | } else { |
| 374 | return error("Proxy requested wrong authentication method %02x", pchRet1[1]); |
| 375 | } |
| 376 | std::vector<uint8_t> vSocks5; |
no test coverage detected