| 365 | } |
| 366 | |
| 367 | bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& sock) |
| 368 | { |
| 369 | IntrRecvError recvr; |
| 370 | LogPrint(BCLog::NET, "SOCKS5 connecting %s\n", strDest); |
| 371 | if (strDest.size() > 255) { |
| 372 | return error("Hostname too long"); |
| 373 | } |
| 374 | // Construct the version identifier/method selection message |
| 375 | std::vector<uint8_t> vSocks5Init; |
| 376 | vSocks5Init.push_back(SOCKSVersion::SOCKS5); // We want the SOCK5 protocol |
| 377 | if (auth) { |
| 378 | vSocks5Init.push_back(0x02); // 2 method identifiers follow... |
| 379 | vSocks5Init.push_back(SOCKS5Method::NOAUTH); |
| 380 | vSocks5Init.push_back(SOCKS5Method::USER_PASS); |
| 381 | } else { |
| 382 | vSocks5Init.push_back(0x01); // 1 method identifier follows... |
| 383 | vSocks5Init.push_back(SOCKS5Method::NOAUTH); |
| 384 | } |
| 385 | ssize_t ret = sock.Send(vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL); |
| 386 | if (ret != (ssize_t)vSocks5Init.size()) { |
| 387 | return error("Error sending to proxy"); |
| 388 | } |
| 389 | uint8_t pchRet1[2]; |
| 390 | if ((recvr = InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) { |
| 391 | LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port); |
| 392 | return false; |
| 393 | } |
| 394 | if (pchRet1[0] != SOCKSVersion::SOCKS5) { |
| 395 | return error("Proxy failed to initialize"); |
| 396 | } |
| 397 | if (pchRet1[1] == SOCKS5Method::USER_PASS && auth) { |
| 398 | // Perform username/password authentication (as described in RFC1929) |
| 399 | std::vector<uint8_t> vAuth; |
| 400 | vAuth.push_back(0x01); // Current (and only) version of user/pass subnegotiation |
| 401 | if (auth->username.size() > 255 || auth->password.size() > 255) |
| 402 | return error("Proxy username or password too long"); |
| 403 | vAuth.push_back(auth->username.size()); |
| 404 | vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end()); |
| 405 | vAuth.push_back(auth->password.size()); |
| 406 | vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end()); |
| 407 | ret = sock.Send(vAuth.data(), vAuth.size(), MSG_NOSIGNAL); |
| 408 | if (ret != (ssize_t)vAuth.size()) { |
| 409 | return error("Error sending authentication to proxy"); |
| 410 | } |
| 411 | LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password); |
| 412 | uint8_t pchRetA[2]; |
| 413 | if ((recvr = InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) { |
| 414 | return error("Error reading proxy authentication response"); |
| 415 | } |
| 416 | if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) { |
| 417 | return error("Proxy authentication unsuccessful"); |
| 418 | } |
| 419 | } else if (pchRet1[1] == SOCKS5Method::NOAUTH) { |
| 420 | // Perform no authentication |
| 421 | } else { |
| 422 | return error("Proxy requested wrong authentication method %02x", pchRet1[1]); |
| 423 | } |
| 424 | std::vector<uint8_t> vSocks5; |