blob: adb26ae9d046f8befe3a88fdb2a627d5849e6fff [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000034 ~ProxyBinding() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 sigslot::signal1<ProxyBinding*> SignalDestroyed;
36
37 private:
38 void OnConnectRequest(AsyncProxyServerSocket* socket,
39 const SocketAddress& addr);
40 void OnInternalRead(AsyncSocket* socket);
41 void OnInternalWrite(AsyncSocket* socket);
42 void OnInternalClose(AsyncSocket* socket, int err);
43 void OnExternalConnect(AsyncSocket* socket);
44 void OnExternalRead(AsyncSocket* socket);
45 void OnExternalWrite(AsyncSocket* socket);
46 void OnExternalClose(AsyncSocket* socket, int err);
47
48 static void Read(AsyncSocket* socket, FifoBuffer* buffer);
49 static void Write(AsyncSocket* socket, FifoBuffer* buffer);
50 void Destroy();
51
52 static const int kBufferSize = 4096;
53 scoped_ptr<AsyncProxyServerSocket> int_socket_;
54 scoped_ptr<AsyncSocket> ext_socket_;
55 bool connected_;
56 FifoBuffer out_buffer_;
57 FifoBuffer in_buffer_;
henrikg3c089d72015-09-16 05:37:44 -070058 RTC_DISALLOW_COPY_AND_ASSIGN(ProxyBinding);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059};
60
61class ProxyServer : public sigslot::has_slots<> {
62 public:
63 ProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr,
64 SocketFactory* ext_factory, const SocketAddress& ext_ip);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000065 ~ProxyServer() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
deadbeefc5d0d952015-07-16 10:22:21 -070067 // Returns the address to which the proxy server is bound
68 SocketAddress GetServerAddress();
69
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 protected:
71 void OnAcceptEvent(AsyncSocket* socket);
72 virtual AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) = 0;
73 void OnBindingDestroyed(ProxyBinding* binding);
74
75 private:
76 typedef std::list<ProxyBinding*> BindingList;
77 SocketFactory* ext_factory_;
78 SocketAddress ext_ip_;
79 scoped_ptr<AsyncSocket> server_socket_;
80 BindingList bindings_;
henrikg3c089d72015-09-16 05:37:44 -070081 RTC_DISALLOW_COPY_AND_ASSIGN(ProxyServer);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082};
83
84// SocksProxyServer is a simple extension of ProxyServer to implement SOCKS.
85class SocksProxyServer : public ProxyServer {
86 public:
87 SocksProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr,
88 SocketFactory* ext_factory, const SocketAddress& ext_ip)
89 : ProxyServer(int_factory, int_addr, ext_factory, ext_ip) {
90 }
91 protected:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000092 AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) override;
henrikg3c089d72015-09-16 05:37:44 -070093 RTC_DISALLOW_COPY_AND_ASSIGN(SocksProxyServer);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094};
95
96} // namespace rtc
97
98#endif // WEBRTC_BASE_PROXYSERVER_H_