blob: 0b9b655a5e8b66507561d845e258fc7d2a1b1591 [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 <memory>
Niels Möllerd9709672019-03-25 08:57:50 +010015#include <vector>
16
17#include "absl/memory/memory.h"
Niels Möller13339482019-03-28 13:30:15 +010018#include "rtc_base/memory/fifo_buffer.h"
Niels Möller44153152018-12-17 14:04:05 +010019#include "rtc_base/server_socket_adapters.h"
Niels Möllerd0b88792021-08-12 10:32:30 +020020#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/socket_address.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023namespace rtc {
24
25class SocketFactory;
26
27// ProxyServer is a base class that allows for easy construction of proxy
28// servers. With its helper class ProxyBinding, it contains all the necessary
29// logic for receiving and bridging connections. The specific client-server
30// proxy protocol is implemented by an instance of the AsyncProxyServerSocket
31// class; children of ProxyServer implement WrapSocket appropriately to return
32// the correct protocol handler.
33
34class ProxyBinding : public sigslot::has_slots<> {
35 public:
Niels Möllerd0b88792021-08-12 10:32:30 +020036 ProxyBinding(AsyncProxyServerSocket* in_socket, Socket* out_socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037 ~ProxyBinding() override;
Byoungchan Lee14af7622022-01-12 05:24:58 +090038
39 ProxyBinding(const ProxyBinding&) = delete;
40 ProxyBinding& operator=(const ProxyBinding&) = delete;
41
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042 sigslot::signal1<ProxyBinding*> SignalDestroyed;
43
44 private:
45 void OnConnectRequest(AsyncProxyServerSocket* socket,
46 const SocketAddress& addr);
Niels Möllerd0b88792021-08-12 10:32:30 +020047 void OnInternalRead(Socket* socket);
48 void OnInternalWrite(Socket* socket);
49 void OnInternalClose(Socket* socket, int err);
50 void OnExternalConnect(Socket* socket);
51 void OnExternalRead(Socket* socket);
52 void OnExternalWrite(Socket* socket);
53 void OnExternalClose(Socket* socket, int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054
Niels Möllerd0b88792021-08-12 10:32:30 +020055 static void Read(Socket* socket, FifoBuffer* buffer);
56 static void Write(Socket* socket, FifoBuffer* buffer);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057 void Destroy();
58
59 static const int kBufferSize = 4096;
60 std::unique_ptr<AsyncProxyServerSocket> int_socket_;
Niels Möllerd0b88792021-08-12 10:32:30 +020061 std::unique_ptr<Socket> ext_socket_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062 bool connected_;
63 FifoBuffer out_buffer_;
64 FifoBuffer in_buffer_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065};
66
67class ProxyServer : public sigslot::has_slots<> {
68 public:
Yves Gerey665174f2018-06-19 15:03:05 +020069 ProxyServer(SocketFactory* int_factory,
70 const SocketAddress& int_addr,
71 SocketFactory* ext_factory,
72 const SocketAddress& ext_ip);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 ~ProxyServer() override;
74
Byoungchan Lee14af7622022-01-12 05:24:58 +090075 ProxyServer(const ProxyServer&) = delete;
76 ProxyServer& operator=(const ProxyServer&) = delete;
77
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078 // Returns the address to which the proxy server is bound
79 SocketAddress GetServerAddress();
80
81 protected:
Niels Möllerd0b88792021-08-12 10:32:30 +020082 void OnAcceptEvent(Socket* socket);
83 virtual AsyncProxyServerSocket* WrapSocket(Socket* socket) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084
85 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086 SocketFactory* ext_factory_;
87 SocketAddress ext_ip_;
Niels Möllerd0b88792021-08-12 10:32:30 +020088 std::unique_ptr<Socket> server_socket_;
Niels Möllerd9709672019-03-25 08:57:50 +010089 std::vector<std::unique_ptr<ProxyBinding>> bindings_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090};
91
92// SocksProxyServer is a simple extension of ProxyServer to implement SOCKS.
93class SocksProxyServer : public ProxyServer {
94 public:
Yves Gerey665174f2018-06-19 15:03:05 +020095 SocksProxyServer(SocketFactory* int_factory,
96 const SocketAddress& int_addr,
97 SocketFactory* ext_factory,
98 const SocketAddress& ext_ip)
99 : ProxyServer(int_factory, int_addr, ext_factory, ext_ip) {}
100
Byoungchan Lee14af7622022-01-12 05:24:58 +0900101 SocksProxyServer(const SocksProxyServer&) = delete;
102 SocksProxyServer& operator=(const SocksProxyServer&) = delete;
103
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104 protected:
Niels Möllerd0b88792021-08-12 10:32:30 +0200105 AsyncProxyServerSocket* WrapSocket(Socket* socket) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106};
107
108} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109
Steve Anton10542f22019-01-11 09:11:00 -0800110#endif // RTC_BASE_PROXY_SERVER_H_