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 11 #ifndef RTC_BASE_SOCKET_ADAPTERS_H_ 12 #define RTC_BASE_SOCKET_ADAPTERS_H_ 13 14 #include <string> 15 16 #include "absl/strings/string_view.h" 17 #include "api/array_view.h" 18 #include "rtc_base/async_socket.h" 19 #include "rtc_base/crypt_string.h" 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: 34 BufferedReadAdapter(Socket* socket, size_t buffer_size); 35 ~BufferedReadAdapter() override; 36 37 BufferedReadAdapter(const BufferedReadAdapter&) = delete; 38 BufferedReadAdapter& operator=(const BufferedReadAdapter&) = delete; 39 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: DirectSend(const void * pv,size_t cb)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 51 void OnReadEvent(Socket* socket) override; 52 53 private: 54 char* buffer_; 55 size_t buffer_size_, data_len_; 56 bool buffering_; 57 }; 58 59 /////////////////////////////////////////////////////////////////////////////// 60 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: 65 static ArrayView<const uint8_t> SslClientHello(); 66 static ArrayView<const uint8_t> SslServerHello(); 67 68 explicit AsyncSSLSocket(Socket* socket); 69 70 AsyncSSLSocket(const AsyncSSLSocket&) = delete; 71 AsyncSSLSocket& operator=(const AsyncSSLSocket&) = delete; 72 73 int Connect(const SocketAddress& addr) override; 74 75 protected: 76 void OnConnectEvent(Socket* socket) override; 77 void ProcessInput(char* data, size_t* len) override; 78 }; 79 80 /////////////////////////////////////////////////////////////////////////////// 81 82 // Implements a socket adapter that speaks the HTTP/S proxy protocol. 83 class AsyncHttpsProxySocket : public BufferedReadAdapter { 84 public: 85 AsyncHttpsProxySocket(Socket* socket, 86 absl::string_view user_agent, 87 const SocketAddress& proxy, 88 absl::string_view username, 89 const CryptString& password); 90 ~AsyncHttpsProxySocket() override; 91 92 AsyncHttpsProxySocket(const AsyncHttpsProxySocket&) = delete; 93 AsyncHttpsProxySocket& operator=(const AsyncHttpsProxySocket&) = delete; 94 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. SetForceConnect(bool force)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: 106 void OnConnectEvent(Socket* socket) override; 107 void OnCloseEvent(Socket* socket, int err) override; 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 { 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 135 } state_; 136 HttpAuthContext* context_; 137 std::string unknown_mechanisms_; 138 }; 139 140 /////////////////////////////////////////////////////////////////////////////// 141 142 // Implements a socket adapter that speaks the SOCKS proxy protocol. 143 class AsyncSocksProxySocket : public BufferedReadAdapter { 144 public: 145 AsyncSocksProxySocket(Socket* socket, 146 const SocketAddress& proxy, 147 absl::string_view username, 148 const CryptString& password); 149 ~AsyncSocksProxySocket() override; 150 151 AsyncSocksProxySocket(const AsyncSocksProxySocket&) = delete; 152 AsyncSocksProxySocket& operator=(const AsyncSocksProxySocket&) = delete; 153 154 int Connect(const SocketAddress& addr) override; 155 SocketAddress GetRemoteAddress() const override; 156 int Close() override; 157 ConnState GetState() const override; 158 159 protected: 160 void OnConnectEvent(Socket* socket) override; 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: 169 enum State { SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR }; 170 State state_; 171 SocketAddress proxy_, dest_; 172 std::string user_; 173 CryptString pass_; 174 }; 175 176 } // namespace rtc 177 178 #endif // RTC_BASE_SOCKET_ADAPTERS_H_ 179