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 | |
| 11 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 12 | #pragma warning(disable : 4786) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 15 | #include <algorithm> |
| 16 | |
Niels Möller | aa3c1cc | 2018-11-02 10:54:56 +0100 | [diff] [blame] | 17 | #include "absl/strings/match.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/byte_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/http_common.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/socket_adapters.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 24 | #include "rtc_base/strings/string_builder.h" |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 25 | #include "rtc_base/zero_memory.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | namespace rtc { |
| 28 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 29 | BufferedReadAdapter::BufferedReadAdapter(Socket* socket, size_t size) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | : AsyncSocketAdapter(socket), |
| 31 | buffer_size_(size), |
| 32 | data_len_(0), |
| 33 | buffering_(false) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | buffer_ = new char[buffer_size_]; |
| 35 | } |
| 36 | |
| 37 | BufferedReadAdapter::~BufferedReadAdapter() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 38 | delete[] buffer_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 41 | int BufferedReadAdapter::Send(const void* pv, size_t cb) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | if (buffering_) { |
| 43 | // TODO: Spoof error better; Signal Writeable |
Niels Möller | 8729d78 | 2021-08-11 11:22:44 +0200 | [diff] [blame] | 44 | SetError(EWOULDBLOCK); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | return -1; |
| 46 | } |
| 47 | return AsyncSocketAdapter::Send(pv, cb); |
| 48 | } |
| 49 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 50 | int BufferedReadAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | if (buffering_) { |
Niels Möller | 8729d78 | 2021-08-11 11:22:44 +0200 | [diff] [blame] | 52 | SetError(EWOULDBLOCK); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | size_t read = 0; |
| 57 | |
| 58 | if (data_len_) { |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 59 | read = std::min(cb, data_len_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | memcpy(pv, buffer_, read); |
| 61 | data_len_ -= read; |
| 62 | if (data_len_ > 0) { |
| 63 | memmove(buffer_, buffer_ + read, data_len_); |
| 64 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 65 | pv = static_cast<char*>(pv) + read; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | cb -= read; |
| 67 | } |
| 68 | |
| 69 | // FIX: If cb == 0, we won't generate another read event |
| 70 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 71 | int res = AsyncSocketAdapter::Recv(pv, cb, timestamp); |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 72 | if (res >= 0) { |
| 73 | // Read from socket and possibly buffer; return combined length |
| 74 | return res + static_cast<int>(read); |
| 75 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 77 | if (read > 0) { |
| 78 | // Failed to read from socket, but still read something from buffer |
| 79 | return static_cast<int>(read); |
| 80 | } |
| 81 | |
| 82 | // Didn't read anything; return error from socket |
| 83 | return res; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void BufferedReadAdapter::BufferInput(bool on) { |
| 87 | buffering_ = on; |
| 88 | } |
| 89 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 90 | void BufferedReadAdapter::OnReadEvent(Socket* socket) { |
Niels Möller | 8729d78 | 2021-08-11 11:22:44 +0200 | [diff] [blame] | 91 | RTC_DCHECK(socket == GetSocket()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | |
| 93 | if (!buffering_) { |
| 94 | AsyncSocketAdapter::OnReadEvent(socket); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (data_len_ >= buffer_size_) { |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 99 | RTC_LOG(LS_ERROR) << "Input buffer overflow"; |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame^] | 100 | RTC_DCHECK_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | data_len_ = 0; |
| 102 | } |
| 103 | |
Niels Möller | 8729d78 | 2021-08-11 11:22:44 +0200 | [diff] [blame] | 104 | int len = AsyncSocketAdapter::Recv(buffer_ + data_len_, |
| 105 | buffer_size_ - data_len_, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | if (len < 0) { |
| 107 | // TODO: Do something better like forwarding the error to the user. |
Harald Alvestrand | 97597c0 | 2021-11-04 12:01:23 +0000 | [diff] [blame] | 108 | RTC_LOG_ERR(LS_INFO) << "Recv"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | data_len_ += len; |
| 113 | |
| 114 | ProcessInput(buffer_, &data_len_); |
| 115 | } |
| 116 | |
| 117 | /////////////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | // This is a SSL v2 CLIENT_HELLO message. |
| 120 | // TODO: Should this have a session id? The response doesn't have a |
| 121 | // certificate, so the hello should have a session id. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 122 | static const uint8_t kSslClientHello[] = { |
| 123 | 0x80, 0x46, // msg len |
| 124 | 0x01, // CLIENT_HELLO |
| 125 | 0x03, 0x01, // SSL 3.1 |
| 126 | 0x00, 0x2d, // ciphersuite len |
| 127 | 0x00, 0x00, // session id len |
| 128 | 0x00, 0x10, // challenge len |
| 129 | 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites |
| 130 | 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, // |
| 131 | 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, // |
| 132 | 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, // |
| 133 | 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, // |
| 134 | 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge |
| 135 | 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea // |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Niels Möller | 4415315 | 2018-12-17 14:04:05 +0100 | [diff] [blame] | 138 | // static |
| 139 | ArrayView<const uint8_t> AsyncSSLSocket::SslClientHello() { |
| 140 | // Implicit conversion directly from kSslClientHello to ArrayView fails when |
| 141 | // built with gcc. |
| 142 | return {kSslClientHello, sizeof(kSslClientHello)}; |
| 143 | } |
| 144 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 145 | // This is a TLSv1 SERVER_HELLO message. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 146 | static const uint8_t kSslServerHello[] = { |
| 147 | 0x16, // handshake message |
| 148 | 0x03, 0x01, // SSL 3.1 |
| 149 | 0x00, 0x4a, // message len |
| 150 | 0x02, // SERVER_HELLO |
| 151 | 0x00, 0x00, 0x46, // handshake len |
| 152 | 0x03, 0x01, // SSL 3.1 |
| 153 | 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random |
| 154 | 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, // |
| 155 | 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, // |
| 156 | 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, // |
| 157 | 0x20, // session id len |
| 158 | 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id |
| 159 | 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, // |
| 160 | 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, // |
| 161 | 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, // |
| 162 | 0x00, 0x04, // RSA/RC4-128/MD5 |
| 163 | 0x00 // null compression |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Niels Möller | 4415315 | 2018-12-17 14:04:05 +0100 | [diff] [blame] | 166 | // static |
| 167 | ArrayView<const uint8_t> AsyncSSLSocket::SslServerHello() { |
| 168 | return {kSslServerHello, sizeof(kSslServerHello)}; |
| 169 | } |
| 170 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 171 | AsyncSSLSocket::AsyncSSLSocket(Socket* socket) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 172 | : BufferedReadAdapter(socket, 1024) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | |
| 174 | int AsyncSSLSocket::Connect(const SocketAddress& addr) { |
| 175 | // Begin buffering before we connect, so that there isn't a race condition |
| 176 | // between potential senders and receiving the OnConnectEvent signal |
| 177 | BufferInput(true); |
| 178 | return BufferedReadAdapter::Connect(addr); |
| 179 | } |
| 180 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 181 | void AsyncSSLSocket::OnConnectEvent(Socket* socket) { |
Niels Möller | 8729d78 | 2021-08-11 11:22:44 +0200 | [diff] [blame] | 182 | RTC_DCHECK(socket == GetSocket()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | // TODO: we could buffer output too... |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 184 | const int res = DirectSend(kSslClientHello, sizeof(kSslClientHello)); |
Niels Möller | b0cb4d1 | 2021-08-20 11:04:04 +0200 | [diff] [blame] | 185 | if (res != sizeof(kSslClientHello)) { |
Niels Möller | 2eb465f | 2021-08-23 11:17:56 +0200 | [diff] [blame] | 186 | RTC_LOG(LS_ERROR) << "Sending fake SSL ClientHello message failed."; |
Niels Möller | b0cb4d1 | 2021-08-20 11:04:04 +0200 | [diff] [blame] | 187 | Close(); |
| 188 | SignalCloseEvent(this, 0); |
| 189 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void AsyncSSLSocket::ProcessInput(char* data, size_t* len) { |
| 193 | if (*len < sizeof(kSslServerHello)) |
| 194 | return; |
| 195 | |
| 196 | if (memcmp(kSslServerHello, data, sizeof(kSslServerHello)) != 0) { |
Niels Möller | 2eb465f | 2021-08-23 11:17:56 +0200 | [diff] [blame] | 197 | RTC_LOG(LS_ERROR) << "Received non-matching fake SSL ServerHello message."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 198 | Close(); |
| 199 | SignalCloseEvent(this, 0); // TODO: error code? |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | *len -= sizeof(kSslServerHello); |
| 204 | if (*len > 0) { |
| 205 | memmove(data, data + sizeof(kSslServerHello), *len); |
| 206 | } |
| 207 | |
| 208 | bool remainder = (*len > 0); |
| 209 | BufferInput(false); |
| 210 | SignalConnectEvent(this); |
| 211 | |
| 212 | // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 213 | if (remainder) |
| 214 | SignalReadEvent(this); |
| 215 | } |
| 216 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 217 | /////////////////////////////////////////////////////////////////////////////// |
| 218 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 219 | AsyncHttpsProxySocket::AsyncHttpsProxySocket(Socket* socket, |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 220 | const std::string& user_agent, |
| 221 | const SocketAddress& proxy, |
| 222 | const std::string& username, |
| 223 | const CryptString& password) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 224 | : BufferedReadAdapter(socket, 1024), |
| 225 | proxy_(proxy), |
| 226 | agent_(user_agent), |
| 227 | user_(username), |
| 228 | pass_(password), |
| 229 | force_connect_(false), |
| 230 | state_(PS_ERROR), |
| 231 | context_(0) {} |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 232 | |
| 233 | AsyncHttpsProxySocket::~AsyncHttpsProxySocket() { |
| 234 | delete context_; |
| 235 | } |
| 236 | |
| 237 | int AsyncHttpsProxySocket::Connect(const SocketAddress& addr) { |
| 238 | int ret; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 239 | RTC_LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::Connect(" |
| 240 | << proxy_.ToSensitiveString() << ")"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 241 | dest_ = addr; |
| 242 | state_ = PS_INIT; |
| 243 | if (ShouldIssueConnect()) { |
| 244 | BufferInput(true); |
| 245 | } |
| 246 | ret = BufferedReadAdapter::Connect(proxy_); |
| 247 | // TODO: Set state_ appropriately if Connect fails. |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | SocketAddress AsyncHttpsProxySocket::GetRemoteAddress() const { |
| 252 | return dest_; |
| 253 | } |
| 254 | |
| 255 | int AsyncHttpsProxySocket::Close() { |
| 256 | headers_.clear(); |
| 257 | state_ = PS_ERROR; |
| 258 | dest_.Clear(); |
| 259 | delete context_; |
| 260 | context_ = nullptr; |
| 261 | return BufferedReadAdapter::Close(); |
| 262 | } |
| 263 | |
| 264 | Socket::ConnState AsyncHttpsProxySocket::GetState() const { |
| 265 | if (state_ < PS_TUNNEL) { |
| 266 | return CS_CONNECTING; |
| 267 | } else if (state_ == PS_TUNNEL) { |
| 268 | return CS_CONNECTED; |
| 269 | } else { |
| 270 | return CS_CLOSED; |
| 271 | } |
| 272 | } |
| 273 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 274 | void AsyncHttpsProxySocket::OnConnectEvent(Socket* socket) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 275 | RTC_LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnConnectEvent"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 276 | if (!ShouldIssueConnect()) { |
| 277 | state_ = PS_TUNNEL; |
| 278 | BufferedReadAdapter::OnConnectEvent(socket); |
| 279 | return; |
| 280 | } |
| 281 | SendRequest(); |
| 282 | } |
| 283 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 284 | void AsyncHttpsProxySocket::OnCloseEvent(Socket* socket, int err) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 285 | RTC_LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnCloseEvent(" << err << ")"; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 286 | if ((state_ == PS_WAIT_CLOSE) && (err == 0)) { |
| 287 | state_ = PS_ERROR; |
| 288 | Connect(dest_); |
| 289 | } else { |
| 290 | BufferedReadAdapter::OnCloseEvent(socket, err); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void AsyncHttpsProxySocket::ProcessInput(char* data, size_t* len) { |
| 295 | size_t start = 0; |
| 296 | for (size_t pos = start; state_ < PS_TUNNEL && pos < *len;) { |
| 297 | if (state_ == PS_SKIP_BODY) { |
| 298 | size_t consume = std::min(*len - pos, content_length_); |
| 299 | pos += consume; |
| 300 | start = pos; |
| 301 | content_length_ -= consume; |
| 302 | if (content_length_ == 0) { |
| 303 | EndResponse(); |
| 304 | } |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | if (data[pos++] != '\n') |
| 309 | continue; |
| 310 | |
Mirko Bonadei | 54c90f2 | 2021-10-03 11:26:11 +0200 | [diff] [blame] | 311 | size_t length = pos - start - 1; |
| 312 | if ((length > 0) && (data[start + length - 1] == '\r')) |
| 313 | --length; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 314 | |
Mirko Bonadei | 54c90f2 | 2021-10-03 11:26:11 +0200 | [diff] [blame] | 315 | data[start + length] = 0; |
| 316 | ProcessLine(data + start, length); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 317 | start = pos; |
| 318 | } |
| 319 | |
| 320 | *len -= start; |
| 321 | if (*len > 0) { |
| 322 | memmove(data, data + start, *len); |
| 323 | } |
| 324 | |
| 325 | if (state_ != PS_TUNNEL) |
| 326 | return; |
| 327 | |
| 328 | bool remainder = (*len > 0); |
| 329 | BufferInput(false); |
| 330 | SignalConnectEvent(this); |
| 331 | |
| 332 | // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 333 | if (remainder) |
| 334 | SignalReadEvent(this); // TODO: signal this?? |
| 335 | } |
| 336 | |
| 337 | bool AsyncHttpsProxySocket::ShouldIssueConnect() const { |
| 338 | // TODO: Think about whether a more sophisticated test |
| 339 | // than dest port == 80 is needed. |
| 340 | return force_connect_ || (dest_.port() != 80); |
| 341 | } |
| 342 | |
| 343 | void AsyncHttpsProxySocket::SendRequest() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 344 | rtc::StringBuilder ss; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 345 | ss << "CONNECT " << dest_.ToString() << " HTTP/1.0\r\n"; |
| 346 | ss << "User-Agent: " << agent_ << "\r\n"; |
| 347 | ss << "Host: " << dest_.HostAsURIString() << "\r\n"; |
| 348 | ss << "Content-Length: 0\r\n"; |
| 349 | ss << "Proxy-Connection: Keep-Alive\r\n"; |
| 350 | ss << headers_; |
| 351 | ss << "\r\n"; |
| 352 | std::string str = ss.str(); |
| 353 | DirectSend(str.c_str(), str.size()); |
| 354 | state_ = PS_LEADER; |
| 355 | expect_close_ = true; |
| 356 | content_length_ = 0; |
| 357 | headers_.clear(); |
| 358 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 359 | RTC_LOG(LS_VERBOSE) << "AsyncHttpsProxySocket >> " << str; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 362 | void AsyncHttpsProxySocket::ProcessLine(char* data, size_t len) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 363 | RTC_LOG(LS_VERBOSE) << "AsyncHttpsProxySocket << " << data; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 364 | |
| 365 | if (len == 0) { |
| 366 | if (state_ == PS_TUNNEL_HEADERS) { |
| 367 | state_ = PS_TUNNEL; |
| 368 | } else if (state_ == PS_ERROR_HEADERS) { |
| 369 | Error(defer_error_); |
| 370 | return; |
| 371 | } else if (state_ == PS_SKIP_HEADERS) { |
| 372 | if (content_length_) { |
| 373 | state_ = PS_SKIP_BODY; |
| 374 | } else { |
| 375 | EndResponse(); |
| 376 | return; |
| 377 | } |
| 378 | } else { |
Anders Klemets | 1425d40 | 2019-12-09 09:45:02 -0800 | [diff] [blame] | 379 | if (!unknown_mechanisms_.empty()) { |
| 380 | RTC_LOG(LS_ERROR) << "Unsupported authentication methods: " |
| 381 | << unknown_mechanisms_; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 382 | } |
| 383 | // Unexpected end of headers |
| 384 | Error(0); |
| 385 | return; |
| 386 | } |
| 387 | } else if (state_ == PS_LEADER) { |
| 388 | unsigned int code; |
| 389 | if (sscanf(data, "HTTP/%*u.%*u %u", &code) != 1) { |
| 390 | Error(0); |
| 391 | return; |
| 392 | } |
| 393 | switch (code) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 394 | case 200: |
| 395 | // connection good! |
| 396 | state_ = PS_TUNNEL_HEADERS; |
| 397 | return; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 398 | #if defined(HTTP_STATUS_PROXY_AUTH_REQ) && (HTTP_STATUS_PROXY_AUTH_REQ != 407) |
| 399 | #error Wrong code for HTTP_STATUS_PROXY_AUTH_REQ |
| 400 | #endif |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 401 | case 407: // HTTP_STATUS_PROXY_AUTH_REQ |
| 402 | state_ = PS_AUTHENTICATE; |
| 403 | return; |
| 404 | default: |
| 405 | defer_error_ = 0; |
| 406 | state_ = PS_ERROR_HEADERS; |
| 407 | return; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 408 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 409 | } else if ((state_ == PS_AUTHENTICATE) && |
Niels Möller | aa3c1cc | 2018-11-02 10:54:56 +0100 | [diff] [blame] | 410 | absl::StartsWithIgnoreCase(data, "Proxy-Authenticate:")) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 411 | std::string response, auth_method; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 412 | switch (HttpAuthenticate(data + 19, len - 19, proxy_, "CONNECT", "/", user_, |
| 413 | pass_, context_, response, auth_method)) { |
| 414 | case HAR_IGNORE: |
| 415 | RTC_LOG(LS_VERBOSE) << "Ignoring Proxy-Authenticate: " << auth_method; |
| 416 | if (!unknown_mechanisms_.empty()) |
| 417 | unknown_mechanisms_.append(", "); |
| 418 | unknown_mechanisms_.append(auth_method); |
| 419 | break; |
| 420 | case HAR_RESPONSE: |
| 421 | headers_ = "Proxy-Authorization: "; |
| 422 | headers_.append(response); |
| 423 | headers_.append("\r\n"); |
| 424 | state_ = PS_SKIP_HEADERS; |
| 425 | unknown_mechanisms_.clear(); |
| 426 | break; |
| 427 | case HAR_CREDENTIALS: |
| 428 | defer_error_ = SOCKET_EACCES; |
| 429 | state_ = PS_ERROR_HEADERS; |
| 430 | unknown_mechanisms_.clear(); |
| 431 | break; |
| 432 | case HAR_ERROR: |
| 433 | defer_error_ = 0; |
| 434 | state_ = PS_ERROR_HEADERS; |
| 435 | unknown_mechanisms_.clear(); |
| 436 | break; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 437 | } |
Niels Möller | aa3c1cc | 2018-11-02 10:54:56 +0100 | [diff] [blame] | 438 | } else if (absl::StartsWithIgnoreCase(data, "Content-Length:")) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 439 | content_length_ = strtoul(data + 15, 0, 0); |
Niels Möller | aa3c1cc | 2018-11-02 10:54:56 +0100 | [diff] [blame] | 440 | } else if (absl::StartsWithIgnoreCase(data, "Proxy-Connection: Keep-Alive")) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 441 | expect_close_ = false; |
| 442 | /* |
Niels Möller | aa3c1cc | 2018-11-02 10:54:56 +0100 | [diff] [blame] | 443 | } else if (absl::StartsWithIgnoreCase(data, "Connection: close") { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 444 | expect_close_ = true; |
| 445 | */ |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void AsyncHttpsProxySocket::EndResponse() { |
| 450 | if (!expect_close_) { |
| 451 | SendRequest(); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | // No point in waiting for the server to close... let's close now |
| 456 | // TODO: Refactor out PS_WAIT_CLOSE |
| 457 | state_ = PS_WAIT_CLOSE; |
| 458 | BufferedReadAdapter::Close(); |
| 459 | OnCloseEvent(this, 0); |
| 460 | } |
| 461 | |
| 462 | void AsyncHttpsProxySocket::Error(int error) { |
| 463 | BufferInput(false); |
| 464 | Close(); |
| 465 | SetError(error); |
| 466 | SignalCloseEvent(this, error); |
| 467 | } |
| 468 | |
| 469 | /////////////////////////////////////////////////////////////////////////////// |
| 470 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 471 | AsyncSocksProxySocket::AsyncSocksProxySocket(Socket* socket, |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 472 | const SocketAddress& proxy, |
| 473 | const std::string& username, |
| 474 | const CryptString& password) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 475 | : BufferedReadAdapter(socket, 1024), |
| 476 | state_(SS_ERROR), |
| 477 | proxy_(proxy), |
| 478 | user_(username), |
| 479 | pass_(password) {} |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 480 | |
| 481 | AsyncSocksProxySocket::~AsyncSocksProxySocket() = default; |
| 482 | |
| 483 | int AsyncSocksProxySocket::Connect(const SocketAddress& addr) { |
| 484 | int ret; |
| 485 | dest_ = addr; |
| 486 | state_ = SS_INIT; |
| 487 | BufferInput(true); |
| 488 | ret = BufferedReadAdapter::Connect(proxy_); |
| 489 | // TODO: Set state_ appropriately if Connect fails. |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | SocketAddress AsyncSocksProxySocket::GetRemoteAddress() const { |
| 494 | return dest_; |
| 495 | } |
| 496 | |
| 497 | int AsyncSocksProxySocket::Close() { |
| 498 | state_ = SS_ERROR; |
| 499 | dest_.Clear(); |
| 500 | return BufferedReadAdapter::Close(); |
| 501 | } |
| 502 | |
| 503 | Socket::ConnState AsyncSocksProxySocket::GetState() const { |
| 504 | if (state_ < SS_TUNNEL) { |
| 505 | return CS_CONNECTING; |
| 506 | } else if (state_ == SS_TUNNEL) { |
| 507 | return CS_CONNECTED; |
| 508 | } else { |
| 509 | return CS_CLOSED; |
| 510 | } |
| 511 | } |
| 512 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 513 | void AsyncSocksProxySocket::OnConnectEvent(Socket* socket) { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 514 | SendHello(); |
| 515 | } |
| 516 | |
| 517 | void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { |
| 518 | RTC_DCHECK(state_ < SS_TUNNEL); |
| 519 | |
| 520 | ByteBufferReader response(data, *len); |
| 521 | |
| 522 | if (state_ == SS_HELLO) { |
| 523 | uint8_t ver, method; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 524 | if (!response.ReadUInt8(&ver) || !response.ReadUInt8(&method)) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 525 | return; |
| 526 | |
| 527 | if (ver != 5) { |
| 528 | Error(0); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | if (method == 0) { |
| 533 | SendConnect(); |
| 534 | } else if (method == 2) { |
| 535 | SendAuth(); |
| 536 | } else { |
| 537 | Error(0); |
| 538 | return; |
| 539 | } |
| 540 | } else if (state_ == SS_AUTH) { |
| 541 | uint8_t ver, status; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 542 | if (!response.ReadUInt8(&ver) || !response.ReadUInt8(&status)) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 543 | return; |
| 544 | |
| 545 | if ((ver != 1) || (status != 0)) { |
| 546 | Error(SOCKET_EACCES); |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | SendConnect(); |
| 551 | } else if (state_ == SS_CONNECT) { |
| 552 | uint8_t ver, rep, rsv, atyp; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 553 | if (!response.ReadUInt8(&ver) || !response.ReadUInt8(&rep) || |
| 554 | !response.ReadUInt8(&rsv) || !response.ReadUInt8(&atyp)) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 555 | return; |
| 556 | |
| 557 | if ((ver != 5) || (rep != 0)) { |
| 558 | Error(0); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | uint16_t port; |
| 563 | if (atyp == 1) { |
| 564 | uint32_t addr; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 565 | if (!response.ReadUInt32(&addr) || !response.ReadUInt16(&port)) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 566 | return; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 567 | RTC_LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 568 | } else if (atyp == 3) { |
Mirko Bonadei | 54c90f2 | 2021-10-03 11:26:11 +0200 | [diff] [blame] | 569 | uint8_t length; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 570 | std::string addr; |
Mirko Bonadei | 54c90f2 | 2021-10-03 11:26:11 +0200 | [diff] [blame] | 571 | if (!response.ReadUInt8(&length) || !response.ReadString(&addr, length) || |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 572 | !response.ReadUInt16(&port)) |
| 573 | return; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 574 | RTC_LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 575 | } else if (atyp == 4) { |
| 576 | std::string addr; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 577 | if (!response.ReadString(&addr, 16) || !response.ReadUInt16(&port)) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 578 | return; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 579 | RTC_LOG(LS_VERBOSE) << "Bound on <IPV6>:" << port; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 580 | } else { |
| 581 | Error(0); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | state_ = SS_TUNNEL; |
| 586 | } |
| 587 | |
| 588 | // Consume parsed data |
| 589 | *len = response.Length(); |
| 590 | memmove(data, response.Data(), *len); |
| 591 | |
| 592 | if (state_ != SS_TUNNEL) |
| 593 | return; |
| 594 | |
| 595 | bool remainder = (*len > 0); |
| 596 | BufferInput(false); |
| 597 | SignalConnectEvent(this); |
| 598 | |
| 599 | // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 600 | if (remainder) |
| 601 | SignalReadEvent(this); // TODO: signal this?? |
| 602 | } |
| 603 | |
| 604 | void AsyncSocksProxySocket::SendHello() { |
| 605 | ByteBufferWriter request; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 606 | request.WriteUInt8(5); // Socks Version |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 607 | if (user_.empty()) { |
| 608 | request.WriteUInt8(1); // Authentication Mechanisms |
| 609 | request.WriteUInt8(0); // No authentication |
| 610 | } else { |
| 611 | request.WriteUInt8(2); // Authentication Mechanisms |
| 612 | request.WriteUInt8(0); // No authentication |
| 613 | request.WriteUInt8(2); // Username/Password |
| 614 | } |
| 615 | DirectSend(request.Data(), request.Length()); |
| 616 | state_ = SS_HELLO; |
| 617 | } |
| 618 | |
| 619 | void AsyncSocksProxySocket::SendAuth() { |
Joachim Bauch | 4c6a30c | 2018-03-08 00:55:33 +0100 | [diff] [blame] | 620 | ByteBufferWriterT<ZeroOnFreeBuffer<char>> request; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 621 | request.WriteUInt8(1); // Negotiation Version |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 622 | request.WriteUInt8(static_cast<uint8_t>(user_.size())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 623 | request.WriteString(user_); // Username |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 624 | request.WriteUInt8(static_cast<uint8_t>(pass_.GetLength())); |
| 625 | size_t len = pass_.GetLength() + 1; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 626 | char* sensitive = new char[len]; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 627 | pass_.CopyTo(sensitive, true); |
Joachim Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 628 | request.WriteBytes(sensitive, pass_.GetLength()); // Password |
| 629 | ExplicitZeroMemory(sensitive, len); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 630 | delete[] sensitive; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 631 | DirectSend(request.Data(), request.Length()); |
| 632 | state_ = SS_AUTH; |
| 633 | } |
| 634 | |
| 635 | void AsyncSocksProxySocket::SendConnect() { |
| 636 | ByteBufferWriter request; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 637 | request.WriteUInt8(5); // Socks Version |
| 638 | request.WriteUInt8(1); // CONNECT |
| 639 | request.WriteUInt8(0); // Reserved |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 640 | if (dest_.IsUnresolvedIP()) { |
| 641 | std::string hostname = dest_.hostname(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 642 | request.WriteUInt8(3); // DOMAINNAME |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 643 | request.WriteUInt8(static_cast<uint8_t>(hostname.size())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 644 | request.WriteString(hostname); // Destination Hostname |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 645 | } else { |
| 646 | request.WriteUInt8(1); // IPV4 |
| 647 | request.WriteUInt32(dest_.ip()); // Destination IP |
| 648 | } |
| 649 | request.WriteUInt16(dest_.port()); // Destination Port |
| 650 | DirectSend(request.Data(), request.Length()); |
| 651 | state_ = SS_CONNECT; |
| 652 | } |
| 653 | |
| 654 | void AsyncSocksProxySocket::Error(int error) { |
| 655 | state_ = SS_ERROR; |
| 656 | BufferInput(false); |
| 657 | Close(); |
| 658 | SetError(SOCKET_EACCES); |
| 659 | SignalCloseEvent(this, error); |
| 660 | } |
| 661 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 662 | } // namespace rtc |