summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quiche/quic/tools/connect_tunnel.h
blob: d18d63fef135fa8ed0b132009eff0f9ad4010040 (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
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef QUICHE_QUIC_TOOLS_CONNECT_TUNNEL_H_
#define QUICHE_QUIC_TOOLS_CONNECT_TUNNEL_H_

#include <cstdint>
#include <memory>
#include <string>
#include <utility>

#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "quiche/quic/core/io/socket_factory.h"
#include "quiche/quic/core/io/stream_client_socket.h"
#include "quiche/quic/core/quic_error_codes.h"
#include "quiche/quic/tools/quic_simple_server_backend.h"
#include "quiche/common/platform/api/quiche_mem_slice.h"
#include "quiche/spdy/core/http2_header_block.h"

namespace quic {

// Manages a single connection tunneled over a CONNECT proxy.
class ConnectTunnel : public StreamClientSocket::AsyncVisitor {
 public:
  struct HostAndPort {
    HostAndPort(std::string host, uint16_t port);

    bool operator==(const HostAndPort& other) const;

    template <typename H>
    friend H AbslHashValue(H h, const HostAndPort& host_and_port) {
      return H::combine(std::move(h), host_and_port.host, host_and_port.port);
    }

    std::string host;
    uint16_t port;
  };

  // `client_stream_request_handler` and `socket_factory` must both outlive the
  // created ConnectTunnel.
  ConnectTunnel(
      QuicSimpleServerBackend::RequestHandler* client_stream_request_handler,
      SocketFactory* socket_factory,
      absl::flat_hash_set<HostAndPort> acceptable_destinations);
  ~ConnectTunnel();
  ConnectTunnel(const ConnectTunnel&) = delete;
  ConnectTunnel& operator=(const ConnectTunnel&) = delete;

  // Attempts to open TCP connection to destination server and then sends
  // appropriate success/error response to the request stream. `request_headers`
  // must represent headers from a CONNECT request, that is ":method"="CONNECT"
  // and no ":protocol".
  void OpenTunnel(const spdy::Http2HeaderBlock& request_headers);

  // Returns true iff the connection to the destination server is currently open
  bool IsConnectedToDestination() const;

  void SendDataToDestination(absl::string_view data);

  // Called when the client stream has been closed.  Connection to destination
  // server is closed if connected.  The RequestHandler will no longer be
  // interacted with after completion.
  void OnClientStreamClose();

  // StreamClientSocket::AsyncVisitor:
  void ConnectComplete(absl::Status status) override;
  void ReceiveComplete(absl::StatusOr<quiche::QuicheMemSlice> data) override;
  void SendComplete(absl::Status status) override;

 private:
  void BeginAsyncReadFromDestination();
  void OnDataReceivedFromDestination(bool success);

  // For normal (FIN) closure. Errors (RST) should result in directly calling
  // TerminateClientStream().
  void OnDestinationConnectionClosed();

  void SendConnectResponse();
  void TerminateClientStream(
      absl::string_view error_description,
      QuicResetStreamError error_code =
          QuicResetStreamError::FromIetf(QuicHttp3ErrorCode::CONNECT_ERROR));

  const absl::flat_hash_set<HostAndPort> acceptable_destinations_;
  SocketFactory* const socket_factory_;

  // Null when client stream closed.
  QuicSimpleServerBackend::RequestHandler* client_stream_request_handler_;

  // Null when destination connection disconnected.
  std::unique_ptr<StreamClientSocket> destination_socket_;

  bool receive_started_ = false;
};

}  // namespace quic

#endif  // QUICHE_QUIC_TOOLS_CONNECT_TUNNEL_H_