proxy_server.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef RTC_BASE_PROXY_SERVER_H_
  11. #define RTC_BASE_PROXY_SERVER_H_
  12. #include <memory>
  13. #include <vector>
  14. #include "absl/memory/memory.h"
  15. #include "rtc_base/async_socket.h"
  16. #include "rtc_base/constructor_magic.h"
  17. #include "rtc_base/memory/fifo_buffer.h"
  18. #include "rtc_base/server_socket_adapters.h"
  19. #include "rtc_base/socket_address.h"
  20. namespace rtc {
  21. class SocketFactory;
  22. // ProxyServer is a base class that allows for easy construction of proxy
  23. // servers. With its helper class ProxyBinding, it contains all the necessary
  24. // logic for receiving and bridging connections. The specific client-server
  25. // proxy protocol is implemented by an instance of the AsyncProxyServerSocket
  26. // class; children of ProxyServer implement WrapSocket appropriately to return
  27. // the correct protocol handler.
  28. class ProxyBinding : public sigslot::has_slots<> {
  29. public:
  30. ProxyBinding(AsyncProxyServerSocket* in_socket, AsyncSocket* out_socket);
  31. ~ProxyBinding() override;
  32. sigslot::signal1<ProxyBinding*> SignalDestroyed;
  33. private:
  34. void OnConnectRequest(AsyncProxyServerSocket* socket,
  35. const SocketAddress& addr);
  36. void OnInternalRead(AsyncSocket* socket);
  37. void OnInternalWrite(AsyncSocket* socket);
  38. void OnInternalClose(AsyncSocket* socket, int err);
  39. void OnExternalConnect(AsyncSocket* socket);
  40. void OnExternalRead(AsyncSocket* socket);
  41. void OnExternalWrite(AsyncSocket* socket);
  42. void OnExternalClose(AsyncSocket* socket, int err);
  43. static void Read(AsyncSocket* socket, FifoBuffer* buffer);
  44. static void Write(AsyncSocket* socket, FifoBuffer* buffer);
  45. void Destroy();
  46. static const int kBufferSize = 4096;
  47. std::unique_ptr<AsyncProxyServerSocket> int_socket_;
  48. std::unique_ptr<AsyncSocket> ext_socket_;
  49. bool connected_;
  50. FifoBuffer out_buffer_;
  51. FifoBuffer in_buffer_;
  52. RTC_DISALLOW_COPY_AND_ASSIGN(ProxyBinding);
  53. };
  54. class ProxyServer : public sigslot::has_slots<> {
  55. public:
  56. ProxyServer(SocketFactory* int_factory,
  57. const SocketAddress& int_addr,
  58. SocketFactory* ext_factory,
  59. const SocketAddress& ext_ip);
  60. ~ProxyServer() override;
  61. // Returns the address to which the proxy server is bound
  62. SocketAddress GetServerAddress();
  63. protected:
  64. void OnAcceptEvent(AsyncSocket* socket);
  65. virtual AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) = 0;
  66. private:
  67. SocketFactory* ext_factory_;
  68. SocketAddress ext_ip_;
  69. std::unique_ptr<AsyncSocket> server_socket_;
  70. std::vector<std::unique_ptr<ProxyBinding>> bindings_;
  71. RTC_DISALLOW_COPY_AND_ASSIGN(ProxyServer);
  72. };
  73. // SocksProxyServer is a simple extension of ProxyServer to implement SOCKS.
  74. class SocksProxyServer : public ProxyServer {
  75. public:
  76. SocksProxyServer(SocketFactory* int_factory,
  77. const SocketAddress& int_addr,
  78. SocketFactory* ext_factory,
  79. const SocketAddress& ext_ip)
  80. : ProxyServer(int_factory, int_addr, ext_factory, ext_ip) {}
  81. protected:
  82. AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) override;
  83. RTC_DISALLOW_COPY_AND_ASSIGN(SocksProxyServer);
  84. };
  85. } // namespace rtc
  86. #endif // RTC_BASE_PROXY_SERVER_H_