3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#include "talk/base/autodetectproxy.h"
29
#include "talk/base/httpcommon.h"
30
#include "talk/base/httpcommon-inl.h"
31
#include "talk/base/proxydetect.h"
35
enum { MSG_TIMEOUT = SignalThread::ST_MSG_FIRST_AVAILABLE };
37
static const ProxyType TEST_ORDER[] = {
38
PROXY_HTTPS, PROXY_SOCKS5, PROXY_UNKNOWN
41
static const int kSavedStringLimit = 128;
43
static void SaveStringToStack(char *dst,
44
const std::string &src,
46
strncpy(dst, src.c_str(), dst_size - 1);
47
dst[dst_size - 1] = '\0';
50
AutoDetectProxy::AutoDetectProxy(const std::string& user_agent)
51
: agent_(user_agent), socket_(NULL), next_(0) {
54
AutoDetectProxy::~AutoDetectProxy() {
57
void AutoDetectProxy::DoWork() {
58
// TODO: Try connecting to server_url without proxy first here?
59
if (!server_url_.empty()) {
60
LOG(LS_INFO) << "GetProxySettingsForUrl(" << server_url_ << ") - start";
61
GetProxySettingsForUrl(agent_.c_str(), server_url_.c_str(), proxy_, true);
62
LOG(LS_INFO) << "GetProxySettingsForUrl - stop";
64
Url<char> url(proxy_.address.IPAsString());
66
LOG(LS_WARNING) << "AutoDetectProxy removing http prefix on proxy host";
67
proxy_.address.SetIP(url.host());
69
LOG(LS_INFO) << "AutoDetectProxy found proxy at " << proxy_.address;
70
if (proxy_.type == PROXY_UNKNOWN) {
71
LOG(LS_INFO) << "AutoDetectProxy initiating proxy classification";
73
// Process I/O until Stop()
74
Thread::Current()->ProcessMessages(kForever);
75
// Clean up the autodetect socket, from the thread that created it
78
// TODO: If we found a proxy, try to use it to verify that it
79
// works by sending a request to server_url. This could either be
80
// done here or by the HttpPortAllocator.
83
void AutoDetectProxy::OnMessage(Message *msg) {
84
if (MSG_TIMEOUT == msg->message_id) {
85
OnCloseEvent(socket_, ETIMEDOUT);
87
// This must be the ST_MSG_WORKER_DONE message that deletes the
88
// AutoDetectProxy object. We have observed crashes within this stack that
89
// seem to be highly reproducible for a small subset of users and thus are
90
// probably correlated with a specific proxy setting, so copy potentially
91
// relevant information onto the stack to make it available in Windows
94
// Save the user agent and the number of auto-detection passes that we
96
char agent[kSavedStringLimit];
97
SaveStringToStack(agent, agent_, sizeof agent);
101
// Now the detected proxy config (minus the password field, which could be
103
ProxyType type = proxy().type;
105
char address_hostname[kSavedStringLimit];
106
SaveStringToStack(address_hostname,
107
proxy().address.hostname(),
108
sizeof address_hostname);
110
IPAddress address_ip = proxy().address.ipaddr();
112
uint16 address_port = proxy().address.port();
114
char autoconfig_url[kSavedStringLimit];
115
SaveStringToStack(autoconfig_url,
116
proxy().autoconfig_url,
117
sizeof autoconfig_url);
119
bool autodetect = proxy().autodetect;
121
char bypass_list[kSavedStringLimit];
122
SaveStringToStack(bypass_list, proxy().bypass_list, sizeof bypass_list);
124
char username[kSavedStringLimit];
125
SaveStringToStack(username, proxy().username, sizeof username);
127
SignalThread::OnMessage(msg);
129
// Log the gathered data at a log level that will never actually be enabled
130
// so that the compiler is forced to retain the data on the stack.
131
LOG(LS_SENSITIVE) << agent << " " << next << " " << type << " "
132
<< address_hostname << " " << address_ip << " "
133
<< address_port << " " << autoconfig_url << " "
134
<< autodetect << " " << bypass_list << " " << username;
138
void AutoDetectProxy::Next() {
139
if (TEST_ORDER[next_] >= PROXY_UNKNOWN) {
140
Complete(PROXY_UNKNOWN);
144
LOG(LS_VERBOSE) << "AutoDetectProxy connecting to "
145
<< proxy_.address.ToString();
148
Thread::Current()->Clear(this, MSG_TIMEOUT);
150
Thread::Current()->Dispose(socket_);
154
socket_ = Thread::Current()->socketserver()->CreateAsyncSocket(SOCK_STREAM);
155
socket_->SignalConnectEvent.connect(this, &AutoDetectProxy::OnConnectEvent);
156
socket_->SignalReadEvent.connect(this, &AutoDetectProxy::OnReadEvent);
157
socket_->SignalCloseEvent.connect(this, &AutoDetectProxy::OnCloseEvent);
158
socket_->Connect(proxy_.address);
160
// Timeout after 2 seconds
161
Thread::Current()->PostDelayed(2000, this, MSG_TIMEOUT);
164
void AutoDetectProxy::Complete(ProxyType type) {
165
Thread::Current()->Clear(this, MSG_TIMEOUT);
169
LoggingSeverity sev = (proxy_.type == PROXY_UNKNOWN) ? LS_ERROR : LS_INFO;
170
LOG_V(sev) << "AutoDetectProxy detected " << proxy_.address.ToString()
171
<< " as type " << proxy_.type;
173
Thread::Current()->Quit();
176
void AutoDetectProxy::OnConnectEvent(AsyncSocket * socket) {
179
switch (TEST_ORDER[next_]) {
181
probe.assign("CONNECT www.google.com:443 HTTP/1.0\r\n"
183
probe.append(agent_);
185
"Host: www.google.com\r\n"
186
"Content-Length: 0\r\n"
187
"Proxy-Connection: Keep-Alive\r\n"
191
probe.assign("\005\001\000", 3);
198
LOG(LS_VERBOSE) << "AutoDetectProxy probing type " << TEST_ORDER[next_]
199
<< " sending " << probe.size() << " bytes";
200
socket_->Send(probe.data(), probe.size());
203
void AutoDetectProxy::OnReadEvent(AsyncSocket * socket) {
205
int len = socket_->Recv(data, 256);
208
LOG(LS_VERBOSE) << "AutoDetectProxy read " << len << " bytes";
211
switch (TEST_ORDER[next_]) {
213
if ((len >= 2) && (data[0] == '\x05')) {
214
Complete(PROXY_SOCKS5);
217
if ((len >= 5) && (strncmp(data, "HTTP/", 5) == 0)) {
218
Complete(PROXY_HTTPS);
223
if ((len >= 2) && (data[0] == '\x05')) {
224
Complete(PROXY_SOCKS5);
237
void AutoDetectProxy::OnCloseEvent(AsyncSocket * socket, int error) {
238
LOG(LS_VERBOSE) << "AutoDetectProxy closed with error: " << error;
243
} // namespace talk_base