You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/server_socket_adapters.h"
  11. #include <string>
  12. #include "rtc_base/byte_buffer.h"
  13. namespace rtc {
  14. AsyncProxyServerSocket::AsyncProxyServerSocket(AsyncSocket* socket,
  15. size_t buffer_size)
  16. : BufferedReadAdapter(socket, buffer_size) {}
  17. AsyncProxyServerSocket::~AsyncProxyServerSocket() = default;
  18. AsyncSSLServerSocket::AsyncSSLServerSocket(AsyncSocket* socket)
  19. : BufferedReadAdapter(socket, 1024) {
  20. BufferInput(true);
  21. }
  22. void AsyncSSLServerSocket::ProcessInput(char* data, size_t* len) {
  23. // We only accept client hello messages.
  24. const ArrayView<const uint8_t> client_hello =
  25. AsyncSSLSocket::SslClientHello();
  26. if (*len < client_hello.size()) {
  27. return;
  28. }
  29. if (memcmp(client_hello.data(), data, client_hello.size()) != 0) {
  30. Close();
  31. SignalCloseEvent(this, 0);
  32. return;
  33. }
  34. *len -= client_hello.size();
  35. // Clients should not send more data until the handshake is completed.
  36. RTC_DCHECK(*len == 0);
  37. const ArrayView<const uint8_t> server_hello =
  38. AsyncSSLSocket::SslServerHello();
  39. // Send a server hello back to the client.
  40. DirectSend(server_hello.data(), server_hello.size());
  41. // Handshake completed for us, redirect input to our parent.
  42. BufferInput(false);
  43. }
  44. AsyncSocksProxyServerSocket::AsyncSocksProxyServerSocket(AsyncSocket* socket)
  45. : AsyncProxyServerSocket(socket, kBufferSize), state_(SS_HELLO) {
  46. BufferInput(true);
  47. }
  48. void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) {
  49. RTC_DCHECK(state_ < SS_CONNECT_PENDING);
  50. ByteBufferReader response(data, *len);
  51. if (state_ == SS_HELLO) {
  52. HandleHello(&response);
  53. } else if (state_ == SS_AUTH) {
  54. HandleAuth(&response);
  55. } else if (state_ == SS_CONNECT) {
  56. HandleConnect(&response);
  57. }
  58. // Consume parsed data
  59. *len = response.Length();
  60. memmove(data, response.Data(), *len);
  61. }
  62. void AsyncSocksProxyServerSocket::DirectSend(const ByteBufferWriter& buf) {
  63. BufferedReadAdapter::DirectSend(buf.Data(), buf.Length());
  64. }
  65. void AsyncSocksProxyServerSocket::HandleHello(ByteBufferReader* request) {
  66. uint8_t ver, num_methods;
  67. if (!request->ReadUInt8(&ver) || !request->ReadUInt8(&num_methods)) {
  68. Error(0);
  69. return;
  70. }
  71. if (ver != 5) {
  72. Error(0);
  73. return;
  74. }
  75. // Handle either no-auth (0) or user/pass auth (2)
  76. uint8_t method = 0xFF;
  77. if (num_methods > 0 && !request->ReadUInt8(&method)) {
  78. Error(0);
  79. return;
  80. }
  81. SendHelloReply(method);
  82. if (method == 0) {
  83. state_ = SS_CONNECT;
  84. } else if (method == 2) {
  85. state_ = SS_AUTH;
  86. } else {
  87. state_ = SS_ERROR;
  88. }
  89. }
  90. void AsyncSocksProxyServerSocket::SendHelloReply(uint8_t method) {
  91. ByteBufferWriter response;
  92. response.WriteUInt8(5); // Socks Version
  93. response.WriteUInt8(method); // Auth method
  94. DirectSend(response);
  95. }
  96. void AsyncSocksProxyServerSocket::HandleAuth(ByteBufferReader* request) {
  97. uint8_t ver, user_len, pass_len;
  98. std::string user, pass;
  99. if (!request->ReadUInt8(&ver) || !request->ReadUInt8(&user_len) ||
  100. !request->ReadString(&user, user_len) || !request->ReadUInt8(&pass_len) ||
  101. !request->ReadString(&pass, pass_len)) {
  102. Error(0);
  103. return;
  104. }
  105. SendAuthReply(0);
  106. state_ = SS_CONNECT;
  107. }
  108. void AsyncSocksProxyServerSocket::SendAuthReply(uint8_t result) {
  109. ByteBufferWriter response;
  110. response.WriteUInt8(1); // Negotiation Version
  111. response.WriteUInt8(result);
  112. DirectSend(response);
  113. }
  114. void AsyncSocksProxyServerSocket::HandleConnect(ByteBufferReader* request) {
  115. uint8_t ver, command, reserved, addr_type;
  116. uint32_t ip;
  117. uint16_t port;
  118. if (!request->ReadUInt8(&ver) || !request->ReadUInt8(&command) ||
  119. !request->ReadUInt8(&reserved) || !request->ReadUInt8(&addr_type) ||
  120. !request->ReadUInt32(&ip) || !request->ReadUInt16(&port)) {
  121. Error(0);
  122. return;
  123. }
  124. if (ver != 5 || command != 1 || reserved != 0 || addr_type != 1) {
  125. Error(0);
  126. return;
  127. }
  128. SignalConnectRequest(this, SocketAddress(ip, port));
  129. state_ = SS_CONNECT_PENDING;
  130. }
  131. void AsyncSocksProxyServerSocket::SendConnectResult(int result,
  132. const SocketAddress& addr) {
  133. if (state_ != SS_CONNECT_PENDING)
  134. return;
  135. ByteBufferWriter response;
  136. response.WriteUInt8(5); // Socks version
  137. response.WriteUInt8((result != 0)); // 0x01 is generic error
  138. response.WriteUInt8(0); // reserved
  139. response.WriteUInt8(1); // IPv4 address
  140. response.WriteUInt32(addr.ip());
  141. response.WriteUInt16(addr.port());
  142. DirectSend(response);
  143. BufferInput(false);
  144. state_ = SS_TUNNEL;
  145. }
  146. void AsyncSocksProxyServerSocket::Error(int error) {
  147. state_ = SS_ERROR;
  148. BufferInput(false);
  149. Close();
  150. SetError(SOCKET_EACCES);
  151. SignalCloseEvent(this, error);
  152. }
  153. } // namespace rtc