OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include <winsock2.h> | 21 #include <winsock2.h> |
22 #include <ws2tcpip.h> | 22 #include <ws2tcpip.h> |
23 #define SECURITY_WIN32 | 23 #define SECURITY_WIN32 |
24 #include <security.h> | 24 #include <security.h> |
25 #endif | 25 #endif |
26 | 26 |
27 #include <algorithm> | 27 #include <algorithm> |
28 | 28 |
29 #include "webrtc/base/bytebuffer.h" | 29 #include "webrtc/base/bytebuffer.h" |
30 #include "webrtc/base/checks.h" | 30 #include "webrtc/base/checks.h" |
| 31 #include "webrtc/base/httpcommon.h" |
31 #include "webrtc/base/logging.h" | 32 #include "webrtc/base/logging.h" |
32 #include "webrtc/base/socketadapters.h" | 33 #include "webrtc/base/socketadapters.h" |
33 #include "webrtc/base/stringencode.h" | 34 #include "webrtc/base/stringencode.h" |
34 #include "webrtc/base/stringutils.h" | 35 #include "webrtc/base/stringutils.h" |
35 | 36 |
36 namespace rtc { | 37 namespace rtc { |
37 | 38 |
38 BufferedReadAdapter::BufferedReadAdapter(AsyncSocket* socket, size_t size) | 39 BufferedReadAdapter::BufferedReadAdapter(AsyncSocket* socket, size_t size) |
39 : AsyncSocketAdapter(socket), buffer_size_(size), | 40 : AsyncSocketAdapter(socket), buffer_size_(size), |
40 data_len_(0), buffering_(false) { | 41 data_len_(0), buffering_(false) { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // Clients should not send more data until the handshake is completed. | 235 // Clients should not send more data until the handshake is completed. |
235 RTC_DCHECK(*len == 0); | 236 RTC_DCHECK(*len == 0); |
236 | 237 |
237 // Send a server hello back to the client. | 238 // Send a server hello back to the client. |
238 DirectSend(kSslServerHello, sizeof(kSslServerHello)); | 239 DirectSend(kSslServerHello, sizeof(kSslServerHello)); |
239 | 240 |
240 // Handshake completed for us, redirect input to our parent. | 241 // Handshake completed for us, redirect input to our parent. |
241 BufferInput(false); | 242 BufferInput(false); |
242 } | 243 } |
243 | 244 |
| 245 /////////////////////////////////////////////////////////////////////////////// |
| 246 |
| 247 AsyncHttpsProxySocket::AsyncHttpsProxySocket(AsyncSocket* socket, |
| 248 const std::string& user_agent, |
| 249 const SocketAddress& proxy, |
| 250 const std::string& username, |
| 251 const CryptString& password) |
| 252 : BufferedReadAdapter(socket, 1024), proxy_(proxy), agent_(user_agent), |
| 253 user_(username), pass_(password), force_connect_(false), state_(PS_ERROR), |
| 254 context_(0) { |
| 255 } |
| 256 |
| 257 AsyncHttpsProxySocket::~AsyncHttpsProxySocket() { |
| 258 delete context_; |
| 259 } |
| 260 |
| 261 int AsyncHttpsProxySocket::Connect(const SocketAddress& addr) { |
| 262 int ret; |
| 263 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::Connect(" |
| 264 << proxy_.ToSensitiveString() << ")"; |
| 265 dest_ = addr; |
| 266 state_ = PS_INIT; |
| 267 if (ShouldIssueConnect()) { |
| 268 BufferInput(true); |
| 269 } |
| 270 ret = BufferedReadAdapter::Connect(proxy_); |
| 271 // TODO: Set state_ appropriately if Connect fails. |
| 272 return ret; |
| 273 } |
| 274 |
| 275 SocketAddress AsyncHttpsProxySocket::GetRemoteAddress() const { |
| 276 return dest_; |
| 277 } |
| 278 |
| 279 int AsyncHttpsProxySocket::Close() { |
| 280 headers_.clear(); |
| 281 state_ = PS_ERROR; |
| 282 dest_.Clear(); |
| 283 delete context_; |
| 284 context_ = nullptr; |
| 285 return BufferedReadAdapter::Close(); |
| 286 } |
| 287 |
| 288 Socket::ConnState AsyncHttpsProxySocket::GetState() const { |
| 289 if (state_ < PS_TUNNEL) { |
| 290 return CS_CONNECTING; |
| 291 } else if (state_ == PS_TUNNEL) { |
| 292 return CS_CONNECTED; |
| 293 } else { |
| 294 return CS_CLOSED; |
| 295 } |
| 296 } |
| 297 |
| 298 void AsyncHttpsProxySocket::OnConnectEvent(AsyncSocket * socket) { |
| 299 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnConnectEvent"; |
| 300 if (!ShouldIssueConnect()) { |
| 301 state_ = PS_TUNNEL; |
| 302 BufferedReadAdapter::OnConnectEvent(socket); |
| 303 return; |
| 304 } |
| 305 SendRequest(); |
| 306 } |
| 307 |
| 308 void AsyncHttpsProxySocket::OnCloseEvent(AsyncSocket * socket, int err) { |
| 309 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnCloseEvent(" << err << ")"; |
| 310 if ((state_ == PS_WAIT_CLOSE) && (err == 0)) { |
| 311 state_ = PS_ERROR; |
| 312 Connect(dest_); |
| 313 } else { |
| 314 BufferedReadAdapter::OnCloseEvent(socket, err); |
| 315 } |
| 316 } |
| 317 |
| 318 void AsyncHttpsProxySocket::ProcessInput(char* data, size_t* len) { |
| 319 size_t start = 0; |
| 320 for (size_t pos = start; state_ < PS_TUNNEL && pos < *len;) { |
| 321 if (state_ == PS_SKIP_BODY) { |
| 322 size_t consume = std::min(*len - pos, content_length_); |
| 323 pos += consume; |
| 324 start = pos; |
| 325 content_length_ -= consume; |
| 326 if (content_length_ == 0) { |
| 327 EndResponse(); |
| 328 } |
| 329 continue; |
| 330 } |
| 331 |
| 332 if (data[pos++] != '\n') |
| 333 continue; |
| 334 |
| 335 size_t len = pos - start - 1; |
| 336 if ((len > 0) && (data[start + len - 1] == '\r')) |
| 337 --len; |
| 338 |
| 339 data[start + len] = 0; |
| 340 ProcessLine(data + start, len); |
| 341 start = pos; |
| 342 } |
| 343 |
| 344 *len -= start; |
| 345 if (*len > 0) { |
| 346 memmove(data, data + start, *len); |
| 347 } |
| 348 |
| 349 if (state_ != PS_TUNNEL) |
| 350 return; |
| 351 |
| 352 bool remainder = (*len > 0); |
| 353 BufferInput(false); |
| 354 SignalConnectEvent(this); |
| 355 |
| 356 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 357 if (remainder) |
| 358 SignalReadEvent(this); // TODO: signal this?? |
| 359 } |
| 360 |
| 361 bool AsyncHttpsProxySocket::ShouldIssueConnect() const { |
| 362 // TODO: Think about whether a more sophisticated test |
| 363 // than dest port == 80 is needed. |
| 364 return force_connect_ || (dest_.port() != 80); |
| 365 } |
| 366 |
| 367 void AsyncHttpsProxySocket::SendRequest() { |
| 368 std::stringstream ss; |
| 369 ss << "CONNECT " << dest_.ToString() << " HTTP/1.0\r\n"; |
| 370 ss << "User-Agent: " << agent_ << "\r\n"; |
| 371 ss << "Host: " << dest_.HostAsURIString() << "\r\n"; |
| 372 ss << "Content-Length: 0\r\n"; |
| 373 ss << "Proxy-Connection: Keep-Alive\r\n"; |
| 374 ss << headers_; |
| 375 ss << "\r\n"; |
| 376 std::string str = ss.str(); |
| 377 DirectSend(str.c_str(), str.size()); |
| 378 state_ = PS_LEADER; |
| 379 expect_close_ = true; |
| 380 content_length_ = 0; |
| 381 headers_.clear(); |
| 382 |
| 383 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket >> " << str; |
| 384 } |
| 385 |
| 386 void AsyncHttpsProxySocket::ProcessLine(char * data, size_t len) { |
| 387 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket << " << data; |
| 388 |
| 389 if (len == 0) { |
| 390 if (state_ == PS_TUNNEL_HEADERS) { |
| 391 state_ = PS_TUNNEL; |
| 392 } else if (state_ == PS_ERROR_HEADERS) { |
| 393 Error(defer_error_); |
| 394 return; |
| 395 } else if (state_ == PS_SKIP_HEADERS) { |
| 396 if (content_length_) { |
| 397 state_ = PS_SKIP_BODY; |
| 398 } else { |
| 399 EndResponse(); |
| 400 return; |
| 401 } |
| 402 } else { |
| 403 static bool report = false; |
| 404 if (!unknown_mechanisms_.empty() && !report) { |
| 405 report = true; |
| 406 std::string msg( |
| 407 "Unable to connect to the Google Talk service due to an incompatibilit
y " |
| 408 "with your proxy.\r\nPlease help us resolve this issue by submitting t
he " |
| 409 "following information to us using our technical issue submission form
" |
| 410 "at:\r\n\r\n" |
| 411 "http://www.google.com/support/talk/bin/request.py\r\n\r\n" |
| 412 "We apologize for the inconvenience.\r\n\r\n" |
| 413 "Information to submit to Google: " |
| 414 ); |
| 415 //std::string msg("Please report the following information to foo@bar.co
m:\r\nUnknown methods: "); |
| 416 msg.append(unknown_mechanisms_); |
| 417 #if defined(WEBRTC_WIN) |
| 418 MessageBoxA(0, msg.c_str(), "Oops!", MB_OK); |
| 419 #endif |
| 420 #if defined(WEBRTC_POSIX) |
| 421 // TODO: Raise a signal so the UI can be separated. |
| 422 LOG(LS_ERROR) << "Oops!\n\n" << msg; |
| 423 #endif |
| 424 } |
| 425 // Unexpected end of headers |
| 426 Error(0); |
| 427 return; |
| 428 } |
| 429 } else if (state_ == PS_LEADER) { |
| 430 unsigned int code; |
| 431 if (sscanf(data, "HTTP/%*u.%*u %u", &code) != 1) { |
| 432 Error(0); |
| 433 return; |
| 434 } |
| 435 switch (code) { |
| 436 case 200: |
| 437 // connection good! |
| 438 state_ = PS_TUNNEL_HEADERS; |
| 439 return; |
| 440 #if defined(HTTP_STATUS_PROXY_AUTH_REQ) && (HTTP_STATUS_PROXY_AUTH_REQ != 407) |
| 441 #error Wrong code for HTTP_STATUS_PROXY_AUTH_REQ |
| 442 #endif |
| 443 case 407: // HTTP_STATUS_PROXY_AUTH_REQ |
| 444 state_ = PS_AUTHENTICATE; |
| 445 return; |
| 446 default: |
| 447 defer_error_ = 0; |
| 448 state_ = PS_ERROR_HEADERS; |
| 449 return; |
| 450 } |
| 451 } else if ((state_ == PS_AUTHENTICATE) |
| 452 && (_strnicmp(data, "Proxy-Authenticate:", 19) == 0)) { |
| 453 std::string response, auth_method; |
| 454 switch (HttpAuthenticate(data + 19, len - 19, |
| 455 proxy_, "CONNECT", "/", |
| 456 user_, pass_, context_, response, auth_method)) { |
| 457 case HAR_IGNORE: |
| 458 LOG(LS_VERBOSE) << "Ignoring Proxy-Authenticate: " << auth_method; |
| 459 if (!unknown_mechanisms_.empty()) |
| 460 unknown_mechanisms_.append(", "); |
| 461 unknown_mechanisms_.append(auth_method); |
| 462 break; |
| 463 case HAR_RESPONSE: |
| 464 headers_ = "Proxy-Authorization: "; |
| 465 headers_.append(response); |
| 466 headers_.append("\r\n"); |
| 467 state_ = PS_SKIP_HEADERS; |
| 468 unknown_mechanisms_.clear(); |
| 469 break; |
| 470 case HAR_CREDENTIALS: |
| 471 defer_error_ = SOCKET_EACCES; |
| 472 state_ = PS_ERROR_HEADERS; |
| 473 unknown_mechanisms_.clear(); |
| 474 break; |
| 475 case HAR_ERROR: |
| 476 defer_error_ = 0; |
| 477 state_ = PS_ERROR_HEADERS; |
| 478 unknown_mechanisms_.clear(); |
| 479 break; |
| 480 } |
| 481 } else if (_strnicmp(data, "Content-Length:", 15) == 0) { |
| 482 content_length_ = strtoul(data + 15, 0, 0); |
| 483 } else if (_strnicmp(data, "Proxy-Connection: Keep-Alive", 28) == 0) { |
| 484 expect_close_ = false; |
| 485 /* |
| 486 } else if (_strnicmp(data, "Connection: close", 17) == 0) { |
| 487 expect_close_ = true; |
| 488 */ |
| 489 } |
| 490 } |
| 491 |
| 492 void AsyncHttpsProxySocket::EndResponse() { |
| 493 if (!expect_close_) { |
| 494 SendRequest(); |
| 495 return; |
| 496 } |
| 497 |
| 498 // No point in waiting for the server to close... let's close now |
| 499 // TODO: Refactor out PS_WAIT_CLOSE |
| 500 state_ = PS_WAIT_CLOSE; |
| 501 BufferedReadAdapter::Close(); |
| 502 OnCloseEvent(this, 0); |
| 503 } |
| 504 |
| 505 void AsyncHttpsProxySocket::Error(int error) { |
| 506 BufferInput(false); |
| 507 Close(); |
| 508 SetError(error); |
| 509 SignalCloseEvent(this, error); |
| 510 } |
| 511 |
| 512 /////////////////////////////////////////////////////////////////////////////// |
| 513 |
| 514 AsyncSocksProxySocket::AsyncSocksProxySocket(AsyncSocket* socket, |
| 515 const SocketAddress& proxy, |
| 516 const std::string& username, |
| 517 const CryptString& password) |
| 518 : BufferedReadAdapter(socket, 1024), state_(SS_ERROR), proxy_(proxy), |
| 519 user_(username), pass_(password) { |
| 520 } |
| 521 |
| 522 AsyncSocksProxySocket::~AsyncSocksProxySocket() = default; |
| 523 |
| 524 int AsyncSocksProxySocket::Connect(const SocketAddress& addr) { |
| 525 int ret; |
| 526 dest_ = addr; |
| 527 state_ = SS_INIT; |
| 528 BufferInput(true); |
| 529 ret = BufferedReadAdapter::Connect(proxy_); |
| 530 // TODO: Set state_ appropriately if Connect fails. |
| 531 return ret; |
| 532 } |
| 533 |
| 534 SocketAddress AsyncSocksProxySocket::GetRemoteAddress() const { |
| 535 return dest_; |
| 536 } |
| 537 |
| 538 int AsyncSocksProxySocket::Close() { |
| 539 state_ = SS_ERROR; |
| 540 dest_.Clear(); |
| 541 return BufferedReadAdapter::Close(); |
| 542 } |
| 543 |
| 544 Socket::ConnState AsyncSocksProxySocket::GetState() const { |
| 545 if (state_ < SS_TUNNEL) { |
| 546 return CS_CONNECTING; |
| 547 } else if (state_ == SS_TUNNEL) { |
| 548 return CS_CONNECTED; |
| 549 } else { |
| 550 return CS_CLOSED; |
| 551 } |
| 552 } |
| 553 |
| 554 void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) { |
| 555 SendHello(); |
| 556 } |
| 557 |
| 558 void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { |
| 559 RTC_DCHECK(state_ < SS_TUNNEL); |
| 560 |
| 561 ByteBufferReader response(data, *len); |
| 562 |
| 563 if (state_ == SS_HELLO) { |
| 564 uint8_t ver, method; |
| 565 if (!response.ReadUInt8(&ver) || |
| 566 !response.ReadUInt8(&method)) |
| 567 return; |
| 568 |
| 569 if (ver != 5) { |
| 570 Error(0); |
| 571 return; |
| 572 } |
| 573 |
| 574 if (method == 0) { |
| 575 SendConnect(); |
| 576 } else if (method == 2) { |
| 577 SendAuth(); |
| 578 } else { |
| 579 Error(0); |
| 580 return; |
| 581 } |
| 582 } else if (state_ == SS_AUTH) { |
| 583 uint8_t ver, status; |
| 584 if (!response.ReadUInt8(&ver) || |
| 585 !response.ReadUInt8(&status)) |
| 586 return; |
| 587 |
| 588 if ((ver != 1) || (status != 0)) { |
| 589 Error(SOCKET_EACCES); |
| 590 return; |
| 591 } |
| 592 |
| 593 SendConnect(); |
| 594 } else if (state_ == SS_CONNECT) { |
| 595 uint8_t ver, rep, rsv, atyp; |
| 596 if (!response.ReadUInt8(&ver) || |
| 597 !response.ReadUInt8(&rep) || |
| 598 !response.ReadUInt8(&rsv) || |
| 599 !response.ReadUInt8(&atyp)) |
| 600 return; |
| 601 |
| 602 if ((ver != 5) || (rep != 0)) { |
| 603 Error(0); |
| 604 return; |
| 605 } |
| 606 |
| 607 uint16_t port; |
| 608 if (atyp == 1) { |
| 609 uint32_t addr; |
| 610 if (!response.ReadUInt32(&addr) || |
| 611 !response.ReadUInt16(&port)) |
| 612 return; |
| 613 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
| 614 } else if (atyp == 3) { |
| 615 uint8_t len; |
| 616 std::string addr; |
| 617 if (!response.ReadUInt8(&len) || |
| 618 !response.ReadString(&addr, len) || |
| 619 !response.ReadUInt16(&port)) |
| 620 return; |
| 621 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
| 622 } else if (atyp == 4) { |
| 623 std::string addr; |
| 624 if (!response.ReadString(&addr, 16) || |
| 625 !response.ReadUInt16(&port)) |
| 626 return; |
| 627 LOG(LS_VERBOSE) << "Bound on <IPV6>:" << port; |
| 628 } else { |
| 629 Error(0); |
| 630 return; |
| 631 } |
| 632 |
| 633 state_ = SS_TUNNEL; |
| 634 } |
| 635 |
| 636 // Consume parsed data |
| 637 *len = response.Length(); |
| 638 memmove(data, response.Data(), *len); |
| 639 |
| 640 if (state_ != SS_TUNNEL) |
| 641 return; |
| 642 |
| 643 bool remainder = (*len > 0); |
| 644 BufferInput(false); |
| 645 SignalConnectEvent(this); |
| 646 |
| 647 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 648 if (remainder) |
| 649 SignalReadEvent(this); // TODO: signal this?? |
| 650 } |
| 651 |
| 652 void AsyncSocksProxySocket::SendHello() { |
| 653 ByteBufferWriter request; |
| 654 request.WriteUInt8(5); // Socks Version |
| 655 if (user_.empty()) { |
| 656 request.WriteUInt8(1); // Authentication Mechanisms |
| 657 request.WriteUInt8(0); // No authentication |
| 658 } else { |
| 659 request.WriteUInt8(2); // Authentication Mechanisms |
| 660 request.WriteUInt8(0); // No authentication |
| 661 request.WriteUInt8(2); // Username/Password |
| 662 } |
| 663 DirectSend(request.Data(), request.Length()); |
| 664 state_ = SS_HELLO; |
| 665 } |
| 666 |
| 667 void AsyncSocksProxySocket::SendAuth() { |
| 668 ByteBufferWriter request; |
| 669 request.WriteUInt8(1); // Negotiation Version |
| 670 request.WriteUInt8(static_cast<uint8_t>(user_.size())); |
| 671 request.WriteString(user_); // Username |
| 672 request.WriteUInt8(static_cast<uint8_t>(pass_.GetLength())); |
| 673 size_t len = pass_.GetLength() + 1; |
| 674 char * sensitive = new char[len]; |
| 675 pass_.CopyTo(sensitive, true); |
| 676 request.WriteString(sensitive); // Password |
| 677 memset(sensitive, 0, len); |
| 678 delete [] sensitive; |
| 679 DirectSend(request.Data(), request.Length()); |
| 680 state_ = SS_AUTH; |
| 681 } |
| 682 |
| 683 void AsyncSocksProxySocket::SendConnect() { |
| 684 ByteBufferWriter request; |
| 685 request.WriteUInt8(5); // Socks Version |
| 686 request.WriteUInt8(1); // CONNECT |
| 687 request.WriteUInt8(0); // Reserved |
| 688 if (dest_.IsUnresolvedIP()) { |
| 689 std::string hostname = dest_.hostname(); |
| 690 request.WriteUInt8(3); // DOMAINNAME |
| 691 request.WriteUInt8(static_cast<uint8_t>(hostname.size())); |
| 692 request.WriteString(hostname); // Destination Hostname |
| 693 } else { |
| 694 request.WriteUInt8(1); // IPV4 |
| 695 request.WriteUInt32(dest_.ip()); // Destination IP |
| 696 } |
| 697 request.WriteUInt16(dest_.port()); // Destination Port |
| 698 DirectSend(request.Data(), request.Length()); |
| 699 state_ = SS_CONNECT; |
| 700 } |
| 701 |
| 702 void AsyncSocksProxySocket::Error(int error) { |
| 703 state_ = SS_ERROR; |
| 704 BufferInput(false); |
| 705 Close(); |
| 706 SetError(SOCKET_EACCES); |
| 707 SignalCloseEvent(this, error); |
| 708 } |
| 709 |
| 710 AsyncSocksProxyServerSocket::AsyncSocksProxyServerSocket(AsyncSocket* socket) |
| 711 : AsyncProxyServerSocket(socket, kBufferSize), state_(SS_HELLO) { |
| 712 BufferInput(true); |
| 713 } |
| 714 |
| 715 void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) { |
| 716 // TODO: See if the whole message has arrived |
| 717 RTC_DCHECK(state_ < SS_CONNECT_PENDING); |
| 718 |
| 719 ByteBufferReader response(data, *len); |
| 720 if (state_ == SS_HELLO) { |
| 721 HandleHello(&response); |
| 722 } else if (state_ == SS_AUTH) { |
| 723 HandleAuth(&response); |
| 724 } else if (state_ == SS_CONNECT) { |
| 725 HandleConnect(&response); |
| 726 } |
| 727 |
| 728 // Consume parsed data |
| 729 *len = response.Length(); |
| 730 memmove(data, response.Data(), *len); |
| 731 } |
| 732 |
| 733 void AsyncSocksProxyServerSocket::DirectSend(const ByteBufferWriter& buf) { |
| 734 BufferedReadAdapter::DirectSend(buf.Data(), buf.Length()); |
| 735 } |
| 736 |
| 737 void AsyncSocksProxyServerSocket::HandleHello(ByteBufferReader* request) { |
| 738 uint8_t ver, num_methods; |
| 739 if (!request->ReadUInt8(&ver) || |
| 740 !request->ReadUInt8(&num_methods)) { |
| 741 Error(0); |
| 742 return; |
| 743 } |
| 744 |
| 745 if (ver != 5) { |
| 746 Error(0); |
| 747 return; |
| 748 } |
| 749 |
| 750 // Handle either no-auth (0) or user/pass auth (2) |
| 751 uint8_t method = 0xFF; |
| 752 if (num_methods > 0 && !request->ReadUInt8(&method)) { |
| 753 Error(0); |
| 754 return; |
| 755 } |
| 756 |
| 757 // TODO: Ask the server which method to use. |
| 758 SendHelloReply(method); |
| 759 if (method == 0) { |
| 760 state_ = SS_CONNECT; |
| 761 } else if (method == 2) { |
| 762 state_ = SS_AUTH; |
| 763 } else { |
| 764 state_ = SS_ERROR; |
| 765 } |
| 766 } |
| 767 |
| 768 void AsyncSocksProxyServerSocket::SendHelloReply(uint8_t method) { |
| 769 ByteBufferWriter response; |
| 770 response.WriteUInt8(5); // Socks Version |
| 771 response.WriteUInt8(method); // Auth method |
| 772 DirectSend(response); |
| 773 } |
| 774 |
| 775 void AsyncSocksProxyServerSocket::HandleAuth(ByteBufferReader* request) { |
| 776 uint8_t ver, user_len, pass_len; |
| 777 std::string user, pass; |
| 778 if (!request->ReadUInt8(&ver) || |
| 779 !request->ReadUInt8(&user_len) || |
| 780 !request->ReadString(&user, user_len) || |
| 781 !request->ReadUInt8(&pass_len) || |
| 782 !request->ReadString(&pass, pass_len)) { |
| 783 Error(0); |
| 784 return; |
| 785 } |
| 786 |
| 787 // TODO: Allow for checking of credentials. |
| 788 SendAuthReply(0); |
| 789 state_ = SS_CONNECT; |
| 790 } |
| 791 |
| 792 void AsyncSocksProxyServerSocket::SendAuthReply(uint8_t result) { |
| 793 ByteBufferWriter response; |
| 794 response.WriteUInt8(1); // Negotiation Version |
| 795 response.WriteUInt8(result); |
| 796 DirectSend(response); |
| 797 } |
| 798 |
| 799 void AsyncSocksProxyServerSocket::HandleConnect(ByteBufferReader* request) { |
| 800 uint8_t ver, command, reserved, addr_type; |
| 801 uint32_t ip; |
| 802 uint16_t port; |
| 803 if (!request->ReadUInt8(&ver) || |
| 804 !request->ReadUInt8(&command) || |
| 805 !request->ReadUInt8(&reserved) || |
| 806 !request->ReadUInt8(&addr_type) || |
| 807 !request->ReadUInt32(&ip) || |
| 808 !request->ReadUInt16(&port)) { |
| 809 Error(0); |
| 810 return; |
| 811 } |
| 812 |
| 813 if (ver != 5 || command != 1 || |
| 814 reserved != 0 || addr_type != 1) { |
| 815 Error(0); |
| 816 return; |
| 817 } |
| 818 |
| 819 SignalConnectRequest(this, SocketAddress(ip, port)); |
| 820 state_ = SS_CONNECT_PENDING; |
| 821 } |
| 822 |
| 823 void AsyncSocksProxyServerSocket::SendConnectResult(int result, |
| 824 const SocketAddress& addr) { |
| 825 if (state_ != SS_CONNECT_PENDING) |
| 826 return; |
| 827 |
| 828 ByteBufferWriter response; |
| 829 response.WriteUInt8(5); // Socks version |
| 830 response.WriteUInt8((result != 0)); // 0x01 is generic error |
| 831 response.WriteUInt8(0); // reserved |
| 832 response.WriteUInt8(1); // IPv4 address |
| 833 response.WriteUInt32(addr.ip()); |
| 834 response.WriteUInt16(addr.port()); |
| 835 DirectSend(response); |
| 836 BufferInput(false); |
| 837 state_ = SS_TUNNEL; |
| 838 } |
| 839 |
| 840 void AsyncSocksProxyServerSocket::Error(int error) { |
| 841 state_ = SS_ERROR; |
| 842 BufferInput(false); |
| 843 Close(); |
| 844 SetError(SOCKET_EACCES); |
| 845 SignalCloseEvent(this, error); |
| 846 } |
| 847 |
244 } // namespace rtc | 848 } // namespace rtc |
OLD | NEW |