| 6 |
| #include "PrivateDefines.h" |
| 7 | #include "Buffers.h" |
| 8 | #include "NetworkSocket.h" |
| 9 | #include "VoIPController.h" |
| 10 | #include "VoIPServerConfig.h" |
| 11 |
| 12 | #if defined(_WIN32) |
| 13 | #include "os/windows/NetworkSocketWinsock.h" |
| 14 | #include <winsock2.h> |
| 15 | #else |
| 16 | #include "os/posix/NetworkSocketPosix.h" |
| 17 | #endif |
| 18 |
| 19 | #include <algorithm> |
| 20 | #include <cstdlib> |
| 21 | #include <cstring> |
| 22 | #include <stdexcept> |
| 23 |
| 24 | #define MIN_UDP_PORT 16384 |
| 25 | #define MAX_UDP_PORT 32768 |
| 26 |
| 27 | using namespace tgvoip; |
| 28 |
| 29 | NetworkAddress::NetworkAddress() = default; |
| 30 |
| 31 | NetworkPacket::NetworkPacket(Buffer data, const NetworkAddress& address, std::uint16_t port, NetworkProtocol protocol) |
| 32 | : data(std::move(data)) |
| 33 | , address(address) |
| 34 | , protocol(protocol) |
| 35 | , port(port) |
| 36 | { |
| 37 | } |
| 38 |
| 39 | NetworkPacket NetworkPacket::Empty() |
| 40 | { |
| 41 | return NetworkPacket{ Buffer(), NetworkAddress::Empty(), 0, NetworkProtocol::UDP }; |
| 42 | } |
| 43 |
| 44 | bool NetworkPacket::IsEmpty() const |
| 45 | { |
| 46 | return data.IsEmpty() || (protocol == NetworkProtocol::UDP && (port == 0 || address.IsEmpty())); |
| 47 | } |
| 48 |
| 49 | NetworkSocket::NetworkSocket(NetworkProtocol protocol) |
| 50 | : m_ipv6Timeout(ServerConfig::GetSharedInstance()->GetDouble("nat64_fallback_timeout", 3)) |
| 51 | , m_protocol(protocol) |
| 52 | , m_failed(false) |
| 53 | { |
| 54 | } |
| 55 |
| 56 | NetworkSocket::~NetworkSocket() = default; |
| 57 |
| 58 | std::string NetworkSocket::GetLocalInterfaceInfo(NetworkAddress* inet4addr, NetworkAddress* inet6addr) |
| 59 | { |
| 60 | return "not implemented"; |
| 61 | } |
| 62 |
| 63 | std::uint16_t NetworkSocket::GenerateLocalPort() |
| 64 | { |
| 65 | std::uint16_t rnd; |
| 66 | VoIPController::crypto.rand_bytes(reinterpret_cast<std::uint8_t*>(&rnd), 2); |
| 67 | return static_cast<std::uint16_t>((rnd % (MAX_UDP_PORT - MIN_UDP_PORT)) + MIN_UDP_PORT); |
| 68 | } |
| 69 |
| 70 | void NetworkSocket::SetMaxPriority() |
| 71 | { |
| 72 | } |
| 73 |
| 74 | std::uint16_t NetworkSocket::GetLocalPort() |
| 75 | { |
| 76 | return 0; |
| 77 | } |
| 78 |
| 79 | void NetworkSocket::OnActiveInterfaceChanged() |
| 80 | { |
| 81 | } |
| 82 |
| 83 | NetworkAddress NetworkSocket::GetConnectedAddress() |
| 84 | { |
| 85 | return NetworkAddress::Empty(); |
| 86 | } |
| 87 |
| 88 | std::uint16_t NetworkSocket::GetConnectedPort() |
| 89 | { |
| 90 | return 0; |
| 91 | } |
| 92 |
| 93 | void NetworkSocket::SetTimeouts(int sendTimeout, int recvTimeout) |
| 94 | { |
| 95 | } |
| 96 |
| 97 | bool NetworkSocket::IsFailed() const |
| 98 | { |
| 99 | return m_failed; |
| 100 | } |
| 101 |
| 102 | bool NetworkSocket::IsReadyToSend() const |
| 103 | { |
| 104 | return m_readyToSend; |
| 105 | } |
| 106 |
| 107 | bool NetworkSocket::OnReadyToSend() |
| 108 | { |
| 109 | m_readyToSend = true; |
| 110 | return true; |
| 111 | } |
| 112 |
| 113 | bool NetworkSocket::OnReadyToReceive() |
| 114 | { |
| 115 | return true; |
| 116 | } |
| 117 |
| 118 | void NetworkSocket::SetTimeout(double timeout) |
| 119 | { |
| 120 | m_timeout = timeout; |
| 121 | } |
| 122 |
| 123 | NetworkSocket* NetworkSocket::Create(NetworkProtocol protocol) |
| 124 | { |
| 125 | #ifndef _WIN32 |
| 126 | return new NetworkSocketPosix(protocol); |
| 127 | #else |
| 128 | return new NetworkSocketWinsock(protocol); |
| 129 | #endif |
| 130 | } |
| 131 |
| 132 | NetworkAddress NetworkSocket::ResolveDomainName(const std::string& name) |
| 133 | { |
| 134 | #ifndef _WIN32 |
| 135 | return NetworkSocketPosix::ResolveDomainName(name); |
| 136 | #else |
| 137 | return NetworkSocketWinsock::ResolveDomainName(name); |
| 138 | #endif |
| 139 | } |
| 140 |
| 141 | void NetworkSocket::GenerateTCPO2States(std::uint8_t* buffer, TCPO2State* recvState, TCPO2State* sendState) |
| 142 | { |
| 143 | std::memset(recvState, 0, sizeof(TCPO2State)); |
| 144 | std::memset(sendState, 0, sizeof(TCPO2State)); |
| 145 |
| 146 | std::array<std::uint8_t, 64> nonce; |
| 147 |
| 148 | std::uint32_t *first = reinterpret_cast<std::uint32_t*>(nonce.data()), *second = first + 1; |
| 149 | std::uint32_t first1 = 0x44414548u, first2 = 0x54534F50u, first3 = 0x20544547u, first4 = 0x20544547u, first5 = 0xEEEEEEEEu; |
| 150 | std::uint32_t second1 = 0; |
| 151 | do |
| 152 | { |
| 153 | VoIPController::crypto.rand_bytes(nonce.data(), STD_ARRAY_SIZEOF(nonce)); |
| 154 | } while (*first == first1 || *first == first2 || *first == first3 || *first == first4 || |
| 155 | *first == first5 || *second == second1 || nonce.front() == 0xEF); |
| 156 |
| 157 | // prepare encryption key/iv |
| 158 | std::memcpy(sendState->key, nonce.data() + 8, 32); |
| 159 | std::memcpy(sendState->iv, nonce.data() + 8 + 32, 16); |
| 160 |
| 161 | // prepare decryption key/iv |
| 162 | std::array<char, 48> reversed; |
| 163 |
| 164 | std::memcpy(reversed.data(), nonce.data() + 8, STD_ARRAY_SIZEOF(reversed)); |
| 165 | std::reverse(reversed.begin(), reversed.end()); |
| 166 | std::memcpy(recvState->key, reversed.data(), 32); |
| 167 | std::memcpy(recvState->iv, reversed.data() + 32, 16); |
| 168 |
| 169 | // write protocol identifier |
| 170 | *reinterpret_cast<std::uint32_t*>(nonce.data() + 56) = 0xEFEFEFEFu; |
| 171 | std::memcpy(buffer, nonce.data(), 56); |
| 172 | EncryptForTCPO2(nonce.data(), STD_ARRAY_SIZEOF(nonce), sendState); |
| 173 | std::memcpy(buffer + 56, nonce.data() + 56, 8); |
| 174 | } |
| 175 |
| 176 | void NetworkSocket::EncryptForTCPO2(std::uint8_t* buffer, std::size_t len, TCPO2State* state) |
| 177 | { |
| 178 | VoIPController::crypto.aes_ctr_encrypt(buffer, len, state->key, state->iv, state->ecount, &state->num); |
| 179 | } |
| 180 |
| 181 | std::size_t NetworkSocket::Receive(std::uint8_t* buffer, std::size_t len) |
| 182 | { |
| 183 | NetworkPacket pkt = Receive(len); |
| 184 | if (pkt.IsEmpty()) |
| 185 | return 0; |
| 186 | std::size_t actualLen = std::min(len, pkt.data.Length()); |
| 187 | std::memcpy(buffer, *pkt.data, actualLen); |
| 188 | return actualLen; |
| 189 | } |
| 190 |
| 191 | NetworkSocketWrapper::NetworkSocketWrapper(NetworkProtocol protocol) |
| 192 | : NetworkSocket(protocol) |
| 193 | { |
| 194 | } |
| 195 |
| 196 | NetworkSocketWrapper::~NetworkSocketWrapper() = default; |
| 197 |
| 198 | void NetworkSocketWrapper::SetNonBlocking(bool) |
| 199 | { |
| 200 | } |
| 201 |
| 202 | bool NetworkAddress::operator==(const NetworkAddress& other) const |
| 203 | { |
| 204 | if (isIPv6 != other.isIPv6) |
| 205 | return false; |
| 206 | if (!isIPv6) |
| 207 | return addr.ipv4 == other.addr.ipv4; |
| 208 |
| 209 | return std::memcmp(addr.ipv6, other.addr.ipv6, 16) == 0; |
| 210 | } |
| 211 |
| 212 | bool NetworkAddress::operator!=(const NetworkAddress& other) const |
| 213 | { |
| 214 | return !(*this == other); |
| 215 | } |
| 216 |
| 217 | std::string NetworkAddress::ToString() const |
| 218 | { |
| 219 | if (isIPv6) |
| 220 | { |
| 221 | #ifndef _WIN32 |
| 222 | return NetworkSocketPosix::V6AddressToString(addr.ipv6); |
| 223 | #else |
| 224 | return NetworkSocketWinsock::V6AddressToString(addr.ipv6); |
| 225 | #endif |
| 226 | } |
| 227 | #ifndef _WIN32 |
| 228 | return NetworkSocketPosix::V4AddressToString(addr.ipv4); |
| 229 | #else |
| 230 | return NetworkSocketWinsock::V4AddressToString(addr.ipv4); |
| 231 | #endif |
| 232 | } |
| 233 |
| 234 | bool NetworkAddress::IsEmpty() const |
| 235 | { |
| 236 | if (isIPv6) |
| 237 | { |
| 238 | const std::uint64_t* a = reinterpret_cast<const std::uint64_t*>(addr.ipv6); |
| 239 | return a[0] == 0LL && a[1] == 0LL; |
| 240 | } |
| 241 | return addr.ipv4 == 0; |
| 242 | } |
| 243 |
| 244 | bool NetworkAddress::PrefixMatches(const unsigned int prefix, const NetworkAddress& other) const |
| 245 | { |
| 246 | if (isIPv6 != other.isIPv6) |
| 247 | return false; |
| 248 | if (!isIPv6) |
| 249 | { |
| 250 | std::uint32_t mask = 0xFFFFFFFF << (32 - prefix); |
| 251 | return (addr.ipv4 & mask) == (other.addr.ipv4 & mask); |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 |
| 256 | NetworkAddress NetworkAddress::Empty() |
| 257 | { |
| 258 | NetworkAddress addr; |
| 259 | addr.isIPv6 = false; |
| 260 | addr.addr.ipv4 = 0; |
| 261 | return addr; |
| 262 | } |
| 263 |
| 264 | NetworkAddress NetworkAddress::IPv4(const std::string& str) |
| 265 | { |
| 266 | NetworkAddress addr; |
| 267 | addr.isIPv6 = false; |
| 268 | #ifndef _WIN32 |
| 269 | addr.addr.ipv4 = NetworkSocketPosix::StringToV4Address(str); |
| 270 | #else |
| 271 | addr.addr.ipv4 = NetworkSocketWinsock::StringToV4Address(str); |
| 272 | #endif |
| 273 | return addr; |
| 274 | } |
| 275 |
| 276 | NetworkAddress NetworkAddress::IPv4(std::uint32_t addr) |
| 277 | { |
| 278 | NetworkAddress a; |
| 279 | a.isIPv6 = false; |
| 280 | a.addr.ipv4 = addr; |
| 281 | return a; |
| 282 | } |
| 283 |
| 284 | NetworkAddress NetworkAddress::IPv6(const std::string& str) |
| 285 | { |
| 286 | NetworkAddress addr; |
| 287 | addr.isIPv6 = false; |
| 288 | #ifndef _WIN32 |
| 289 | NetworkSocketPosix::StringToV6Address(str, addr.addr.ipv6); |
| 290 | #else |
| 291 | NetworkSocketWinsock::StringToV6Address(str, addr.addr.ipv6); |
| 292 | #endif |
| 293 | return addr; |
| 294 | } |
| 295 |
| 296 | NetworkAddress NetworkAddress::IPv6(const std::uint8_t addr[16]) |
| 297 | { |
| 298 | NetworkAddress a; |
| 299 | a.isIPv6 = true; |
| 300 | std::memcpy(a.addr.ipv6, addr, 16); |
| 301 | return a; |
| 302 | } |
| 303 |
| 304 | bool NetworkSocket::Select(std::list<NetworkSocket*>& readFds, std::list<NetworkSocket*>& writeFds, std::list<NetworkSocket*>& errorFds, SocketSelectCanceller* canceller) |
| 305 | { |
| 306 | #ifndef _WIN32 |
| 307 | return NetworkSocketPosix::Select(readFds, writeFds, errorFds, canceller); |
| 308 | #else |
| 309 | return NetworkSocketWinsock::Select(readFds, writeFds, errorFds, canceller); |
| 310 | #endif |
| 311 | } |
| 312 |
| 313 | SocketSelectCanceller::~SocketSelectCanceller() = default; |
| 314 |
| 315 | SocketSelectCanceller* SocketSelectCanceller::Create() |
| 316 | { |
| 317 | #ifndef _WIN32 |
| 318 | return new SocketSelectCancellerPosix(); |
| 319 | #else |
| 320 | return new SocketSelectCancellerWin32(); |
| 321 | #endif |
| 322 | } |
| 323 |
| 324 | NetworkSocketTCPObfuscated::NetworkSocketTCPObfuscated(NetworkSocket* wrapped) |
| 325 | : NetworkSocketWrapper(NetworkProtocol::TCP) |
| 326 | , m_wrapped(wrapped) |
| 327 | { |
| 328 | } |
| 329 |
| 330 | NetworkSocketTCPObfuscated::~NetworkSocketTCPObfuscated() |
| 331 | { |
| 332 | delete m_wrapped; |
| 333 | } |
| 334 |
| 335 | NetworkSocket* NetworkSocketTCPObfuscated::GetWrapped() |
| 336 | { |
| 337 | return m_wrapped; |
| 338 | } |
| 339 |
| 340 | void NetworkSocketTCPObfuscated::InitConnection() |
| 341 | { |
| 342 | Buffer buf(64); |
| 343 | GenerateTCPO2States(*buf, &m_recvState, &m_sendState); |
| 344 | m_wrapped->Send(NetworkPacket |
| 345 | { |
| 346 | std::move(buf), |
| 347 | NetworkAddress::Empty(), |
| 348 | 0, |
| 349 | NetworkProtocol::TCP |
| 350 | }); |
| 351 | } |
| 352 |
| 353 | void NetworkSocketTCPObfuscated::Send(NetworkPacket packet) |
| 354 | { |
| 355 | BufferOutputStream os(packet.data.Length() + 4); |
| 356 | std::size_t len = packet.data.Length() / 4; |
| 357 | if (len < 0x7F) |
| 358 | { |
| 359 | os.WriteUInt8(static_cast<std::uint8_t>(len)); |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | os.WriteUInt8(std::uint8_t{0x7F}); |
| 364 | os.WriteUInt8(static_cast<std::uint8_t>((len >> 0) & 0xFF)); |
| 365 | os.WriteUInt8(static_cast<std::uint8_t>((len >> 8) & 0xFF)); |
| 366 | os.WriteUInt8(static_cast<std::uint8_t>((len >> 16) & 0xFF)); |
| 367 | } |
| 368 | os.WriteBytes(packet.data); |
| 369 | EncryptForTCPO2(os.GetBuffer(), os.GetLength(), &m_sendState); |
| 370 | m_wrapped->Send(NetworkPacket |
| 371 | { |
| 372 | Buffer(std::move(os)), |
| 373 | NetworkAddress::Empty(), |
| 374 | 0, |
| 375 | NetworkProtocol::TCP |
| 376 | }); |
| 377 | } |
| 378 |
| 379 | bool NetworkSocketTCPObfuscated::OnReadyToSend() |
| 380 | { |
| 381 | LOGV("TCPO socket ready to send"); |
| 382 | if (!m_initialized) |
| 383 | { |
| 384 | LOGV("Initializing TCPO2 connection"); |
| 385 | m_initialized = true; |
| 386 | InitConnection(); |
| 387 | m_readyToSend = true; |
| 388 | return false; |
| 389 | } |
| 390 | return m_wrapped->OnReadyToSend(); |
| 391 | } |
| 392 |
| 393 | NetworkPacket NetworkSocketTCPObfuscated::Receive(std::size_t maxLen) |
| 394 | { |
| 395 | std::uint8_t len1; |
| 396 | std::size_t packetLen = 0; |
| 397 | std::size_t offset = 0; |
| 398 | std::size_t len; |
| 399 | len = m_wrapped->Receive(&len1, 1); |
| 400 | if (len <= 0) |
| 401 | { |
| 402 | return NetworkPacket::Empty(); |
| 403 | } |
| 404 | EncryptForTCPO2(&len1, 1, &m_recvState); |
| 405 |
| 406 | if (len1 < 0x7F) |
| 407 | { |
| 408 | packetLen = static_cast<std::size_t>(len1) * 4; |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | std::uint8_t len2[3]; |
| 413 | len = m_wrapped->Receive(len2, 3); |
| 414 | if (len <= 0) |
| 415 | { |
| 416 | return NetworkPacket::Empty(); |
| 417 | } |
| 418 | EncryptForTCPO2(len2, 3, &m_recvState); |
| 419 | packetLen = ((static_cast<std::size_t>(len2[0]) << 0) | |
| 420 | (static_cast<std::size_t>(len2[1]) << 8) | |
| 421 | (static_cast<std::size_t>(len2[2]) << 16)) * 4; |
| 422 | } |
| 423 |
| 424 | if (packetLen > 1500) |
| 425 | { |
| 426 | LOGW("packet too big to fit into buffer (%u vs %u)", static_cast<unsigned>(packetLen), 1500u); |
| 427 | return NetworkPacket::Empty(); |
| 428 | } |
| 429 | Buffer buf(packetLen); |
| 430 |
| 431 | while (offset < packetLen) |
| 432 | { |
| 433 | len = m_wrapped->Receive(*buf, packetLen - offset); |
| 434 | if (len <= 0) |
| 435 | { |
| 436 | return NetworkPacket::Empty(); |
| 437 | } |
| 438 | offset += len; |
| 439 | } |
| 440 | EncryptForTCPO2(*buf, packetLen, &m_recvState); |
| 441 | return NetworkPacket |
| 442 | { |
| 443 | std::move(buf), |
| 444 | m_wrapped->GetConnectedAddress(), |
| 445 | m_wrapped->GetConnectedPort(), |
| 446 | NetworkProtocol::TCP |
| 447 | }; |
| 448 | } |
| 449 |
| 450 | void NetworkSocketTCPObfuscated::Open() |
| 451 | { |
| 452 | } |
| 453 |
| 454 | void NetworkSocketTCPObfuscated::Close() |
| 455 | { |
| 456 | m_wrapped->Close(); |
| 457 | } |
| 458 |
| 459 | void NetworkSocketTCPObfuscated::Connect(const NetworkAddress& address, std::uint16_t port) |
| 460 | { |
| 461 | m_wrapped->Connect(address, port); |
| 462 | } |
| 463 |
| 464 | bool NetworkSocketTCPObfuscated::IsFailed() const |
| 465 | { |
| 466 | return m_wrapped->IsFailed(); |
| 467 | } |
| 468 |
| 469 | bool NetworkSocketTCPObfuscated::IsReadyToSend() const |
| 470 | { |
| 471 | return m_readyToSend && m_wrapped->IsReadyToSend(); |
| 472 | } |
| 473 |
| 474 | NetworkSocketSOCKS5Proxy::NetworkSocketSOCKS5Proxy(NetworkSocket* tcp, NetworkSocket* udp, std::string username, std::string password) |
| 475 | : NetworkSocketWrapper(udp ? NetworkProtocol::UDP : NetworkProtocol::TCP) |
| 476 | , m_tcp(tcp) |
| 477 | , m_udp(udp) |
| 478 | , m_username(std::move(username)) |
| 479 | , m_password(std::move(password)) |
| 480 | { |
| 481 | } |
| 482 |
| 483 | NetworkSocketSOCKS5Proxy::~NetworkSocketSOCKS5Proxy() |
| 484 | { |
| 485 | delete m_tcp; |
| 486 | } |
| 487 |
| 488 | void NetworkSocketSOCKS5Proxy::Send(NetworkPacket packet) |
| 489 | { |
| 490 | if (m_protocol == NetworkProtocol::TCP) |
| 491 | { |
| 492 | m_tcp->Send(std::move(packet)); |
| 493 | } |
| 494 | else if (m_protocol == NetworkProtocol::UDP) |
| 495 | { |
| 496 | BufferOutputStream out(1500); |
| 497 | out.WriteInt16(0); // RSV |
| 498 | out.WriteUInt8(0); // FRAG |
| 499 | if (!packet.address.isIPv6) |
| 500 | { |
| 501 | out.WriteUInt8(1); // ATYP (IPv4) |
| 502 | out.WriteUInt32(packet.address.addr.ipv4); |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | out.WriteUInt8(4); // ATYP (IPv6) |
| 507 | out.WriteBytes(packet.address.addr.ipv6, 16); |
| 508 | } |
| 509 | out.WriteUInt16(htons(packet.port)); |
| 510 | out.WriteBytes(packet.data); |
| 511 | m_udp->Send(NetworkPacket |
| 512 | { |
| 513 | Buffer(std::move(out)), |
| 514 | m_connectedAddress, |
| 515 | m_connectedPort, |
| 516 | NetworkProtocol::UDP |
| 517 | }); |
| 518 | } |
| 519 | } |
| 520 |
| 521 | NetworkPacket NetworkSocketSOCKS5Proxy::Receive(std::size_t maxLen) |
| 522 | { |
| 523 | if (m_protocol == NetworkProtocol::TCP) |
| 524 | { |
| 525 | NetworkPacket packet = m_tcp->Receive(0); |
| 526 | packet.address = m_connectedAddress; |
| 527 | packet.port = m_connectedPort; |
| 528 | return packet; |
| 529 | } |
| 530 | NetworkPacket p = m_udp->Receive(0); |
| 531 | if (!p.IsEmpty() && p.address == m_connectedAddress && p.port == m_connectedPort) |
| 532 | { |
| 533 | BufferInputStream in(p.data); |
| 534 | in.ReadInt16(); // RSV |
| 535 | in.ReadUInt8(); // FRAG |
| 536 | std::uint8_t atyp = in.ReadUInt8(); |
| 537 | NetworkAddress address = NetworkAddress::Empty(); |
| 538 | if (atyp == 1) |
| 539 | { // IPv4 |
| 540 | address = NetworkAddress::IPv4(in.ReadUInt32()); |
| 541 | } |
| 542 | else if (atyp == 4) |
| 543 | { // IPv6 |
| 544 | std::uint8_t addr[16]; |
| 545 | in.ReadBytes(addr, 16); |
| 546 | address = NetworkAddress::IPv6(addr); |
| 547 | } |
| 548 | return NetworkPacket |
| 549 | { |
| 550 | Buffer::CopyOf(p.data, in.GetOffset(), in.Remaining()), |
| 551 | address, |
| 552 | htons(in.ReadUInt16()), |
| 553 | m_protocol |
| 554 | }; |
| 555 | } |
| 556 | return NetworkPacket::Empty(); |
| 557 | } |
| 558 |
| 559 | void NetworkSocketSOCKS5Proxy::Open() |
| 560 | { |
| 561 | } |
| 562 |
| 563 | void NetworkSocketSOCKS5Proxy::Close() |
| 564 | { |
| 565 | m_tcp->Close(); |
| 566 | } |
| 567 |
| 568 | void NetworkSocketSOCKS5Proxy::Connect(const NetworkAddress& address, std::uint16_t port) |
| 569 | { |
| 570 | m_connectedAddress = address; |
| 571 | m_connectedPort = port; |
| 572 | } |
| 573 |
| 574 | NetworkSocket* NetworkSocketSOCKS5Proxy::GetWrapped() |
| 575 | { |
| 576 | return m_protocol == NetworkProtocol::TCP ? m_tcp : m_udp; |
| 577 | } |
| 578 |
| 579 | void NetworkSocketSOCKS5Proxy::InitConnection() |
| 580 | { |
| 581 | } |
| 582 |
| 583 | bool NetworkSocketSOCKS5Proxy::IsFailed() const |
| 584 | { |
| 585 | return NetworkSocket::IsFailed() || m_tcp->IsFailed(); |
| 586 | } |
| 587 |
| 588 | NetworkAddress NetworkSocketSOCKS5Proxy::GetConnectedAddress() |
| 589 | { |
| 590 | return m_connectedAddress; |
| 591 | } |
| 592 |
| 593 | std::uint16_t NetworkSocketSOCKS5Proxy::GetConnectedPort() |
| 594 | { |
| 595 | return m_connectedPort; |
| 596 | } |
| 597 |
| 598 | bool NetworkSocketSOCKS5Proxy::OnReadyToSend() |
| 599 | { |
| 600 | if (m_state == ConnectionState::INITIAL) |
| 601 | { |
| 602 | BufferOutputStream p(16); |
| 603 | p.WriteUInt8(std::uint8_t{5}); // VER |
| 604 | if (!m_username.empty()) |
| 605 | { |
| 606 | p.WriteUInt8(std::uint8_t{2}); // NMETHODS |
| 607 | p.WriteUInt8(std::uint8_t{0}); // no auth |
| 608 | p.WriteUInt8(std::uint8_t{2}); // user/pass |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | p.WriteUInt8(std::uint8_t{1}); // NMETHODS |
| 613 | p.WriteUInt8(std::uint8_t{0}); // no auth |
| 614 | } |
| 615 | m_tcp->Send(NetworkPacket { |
| 616 | Buffer(std::move(p)), |
| 617 | NetworkAddress::Empty(), |
| 618 | 0, |
| 619 | NetworkProtocol::TCP}); |
| 620 | m_state = ConnectionState::WAITING_FOR_AUTH_METHOD; |
| 621 | return false; |
| 622 | } |
| 623 | return m_udp ? m_udp->OnReadyToSend() : m_tcp->OnReadyToSend(); |
| 624 | } |
| 625 |
| 626 | bool NetworkSocketSOCKS5Proxy::OnReadyToReceive() |
| 627 | { |
| 628 | std::uint8_t buf[1024]; |
| 629 | switch (m_state) |
| 630 | { |
| 631 | case ConnectionState::INITIAL: |
| 632 | LOGE("NetworkSocketSOCKS5Proxy: connection state Initial"); |
| 633 | break; |
| 634 | case ConnectionState::CONNECTED: |
| 635 | LOGE("NetworkSocketSOCKS5Proxy: connection state Connected"); |
| 636 | break; |
| 637 | case ConnectionState::WAITING_FOR_AUTH_METHOD: |
| 638 | { |
| 639 | std::size_t l = m_tcp->Receive(buf, sizeof(buf)); |
| 640 | if (l < 2 || m_tcp->IsFailed()) |
| 641 | { |
| 642 | m_failed = true; |
| 643 | return false; |
| 644 | } |
| 645 | BufferInputStream in(buf, l); |
| 646 | std::uint8_t ver = in.ReadUInt8(); |
| 647 | std::uint8_t chosenMethod = in.ReadUInt8(); |
| 648 | LOGV("socks5: VER=%02X, METHOD=%02X", ver, chosenMethod); |
| 649 | if (ver != 5) |
| 650 | { |
| 651 | LOGW("socks5: incorrect VER in response"); |
| 652 | m_failed = true; |
| 653 | return false; |
| 654 | } |
| 655 | if (chosenMethod == 0) |
| 656 | { |
| 657 | // connected, no further auth needed |
| 658 | SendConnectionCommand(); |
| 659 | } |
| 660 | else if (chosenMethod == 2 && !m_username.empty()) |
| 661 | { |
| 662 | BufferOutputStream p(512); |
| 663 | p.WriteUInt8(std::uint8_t{1}); // VER |
| 664 | p.WriteUInt8(static_cast<std::uint8_t>(m_username.length() > 255 ? 255 : m_username.length())); // ULEN |
| 665 | p.WriteBytes(reinterpret_cast<const std::uint8_t*>(m_username.c_str()), m_username.length() > 255 ? 255 : m_username.length()); // UNAME |
| 666 | p.WriteUInt8(static_cast<std::uint8_t>(m_password.length() > 255 ? 255 : m_password.length())); // PLEN |
| 667 | p.WriteBytes(reinterpret_cast<const std::uint8_t*>(m_password.c_str()), m_password.length() > 255 ? 255 : m_password.length()); // PASSWD |
| 668 | m_tcp->Send(NetworkPacket |
| 669 | { |
| 670 | Buffer(std::move(p)), |
| 671 | NetworkAddress::Empty(), |
| 672 | 0, |
| 673 | NetworkProtocol::TCP |
| 674 | }); |
| 675 | m_state = ConnectionState::WAITING_FOR_AUTH_RESULT; |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | LOGW("socks5: unsupported auth method"); |
| 680 | m_failed = true; |
| 681 | return false; |
| 682 | } |
| 683 | return false; |
| 684 | } |
| 685 | case ConnectionState::WAITING_FOR_AUTH_RESULT: |
| 686 | { |
| 687 | std::size_t l = m_tcp->Receive(buf, sizeof(buf)); |
| 688 | if (l < 2 || m_tcp->IsFailed()) |
| 689 | { |
| 690 | m_failed = true; |
| 691 | return false; |
| 692 | } |
| 693 | BufferInputStream in(buf, l); |
| 694 | std::uint8_t ver = in.ReadUInt8(); |
| 695 | std::uint8_t status = in.ReadUInt8(); |
| 696 | LOGV("socks5: auth response VER=%02X, STATUS=%02X", ver, status); |
| 697 | if (ver != 1) |
| 698 | { |
| 699 | LOGW("socks5: auth response VER is incorrect"); |
| 700 | m_failed = true; |
| 701 | return false; |
| 702 | } |
| 703 | if (status != 0) |
| 704 | { |
| 705 | LOGW("socks5: username/password auth failed"); |
| 706 | m_failed = true; |
| 707 | return false; |
| 708 | } |
| 709 | LOGV("socks5: authentication succeeded"); |
| 710 | SendConnectionCommand(); |
| 711 | return false; |
| 712 | } |
| 713 | case ConnectionState::WAITING_FOR_COMMAND_RESULT: |
| 714 | { |
| 715 | std::size_t l = m_tcp->Receive(buf, sizeof(buf)); |
| 716 | switch (m_protocol) |
| 717 | { |
| 718 | case NetworkProtocol::TCP: |
| 719 | { |
| 720 | if (l < 2 || m_tcp->IsFailed()) |
| 721 | { |
| 722 | LOGW("socks5: connect failed") |
| 723 | m_failed = true; |
| 724 | return false; |
| 725 | } |
| 726 | BufferInputStream in(buf, l); |
| 727 | std::uint8_t ver = in.ReadUInt8(); |
| 728 | if (ver != 5) |
| 729 | { |
| 730 | LOGW("socks5: connect: wrong ver in response"); |
| 731 | m_failed = true; |
| 732 | return false; |
| 733 | } |
| 734 | std::uint8_t rep = in.ReadUInt8(); |
| 735 | if (rep != 0) |
| 736 | { |
| 737 | LOGW("socks5: connect: failed with error %02X", rep); |
| 738 | m_failed = true; |
| 739 | return false; |
| 740 | } |
| 741 | LOGV("socks5: connect succeeded"); |
| 742 | m_state = ConnectionState::CONNECTED; |
| 743 | m_tcp = new NetworkSocketTCPObfuscated(m_tcp); |
| 744 | m_readyToSend = true; |
| 745 | return m_tcp->OnReadyToSend(); |
| 746 | } |
| 747 | case NetworkProtocol::UDP: |
| 748 | { |
| 749 | if (l < 2 || m_tcp->IsFailed()) |
| 750 | { |
| 751 | LOGW("socks5: udp associate failed"); |
| 752 | m_failed = true; |
| 753 | return false; |
| 754 | } |
| 755 | try |
| 756 | { |
| 757 | BufferInputStream in(buf, l); |
| 758 | std::uint8_t ver = in.ReadUInt8(); |
| 759 | std::uint8_t rep = in.ReadUInt8(); |
| 760 | if (ver != 5) |
| 761 | { |
| 762 | LOGW("socks5: udp associate: wrong ver in response"); |
| 763 | m_failed = true; |
| 764 | return false; |
| 765 | } |
| 766 | if (rep != 0) |
| 767 | { |
| 768 | LOGW("socks5: udp associate failed with error %02X", rep); |
| 769 | m_failed = true; |
| 770 | return false; |
| 771 | } |
| 772 | in.ReadUInt8(); // RSV |
| 773 | std::uint8_t atyp = in.ReadUInt8(); |
| 774 | if (atyp == 1) |
| 775 | { |
| 776 | std::uint32_t addr = in.ReadUInt32(); |
| 777 | m_connectedAddress = NetworkAddress::IPv4(addr); |
| 778 | } |
| 779 | else if (atyp == 3) |
| 780 | { |
| 781 | std::uint8_t len = in.ReadUInt8(); |
| 782 | char domain[256]; |
| 783 | std::memset(domain, 0, sizeof(domain)); |
| 784 | in.ReadBytes(reinterpret_cast<std::uint8_t*>(domain), len); |
| 785 | LOGD("address type is domain, address=%s", domain); |
| 786 | m_connectedAddress = ResolveDomainName(std::string(domain)); |
| 787 | if (m_connectedAddress.IsEmpty()) |
| 788 | { |
| 789 | LOGW("socks5: failed to resolve domain name '%s'", domain); |
| 790 | m_failed = true; |
| 791 | return false; |
| 792 | } |
| 793 | } |
| 794 | else if (atyp == 4) |
| 795 | { |
| 796 | std::uint8_t addr[16]; |
| 797 | in.ReadBytes(addr, 16); |
| 798 | m_connectedAddress = NetworkAddress::IPv6(addr); |
| 799 | } |
| 800 | else |
| 801 | { |
| 802 | LOGW("socks5: unknown address type %d", atyp); |
| 803 | m_failed = true; |
| 804 | return false; |
| 805 | } |
| 806 | m_connectedPort = ntohs(in.ReadUInt16()); |
| 807 | m_state = ConnectionState::CONNECTED; |
| 808 | m_readyToSend = true; |
| 809 | LOGV("socks5: udp associate successful, given endpoint %s:%d", m_connectedAddress.ToString().c_str(), m_connectedPort); |
| 810 | } |
| 811 | catch (const std::out_of_range& exception) |
| 812 | { |
| 813 | LOGW("socks5: udp associate response parse failed.\nwhat():\n%s", exception.what()); |
| 814 | m_failed = true; |
| 815 | } |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | return m_udp ? m_udp->OnReadyToReceive() : m_tcp->OnReadyToReceive(); |
| 822 | } |
| 823 |
| 824 | void NetworkSocketSOCKS5Proxy::SendConnectionCommand() |
| 825 | { |
| 826 | BufferOutputStream out(1024); |
| 827 | switch (m_protocol) |
| 828 | { |
| 829 | case NetworkProtocol::TCP: |
| 830 | out.WriteUInt8(5); // VER |
| 831 | out.WriteUInt8(1); // CMD (CONNECT) |
| 832 | out.WriteUInt8(0); // RSV |
| 833 | if (!m_connectedAddress.isIPv6) |
| 834 | { |
| 835 | out.WriteUInt8(1); // ATYP (IPv4) |
| 836 | out.WriteInt32(static_cast<std::int32_t>(m_connectedAddress.addr.ipv4)); |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | out.WriteUInt8(4); // ATYP (IPv6) |
| 841 | out.WriteBytes(reinterpret_cast<std::uint8_t*>(m_connectedAddress.addr.ipv6), 16); |
| 842 | } |
| 843 | out.WriteUInt16(htons(m_connectedPort)); // DST.PORT |
| 844 | break; |
| 845 | case NetworkProtocol::UDP: |
| 846 | LOGV("Sending udp associate"); |
| 847 | out.WriteUInt8(5); // VER |
| 848 | out.WriteUInt8(3); // CMD (UDP ASSOCIATE) |
| 849 | out.WriteUInt8(0); // RSV |
| 850 | out.WriteUInt8(1); // ATYP (IPv4) |
| 851 | out.WriteInt32(0); // DST.ADDR |
| 852 | out.WriteInt16(0); // DST.PORT |
| 853 | break; |
| 854 | } |
| 855 | m_tcp->Send(NetworkPacket |
| 856 | { |
| 857 | Buffer(std::move(out)), |
| 858 | NetworkAddress::Empty(), |
| 859 | 0, |
| 860 | NetworkProtocol::TCP |
| 861 | }); |
| 862 | m_state = ConnectionState::WAITING_FOR_COMMAND_RESULT; |
| 863 | } |
| 864 |
| 865 | bool NetworkSocketSOCKS5Proxy::NeedSelectForSending() const |
| 866 | { |
| 867 | return m_state == ConnectionState::INITIAL || m_state == ConnectionState::CONNECTED; |
| 868 | } |
| 869 |