OLD | NEW |
| (Empty) |
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 | |
12 #pragma warning(disable:4786) | |
13 #endif | |
14 | |
15 #include <time.h> | |
16 #include <errno.h> | |
17 | |
18 #if defined(WEBRTC_WIN) | |
19 #define WIN32_LEAN_AND_MEAN | |
20 #include <windows.h> | |
21 #include <winsock2.h> | |
22 #include <ws2tcpip.h> | |
23 #define SECURITY_WIN32 | |
24 #include <security.h> | |
25 #endif | |
26 | |
27 #include <algorithm> | |
28 | |
29 #include "webrtc/base/bytebuffer.h" | |
30 #include "webrtc/base/checks.h" | |
31 #include "webrtc/base/httpcommon.h" | |
32 #include "webrtc/base/logging.h" | |
33 #include "webrtc/base/socketadapters.h" | |
34 #include "webrtc/base/stringencode.h" | |
35 #include "webrtc/base/stringutils.h" | |
36 | |
37 namespace rtc { | |
38 | |
39 BufferedReadAdapter::BufferedReadAdapter(AsyncSocket* socket, size_t size) | |
40 : AsyncSocketAdapter(socket), buffer_size_(size), | |
41 data_len_(0), buffering_(false) { | |
42 buffer_ = new char[buffer_size_]; | |
43 } | |
44 | |
45 BufferedReadAdapter::~BufferedReadAdapter() { | |
46 delete [] buffer_; | |
47 } | |
48 | |
49 int BufferedReadAdapter::Send(const void *pv, size_t cb) { | |
50 if (buffering_) { | |
51 // TODO: Spoof error better; Signal Writeable | |
52 socket_->SetError(EWOULDBLOCK); | |
53 return -1; | |
54 } | |
55 return AsyncSocketAdapter::Send(pv, cb); | |
56 } | |
57 | |
58 int BufferedReadAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { | |
59 if (buffering_) { | |
60 socket_->SetError(EWOULDBLOCK); | |
61 return -1; | |
62 } | |
63 | |
64 size_t read = 0; | |
65 | |
66 if (data_len_) { | |
67 read = std::min(cb, data_len_); | |
68 memcpy(pv, buffer_, read); | |
69 data_len_ -= read; | |
70 if (data_len_ > 0) { | |
71 memmove(buffer_, buffer_ + read, data_len_); | |
72 } | |
73 pv = static_cast<char *>(pv) + read; | |
74 cb -= read; | |
75 } | |
76 | |
77 // FIX: If cb == 0, we won't generate another read event | |
78 | |
79 int res = AsyncSocketAdapter::Recv(pv, cb, timestamp); | |
80 if (res >= 0) { | |
81 // Read from socket and possibly buffer; return combined length | |
82 return res + static_cast<int>(read); | |
83 } | |
84 | |
85 if (read > 0) { | |
86 // Failed to read from socket, but still read something from buffer | |
87 return static_cast<int>(read); | |
88 } | |
89 | |
90 // Didn't read anything; return error from socket | |
91 return res; | |
92 } | |
93 | |
94 void BufferedReadAdapter::BufferInput(bool on) { | |
95 buffering_ = on; | |
96 } | |
97 | |
98 void BufferedReadAdapter::OnReadEvent(AsyncSocket * socket) { | |
99 RTC_DCHECK(socket == socket_); | |
100 | |
101 if (!buffering_) { | |
102 AsyncSocketAdapter::OnReadEvent(socket); | |
103 return; | |
104 } | |
105 | |
106 if (data_len_ >= buffer_size_) { | |
107 LOG(INFO) << "Input buffer overflow"; | |
108 RTC_NOTREACHED(); | |
109 data_len_ = 0; | |
110 } | |
111 | |
112 int len = | |
113 socket_->Recv(buffer_ + data_len_, buffer_size_ - data_len_, nullptr); | |
114 if (len < 0) { | |
115 // TODO: Do something better like forwarding the error to the user. | |
116 LOG_ERR(INFO) << "Recv"; | |
117 return; | |
118 } | |
119 | |
120 data_len_ += len; | |
121 | |
122 ProcessInput(buffer_, &data_len_); | |
123 } | |
124 | |
125 AsyncProxyServerSocket::AsyncProxyServerSocket(AsyncSocket* socket, | |
126 size_t buffer_size) | |
127 : BufferedReadAdapter(socket, buffer_size) { | |
128 } | |
129 | |
130 AsyncProxyServerSocket::~AsyncProxyServerSocket() = default; | |
131 | |
132 /////////////////////////////////////////////////////////////////////////////// | |
133 | |
134 // This is a SSL v2 CLIENT_HELLO message. | |
135 // TODO: Should this have a session id? The response doesn't have a | |
136 // certificate, so the hello should have a session id. | |
137 static const uint8_t kSslClientHello[] = { | |
138 0x80, 0x46, // msg len | |
139 0x01, // CLIENT_HELLO | |
140 0x03, 0x01, // SSL 3.1 | |
141 0x00, 0x2d, // ciphersuite len | |
142 0x00, 0x00, // session id len | |
143 0x00, 0x10, // challenge len | |
144 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites | |
145 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, // | |
146 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, // | |
147 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, // | |
148 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, // | |
149 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge | |
150 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea // | |
151 }; | |
152 | |
153 // This is a TLSv1 SERVER_HELLO message. | |
154 static const uint8_t kSslServerHello[] = { | |
155 0x16, // handshake message | |
156 0x03, 0x01, // SSL 3.1 | |
157 0x00, 0x4a, // message len | |
158 0x02, // SERVER_HELLO | |
159 0x00, 0x00, 0x46, // handshake len | |
160 0x03, 0x01, // SSL 3.1 | |
161 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random | |
162 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, // | |
163 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, // | |
164 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, // | |
165 0x20, // session id len | |
166 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id | |
167 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, // | |
168 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, // | |
169 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, // | |
170 0x00, 0x04, // RSA/RC4-128/MD5 | |
171 0x00 // null compression | |
172 }; | |
173 | |
174 AsyncSSLSocket::AsyncSSLSocket(AsyncSocket* socket) | |
175 : BufferedReadAdapter(socket, 1024) { | |
176 } | |
177 | |
178 int AsyncSSLSocket::Connect(const SocketAddress& addr) { | |
179 // Begin buffering before we connect, so that there isn't a race condition | |
180 // between potential senders and receiving the OnConnectEvent signal | |
181 BufferInput(true); | |
182 return BufferedReadAdapter::Connect(addr); | |
183 } | |
184 | |
185 void AsyncSSLSocket::OnConnectEvent(AsyncSocket * socket) { | |
186 RTC_DCHECK(socket == socket_); | |
187 // TODO: we could buffer output too... | |
188 const int res = DirectSend(kSslClientHello, sizeof(kSslClientHello)); | |
189 RTC_DCHECK_EQ(sizeof(kSslClientHello), res); | |
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) { | |
197 Close(); | |
198 SignalCloseEvent(this, 0); // TODO: error code? | |
199 return; | |
200 } | |
201 | |
202 *len -= sizeof(kSslServerHello); | |
203 if (*len > 0) { | |
204 memmove(data, data + sizeof(kSslServerHello), *len); | |
205 } | |
206 | |
207 bool remainder = (*len > 0); | |
208 BufferInput(false); | |
209 SignalConnectEvent(this); | |
210 | |
211 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble | |
212 if (remainder) | |
213 SignalReadEvent(this); | |
214 } | |
215 | |
216 AsyncSSLServerSocket::AsyncSSLServerSocket(AsyncSocket* socket) | |
217 : BufferedReadAdapter(socket, 1024) { | |
218 BufferInput(true); | |
219 } | |
220 | |
221 void AsyncSSLServerSocket::ProcessInput(char* data, size_t* len) { | |
222 // We only accept client hello messages. | |
223 if (*len < sizeof(kSslClientHello)) { | |
224 return; | |
225 } | |
226 | |
227 if (memcmp(kSslClientHello, data, sizeof(kSslClientHello)) != 0) { | |
228 Close(); | |
229 SignalCloseEvent(this, 0); | |
230 return; | |
231 } | |
232 | |
233 *len -= sizeof(kSslClientHello); | |
234 | |
235 // Clients should not send more data until the handshake is completed. | |
236 RTC_DCHECK(*len == 0); | |
237 | |
238 // Send a server hello back to the client. | |
239 DirectSend(kSslServerHello, sizeof(kSslServerHello)); | |
240 | |
241 // Handshake completed for us, redirect input to our parent. | |
242 BufferInput(false); | |
243 } | |
244 | |
245 /////////////////////////////////////////////////////////////////////////////// | |
246 | |
247 AsyncHttpsProxySocket::AsyncHttpsProxySocket(AsyncSocket* socket, | |
248 const std::string& user_agent, | |
249 const SocketAddress& proxy, | |
250 const std::string& username, | |
251 const CryptString& password) | |
252 : BufferedReadAdapter(socket, 1024), proxy_(proxy), agent_(user_agent), | |
253 user_(username), pass_(password), force_connect_(false), state_(PS_ERROR), | |
254 context_(0) { | |
255 } | |
256 | |
257 AsyncHttpsProxySocket::~AsyncHttpsProxySocket() { | |
258 delete context_; | |
259 } | |
260 | |
261 int AsyncHttpsProxySocket::Connect(const SocketAddress& addr) { | |
262 int ret; | |
263 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::Connect(" | |
264 << proxy_.ToSensitiveString() << ")"; | |
265 dest_ = addr; | |
266 state_ = PS_INIT; | |
267 if (ShouldIssueConnect()) { | |
268 BufferInput(true); | |
269 } | |
270 ret = BufferedReadAdapter::Connect(proxy_); | |
271 // TODO: Set state_ appropriately if Connect fails. | |
272 return ret; | |
273 } | |
274 | |
275 SocketAddress AsyncHttpsProxySocket::GetRemoteAddress() const { | |
276 return dest_; | |
277 } | |
278 | |
279 int AsyncHttpsProxySocket::Close() { | |
280 headers_.clear(); | |
281 state_ = PS_ERROR; | |
282 dest_.Clear(); | |
283 delete context_; | |
284 context_ = nullptr; | |
285 return BufferedReadAdapter::Close(); | |
286 } | |
287 | |
288 Socket::ConnState AsyncHttpsProxySocket::GetState() const { | |
289 if (state_ < PS_TUNNEL) { | |
290 return CS_CONNECTING; | |
291 } else if (state_ == PS_TUNNEL) { | |
292 return CS_CONNECTED; | |
293 } else { | |
294 return CS_CLOSED; | |
295 } | |
296 } | |
297 | |
298 void AsyncHttpsProxySocket::OnConnectEvent(AsyncSocket * socket) { | |
299 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnConnectEvent"; | |
300 if (!ShouldIssueConnect()) { | |
301 state_ = PS_TUNNEL; | |
302 BufferedReadAdapter::OnConnectEvent(socket); | |
303 return; | |
304 } | |
305 SendRequest(); | |
306 } | |
307 | |
308 void AsyncHttpsProxySocket::OnCloseEvent(AsyncSocket * socket, int err) { | |
309 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnCloseEvent(" << err << ")"; | |
310 if ((state_ == PS_WAIT_CLOSE) && (err == 0)) { | |
311 state_ = PS_ERROR; | |
312 Connect(dest_); | |
313 } else { | |
314 BufferedReadAdapter::OnCloseEvent(socket, err); | |
315 } | |
316 } | |
317 | |
318 void AsyncHttpsProxySocket::ProcessInput(char* data, size_t* len) { | |
319 size_t start = 0; | |
320 for (size_t pos = start; state_ < PS_TUNNEL && pos < *len;) { | |
321 if (state_ == PS_SKIP_BODY) { | |
322 size_t consume = std::min(*len - pos, content_length_); | |
323 pos += consume; | |
324 start = pos; | |
325 content_length_ -= consume; | |
326 if (content_length_ == 0) { | |
327 EndResponse(); | |
328 } | |
329 continue; | |
330 } | |
331 | |
332 if (data[pos++] != '\n') | |
333 continue; | |
334 | |
335 size_t len = pos - start - 1; | |
336 if ((len > 0) && (data[start + len - 1] == '\r')) | |
337 --len; | |
338 | |
339 data[start + len] = 0; | |
340 ProcessLine(data + start, len); | |
341 start = pos; | |
342 } | |
343 | |
344 *len -= start; | |
345 if (*len > 0) { | |
346 memmove(data, data + start, *len); | |
347 } | |
348 | |
349 if (state_ != PS_TUNNEL) | |
350 return; | |
351 | |
352 bool remainder = (*len > 0); | |
353 BufferInput(false); | |
354 SignalConnectEvent(this); | |
355 | |
356 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble | |
357 if (remainder) | |
358 SignalReadEvent(this); // TODO: signal this?? | |
359 } | |
360 | |
361 bool AsyncHttpsProxySocket::ShouldIssueConnect() const { | |
362 // TODO: Think about whether a more sophisticated test | |
363 // than dest port == 80 is needed. | |
364 return force_connect_ || (dest_.port() != 80); | |
365 } | |
366 | |
367 void AsyncHttpsProxySocket::SendRequest() { | |
368 std::stringstream ss; | |
369 ss << "CONNECT " << dest_.ToString() << " HTTP/1.0\r\n"; | |
370 ss << "User-Agent: " << agent_ << "\r\n"; | |
371 ss << "Host: " << dest_.HostAsURIString() << "\r\n"; | |
372 ss << "Content-Length: 0\r\n"; | |
373 ss << "Proxy-Connection: Keep-Alive\r\n"; | |
374 ss << headers_; | |
375 ss << "\r\n"; | |
376 std::string str = ss.str(); | |
377 DirectSend(str.c_str(), str.size()); | |
378 state_ = PS_LEADER; | |
379 expect_close_ = true; | |
380 content_length_ = 0; | |
381 headers_.clear(); | |
382 | |
383 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket >> " << str; | |
384 } | |
385 | |
386 void AsyncHttpsProxySocket::ProcessLine(char * data, size_t len) { | |
387 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket << " << data; | |
388 | |
389 if (len == 0) { | |
390 if (state_ == PS_TUNNEL_HEADERS) { | |
391 state_ = PS_TUNNEL; | |
392 } else if (state_ == PS_ERROR_HEADERS) { | |
393 Error(defer_error_); | |
394 return; | |
395 } else if (state_ == PS_SKIP_HEADERS) { | |
396 if (content_length_) { | |
397 state_ = PS_SKIP_BODY; | |
398 } else { | |
399 EndResponse(); | |
400 return; | |
401 } | |
402 } else { | |
403 static bool report = false; | |
404 if (!unknown_mechanisms_.empty() && !report) { | |
405 report = true; | |
406 std::string msg( | |
407 "Unable to connect to the Google Talk service due to an incompatibilit
y " | |
408 "with your proxy.\r\nPlease help us resolve this issue by submitting t
he " | |
409 "following information to us using our technical issue submission form
" | |
410 "at:\r\n\r\n" | |
411 "http://www.google.com/support/talk/bin/request.py\r\n\r\n" | |
412 "We apologize for the inconvenience.\r\n\r\n" | |
413 "Information to submit to Google: " | |
414 ); | |
415 //std::string msg("Please report the following information to foo@bar.co
m:\r\nUnknown methods: "); | |
416 msg.append(unknown_mechanisms_); | |
417 #if defined(WEBRTC_WIN) | |
418 MessageBoxA(0, msg.c_str(), "Oops!", MB_OK); | |
419 #endif | |
420 #if defined(WEBRTC_POSIX) | |
421 // TODO: Raise a signal so the UI can be separated. | |
422 LOG(LS_ERROR) << "Oops!\n\n" << msg; | |
423 #endif | |
424 } | |
425 // Unexpected end of headers | |
426 Error(0); | |
427 return; | |
428 } | |
429 } else if (state_ == PS_LEADER) { | |
430 unsigned int code; | |
431 if (sscanf(data, "HTTP/%*u.%*u %u", &code) != 1) { | |
432 Error(0); | |
433 return; | |
434 } | |
435 switch (code) { | |
436 case 200: | |
437 // connection good! | |
438 state_ = PS_TUNNEL_HEADERS; | |
439 return; | |
440 #if defined(HTTP_STATUS_PROXY_AUTH_REQ) && (HTTP_STATUS_PROXY_AUTH_REQ != 407) | |
441 #error Wrong code for HTTP_STATUS_PROXY_AUTH_REQ | |
442 #endif | |
443 case 407: // HTTP_STATUS_PROXY_AUTH_REQ | |
444 state_ = PS_AUTHENTICATE; | |
445 return; | |
446 default: | |
447 defer_error_ = 0; | |
448 state_ = PS_ERROR_HEADERS; | |
449 return; | |
450 } | |
451 } else if ((state_ == PS_AUTHENTICATE) | |
452 && (_strnicmp(data, "Proxy-Authenticate:", 19) == 0)) { | |
453 std::string response, auth_method; | |
454 switch (HttpAuthenticate(data + 19, len - 19, | |
455 proxy_, "CONNECT", "/", | |
456 user_, pass_, context_, response, auth_method)) { | |
457 case HAR_IGNORE: | |
458 LOG(LS_VERBOSE) << "Ignoring Proxy-Authenticate: " << auth_method; | |
459 if (!unknown_mechanisms_.empty()) | |
460 unknown_mechanisms_.append(", "); | |
461 unknown_mechanisms_.append(auth_method); | |
462 break; | |
463 case HAR_RESPONSE: | |
464 headers_ = "Proxy-Authorization: "; | |
465 headers_.append(response); | |
466 headers_.append("\r\n"); | |
467 state_ = PS_SKIP_HEADERS; | |
468 unknown_mechanisms_.clear(); | |
469 break; | |
470 case HAR_CREDENTIALS: | |
471 defer_error_ = SOCKET_EACCES; | |
472 state_ = PS_ERROR_HEADERS; | |
473 unknown_mechanisms_.clear(); | |
474 break; | |
475 case HAR_ERROR: | |
476 defer_error_ = 0; | |
477 state_ = PS_ERROR_HEADERS; | |
478 unknown_mechanisms_.clear(); | |
479 break; | |
480 } | |
481 } else if (_strnicmp(data, "Content-Length:", 15) == 0) { | |
482 content_length_ = strtoul(data + 15, 0, 0); | |
483 } else if (_strnicmp(data, "Proxy-Connection: Keep-Alive", 28) == 0) { | |
484 expect_close_ = false; | |
485 /* | |
486 } else if (_strnicmp(data, "Connection: close", 17) == 0) { | |
487 expect_close_ = true; | |
488 */ | |
489 } | |
490 } | |
491 | |
492 void AsyncHttpsProxySocket::EndResponse() { | |
493 if (!expect_close_) { | |
494 SendRequest(); | |
495 return; | |
496 } | |
497 | |
498 // No point in waiting for the server to close... let's close now | |
499 // TODO: Refactor out PS_WAIT_CLOSE | |
500 state_ = PS_WAIT_CLOSE; | |
501 BufferedReadAdapter::Close(); | |
502 OnCloseEvent(this, 0); | |
503 } | |
504 | |
505 void AsyncHttpsProxySocket::Error(int error) { | |
506 BufferInput(false); | |
507 Close(); | |
508 SetError(error); | |
509 SignalCloseEvent(this, error); | |
510 } | |
511 | |
512 /////////////////////////////////////////////////////////////////////////////// | |
513 | |
514 AsyncSocksProxySocket::AsyncSocksProxySocket(AsyncSocket* socket, | |
515 const SocketAddress& proxy, | |
516 const std::string& username, | |
517 const CryptString& password) | |
518 : BufferedReadAdapter(socket, 1024), state_(SS_ERROR), proxy_(proxy), | |
519 user_(username), pass_(password) { | |
520 } | |
521 | |
522 AsyncSocksProxySocket::~AsyncSocksProxySocket() = default; | |
523 | |
524 int AsyncSocksProxySocket::Connect(const SocketAddress& addr) { | |
525 int ret; | |
526 dest_ = addr; | |
527 state_ = SS_INIT; | |
528 BufferInput(true); | |
529 ret = BufferedReadAdapter::Connect(proxy_); | |
530 // TODO: Set state_ appropriately if Connect fails. | |
531 return ret; | |
532 } | |
533 | |
534 SocketAddress AsyncSocksProxySocket::GetRemoteAddress() const { | |
535 return dest_; | |
536 } | |
537 | |
538 int AsyncSocksProxySocket::Close() { | |
539 state_ = SS_ERROR; | |
540 dest_.Clear(); | |
541 return BufferedReadAdapter::Close(); | |
542 } | |
543 | |
544 Socket::ConnState AsyncSocksProxySocket::GetState() const { | |
545 if (state_ < SS_TUNNEL) { | |
546 return CS_CONNECTING; | |
547 } else if (state_ == SS_TUNNEL) { | |
548 return CS_CONNECTED; | |
549 } else { | |
550 return CS_CLOSED; | |
551 } | |
552 } | |
553 | |
554 void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) { | |
555 SendHello(); | |
556 } | |
557 | |
558 void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { | |
559 RTC_DCHECK(state_ < SS_TUNNEL); | |
560 | |
561 ByteBufferReader response(data, *len); | |
562 | |
563 if (state_ == SS_HELLO) { | |
564 uint8_t ver, method; | |
565 if (!response.ReadUInt8(&ver) || | |
566 !response.ReadUInt8(&method)) | |
567 return; | |
568 | |
569 if (ver != 5) { | |
570 Error(0); | |
571 return; | |
572 } | |
573 | |
574 if (method == 0) { | |
575 SendConnect(); | |
576 } else if (method == 2) { | |
577 SendAuth(); | |
578 } else { | |
579 Error(0); | |
580 return; | |
581 } | |
582 } else if (state_ == SS_AUTH) { | |
583 uint8_t ver, status; | |
584 if (!response.ReadUInt8(&ver) || | |
585 !response.ReadUInt8(&status)) | |
586 return; | |
587 | |
588 if ((ver != 1) || (status != 0)) { | |
589 Error(SOCKET_EACCES); | |
590 return; | |
591 } | |
592 | |
593 SendConnect(); | |
594 } else if (state_ == SS_CONNECT) { | |
595 uint8_t ver, rep, rsv, atyp; | |
596 if (!response.ReadUInt8(&ver) || | |
597 !response.ReadUInt8(&rep) || | |
598 !response.ReadUInt8(&rsv) || | |
599 !response.ReadUInt8(&atyp)) | |
600 return; | |
601 | |
602 if ((ver != 5) || (rep != 0)) { | |
603 Error(0); | |
604 return; | |
605 } | |
606 | |
607 uint16_t port; | |
608 if (atyp == 1) { | |
609 uint32_t addr; | |
610 if (!response.ReadUInt32(&addr) || | |
611 !response.ReadUInt16(&port)) | |
612 return; | |
613 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; | |
614 } else if (atyp == 3) { | |
615 uint8_t len; | |
616 std::string addr; | |
617 if (!response.ReadUInt8(&len) || | |
618 !response.ReadString(&addr, len) || | |
619 !response.ReadUInt16(&port)) | |
620 return; | |
621 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; | |
622 } else if (atyp == 4) { | |
623 std::string addr; | |
624 if (!response.ReadString(&addr, 16) || | |
625 !response.ReadUInt16(&port)) | |
626 return; | |
627 LOG(LS_VERBOSE) << "Bound on <IPV6>:" << port; | |
628 } else { | |
629 Error(0); | |
630 return; | |
631 } | |
632 | |
633 state_ = SS_TUNNEL; | |
634 } | |
635 | |
636 // Consume parsed data | |
637 *len = response.Length(); | |
638 memmove(data, response.Data(), *len); | |
639 | |
640 if (state_ != SS_TUNNEL) | |
641 return; | |
642 | |
643 bool remainder = (*len > 0); | |
644 BufferInput(false); | |
645 SignalConnectEvent(this); | |
646 | |
647 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble | |
648 if (remainder) | |
649 SignalReadEvent(this); // TODO: signal this?? | |
650 } | |
651 | |
652 void AsyncSocksProxySocket::SendHello() { | |
653 ByteBufferWriter request; | |
654 request.WriteUInt8(5); // Socks Version | |
655 if (user_.empty()) { | |
656 request.WriteUInt8(1); // Authentication Mechanisms | |
657 request.WriteUInt8(0); // No authentication | |
658 } else { | |
659 request.WriteUInt8(2); // Authentication Mechanisms | |
660 request.WriteUInt8(0); // No authentication | |
661 request.WriteUInt8(2); // Username/Password | |
662 } | |
663 DirectSend(request.Data(), request.Length()); | |
664 state_ = SS_HELLO; | |
665 } | |
666 | |
667 void AsyncSocksProxySocket::SendAuth() { | |
668 ByteBufferWriter request; | |
669 request.WriteUInt8(1); // Negotiation Version | |
670 request.WriteUInt8(static_cast<uint8_t>(user_.size())); | |
671 request.WriteString(user_); // Username | |
672 request.WriteUInt8(static_cast<uint8_t>(pass_.GetLength())); | |
673 size_t len = pass_.GetLength() + 1; | |
674 char * sensitive = new char[len]; | |
675 pass_.CopyTo(sensitive, true); | |
676 request.WriteString(sensitive); // Password | |
677 memset(sensitive, 0, len); | |
678 delete [] sensitive; | |
679 DirectSend(request.Data(), request.Length()); | |
680 state_ = SS_AUTH; | |
681 } | |
682 | |
683 void AsyncSocksProxySocket::SendConnect() { | |
684 ByteBufferWriter request; | |
685 request.WriteUInt8(5); // Socks Version | |
686 request.WriteUInt8(1); // CONNECT | |
687 request.WriteUInt8(0); // Reserved | |
688 if (dest_.IsUnresolvedIP()) { | |
689 std::string hostname = dest_.hostname(); | |
690 request.WriteUInt8(3); // DOMAINNAME | |
691 request.WriteUInt8(static_cast<uint8_t>(hostname.size())); | |
692 request.WriteString(hostname); // Destination Hostname | |
693 } else { | |
694 request.WriteUInt8(1); // IPV4 | |
695 request.WriteUInt32(dest_.ip()); // Destination IP | |
696 } | |
697 request.WriteUInt16(dest_.port()); // Destination Port | |
698 DirectSend(request.Data(), request.Length()); | |
699 state_ = SS_CONNECT; | |
700 } | |
701 | |
702 void AsyncSocksProxySocket::Error(int error) { | |
703 state_ = SS_ERROR; | |
704 BufferInput(false); | |
705 Close(); | |
706 SetError(SOCKET_EACCES); | |
707 SignalCloseEvent(this, error); | |
708 } | |
709 | |
710 AsyncSocksProxyServerSocket::AsyncSocksProxyServerSocket(AsyncSocket* socket) | |
711 : AsyncProxyServerSocket(socket, kBufferSize), state_(SS_HELLO) { | |
712 BufferInput(true); | |
713 } | |
714 | |
715 void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) { | |
716 // TODO: See if the whole message has arrived | |
717 RTC_DCHECK(state_ < SS_CONNECT_PENDING); | |
718 | |
719 ByteBufferReader response(data, *len); | |
720 if (state_ == SS_HELLO) { | |
721 HandleHello(&response); | |
722 } else if (state_ == SS_AUTH) { | |
723 HandleAuth(&response); | |
724 } else if (state_ == SS_CONNECT) { | |
725 HandleConnect(&response); | |
726 } | |
727 | |
728 // Consume parsed data | |
729 *len = response.Length(); | |
730 memmove(data, response.Data(), *len); | |
731 } | |
732 | |
733 void AsyncSocksProxyServerSocket::DirectSend(const ByteBufferWriter& buf) { | |
734 BufferedReadAdapter::DirectSend(buf.Data(), buf.Length()); | |
735 } | |
736 | |
737 void AsyncSocksProxyServerSocket::HandleHello(ByteBufferReader* request) { | |
738 uint8_t ver, num_methods; | |
739 if (!request->ReadUInt8(&ver) || | |
740 !request->ReadUInt8(&num_methods)) { | |
741 Error(0); | |
742 return; | |
743 } | |
744 | |
745 if (ver != 5) { | |
746 Error(0); | |
747 return; | |
748 } | |
749 | |
750 // Handle either no-auth (0) or user/pass auth (2) | |
751 uint8_t method = 0xFF; | |
752 if (num_methods > 0 && !request->ReadUInt8(&method)) { | |
753 Error(0); | |
754 return; | |
755 } | |
756 | |
757 // TODO: Ask the server which method to use. | |
758 SendHelloReply(method); | |
759 if (method == 0) { | |
760 state_ = SS_CONNECT; | |
761 } else if (method == 2) { | |
762 state_ = SS_AUTH; | |
763 } else { | |
764 state_ = SS_ERROR; | |
765 } | |
766 } | |
767 | |
768 void AsyncSocksProxyServerSocket::SendHelloReply(uint8_t method) { | |
769 ByteBufferWriter response; | |
770 response.WriteUInt8(5); // Socks Version | |
771 response.WriteUInt8(method); // Auth method | |
772 DirectSend(response); | |
773 } | |
774 | |
775 void AsyncSocksProxyServerSocket::HandleAuth(ByteBufferReader* request) { | |
776 uint8_t ver, user_len, pass_len; | |
777 std::string user, pass; | |
778 if (!request->ReadUInt8(&ver) || | |
779 !request->ReadUInt8(&user_len) || | |
780 !request->ReadString(&user, user_len) || | |
781 !request->ReadUInt8(&pass_len) || | |
782 !request->ReadString(&pass, pass_len)) { | |
783 Error(0); | |
784 return; | |
785 } | |
786 | |
787 // TODO: Allow for checking of credentials. | |
788 SendAuthReply(0); | |
789 state_ = SS_CONNECT; | |
790 } | |
791 | |
792 void AsyncSocksProxyServerSocket::SendAuthReply(uint8_t result) { | |
793 ByteBufferWriter response; | |
794 response.WriteUInt8(1); // Negotiation Version | |
795 response.WriteUInt8(result); | |
796 DirectSend(response); | |
797 } | |
798 | |
799 void AsyncSocksProxyServerSocket::HandleConnect(ByteBufferReader* request) { | |
800 uint8_t ver, command, reserved, addr_type; | |
801 uint32_t ip; | |
802 uint16_t port; | |
803 if (!request->ReadUInt8(&ver) || | |
804 !request->ReadUInt8(&command) || | |
805 !request->ReadUInt8(&reserved) || | |
806 !request->ReadUInt8(&addr_type) || | |
807 !request->ReadUInt32(&ip) || | |
808 !request->ReadUInt16(&port)) { | |
809 Error(0); | |
810 return; | |
811 } | |
812 | |
813 if (ver != 5 || command != 1 || | |
814 reserved != 0 || addr_type != 1) { | |
815 Error(0); | |
816 return; | |
817 } | |
818 | |
819 SignalConnectRequest(this, SocketAddress(ip, port)); | |
820 state_ = SS_CONNECT_PENDING; | |
821 } | |
822 | |
823 void AsyncSocksProxyServerSocket::SendConnectResult(int result, | |
824 const SocketAddress& addr) { | |
825 if (state_ != SS_CONNECT_PENDING) | |
826 return; | |
827 | |
828 ByteBufferWriter response; | |
829 response.WriteUInt8(5); // Socks version | |
830 response.WriteUInt8((result != 0)); // 0x01 is generic error | |
831 response.WriteUInt8(0); // reserved | |
832 response.WriteUInt8(1); // IPv4 address | |
833 response.WriteUInt32(addr.ip()); | |
834 response.WriteUInt16(addr.port()); | |
835 DirectSend(response); | |
836 BufferInput(false); | |
837 state_ = SS_TUNNEL; | |
838 } | |
839 | |
840 void AsyncSocksProxyServerSocket::Error(int error) { | |
841 state_ = SS_ERROR; | |
842 BufferInput(false); | |
843 Close(); | |
844 SetError(SOCKET_EACCES); | |
845 SignalCloseEvent(this, error); | |
846 } | |
847 | |
848 } // namespace rtc | |
OLD | NEW |