1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
* libjingle
* Copyright 2004--2005, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "talk/base/autodetectproxy.h"
#include "talk/base/httpcommon.h"
#include "talk/base/httpcommon-inl.h"
#include "talk/base/nethelpers.h"
namespace talk_base {
static const ProxyType TEST_ORDER[] = {
PROXY_HTTPS, PROXY_SOCKS5, PROXY_UNKNOWN
};
static const int kSavedStringLimit = 128;
static void SaveStringToStack(char *dst,
const std::string &src,
size_t dst_size) {
strncpy(dst, src.c_str(), dst_size - 1);
dst[dst_size - 1] = '\0';
}
AutoDetectProxy::AutoDetectProxy(const std::string& user_agent)
: agent_(user_agent), resolver_(NULL), socket_(NULL), next_(0) {
}
AutoDetectProxy::~AutoDetectProxy() {
if (resolver_) {
resolver_->Destroy(false);
}
}
void AutoDetectProxy::DoWork() {
// TODO: Try connecting to server_url without proxy first here?
if (!server_url_.empty()) {
LOG(LS_INFO) << "GetProxySettingsForUrl(" << server_url_ << ") - start";
GetProxyForUrl(agent_.c_str(), server_url_.c_str(), &proxy_);
LOG(LS_INFO) << "GetProxySettingsForUrl - stop";
}
Url<char> url(proxy_.address.HostAsURIString());
if (url.valid()) {
LOG(LS_WARNING) << "AutoDetectProxy removing http prefix on proxy host";
proxy_.address.SetIP(url.host());
}
LOG(LS_INFO) << "AutoDetectProxy found proxy at " << proxy_.address;
if (proxy_.type == PROXY_UNKNOWN) {
LOG(LS_INFO) << "AutoDetectProxy initiating proxy classification";
Next();
// Process I/O until Stop()
Thread::Current()->ProcessMessages(kForever);
// Clean up the autodetect socket, from the thread that created it
delete socket_;
}
// TODO: If we found a proxy, try to use it to verify that it
// works by sending a request to server_url. This could either be
// done here or by the HttpPortAllocator.
}
void AutoDetectProxy::OnMessage(Message *msg) {
if (MSG_UNRESOLVABLE == msg->message_id) {
// If we can't resolve the proxy, skip straight to failure.
Complete(PROXY_UNKNOWN);
} else if (MSG_TIMEOUT == msg->message_id) {
OnCloseEvent(socket_, ETIMEDOUT);
} else {
// This must be the ST_MSG_WORKER_DONE message that deletes the
// AutoDetectProxy object. We have observed crashes within this stack that
// seem to be highly reproducible for a small subset of users and thus are
// probably correlated with a specific proxy setting, so copy potentially
// relevant information onto the stack to make it available in Windows
// minidumps.
// Save the user agent and the number of auto-detection passes that we
// needed.
char agent[kSavedStringLimit];
SaveStringToStack(agent, agent_, sizeof agent);
int next = next_;
// Now the detected proxy config (minus the password field, which could be
// sensitive).
ProxyType type = proxy().type;
char address_hostname[kSavedStringLimit];
SaveStringToStack(address_hostname,
proxy().address.hostname(),
sizeof address_hostname);
IPAddress address_ip = proxy().address.ipaddr();
uint16 address_port = proxy().address.port();
char autoconfig_url[kSavedStringLimit];
SaveStringToStack(autoconfig_url,
proxy().autoconfig_url,
sizeof autoconfig_url);
bool autodetect = proxy().autodetect;
char bypass_list[kSavedStringLimit];
SaveStringToStack(bypass_list, proxy().bypass_list, sizeof bypass_list);
char username[kSavedStringLimit];
SaveStringToStack(username, proxy().username, sizeof username);
SignalThread::OnMessage(msg);
// Log the gathered data at a log level that will never actually be enabled
// so that the compiler is forced to retain the data on the stack.
LOG(LS_SENSITIVE) << agent << " " << next << " " << type << " "
<< address_hostname << " " << address_ip << " "
<< address_port << " " << autoconfig_url << " "
<< autodetect << " " << bypass_list << " " << username;
}
}
void AutoDetectProxy::OnResolveResult(AsyncResolverInterface* resolver) {
if (resolver != resolver_) {
return;
}
int error = resolver_->GetError();
if (error == 0) {
LOG(LS_VERBOSE) << "Resolved " << proxy_.address << " to "
<< resolver_->address();
proxy_.address = resolver_->address();
if (!DoConnect()) {
Thread::Current()->Post(this, MSG_TIMEOUT);
}
} else {
LOG(LS_INFO) << "Failed to resolve " << resolver_->address();
resolver_->Destroy(false);
resolver_ = NULL;
proxy_.address = SocketAddress();
Thread::Current()->Post(this, MSG_UNRESOLVABLE);
}
}
void AutoDetectProxy::Next() {
if (TEST_ORDER[next_] >= PROXY_UNKNOWN) {
Complete(PROXY_UNKNOWN);
return;
}
LOG(LS_VERBOSE) << "AutoDetectProxy connecting to "
<< proxy_.address.ToSensitiveString();
if (socket_) {
Thread::Current()->Clear(this, MSG_TIMEOUT);
Thread::Current()->Clear(this, MSG_UNRESOLVABLE);
socket_->Close();
Thread::Current()->Dispose(socket_);
socket_ = NULL;
}
int timeout = 2000;
if (proxy_.address.IsUnresolvedIP()) {
// Launch an asyncresolver. This thread will spin waiting for it.
timeout += 2000;
if (!resolver_) {
resolver_ = new AsyncResolver();
}
resolver_->SignalDone.connect(this, &AutoDetectProxy::OnResolveResult);
resolver_->Start(proxy_.address);
} else {
if (!DoConnect()) {
Thread::Current()->Post(this, MSG_TIMEOUT);
return;
}
}
Thread::Current()->PostDelayed(timeout, this, MSG_TIMEOUT);
}
bool AutoDetectProxy::DoConnect() {
if (resolver_) {
resolver_->Destroy(false);
resolver_ = NULL;
}
socket_ =
Thread::Current()->socketserver()->CreateAsyncSocket(
proxy_.address.family(), SOCK_STREAM);
if (!socket_) {
LOG(LS_VERBOSE) << "Unable to create socket for " << proxy_.address;
return false;
}
socket_->SignalConnectEvent.connect(this, &AutoDetectProxy::OnConnectEvent);
socket_->SignalReadEvent.connect(this, &AutoDetectProxy::OnReadEvent);
socket_->SignalCloseEvent.connect(this, &AutoDetectProxy::OnCloseEvent);
socket_->Connect(proxy_.address);
return true;
}
void AutoDetectProxy::Complete(ProxyType type) {
Thread::Current()->Clear(this, MSG_TIMEOUT);
Thread::Current()->Clear(this, MSG_UNRESOLVABLE);
if (socket_) {
socket_->Close();
}
proxy_.type = type;
LoggingSeverity sev = (proxy_.type == PROXY_UNKNOWN) ? LS_ERROR : LS_INFO;
LOG_V(sev) << "AutoDetectProxy detected "
<< proxy_.address.ToSensitiveString()
<< " as type " << proxy_.type;
Thread::Current()->Quit();
}
void AutoDetectProxy::OnConnectEvent(AsyncSocket * socket) {
std::string probe;
switch (TEST_ORDER[next_]) {
case PROXY_HTTPS:
probe.assign("CONNECT www.google.com:443 HTTP/1.0\r\n"
"User-Agent: ");
probe.append(agent_);
probe.append("\r\n"
"Host: www.google.com\r\n"
"Content-Length: 0\r\n"
"Proxy-Connection: Keep-Alive\r\n"
"\r\n");
break;
case PROXY_SOCKS5:
probe.assign("\005\001\000", 3);
break;
default:
ASSERT(false);
return;
}
LOG(LS_VERBOSE) << "AutoDetectProxy probing type " << TEST_ORDER[next_]
<< " sending " << probe.size() << " bytes";
socket_->Send(probe.data(), probe.size());
}
void AutoDetectProxy::OnReadEvent(AsyncSocket * socket) {
char data[257];
int len = socket_->Recv(data, 256);
if (len > 0) {
data[len] = 0;
LOG(LS_VERBOSE) << "AutoDetectProxy read " << len << " bytes";
}
switch (TEST_ORDER[next_]) {
case PROXY_HTTPS:
if ((len >= 2) && (data[0] == '\x05')) {
Complete(PROXY_SOCKS5);
return;
}
if ((len >= 5) && (strncmp(data, "HTTP/", 5) == 0)) {
Complete(PROXY_HTTPS);
return;
}
break;
case PROXY_SOCKS5:
if ((len >= 2) && (data[0] == '\x05')) {
Complete(PROXY_SOCKS5);
return;
}
break;
default:
ASSERT(false);
return;
}
++next_;
Next();
}
void AutoDetectProxy::OnCloseEvent(AsyncSocket * socket, int error) {
LOG(LS_VERBOSE) << "AutoDetectProxy closed with error: " << error;
++next_;
Next();
}
} // namespace talk_base
|