| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_SOCKETADAPTERS_H_ | 11 #ifndef WEBRTC_BASE_SOCKETADAPTERS_H_ |
| 12 #define WEBRTC_BASE_SOCKETADAPTERS_H_ | 12 #define WEBRTC_BASE_SOCKETADAPTERS_H_ |
| 13 | 13 |
| 14 #include <map> | 14 #include <map> |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "webrtc/base/asyncsocket.h" | 17 #include "webrtc/base/asyncsocket.h" |
| 18 #include "webrtc/base/constructormagic.h" | 18 #include "webrtc/base/constructormagic.h" |
| 19 #include "webrtc/base/cryptstring.h" | 19 #include "webrtc/base/cryptstring.h" |
| 20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 21 | 21 |
| 22 namespace rtc { | 22 namespace rtc { |
| 23 | 23 |
| 24 struct HttpAuthContext; | |
| 25 class ByteBufferReader; | 24 class ByteBufferReader; |
| 26 class ByteBufferWriter; | 25 class ByteBufferWriter; |
| 27 | 26 |
| 28 /////////////////////////////////////////////////////////////////////////////// | 27 /////////////////////////////////////////////////////////////////////////////// |
| 29 | 28 |
| 30 // Implements a socket adapter that can buffer and process data internally, | 29 // Implements a socket adapter that can buffer and process data internally, |
| 31 // as in the case of connecting to a proxy, where you must speak the proxy | 30 // as in the case of connecting to a proxy, where you must speak the proxy |
| 32 // protocol before commencing normal socket behavior. | 31 // protocol before commencing normal socket behavior. |
| 33 class BufferedReadAdapter : public AsyncSocketAdapter { | 32 class BufferedReadAdapter : public AsyncSocketAdapter { |
| 34 public: | 33 public: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // fake SSL handshake. Used when implementing a relay server that does "ssltcp". | 86 // fake SSL handshake. Used when implementing a relay server that does "ssltcp". |
| 88 class AsyncSSLServerSocket : public BufferedReadAdapter { | 87 class AsyncSSLServerSocket : public BufferedReadAdapter { |
| 89 public: | 88 public: |
| 90 explicit AsyncSSLServerSocket(AsyncSocket* socket); | 89 explicit AsyncSSLServerSocket(AsyncSocket* socket); |
| 91 | 90 |
| 92 protected: | 91 protected: |
| 93 void ProcessInput(char* data, size_t* len) override; | 92 void ProcessInput(char* data, size_t* len) override; |
| 94 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSSLServerSocket); | 93 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSSLServerSocket); |
| 95 }; | 94 }; |
| 96 | 95 |
| 97 /////////////////////////////////////////////////////////////////////////////// | |
| 98 | |
| 99 // Implements a socket adapter that speaks the HTTP/S proxy protocol. | |
| 100 class AsyncHttpsProxySocket : public BufferedReadAdapter { | |
| 101 public: | |
| 102 AsyncHttpsProxySocket(AsyncSocket* socket, const std::string& user_agent, | |
| 103 const SocketAddress& proxy, | |
| 104 const std::string& username, const CryptString& password); | |
| 105 ~AsyncHttpsProxySocket() override; | |
| 106 | |
| 107 // If connect is forced, the adapter will always issue an HTTP CONNECT to the | |
| 108 // target address. Otherwise, it will connect only if the destination port | |
| 109 // is not port 80. | |
| 110 void SetForceConnect(bool force) { force_connect_ = force; } | |
| 111 | |
| 112 int Connect(const SocketAddress& addr) override; | |
| 113 SocketAddress GetRemoteAddress() const override; | |
| 114 int Close() override; | |
| 115 ConnState GetState() const override; | |
| 116 | |
| 117 protected: | |
| 118 void OnConnectEvent(AsyncSocket* socket) override; | |
| 119 void OnCloseEvent(AsyncSocket* socket, int err) override; | |
| 120 void ProcessInput(char* data, size_t* len) override; | |
| 121 | |
| 122 bool ShouldIssueConnect() const; | |
| 123 void SendRequest(); | |
| 124 void ProcessLine(char* data, size_t len); | |
| 125 void EndResponse(); | |
| 126 void Error(int error); | |
| 127 | |
| 128 private: | |
| 129 SocketAddress proxy_, dest_; | |
| 130 std::string agent_, user_, headers_; | |
| 131 CryptString pass_; | |
| 132 bool force_connect_; | |
| 133 size_t content_length_; | |
| 134 int defer_error_; | |
| 135 bool expect_close_; | |
| 136 enum ProxyState { | |
| 137 PS_INIT, PS_LEADER, PS_AUTHENTICATE, PS_SKIP_HEADERS, PS_ERROR_HEADERS, | |
| 138 PS_TUNNEL_HEADERS, PS_SKIP_BODY, PS_TUNNEL, PS_WAIT_CLOSE, PS_ERROR | |
| 139 } state_; | |
| 140 HttpAuthContext * context_; | |
| 141 std::string unknown_mechanisms_; | |
| 142 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncHttpsProxySocket); | |
| 143 }; | |
| 144 | |
| 145 /////////////////////////////////////////////////////////////////////////////// | |
| 146 | |
| 147 // Implements a socket adapter that speaks the SOCKS proxy protocol. | |
| 148 class AsyncSocksProxySocket : public BufferedReadAdapter { | |
| 149 public: | |
| 150 AsyncSocksProxySocket(AsyncSocket* socket, const SocketAddress& proxy, | |
| 151 const std::string& username, const CryptString& password); | |
| 152 ~AsyncSocksProxySocket() override; | |
| 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(AsyncSocket* 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 { | |
| 170 SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR | |
| 171 }; | |
| 172 State state_; | |
| 173 SocketAddress proxy_, dest_; | |
| 174 std::string user_; | |
| 175 CryptString pass_; | |
| 176 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSocksProxySocket); | |
| 177 }; | |
| 178 | |
| 179 // Implements a proxy server socket for the SOCKS protocol. | |
| 180 class AsyncSocksProxyServerSocket : public AsyncProxyServerSocket { | |
| 181 public: | |
| 182 explicit AsyncSocksProxyServerSocket(AsyncSocket* socket); | |
| 183 | |
| 184 private: | |
| 185 void ProcessInput(char* data, size_t* len) override; | |
| 186 void DirectSend(const ByteBufferWriter& buf); | |
| 187 | |
| 188 void HandleHello(ByteBufferReader* request); | |
| 189 void SendHelloReply(uint8_t method); | |
| 190 void HandleAuth(ByteBufferReader* request); | |
| 191 void SendAuthReply(uint8_t result); | |
| 192 void HandleConnect(ByteBufferReader* request); | |
| 193 void SendConnectResult(int result, const SocketAddress& addr) override; | |
| 194 | |
| 195 void Error(int error); | |
| 196 | |
| 197 static const int kBufferSize = 1024; | |
| 198 enum State { | |
| 199 SS_HELLO, SS_AUTH, SS_CONNECT, SS_CONNECT_PENDING, SS_TUNNEL, SS_ERROR | |
| 200 }; | |
| 201 State state_; | |
| 202 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSocksProxyServerSocket); | |
| 203 }; | |
| 204 | |
| 205 } // namespace rtc | 96 } // namespace rtc |
| 206 | 97 |
| 207 #endif // WEBRTC_BASE_SOCKETADAPTERS_H_ | 98 #endif // WEBRTC_BASE_SOCKETADAPTERS_H_ |
| OLD | NEW |