~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/base/autodetectproxy.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
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"
32
 
 
33
 
namespace talk_base {
34
 
 
35
 
enum { MSG_TIMEOUT = SignalThread::ST_MSG_FIRST_AVAILABLE };
36
 
 
37
 
static const ProxyType TEST_ORDER[] = {
38
 
  PROXY_HTTPS, PROXY_SOCKS5, PROXY_UNKNOWN
39
 
};
40
 
 
41
 
static const int kSavedStringLimit = 128;
42
 
 
43
 
static void SaveStringToStack(char *dst,
44
 
                              const std::string &src,
45
 
                              size_t dst_size) {
46
 
  strncpy(dst, src.c_str(), dst_size - 1);
47
 
  dst[dst_size - 1] = '\0';
48
 
}
49
 
 
50
 
AutoDetectProxy::AutoDetectProxy(const std::string& user_agent)
51
 
    : agent_(user_agent), socket_(NULL), next_(0) {
52
 
}
53
 
 
54
 
AutoDetectProxy::~AutoDetectProxy() {
55
 
}
56
 
 
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";
63
 
  }
64
 
  Url<char> url(proxy_.address.IPAsString());
65
 
  if (url.valid()) {
66
 
    LOG(LS_WARNING) << "AutoDetectProxy removing http prefix on proxy host";
67
 
    proxy_.address.SetIP(url.host());
68
 
  }
69
 
  LOG(LS_INFO) << "AutoDetectProxy found proxy at " << proxy_.address;
70
 
  if (proxy_.type == PROXY_UNKNOWN) {
71
 
    LOG(LS_INFO) << "AutoDetectProxy initiating proxy classification";
72
 
    Next();
73
 
    // Process I/O until Stop()
74
 
    Thread::Current()->ProcessMessages(kForever);
75
 
    // Clean up the autodetect socket, from the thread that created it
76
 
    delete socket_;
77
 
  }
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.
81
 
}
82
 
 
83
 
void AutoDetectProxy::OnMessage(Message *msg) {
84
 
  if (MSG_TIMEOUT == msg->message_id) {
85
 
    OnCloseEvent(socket_, ETIMEDOUT);
86
 
  } else {
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
92
 
    // minidumps.
93
 
 
94
 
    // Save the user agent and the number of auto-detection passes that we
95
 
    // needed.
96
 
    char agent[kSavedStringLimit];
97
 
    SaveStringToStack(agent, agent_, sizeof agent);
98
 
 
99
 
    int next = next_;
100
 
 
101
 
    // Now the detected proxy config (minus the password field, which could be
102
 
    // sensitive).
103
 
    ProxyType type = proxy().type;
104
 
 
105
 
    char address_hostname[kSavedStringLimit];
106
 
    SaveStringToStack(address_hostname,
107
 
                      proxy().address.hostname(),
108
 
                      sizeof address_hostname);
109
 
 
110
 
    IPAddress address_ip = proxy().address.ipaddr();
111
 
 
112
 
    uint16 address_port = proxy().address.port();
113
 
 
114
 
    char autoconfig_url[kSavedStringLimit];
115
 
    SaveStringToStack(autoconfig_url,
116
 
                      proxy().autoconfig_url,
117
 
                      sizeof autoconfig_url);
118
 
 
119
 
    bool autodetect = proxy().autodetect;
120
 
 
121
 
    char bypass_list[kSavedStringLimit];
122
 
    SaveStringToStack(bypass_list, proxy().bypass_list, sizeof bypass_list);
123
 
 
124
 
    char username[kSavedStringLimit];
125
 
    SaveStringToStack(username, proxy().username, sizeof username);
126
 
 
127
 
    SignalThread::OnMessage(msg);
128
 
 
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;
135
 
  }
136
 
}
137
 
 
138
 
