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