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.

292 lines
9.4 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. #include "talk/base/autodetectproxy.h"
  28. #include "talk/base/httpcommon.h"
  29. #include "talk/base/httpcommon-inl.h"
  30. #include "talk/base/nethelpers.h"
  31. #include "talk/base/proxydetect.h"
  32. namespace talk_base {
  33. enum { MSG_TIMEOUT = SignalThread::ST_MSG_FIRST_AVAILABLE };
  34. static const ProxyType TEST_ORDER[] = {
  35. PROXY_HTTPS, PROXY_SOCKS5, PROXY_UNKNOWN
  36. };
  37. static const int kSavedStringLimit = 128;
  38. static void SaveStringToStack(char *dst,
  39. const std::string &src,
  40. size_t dst_size) {
  41. strncpy(dst, src.c_str(), dst_size - 1);
  42. dst[dst_size - 1] = '\0';
  43. }
  44. AutoDetectProxy::AutoDetectProxy(const std::string& user_agent)
  45. : agent_(user_agent), resolver_(NULL), socket_(NULL), next_(0) {
  46. }
  47. AutoDetectProxy::~AutoDetectProxy() {
  48. if (resolver_) {
  49. resolver_->Destroy(false);
  50. }
  51. }
  52. void AutoDetectProxy::DoWork() {
  53. // TODO: Try connecting to server_url without proxy first here?
  54. if (!server_url_.empty()) {
  55. LOG(LS_INFO) << "GetProxySettingsForUrl(" << server_url_ << ") - start";
  56. GetProxySettingsForUrl(agent_.c_str(), server_url_.c_str(), proxy_, true);
  57. LOG(LS_INFO) << "GetProxySettingsForUrl - stop";
  58. }
  59. Url<char> url(proxy_.address.HostAsURIString());
  60. if (url.valid()) {
  61. LOG(LS_WARNING) << "AutoDetectProxy removing http prefix on proxy host";
  62. proxy_.address.SetIP(url.host());
  63. }
  64. LOG(LS_INFO) << "AutoDetectProxy found proxy at " << proxy_.address;
  65. if (proxy_.type == PROXY_UNKNOWN) {
  66. LOG(LS_INFO) << "AutoDetectProxy initiating proxy classification";
  67. Next();
  68. // Process I/O until Stop()
  69. Thread::Current()->ProcessMessages(kForever);
  70. // Clean up the autodetect socket, from the thread that created it
  71. delete socket_;
  72. }
  73. // TODO: If we found a proxy, try to use it to verify that it
  74. // works by sending a request to server_url. This could either be
  75. // done here or by the HttpPortAllocator.
  76. }
  77. void AutoDetectProxy::OnMessage(Message *msg) {
  78. if (MSG_TIMEOUT == msg->message_id) {
  79. OnCloseEvent(socket_, ETIMEDOUT);
  80. } else {
  81. // This must be the ST_MSG_WORKER_DONE message that deletes the
  82. // AutoDetectProxy object. We have observed crashes within this stack that
  83. // seem to be highly reproducible for a small subset of users and thus are
  84. // probably correlated with a specific proxy setting, so copy potentially
  85. // relevant information onto the stack to make it available in Windows
  86. // minidumps.
  87. // Save the user agent and the number of auto-detection passes that we
  88. // needed.
  89. char agent[kSavedStringLimit];
  90. SaveStringToStack(agent, agent_, sizeof agent);
  91. int next = next_;
  92. // Now the detected proxy config (minus the password field, which could be
  93. // sensitive).
  94. ProxyType type = proxy().type;
  95. char address_hostname[kSavedStringLimit];
  96. SaveStringToStack(address_hostname,
  97. proxy().address.hostname(),
  98. sizeof address_hostname);
  99. IPAddress address_ip = proxy().address.ipaddr();
  100. uint16 address_port = proxy().address.port();
  101. char autoconfig_url[kSavedStringLimit];
  102. SaveStringToStack(autoconfig_url,
  103. proxy().autoconfig_url,
  104. sizeof autoconfig_url);
  105. bool autodetect = proxy().autodetect;
  106. char bypass_list[kSavedStringLimit];
  107. SaveStringToStack(bypass_list, proxy().bypass_list, sizeof bypass_list);
  108. char username[kSavedStringLimit];
  109. SaveStringToStack(username, proxy().username, sizeof username);
  110. SignalThread::OnMessage(msg);
  111. // Log the gathered data at a log level that will never actually be enabled
  112. // so that the compiler is forced to retain the data on the stack.
  113. LOG(LS_SENSITIVE) << agent << " " << next << " " << type << " "
  114. << address_hostname << " " << address_ip << " "
  115. << address_port << " " << autoconfig_url << " "
  116. << autodetect << " " << bypass_list << " " << username;
  117. }
  118. }
  119. void AutoDetectProxy::OnResolveResult(SignalThread* thread) {
  120. if (thread != resolver_) {
  121. return;
  122. }
  123. int error = resolver_->error();
  124. if (error == 0) {
  125. LOG(LS_VERBOSE) << "Resolved " << proxy_.address << " to "
  126. << resolver_->address();
  127. proxy_.address = resolver_->address();
  128. DoConnect();
  129. } else {
  130. LOG(LS_INFO) << "Failed to resolve " << resolver_->address();
  131. resolver_->Destroy(false);
  132. resolver_ = NULL;
  133. proxy_.address = SocketAddress();
  134. Thread::Current()->Post(this, MSG_TIMEOUT);
  135. }
  136. }
  137. void AutoDetectProxy::Next() {
  138. if (TEST_ORDER[next_] >= PROXY_UNKNOWN) {
  139. Complete(PROXY_UNKNOWN);
  140. return;
  141. }
  142. LOG(LS_VERBOSE) << "AutoDetectProxy connecting to "
  143. << proxy_.address.ToString();
  144. if (socket_) {
  145. Thread::Current()->Clear(this, MSG_TIMEOUT);
  146. socket_->Close();
  147. Thread::Current()->Dispose(socket_);
  148. socket_ = NULL;
  149. }
  150. int timeout = 2000;
  151. if (proxy_.address.IsUnresolvedIP()) {
  152. // Launch an asyncresolver. This thread will spin waiting for it.
  153. timeout += 2000;
  154. if (!resolver_) {
  155. resolver_ = new AsyncResolver();
  156. }
  157. resolver_->set_address(proxy_.address);
  158. resolver_->SignalWorkDone.connect(this,
  159. &AutoDetectProxy::OnResolveResult);
  160. resolver_->Start();
  161. } else {
  162. DoConnect();
  163. }
  164. Thread::Current()->PostDelayed(timeout, this, MSG_TIMEOUT);
  165. }
  166. void AutoDetectProxy::DoConnect() {
  167. if (resolver_) {
  168. resolver_->Destroy(false);
  169. resolver_ = NULL;
  170. }
  171. socket_ =
  172. Thread::Current()->socketserver()->CreateAsyncSocket(
  173. proxy_.address.family(), SOCK_STREAM);
  174. if (!socket_) {
  175. LOG(LS_VERBOSE) << "Unable to create socket for " << proxy_.address;
  176. return;
  177. }
  178. socket_->SignalConnectEvent.connect(this, &AutoDetectProxy::OnConnectEvent);
  179. socket_->SignalReadEvent.connect(this, &AutoDetectProxy::OnReadEvent);
  180. socket_->SignalCloseEvent.connect(this, &AutoDetectProxy::OnCloseEvent);
  181. socket_->Connect(proxy_.address);
  182. }
  183. void AutoDetectProxy::Complete(ProxyType type) {
  184. Thread::Current()->Clear(this, MSG_TIMEOUT);
  185. if (socket_) {
  186. socket_->Close();
  187. }
  188. proxy_.type = type;
  189. LoggingSeverity sev = (proxy_.type == PROXY_UNKNOWN) ? LS_ERROR : LS_INFO;
  190. LOG_V(sev) << "AutoDetectProxy detected " << proxy_.address.ToString()
  191. << " as type " << proxy_.type;
  192. Thread::Current()->Quit();
  193. }
  194. void AutoDetectProxy::OnConnectEvent(AsyncSocket * socket) {
  195. std::string probe;
  196. switch (TEST_ORDER[next_]) {
  197. case PROXY_HTTPS:
  198. probe.assign("CONNECT www.google.com:443 HTTP/1.0\r\n"
  199. "User-Agent: ");
  200. probe.append(agent_);
  201. probe.append("\r\n"
  202. "Host: www.google.com\r\n"
  203. "Content-Length: 0\r\n"
  204. "Proxy-Connection: Keep-Alive\r\n"
  205. "\r\n");
  206. break;
  207. case PROXY_SOCKS5:
  208. probe.assign("\005\001\000", 3);
  209. break;
  210. default:
  211. ASSERT(false);
  212. return;
  213. }
  214. LOG(LS_VERBOSE) << "AutoDetectProxy probing type " << TEST_ORDER[next_]
  215. << " sending " << probe.size() << " bytes";
  216. socket_->Send(probe.data(), probe.size());
  217. }
  218. void AutoDetectProxy::OnReadEvent(AsyncSocket * socket) {
  219. char data[257];
  220. int len = socket_->Recv(data, 256);
  221. if (len > 0) {
  222. data[len] = 0;
  223. LOG(LS_VERBOSE) << "AutoDetectProxy read " << len << " bytes";
  224. }
  225. switch (TEST_ORDER[next_]) {
  226. case PROXY_HTTPS:
  227. if ((len >= 2) && (data[0] == '\x05')) {
  228. Complete(PROXY_SOCKS5);
  229. return;
  230. }
  231. if ((len >= 5) && (strncmp(data, "HTTP/", 5) == 0)) {
  232. Complete(PROXY_HTTPS);
  233. return;
  234. }
  235. break;
  236. case PROXY_SOCKS5:
  237. if ((len >= 2) && (data[0] == '\x05')) {
  238. Complete(PROXY_SOCKS5);
  239. return;
  240. }
  241. break;
  242. default:
  243. ASSERT(false);
  244. return;
  245. }
  246. ++next_;
  247. Next();
  248. }
  249. void AutoDetectProxy::OnCloseEvent(AsyncSocket * socket, int error) {
  250. LOG(LS_VERBOSE) << "AutoDetectProxy closed with error: " << error;
  251. ++next_;
  252. Next();
  253. }
  254. } // namespace talk_base