henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
| 11 | #ifndef WEBRTC_BASE_PROXYSERVER_H_ |
| 12 | #define WEBRTC_BASE_PROXYSERVER_H_ |
| 13 | |
| 14 | #include <list> |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 16 | #include "webrtc/base/asyncsocket.h" |
kwiberg | 5fb5bd2 | 2016-04-26 15:14:39 | [diff] [blame] | 17 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 18 | #include "webrtc/base/socketadapters.h" |
| 19 | #include "webrtc/base/socketaddress.h" |
| 20 | #include "webrtc/base/stream.h" |
| 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | class SocketFactory; |
| 25 | |
| 26 | // ProxyServer is a base class that allows for easy construction of proxy |
| 27 | // servers. With its helper class ProxyBinding, it contains all the necessary |
| 28 | // logic for receiving and bridging connections. The specific client-server |
| 29 | // proxy protocol is implemented by an instance of the AsyncProxyServerSocket |
| 30 | // class; children of ProxyServer implement WrapSocket appropriately to return |
| 31 | // the correct protocol handler. |
| 32 | |
| 33 | class ProxyBinding : public sigslot::has_slots<> { |
| 34 | public: |
| 35 | ProxyBinding(AsyncProxyServerSocket* in_socket, AsyncSocket* out_socket); |
kwiberg@webrtc.org | 786b634 | 2015-03-09 22:21:53 | [diff] [blame] | 36 | ~ProxyBinding() override; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 37 | sigslot::signal1<ProxyBinding*> SignalDestroyed; |
| 38 | |
| 39 | private: |
| 40 | void OnConnectRequest(AsyncProxyServerSocket* socket, |
| 41 | const SocketAddress& addr); |
| 42 | void OnInternalRead(AsyncSocket* socket); |
| 43 | void OnInternalWrite(AsyncSocket* socket); |
| 44 | void OnInternalClose(AsyncSocket* socket, int err); |
| 45 | void OnExternalConnect(AsyncSocket* socket); |
| 46 | void OnExternalRead(AsyncSocket* socket); |
| 47 | void OnExternalWrite(AsyncSocket* socket); |
| 48 | void OnExternalClose(AsyncSocket* socket, int err); |
| 49 | |
| 50 | static void Read(AsyncSocket* socket, FifoBuffer* buffer); |
| 51 | static void Write(AsyncSocket* socket, FifoBuffer* buffer); |
| 52 | void Destroy(); |
| 53 | |
| 54 | static const int kBufferSize = 4096; |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame] | 55 | std::unique_ptr<AsyncProxyServerSocket> int_socket_; |
| 56 | std::unique_ptr<AsyncSocket> ext_socket_; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 57 | bool connected_; |
| 58 | FifoBuffer out_buffer_; |
| 59 | FifoBuffer in_buffer_; |
henrikg | 9199c0e | 2015-09-16 12:37:44 | [diff] [blame] | 60 | RTC_DISALLOW_COPY_AND_ASSIGN(ProxyBinding); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class ProxyServer : public sigslot::has_slots<> { |
| 64 | public: |
| 65 | ProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr, |
| 66 | SocketFactory* ext_factory, const SocketAddress& ext_ip); |
kwiberg@webrtc.org | 786b634 | 2015-03-09 22:21:53 | [diff] [blame] | 67 | ~ProxyServer() override; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 68 | |
deadbeef | 81001b0 | 2015-07-16 17:22:21 | [diff] [blame] | 69 | // Returns the address to which the proxy server is bound |
| 70 | SocketAddress GetServerAddress(); |
| 71 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 72 | protected: |
| 73 | void OnAcceptEvent(AsyncSocket* socket); |
| 74 | virtual AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) = 0; |
| 75 | void OnBindingDestroyed(ProxyBinding* binding); |
| 76 | |
| 77 | private: |
| 78 | typedef std::list<ProxyBinding*> BindingList; |
| 79 | SocketFactory* ext_factory_; |
| 80 | SocketAddress ext_ip_; |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame] | 81 | std::unique_ptr<AsyncSocket> server_socket_; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 82 | BindingList bindings_; |
henrikg | 9199c0e | 2015-09-16 12:37:44 | [diff] [blame] | 83 | RTC_DISALLOW_COPY_AND_ASSIGN(ProxyServer); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | // SocksProxyServer is a simple extension of ProxyServer to implement SOCKS. |
| 87 | class SocksProxyServer : public ProxyServer { |
| 88 | public: |
| 89 | SocksProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr, |
| 90 | SocketFactory* ext_factory, const SocketAddress& ext_ip) |
| 91 | : ProxyServer(int_factory, int_addr, ext_factory, ext_ip) { |
| 92 | } |
| 93 | protected: |
kwiberg@webrtc.org | 786b634 | 2015-03-09 22:21:53 | [diff] [blame] | 94 | AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) override; |
henrikg | 9199c0e | 2015-09-16 12:37:44 | [diff] [blame] | 95 | RTC_DISALLOW_COPY_AND_ASSIGN(SocksProxyServer); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace rtc |
| 99 | |
| 100 | #endif // WEBRTC_BASE_PROXYSERVER_H_ |