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