aboutsummaryrefslogtreecommitdiff
path: root/brillo/http/http_proxy.cc
blob: b697518ebd2565fd44a09d3728b9f79073520641 (plain)
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
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <brillo/http/http_proxy.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <base/bind.h>
#include <base/callback.h>
#include <base/logging.h>
#include <base/strings/string_tokenizer.h>
#include <base/strings/string_util.h>
#include <brillo/http/http_transport.h>
#include <chromeos/dbus/service_constants.h>
#include <dbus/bus.h>
#include <dbus/message.h>
#include <dbus/object_proxy.h>

namespace {
bool ParseProxyInfo(dbus::Response* response,
                    std::vector<std::string>* proxies_out) {
  DCHECK(proxies_out);
  if (!response) {
    LOG(ERROR) << chromeos::kNetworkProxyServiceName << " D-Bus call to "
               << chromeos::kNetworkProxyServiceResolveProxyMethod
               << " failed";
    proxies_out->assign({brillo::http::kDirectProxy});
    return false;
  }
  dbus::MessageReader reader(response);
  std::string proxy_info;
  std::string proxy_err;
  if (!reader.PopString(&proxy_info) || !reader.PopString(&proxy_err)) {
    LOG(ERROR) << chromeos::kNetworkProxyServiceName << " D-Bus call to "
               << chromeos::kNetworkProxyServiceResolveProxyMethod
               << " returned an invalid D-Bus response";
    proxies_out->assign({brillo::http::kDirectProxy});
    return false;
  }
  if (!proxy_err.empty()) {
    // This case occurs when on the Chrome side of things it can't connect to
    // the proxy resolver service, we just let this fall through and will end
    // up returning success with only the direct proxy listed.
    LOG(WARNING) << "Got error resolving proxy: " << proxy_err;
  }

  base::StringTokenizer toker(proxy_info, ";");
  while (toker.GetNext()) {
    std::string token = toker.token();
    base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token);

    // Start by finding the first space (if any).
    std::string::iterator space;
    for (space = ++token.begin(); space != token.end(); ++space) {
      if (base::IsAsciiWhitespace(*space)) {
        break;
      }
    }

    std::string scheme = base::ToLowerASCII(std::string(token.begin(), space));
    // Chrome uses "socks" to mean socks4 and "proxy" to mean http.
    if (scheme == "socks") {
      scheme += "4";
    } else if (scheme == "proxy") {
      scheme = "http";
    } else if (scheme != "https" && scheme != "socks4" && scheme != "socks5" &&
               scheme != "direct") {
      LOG(ERROR) << "Invalid proxy scheme found of: " << scheme;
      continue;
    }

    std::string host_and_port = std::string(space, token.end());
    base::TrimWhitespaceASCII(host_and_port, base::TRIM_ALL, &host_and_port);
    if (scheme != "direct" && host_and_port.empty()) {
      LOG(ERROR) << "Invalid host/port information for proxy: " << token;
      continue;
    }
    proxies_out->push_back(scheme + "://" + host_and_port);
  }
  // Always add the direct proxy (i.e. no proxy) as a last resort if not there.
  if (proxies_out->empty() ||
      proxies_out->back() != brillo::http::kDirectProxy) {
    proxies_out->push_back(brillo::http::kDirectProxy);
  }
  return true;
}

void OnResolveProxy(const brillo::http::GetChromeProxyServersCallback& callback,
                    dbus::Response* response) {
  std::vector<std::string> proxies;
  bool result = ParseProxyInfo(response, &proxies);
  callback.Run(result, std::move(proxies));
}
}  // namespace

namespace brillo {
namespace http {

bool GetChromeProxyServers(scoped_refptr<dbus::Bus> bus, const std::string& url,
                           std::vector<std::string>* proxies_out) {
  dbus::ObjectProxy* proxy =
      bus->GetObjectProxy(chromeos::kNetworkProxyServiceName,
                          dbus::ObjectPath(chromeos::kNetworkProxyServicePath));
  dbus::MethodCall method_call(
      chromeos::kNetworkProxyServiceInterface,
      chromeos::kNetworkProxyServiceResolveProxyMethod);
  dbus::MessageWriter writer(&method_call);
  writer.AppendString(url);
  std::unique_ptr<dbus::Response> response = proxy->CallMethodAndBlock(
      &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
  return ParseProxyInfo(response.get(), proxies_out);
}

void GetChromeProxyServersAsync(scoped_refptr<dbus::Bus> bus,
                                const std::string& url,
                                const GetChromeProxyServersCallback& callback) {
  dbus::ObjectProxy* proxy = bus->GetObjectProxy(
      chromeos::kNetworkProxyServiceName,
      dbus::ObjectPath(chromeos::kNetworkProxyServicePath));
  dbus::MethodCall method_call(
      chromeos::kNetworkProxyServiceInterface,
      chromeos::kNetworkProxyServiceResolveProxyMethod);
  dbus::MessageWriter writer(&method_call);
  writer.AppendString(url);
  proxy->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
                    base::Bind(&OnResolveProxy, callback));
}

}  // namespace http
}  // namespace brillo