blob: 86007c3606fc33443cdbc63c3c1916493a047686 [file] [log] [blame]
henrike@webrtc.org47be73b2014-05-13 18:00:261/*
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>
jbauch1286d0e2016-04-26 10:13:2215#include <memory>
henrike@webrtc.org47be73b2014-05-13 18:00:2616#include "webrtc/base/asyncsocket.h"
kwiberg5fb5bd22016-04-26 15:14:3917#include "webrtc/base/constructormagic.h"
henrike@webrtc.org47be73b2014-05-13 18:00:2618#include "webrtc/base/socketadapters.h"
19#include "webrtc/base/socketaddress.h"
20#include "webrtc/base/stream.h"
21
22namespace rtc {
23
24class 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
33class ProxyBinding : public sigslot::has_slots<> {
34 public:
35 ProxyBinding(AsyncProxyServerSocket* in_socket, AsyncSocket* out_socket);
kwiberg@webrtc.org786b6342015-03-09 22:21:5336 ~ProxyBinding() override;
henrike@webrtc.org47be73b2014-05-13 18:00:2637 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;
jbauch1286d0e2016-04-26 10:13:2255 std::unique_ptr<AsyncProxyServerSocket> int_socket_;
56 std::unique_ptr<AsyncSocket> ext_socket_;
henrike@webrtc.org47be73b2014-05-13 18:00:2657 bool connected_;
58 FifoBuffer out_buffer_;
59 FifoBuffer in_buffer_;
henrikg9199c0e2015-09-16 12:37:4460 RTC_DISALLOW_COPY_AND_ASSIGN(ProxyBinding);
henrike@webrtc.org47be73b2014-05-13 18:00:2661};
62
63class 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.org786b6342015-03-09 22:21:5367 ~ProxyServer() override;
henrike@webrtc.org47be73b2014-05-13 18:00:2668
deadbeef81001b02015-07-16 17:22:2169 // Returns the address to which the proxy server is bound
70 SocketAddress GetServerAddress();
71
henrike@webrtc.org47be73b2014-05-13 18:00:2672 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_;
jbauch1286d0e2016-04-26 10:13:2281 std::unique_ptr<AsyncSocket> server_socket_;
henrike@webrtc.org47be73b2014-05-13 18:00:2682 BindingList bindings_;
henrikg9199c0e2015-09-16 12:37:4483 RTC_DISALLOW_COPY_AND_ASSIGN(ProxyServer);
henrike@webrtc.org47be73b2014-05-13 18:00:2684};
85
86// SocksProxyServer is a simple extension of ProxyServer to implement SOCKS.
87class 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.org786b6342015-03-09 22:21:5394 AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) override;
henrikg9199c0e2015-09-16 12:37:4495 RTC_DISALLOW_COPY_AND_ASSIGN(SocksProxyServer);
henrike@webrtc.org47be73b2014-05-13 18:00:2696};
97
98} // namespace rtc
99
100#endif // WEBRTC_BASE_PROXYSERVER_H_