henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007 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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include "webrtc/base/autodetectproxy.h" |
| 14 | #include "webrtc/base/httpcommon.h" |
| 15 | #include "webrtc/base/httpcommon-inl.h" |
| 16 | #include "webrtc/base/socketadapters.h" |
| 17 | #include "webrtc/base/ssladapter.h" |
| 18 | #include "webrtc/base/sslsocketfactory.h" |
| 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | // ProxySocketAdapter |
| 24 | // TODO: Consider combining AutoDetectProxy and ProxySocketAdapter. I think |
| 25 | // the socket adapter is the more appropriate idiom for automatic proxy |
| 26 | // detection. We may or may not want to combine proxydetect.* as well. |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | class ProxySocketAdapter : public AsyncSocketAdapter { |
| 30 | public: |
| 31 | ProxySocketAdapter(SslSocketFactory* factory, int family, int type) |
| 32 | : AsyncSocketAdapter(NULL), factory_(factory), family_(family), |
| 33 | type_(type), detect_(NULL) { |
| 34 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 35 | ~ProxySocketAdapter() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | Close(); |
| 37 | } |
| 38 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 39 | int Connect(const SocketAddress& addr) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | ASSERT(NULL == detect_); |
| 41 | ASSERT(NULL == socket_); |
| 42 | remote_ = addr; |
| 43 | if (remote_.IsAnyIP() && remote_.hostname().empty()) { |
| 44 | LOG_F(LS_ERROR) << "Empty address"; |
| 45 | return SOCKET_ERROR; |
| 46 | } |
| 47 | Url<char> url("/", remote_.HostAsURIString(), remote_.port()); |
| 48 | detect_ = new AutoDetectProxy(factory_->agent_); |
| 49 | detect_->set_server_url(url.url()); |
| 50 | detect_->SignalWorkDone.connect(this, |
| 51 | &ProxySocketAdapter::OnProxyDetectionComplete); |
| 52 | detect_->Start(); |
| 53 | return SOCKET_ERROR; |
| 54 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 55 | int GetError() const override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | if (socket_) { |
| 57 | return socket_->GetError(); |
| 58 | } |
| 59 | return detect_ ? EWOULDBLOCK : EADDRNOTAVAIL; |
| 60 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 61 | int Close() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | if (socket_) { |
| 63 | return socket_->Close(); |
| 64 | } |
| 65 | if (detect_) { |
| 66 | detect_->Destroy(false); |
| 67 | detect_ = NULL; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 71 | ConnState GetState() const override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | if (socket_) { |
| 73 | return socket_->GetState(); |
| 74 | } |
| 75 | return detect_ ? CS_CONNECTING : CS_CLOSED; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | // AutoDetectProxy Slots |
| 80 | void OnProxyDetectionComplete(SignalThread* thread) { |
| 81 | ASSERT(detect_ == thread); |
| 82 | Attach(factory_->CreateProxySocket(detect_->proxy(), family_, type_)); |
| 83 | detect_->Release(); |
| 84 | detect_ = NULL; |
| 85 | if (0 == AsyncSocketAdapter::Connect(remote_)) { |
| 86 | SignalConnectEvent(this); |
| 87 | } else if (!IsBlockingError(socket_->GetError())) { |
| 88 | SignalCloseEvent(this, socket_->GetError()); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | SslSocketFactory* factory_; |
| 93 | int family_; |
| 94 | int type_; |
| 95 | SocketAddress remote_; |
| 96 | AutoDetectProxy* detect_; |
| 97 | }; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | // SslSocketFactory |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 103 | SslSocketFactory::SslSocketFactory(SocketFactory* factory, |
| 104 | const std::string& user_agent) |
| 105 | : factory_(factory), |
| 106 | agent_(user_agent), |
| 107 | autodetect_proxy_(true), |
| 108 | force_connect_(false), |
| 109 | logging_level_(LS_VERBOSE), |
| 110 | binary_mode_(false), |
| 111 | ignore_bad_cert_(false) { |
| 112 | } |
| 113 | |
| 114 | SslSocketFactory::~SslSocketFactory() = default; |
| 115 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | Socket* SslSocketFactory::CreateSocket(int type) { |
| 117 | return CreateSocket(AF_INET, type); |
| 118 | } |
| 119 | |
| 120 | Socket* SslSocketFactory::CreateSocket(int family, int type) { |
| 121 | return factory_->CreateSocket(family, type); |
| 122 | } |
| 123 | |
| 124 | AsyncSocket* SslSocketFactory::CreateAsyncSocket(int type) { |
| 125 | return CreateAsyncSocket(AF_INET, type); |
| 126 | } |
| 127 | |
| 128 | AsyncSocket* SslSocketFactory::CreateAsyncSocket(int family, int type) { |
| 129 | if (autodetect_proxy_) { |
| 130 | return new ProxySocketAdapter(this, family, type); |
| 131 | } else { |
| 132 | return CreateProxySocket(proxy_, family, type); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | AsyncSocket* SslSocketFactory::CreateProxySocket(const ProxyInfo& proxy, |
| 138 | int family, |
| 139 | int type) { |
| 140 | AsyncSocket* socket = factory_->CreateAsyncSocket(family, type); |
| 141 | if (!socket) |
| 142 | return NULL; |
| 143 | |
| 144 | // Binary logging happens at the lowest level |
| 145 | if (!logging_label_.empty() && binary_mode_) { |
| 146 | socket = new LoggingSocketAdapter(socket, logging_level_, |
| 147 | logging_label_.c_str(), binary_mode_); |
| 148 | } |
| 149 | |
| 150 | if (proxy.type) { |
| 151 | AsyncSocket* proxy_socket = 0; |
| 152 | if (proxy_.type == PROXY_SOCKS5) { |
| 153 | proxy_socket = new AsyncSocksProxySocket(socket, proxy.address, |
| 154 | proxy.username, proxy.password); |
| 155 | } else { |
| 156 | // Note: we are trying unknown proxies as HTTPS currently |
| 157 | AsyncHttpsProxySocket* http_proxy = |
| 158 | new AsyncHttpsProxySocket(socket, agent_, proxy.address, |
| 159 | proxy.username, proxy.password); |
| 160 | http_proxy->SetForceConnect(force_connect_ || !hostname_.empty()); |
| 161 | proxy_socket = http_proxy; |
| 162 | } |
| 163 | if (!proxy_socket) { |
| 164 | delete socket; |
| 165 | return NULL; |
| 166 | } |
| 167 | socket = proxy_socket; // for our purposes the proxy is now the socket |
| 168 | } |
| 169 | |
| 170 | if (!hostname_.empty()) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 171 | std::unique_ptr<SSLAdapter> ssl_adapter(SSLAdapter::Create(socket)); |
pthatcher@webrtc.org | 69472e7 | 2015-01-07 18:01:07 +0000 | [diff] [blame] | 172 | if (!ssl_adapter) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | LOG_F(LS_ERROR) << "SSL unavailable"; |
pthatcher@webrtc.org | 69472e7 | 2015-01-07 18:01:07 +0000 | [diff] [blame] | 174 | delete socket; |
| 175 | return NULL; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 176 | } |
pthatcher@webrtc.org | 69472e7 | 2015-01-07 18:01:07 +0000 | [diff] [blame] | 177 | |
| 178 | ssl_adapter->set_ignore_bad_cert(ignore_bad_cert_); |
| 179 | if (ssl_adapter->StartSSL(hostname_.c_str(), true) != 0) { |
| 180 | LOG_F(LS_ERROR) << "SSL failed to start."; |
| 181 | return NULL; |
| 182 | } |
| 183 | socket = ssl_adapter.release(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Regular logging occurs at the highest level |
| 187 | if (!logging_label_.empty() && !binary_mode_) { |
| 188 | socket = new LoggingSocketAdapter(socket, logging_level_, |
| 189 | logging_label_.c_str(), binary_mode_); |
| 190 | } |
| 191 | return socket; |
| 192 | } |
| 193 | |
| 194 | /////////////////////////////////////////////////////////////////////////////// |
| 195 | |
| 196 | } // namespace rtc |