blob: ef4a3c7866eba4c8d6e66f1d72f9972d252f62ad [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_PROXY_SERVER_H_
12#define RTC_BASE_PROXY_SERVER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <list>
15#include <memory>
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/async_socket.h"
17#include "rtc_base/constructor_magic.h"
Niels Möller44153152018-12-17 14:04:05 +010018#include "rtc_base/server_socket_adapters.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/socket_address.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/stream.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022namespace 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);
36 ~ProxyBinding() override;
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;
55 std::unique_ptr<AsyncProxyServerSocket> int_socket_;
56 std::unique_ptr<AsyncSocket> ext_socket_;
57 bool connected_;
58 FifoBuffer out_buffer_;
59 FifoBuffer in_buffer_;
60 RTC_DISALLOW_COPY_AND_ASSIGN(ProxyBinding);
61};
62
63class ProxyServer : public sigslot::has_slots<> {
64 public:
Yves Gerey665174f2018-06-19 15:03:05 +020065 ProxyServer(SocketFactory* int_factory,
66 const SocketAddress& int_addr,
67 SocketFactory* ext_factory,
68 const SocketAddress& ext_ip);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069 ~ProxyServer() override;
70
71 // Returns the address to which the proxy server is bound
72 SocketAddress GetServerAddress();
73
74 protected:
75 void OnAcceptEvent(AsyncSocket* socket);
76 virtual AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) = 0;
77 void OnBindingDestroyed(ProxyBinding* binding);
78
79 private:
80 typedef std::list<ProxyBinding*> BindingList;
81 SocketFactory* ext_factory_;
82 SocketAddress ext_ip_;
83 std::unique_ptr<AsyncSocket> server_socket_;
84 BindingList bindings_;
85 RTC_DISALLOW_COPY_AND_ASSIGN(ProxyServer);
86};
87
88// SocksProxyServer is a simple extension of ProxyServer to implement SOCKS.
89class SocksProxyServer : public ProxyServer {
90 public:
Yves Gerey665174f2018-06-19 15:03:05 +020091 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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097 protected:
98 AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) override;
99 RTC_DISALLOW_COPY_AND_ASSIGN(SocksProxyServer);
100};
101
102} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103
Steve Anton10542f22019-01-11 09:11:00 -0800104#endif // RTC_BASE_PROXY_SERVER_H_