You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/proxy_server.h"
  11. #include <stddef.h>
  12. #include <memory>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/logging.h"
  15. #include "rtc_base/socket_factory.h"
  16. namespace rtc {
  17. // ProxyServer
  18. ProxyServer::ProxyServer(SocketFactory* int_factory,
  19. const SocketAddress& int_addr,
  20. SocketFactory* ext_factory,
  21. const SocketAddress& ext_ip)
  22. : ext_factory_(ext_factory),
  23. ext_ip_(ext_ip.ipaddr(), 0), // strip off port
  24. server_socket_(
  25. int_factory->CreateAsyncSocket(int_addr.family(), SOCK_STREAM)) {
  26. RTC_DCHECK(server_socket_.get() != nullptr);
  27. RTC_DCHECK(int_addr.family() == AF_INET || int_addr.family() == AF_INET6);
  28. server_socket_->Bind(int_addr);
  29. server_socket_->Listen(5);
  30. server_socket_->SignalReadEvent.connect(this, &ProxyServer::OnAcceptEvent);
  31. }
  32. ProxyServer::~ProxyServer() = default;
  33. SocketAddress ProxyServer::GetServerAddress() {
  34. return server_socket_->GetLocalAddress();
  35. }
  36. void ProxyServer::OnAcceptEvent(AsyncSocket* socket) {
  37. RTC_DCHECK(socket);
  38. RTC_DCHECK_EQ(socket, server_socket_.get());
  39. AsyncSocket* int_socket = socket->Accept(nullptr);
  40. AsyncProxyServerSocket* wrapped_socket = WrapSocket(int_socket);
  41. AsyncSocket* ext_socket =
  42. ext_factory_->CreateAsyncSocket(ext_ip_.family(), SOCK_STREAM);
  43. if (ext_socket) {
  44. ext_socket->Bind(ext_ip_);
  45. bindings_.emplace_back(
  46. std::make_unique<ProxyBinding>(wrapped_socket, ext_socket));
  47. } else {
  48. RTC_LOG(LS_ERROR)
  49. << "Unable to create external socket on proxy accept event";
  50. }
  51. }
  52. // ProxyBinding
  53. ProxyBinding::ProxyBinding(AsyncProxyServerSocket* int_socket,
  54. AsyncSocket* ext_socket)
  55. : int_socket_(int_socket),
  56. ext_socket_(ext_socket),
  57. connected_(false),
  58. out_buffer_(kBufferSize),
  59. in_buffer_(kBufferSize) {
  60. int_socket_->SignalConnectRequest.connect(this,
  61. &ProxyBinding::OnConnectRequest);
  62. int_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnInternalRead);
  63. int_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnInternalWrite);
  64. int_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnInternalClose);
  65. ext_socket_->SignalConnectEvent.connect(this,
  66. &ProxyBinding::OnExternalConnect);
  67. ext_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnExternalRead);
  68. ext_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnExternalWrite);
  69. ext_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnExternalClose);
  70. }
  71. ProxyBinding::~ProxyBinding() = default;
  72. void ProxyBinding::OnConnectRequest(AsyncProxyServerSocket* socket,
  73. const SocketAddress& addr) {
  74. RTC_DCHECK(!connected_);
  75. RTC_DCHECK(ext_socket_);
  76. ext_socket_->Connect(addr);
  77. // TODO: handle errors here
  78. }
  79. void ProxyBinding::OnInternalRead(AsyncSocket* socket) {
  80. Read(int_socket_.get(), &out_buffer_);
  81. Write(ext_socket_.get(), &out_buffer_);
  82. }
  83. void ProxyBinding::OnInternalWrite(AsyncSocket* socket) {
  84. Write(int_socket_.get(), &in_buffer_);
  85. }
  86. void ProxyBinding::OnInternalClose(AsyncSocket* socket, int err) {
  87. Destroy();
  88. }
  89. void ProxyBinding::OnExternalConnect(AsyncSocket* socket) {
  90. RTC_DCHECK(socket != nullptr);
  91. connected_ = true;
  92. int_socket_->SendConnectResult(0, socket->GetRemoteAddress());
  93. }
  94. void ProxyBinding::OnExternalRead(AsyncSocket* socket) {
  95. Read(ext_socket_.get(), &in_buffer_);
  96. Write(int_socket_.get(), &in_buffer_);
  97. }
  98. void ProxyBinding::OnExternalWrite(AsyncSocket* socket) {
  99. Write(ext_socket_.get(), &out_buffer_);
  100. }
  101. void ProxyBinding::OnExternalClose(AsyncSocket* socket, int err) {
  102. if (!connected_) {
  103. int_socket_->SendConnectResult(err, SocketAddress());
  104. }
  105. Destroy();
  106. }
  107. void ProxyBinding::Read(AsyncSocket* socket, FifoBuffer* buffer) {
  108. // Only read if the buffer is empty.
  109. RTC_DCHECK(socket != nullptr);
  110. size_t size;
  111. int read;
  112. if (buffer->GetBuffered(&size) && size == 0) {
  113. void* p = buffer->GetWriteBuffer(&size);
  114. read = socket->Recv(p, size, nullptr);
  115. buffer->ConsumeWriteBuffer(std::max(read, 0));
  116. }
  117. }
  118. void ProxyBinding::Write(AsyncSocket* socket, FifoBuffer* buffer) {
  119. RTC_DCHECK(socket != nullptr);
  120. size_t size;
  121. int written;
  122. const void* p = buffer->GetReadData(&size);
  123. written = socket->Send(p, size);
  124. buffer->ConsumeReadData(std::max(written, 0));
  125. }
  126. void ProxyBinding::Destroy() {
  127. SignalDestroyed(this);
  128. }
  129. AsyncProxyServerSocket* SocksProxyServer::WrapSocket(AsyncSocket* socket) {
  130. return new AsyncSocksProxyServerSocket(socket);
  131. }
  132. } // namespace rtc