henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_SOCKET_ADAPTERS_H_ |
| 12 | #define RTC_BASE_SOCKET_ADAPTERS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 16 | #include "absl/strings/string_view.h" |
Niels Möller | 4415315 | 2018-12-17 14:04:05 +0100 | [diff] [blame] | 17 | #include "api/array_view.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/async_socket.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/crypt_string.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | struct HttpAuthContext; |
| 24 | class ByteBufferReader; |
| 25 | class ByteBufferWriter; |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | // Implements a socket adapter that can buffer and process data internally, |
| 30 | // as in the case of connecting to a proxy, where you must speak the proxy |
| 31 | // protocol before commencing normal socket behavior. |
| 32 | class BufferedReadAdapter : public AsyncSocketAdapter { |
| 33 | public: |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 34 | BufferedReadAdapter(Socket* socket, size_t buffer_size); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 35 | ~BufferedReadAdapter() override; |
| 36 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 37 | BufferedReadAdapter(const BufferedReadAdapter&) = delete; |
| 38 | BufferedReadAdapter& operator=(const BufferedReadAdapter&) = delete; |
| 39 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 40 | int Send(const void* pv, size_t cb) override; |
| 41 | int Recv(void* pv, size_t cb, int64_t* timestamp) override; |
| 42 | |
| 43 | protected: |
| 44 | int DirectSend(const void* pv, size_t cb) { |
| 45 | return AsyncSocketAdapter::Send(pv, cb); |
| 46 | } |
| 47 | |
| 48 | void BufferInput(bool on = true); |
| 49 | virtual void ProcessInput(char* data, size_t* len) = 0; |
| 50 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 51 | void OnReadEvent(Socket* socket) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 52 | |
| 53 | private: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 54 | char* buffer_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | size_t buffer_size_, data_len_; |
| 56 | bool buffering_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | // Implements a socket adapter that performs the client side of a |
| 62 | // fake SSL handshake. Used for "ssltcp" P2P functionality. |
| 63 | class AsyncSSLSocket : public BufferedReadAdapter { |
| 64 | public: |
Niels Möller | 4415315 | 2018-12-17 14:04:05 +0100 | [diff] [blame] | 65 | static ArrayView<const uint8_t> SslClientHello(); |
| 66 | static ArrayView<const uint8_t> SslServerHello(); |
| 67 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 68 | explicit AsyncSSLSocket(Socket* socket); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 70 | AsyncSSLSocket(const AsyncSSLSocket&) = delete; |
| 71 | AsyncSSLSocket& operator=(const AsyncSSLSocket&) = delete; |
| 72 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 73 | int Connect(const SocketAddress& addr) override; |
| 74 | |
| 75 | protected: |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 76 | void OnConnectEvent(Socket* socket) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 77 | void ProcessInput(char* data, size_t* len) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | // Implements a socket adapter that speaks the HTTP/S proxy protocol. |
| 83 | class AsyncHttpsProxySocket : public BufferedReadAdapter { |
| 84 | public: |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 85 | AsyncHttpsProxySocket(Socket* socket, |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 86 | absl::string_view user_agent, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 87 | const SocketAddress& proxy, |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 88 | absl::string_view username, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 89 | const CryptString& password); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 90 | ~AsyncHttpsProxySocket() override; |
| 91 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 92 | AsyncHttpsProxySocket(const AsyncHttpsProxySocket&) = delete; |
| 93 | AsyncHttpsProxySocket& operator=(const AsyncHttpsProxySocket&) = delete; |
| 94 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 95 | // If connect is forced, the adapter will always issue an HTTP CONNECT to the |
| 96 | // target address. Otherwise, it will connect only if the destination port |
| 97 | // is not port 80. |
| 98 | void SetForceConnect(bool force) { force_connect_ = force; } |
| 99 | |
| 100 | int Connect(const SocketAddress& addr) override; |
| 101 | SocketAddress GetRemoteAddress() const override; |
| 102 | int Close() override; |
| 103 | ConnState GetState() const override; |
| 104 | |
| 105 | protected: |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 106 | void OnConnectEvent(Socket* socket) override; |
| 107 | void OnCloseEvent(Socket* socket, int err) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | void ProcessInput(char* data, size_t* len) override; |
| 109 | |
| 110 | bool ShouldIssueConnect() const; |
| 111 | void SendRequest(); |
| 112 | void ProcessLine(char* data, size_t len); |
| 113 | void EndResponse(); |
| 114 | void Error(int error); |
| 115 | |
| 116 | private: |
| 117 | SocketAddress proxy_, dest_; |
| 118 | std::string agent_, user_, headers_; |
| 119 | CryptString pass_; |
| 120 | bool force_connect_; |
| 121 | size_t content_length_; |
| 122 | int defer_error_; |
| 123 | bool expect_close_; |
| 124 | enum ProxyState { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 125 | PS_INIT, |
| 126 | PS_LEADER, |
| 127 | PS_AUTHENTICATE, |
| 128 | PS_SKIP_HEADERS, |
| 129 | PS_ERROR_HEADERS, |
| 130 | PS_TUNNEL_HEADERS, |
| 131 | PS_SKIP_BODY, |
| 132 | PS_TUNNEL, |
| 133 | PS_WAIT_CLOSE, |
| 134 | PS_ERROR |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 135 | } state_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 136 | HttpAuthContext* context_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 137 | std::string unknown_mechanisms_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | // Implements a socket adapter that speaks the SOCKS proxy protocol. |
| 143 | class AsyncSocksProxySocket : public BufferedReadAdapter { |
| 144 | public: |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 145 | AsyncSocksProxySocket(Socket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | const SocketAddress& proxy, |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 147 | absl::string_view username, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 148 | const CryptString& password); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 149 | ~AsyncSocksProxySocket() override; |
| 150 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 151 | AsyncSocksProxySocket(const AsyncSocksProxySocket&) = delete; |
| 152 | AsyncSocksProxySocket& operator=(const AsyncSocksProxySocket&) = delete; |
| 153 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 154 | int Connect(const SocketAddress& addr) override; |
| 155 | SocketAddress GetRemoteAddress() const override; |
| 156 | int Close() override; |
| 157 | ConnState GetState() const override; |
| 158 | |
| 159 | protected: |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 160 | void OnConnectEvent(Socket* socket) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 161 | void ProcessInput(char* data, size_t* len) override; |
| 162 | |
| 163 | void SendHello(); |
| 164 | void SendConnect(); |
| 165 | void SendAuth(); |
| 166 | void Error(int error); |
| 167 | |
| 168 | private: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 169 | enum State { SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 170 | State state_; |
| 171 | SocketAddress proxy_, dest_; |
| 172 | std::string user_; |
| 173 | CryptString pass_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 174 | }; |
| 175 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 176 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 178 | #endif // RTC_BASE_SOCKET_ADAPTERS_H_ |