1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // A binary wrapper for QuicClient.
6 // Connects to a host using QUIC, sends a request to the provided URL, and
7 // displays the response.
8 //
9 // Some usage examples:
10 //
11 // Standard request/response:
12 // quic_client http://www.google.com
13 // quic_client http://www.google.com --quiet
14 // quic_client https://www.google.com --port=443
15 //
16 // Use a specific version:
17 // quic_client http://www.google.com --quic_version=23
18 //
19 // Send a POST instead of a GET:
20 // quic_client http://www.google.com --body="this is a POST body"
21 //
22 // Append additional headers to the request:
23 // quic_client http://www.google.com --host=${IP}
24 // --headers="Header-A: 1234; Header-B: 5678"
25 //
26 // Connect to a host different to the URL being requested:
27 // quic_client mail.google.com --host=www.google.com
28 //
29 // Connect to a specific IP:
30 // IP=`dig www.google.com +short | head -1`
31 // quic_client www.google.com --host=${IP}
32 //
33 // Try to connect to a host which does not speak QUIC:
34 // quic_client http://www.example.com
35
36 #include "base/logging.h"
37 #include "base/ranges/algorithm.h"
38 #include "net/base/address_family.h"
39 #include "net/base/net_errors.h"
40 #include "net/quic/address_utils.h"
41 #include "net/third_party/quiche/src/quiche/common/platform/api/quiche_command_line_flags.h"
42 #include "net/third_party/quiche/src/quiche/common/platform/api/quiche_system_event_loop.h"
43 #include "net/third_party/quiche/src/quiche/quic/core/quic_error_codes.h"
44 #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
45 #include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h"
46 #include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"
47 #include "net/third_party/quiche/src/quiche/quic/platform/api/quic_socket_address.h"
48 #include "net/third_party/quiche/src/quiche/quic/tools/quic_toy_client.h"
49 #include "net/tools/quic/quic_simple_client.h"
50 #include "net/tools/quic/synchronous_host_resolver.h"
51 #include "url/scheme_host_port.h"
52 #include "url/url_constants.h"
53
54 using quic::ProofVerifier;
55
56 namespace {
57
58 class QuicSimpleClientFactory : public quic::QuicToyClient::ClientFactory {
59 public:
CreateClient(std::string host_for_handshake,std::string host_for_lookup,int address_family_for_lookup,uint16_t port,quic::ParsedQuicVersionVector versions,const quic::QuicConfig & config,std::unique_ptr<quic::ProofVerifier> verifier,std::unique_ptr<quic::SessionCache>)60 std::unique_ptr<quic::QuicSpdyClientBase> CreateClient(
61 std::string host_for_handshake,
62 std::string host_for_lookup,
63 int address_family_for_lookup,
64 uint16_t port,
65 quic::ParsedQuicVersionVector versions,
66 const quic::QuicConfig& config,
67 std::unique_ptr<quic::ProofVerifier> verifier,
68 std::unique_ptr<quic::SessionCache> /*session_cache*/) override {
69 // Determine IP address to connect to from supplied hostname.
70 quic::QuicIpAddress ip_addr;
71 if (!ip_addr.FromString(host_for_lookup)) {
72 net::AddressList addresses;
73 // TODO(crbug.com/40216365) Let the caller pass in the scheme
74 // rather than guessing "https"
75 int rv = net::SynchronousHostResolver::Resolve(
76 url::SchemeHostPort(url::kHttpsScheme, host_for_lookup, port),
77 &addresses);
78 if (rv != net::OK) {
79 LOG(ERROR) << "Unable to resolve '" << host_for_lookup
80 << "' : " << net::ErrorToShortString(rv);
81 return nullptr;
82 }
83 const auto endpoint = base::ranges::find_if(
84 addresses,
85 [address_family_for_lookup](net::AddressFamily family) {
86 if (address_family_for_lookup == AF_INET)
87 return family == net::AddressFamily::ADDRESS_FAMILY_IPV4;
88 if (address_family_for_lookup == AF_INET6)
89 return family == net::AddressFamily::ADDRESS_FAMILY_IPV6;
90 return address_family_for_lookup == AF_UNSPEC;
91 },
92 &net::IPEndPoint::GetFamily);
93 if (endpoint == addresses.end()) {
94 LOG(ERROR) << "No results for '" << host_for_lookup
95 << "' with appropriate address family";
96 return nullptr;
97 }
98 // Arbitrarily select the first result with a matching address family,
99 // ignoring any subsequent matches.
100 ip_addr = net::ToQuicIpAddress(endpoint->address());
101 port = endpoint->port();
102 }
103
104 quic::QuicServerId server_id(host_for_handshake, port);
105 return std::make_unique<net::QuicSimpleClient>(
106 quic::QuicSocketAddress(ip_addr, port), server_id, versions, config,
107 std::move(verifier));
108 }
109 };
110
111 } // namespace
112
main(int argc,char * argv[])113 int main(int argc, char* argv[]) {
114 quiche::QuicheSystemEventLoop event_loop("quic_client");
115 const char* usage = "Usage: quic_client [options] <url>";
116
117 // All non-flag arguments should be interpreted as URLs to fetch.
118 std::vector<std::string> urls =
119 quiche::QuicheParseCommandLineFlags(usage, argc, argv);
120 if (urls.size() != 1) {
121 quiche::QuichePrintCommandLineFlagHelp(usage);
122 exit(0);
123 }
124
125 QuicSimpleClientFactory factory;
126 quic::QuicToyClient client(&factory);
127 return client.SendRequestsAndPrintResponses(urls);
128 }
129