void AutoDetectProxy::Next() {
139
 
  if (TEST_ORDER[next_] >= PROXY_UNKNOWN) {
140
 
    Complete(PROXY_UNKNOWN);
141
 
    return;
142
 
  }
143
 
 
144
 
  LOG(LS_VERBOSE) << "AutoDetectProxy connecting to "
145
 
                  << proxy_.address.ToString();
146
 
 
147
 
  if (socket_) {
148
 
    Thread::Current()->Clear(this, MSG_TIMEOUT);
149
 
    socket_->Close();
150
 
    Thread::Current()->Dispose(socket_);
151
 
    socket_ = NULL;
152
 
  }
153
 
 
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);
159
 
 
160
 
  // Timeout after 2 seconds
161
 
  Thread::Current()->PostDelayed(2000, this, MSG_TIMEOUT);
162
 
}
163
 
 
164
 
void AutoDetectProxy::Complete(ProxyType type) {
165
 
  Thread::Current()->Clear(this, MSG_TIMEOUT);
166
 
  socket_->Close();
167
 
 
168
 
  proxy_.type = type;
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;
172
 
 
173
 
  Thread::Current()->Quit();
174
 
}
175
 
 
176
 
void AutoDetectProxy::OnConnectEvent(AsyncSocket * socket) {
177
 
  std::string probe;
178
 
 
179
 
  switch (TEST_ORDER[next_]) {
180
 
    case PROXY_HTTPS:
181
 
      probe.assign("CONNECT www.google.com:443 HTTP/1.0\r\n"
182
 
                   "User-Agent: ");
183
 
      probe.append(agent_);
184
 
      probe.append("\r\n"
185
 
                   "Host: www.google.com\r\n"
186
 
                   "Content-Length: 0\r\n"
187
 
                   "Proxy-Connection: Keep-Alive\r\n"
188
 
                   "\r\n");
189
 
      break;
190
 
    case PROXY_SOCKS5:
191
 
      probe.assign("\005\001\000", 3);
192
 
      break;
193
 
    default:
194
 
      ASSERT(false);
195
 
      return;
196
 
  }
197
 
 
198
 
  LOG(LS_VERBOSE) << "AutoDetectProxy probing type " << TEST_ORDER[next_]
199
 
                  << " sending " << probe.size() << " bytes";
200
 
  socket_->Send(probe.data(), probe.size());
201
 
}
202
 
 
203
 
void AutoDetectProxy::OnReadEvent(AsyncSocket * socket) {
204
 
  char data[257];
205
 
  int len = socket_->Recv(data, 256);
206
 
  if (len > 0) {
207
 
    data[len] = 0;
208
 
    LOG(LS_VERBOSE) << "AutoDetectProxy read " << len << " bytes";
209
 
  }
210
 
 
211
 
  switch (TEST_ORDER[next_]) {
212
 
    case PROXY_HTTPS:
213
 
      if ((len >= 2) && (data[0] == '\x05')) {
214
 
        Complete(PROXY_SOCKS5);
215
 
        return;
216
 
      }
217
 
      if ((len >= 5) && (strncmp(data, "HTTP/", 5) == 0)) {
218
 
        Complete(PROXY_HTTPS);
219
 
        return;
220
 
      }
221
 
      break;
222
 
    case PROXY_SOCKS5:
223
 
      if ((len >= 2) && (data[0] == '\x05')) {
224
 
        Complete(PROXY_SOCKS5);
225
 
        return;
226
 
      }
227
 
      break;
228
 
    default:
229
 
      ASSERT(false);
230
 
      return;
231
 
  }
232
 
 
233
 
  ++next_;
234
 
  Next();
235
 
}
236
 
 
237
 
void AutoDetectProxy::OnCloseEvent(AsyncSocket * socket, int error) {
238
 
  LOG(LS_VERBOSE) << "AutoDetectProxy closed with error: " << error;
239
 
  ++next_;
240
 
  Next();
241
 
}
242
 
 
243
 
}  // namespace talk_base