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.

261 lines
8.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_SOCKETADAPTERS_H_
  28. #define TALK_BASE_SOCKETADAPTERS_H_
  29. #include <map>
  30. #include <string>
  31. #include "talk/base/asyncsocket.h"
  32. #include "talk/base/cryptstring.h"
  33. #include "talk/base/logging.h"
  34. namespace talk_base {
  35. struct HttpAuthContext;
  36. class ByteBuffer;
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // Implements a socket adapter that can buffer and process data internally,
  39. // as in the case of connecting to a proxy, where you must speak the proxy
  40. // protocol before commencing normal socket behavior.
  41. class BufferedReadAdapter : public AsyncSocketAdapter {
  42. public:
  43. BufferedReadAdapter(AsyncSocket* socket, size_t buffer_size);
  44. virtual ~BufferedReadAdapter();
  45. virtual int Send(const void* pv, size_t cb);
  46. virtual int Recv(void* pv, size_t cb);
  47. protected:
  48. int DirectSend(const void* pv, size_t cb) {
  49. return AsyncSocketAdapter::Send(pv, cb);
  50. }
  51. void BufferInput(bool on = true);
  52. virtual void ProcessInput(char* data, size_t* len) = 0;
  53. virtual void OnReadEvent(AsyncSocket * socket);
  54. private:
  55. char * buffer_;
  56. size_t buffer_size_, data_len_;
  57. bool buffering_;
  58. DISALLOW_EVIL_CONSTRUCTORS(BufferedReadAdapter);
  59. };
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // Interface for implementing proxy server sockets.
  62. class AsyncProxyServerSocket : public BufferedReadAdapter {
  63. public:
  64. AsyncProxyServerSocket(AsyncSocket* socket, size_t buffer_size)
  65. : BufferedReadAdapter(socket, buffer_size) {}
  66. sigslot::signal2<AsyncProxyServerSocket*,
  67. const SocketAddress&> SignalConnectRequest;
  68. virtual void SendConnectResult(int err, const SocketAddress& addr) = 0;
  69. };
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // Implements a socket adapter that performs the client side of a
  72. // fake SSL handshake. Used for "ssltcp" P2P functionality.
  73. class AsyncSSLSocket : public BufferedReadAdapter {
  74. public:
  75. explicit AsyncSSLSocket(AsyncSocket* socket);
  76. virtual int Connect(const SocketAddress& addr);
  77. protected:
  78. virtual void OnConnectEvent(AsyncSocket* socket);
  79. virtual void ProcessInput(char* data, size_t* len);
  80. DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLSocket);
  81. };
  82. // Implements a socket adapter that performs the server side of a
  83. // fake SSL handshake. Used when implementing a relay server that does "ssltcp".
  84. class AsyncSSLServerSocket : public BufferedReadAdapter {
  85. public:
  86. explicit AsyncSSLServerSocket(AsyncSocket* socket);
  87. protected:
  88. virtual void ProcessInput(char* data, size_t* len);
  89. DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLServerSocket);
  90. };
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // Implements a socket adapter that speaks the HTTP/S proxy protocol.
  93. class AsyncHttpsProxySocket : public BufferedReadAdapter {
  94. public:
  95. AsyncHttpsProxySocket(AsyncSocket* socket, const std::string& user_agent,
  96. const SocketAddress& proxy,
  97. const std::string& username, const CryptString& password);
  98. virtual ~AsyncHttpsProxySocket();
  99. // If connect is forced, the adapter will always issue an HTTP CONNECT to the
  100. // target address. Otherwise, it will connect only if the destination port
  101. // is not port 80.
  102. void SetForceConnect(bool force) { force_connect_ = force; }
  103. virtual int Connect(const SocketAddress& addr);
  104. virtual SocketAddress GetRemoteAddress() const;
  105. virtual int Close();
  106. virtual ConnState GetState() const;
  107. protected:
  108. virtual void OnConnectEvent(AsyncSocket* socket);
  109. virtual void OnCloseEvent(AsyncSocket* socket, int err);
  110. virtual void ProcessInput(char* data, size_t* len);
  111. bool ShouldIssueConnect() const;
  112. void SendRequest();
  113. void ProcessLine(char* data, size_t len);
  114. void EndResponse();
  115. void Error(int error);
  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, PS_LEADER, PS_AUTHENTICATE, PS_SKIP_HEADERS, PS_ERROR_HEADERS,
  126. PS_TUNNEL_HEADERS, PS_SKIP_BODY, PS_TUNNEL, PS_WAIT_CLOSE, PS_ERROR
  127. } state_;
  128. HttpAuthContext * context_;
  129. std::string unknown_mechanisms_;
  130. DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxySocket);
  131. };
  132. /* TODO: Implement this.
  133. class AsyncHttpsProxyServerSocket : public AsyncProxyServerSocket {
  134. public:
  135. explicit AsyncHttpsProxyServerSocket(AsyncSocket* socket);
  136. private:
  137. virtual void ProcessInput(char * data, size_t& len);
  138. void Error(int error);
  139. DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxyServerSocket);
  140. };
  141. */
  142. ///////////////////////////////////////////////////////////////////////////////
  143. // Implements a socket adapter that speaks the SOCKS proxy protocol.
  144. class AsyncSocksProxySocket : public BufferedReadAdapter {
  145. public:
  146. AsyncSocksProxySocket(AsyncSocket* socket, const SocketAddress& proxy,
  147. const std::string& username, const CryptString& password);
  148. virtual int Connect(const SocketAddress& addr);
  149. virtual SocketAddress GetRemoteAddress() const;
  150. virtual int Close();
  151. virtual ConnState GetState() const;
  152. protected:
  153. virtual void OnConnectEvent(AsyncSocket* socket);
  154. virtual void ProcessInput(char* data, size_t* len);
  155. void SendHello();
  156. void SendConnect();
  157. void SendAuth();
  158. void Error(int error);
  159. private:
  160. enum State {
  161. SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR
  162. };
  163. State state_;
  164. SocketAddress proxy_, dest_;
  165. std::string user_;
  166. CryptString pass_;
  167. DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxySocket);
  168. };
  169. // Implements a proxy server socket for the SOCKS protocol.
  170. class AsyncSocksProxyServerSocket : public AsyncProxyServerSocket {
  171. public:
  172. explicit AsyncSocksProxyServerSocket(AsyncSocket* socket);
  173. private:
  174. virtual void ProcessInput(char* data, size_t* len);
  175. void DirectSend(const ByteBuffer& buf);
  176. void HandleHello(ByteBuffer* request);
  177. void SendHelloReply(int method);
  178. void HandleAuth(ByteBuffer* request);
  179. void SendAuthReply(int result);
  180. void HandleConnect(ByteBuffer* request);
  181. virtual void SendConnectResult(int result, const SocketAddress& addr);
  182. void Error(int error);
  183. static const int kBufferSize = 1024;
  184. enum State {
  185. SS_HELLO, SS_AUTH, SS_CONNECT, SS_CONNECT_PENDING, SS_TUNNEL, SS_ERROR
  186. };
  187. State state_;
  188. DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxyServerSocket);
  189. };
  190. ///////////////////////////////////////////////////////////////////////////////
  191. // Implements a socket adapter that logs everything that it sends and receives.
  192. class LoggingSocketAdapter : public AsyncSocketAdapter {
  193. public:
  194. LoggingSocketAdapter(AsyncSocket* socket, LoggingSeverity level,
  195. const char * label, bool hex_mode = false);
  196. virtual int Send(const void *pv, size_t cb);
  197. virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr);
  198. virtual int Recv(void *pv, size_t cb);
  199. virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr);
  200. virtual int Close();
  201. protected:
  202. virtual void OnConnectEvent(AsyncSocket * socket);
  203. virtual void OnCloseEvent(AsyncSocket * socket, int err);
  204. private:
  205. LoggingSeverity level_;
  206. std::string label_;
  207. bool hex_mode_;
  208. LogMultilineState lms_;
  209. DISALLOW_EVIL_CONSTRUCTORS(LoggingSocketAdapter);
  210. };
  211. ///////////////////////////////////////////////////////////////////////////////
  212. } // namespace talk_base
  213. #endif // TALK_BASE_SOCKETADAPTERS_H_