Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/buffer/buffer.h" 4 : #include "envoy/extensions/filters/network/tcp_proxy/v3/tcp_proxy.pb.h" 5 : #include "envoy/http/header_evaluator.h" 6 : #include "envoy/stream_info/stream_info.h" 7 : #include "envoy/tcp/conn_pool.h" 8 : #include "envoy/upstream/upstream.h" 9 : 10 : namespace Envoy { 11 : 12 : namespace Upstream { 13 : class LoadBalancerContext; 14 : class ThreadLocalCluster; 15 : } // namespace Upstream 16 : 17 : namespace TcpProxy { 18 : 19 : class GenericConnectionPoolCallbacks; 20 : class GenericUpstream; 21 : 22 : /** 23 : * A configuration for an individual tunneling TCP over HTTP protocols. 24 : */ 25 : class TunnelingConfigHelper { 26 : public: 27 0 : virtual ~TunnelingConfigHelper() = default; 28 : 29 : // The host name of the tunneling upstream HTTP request. 30 : // This function evaluates command operators if specified. Otherwise it returns host name as is. 31 : virtual std::string host(const StreamInfo::StreamInfo& stream_info) const PURE; 32 : 33 : // The method of the upstream HTTP request. True if using POST method, CONNECT otherwise. 34 : virtual bool usePost() const PURE; 35 : 36 : // The path used for POST method. 37 : virtual const std::string& postPath() const PURE; 38 : 39 : // The evaluator to add additional HTTP request headers to the upstream request. 40 : virtual Envoy::Http::HeaderEvaluator& headerEvaluator() const PURE; 41 : 42 : // Save HTTP response headers to the downstream filter state. 43 : virtual void 44 : propagateResponseHeaders(Http::ResponseHeaderMapPtr&& headers, 45 : const StreamInfo::FilterStateSharedPtr& filter_state) const PURE; 46 : 47 : // Save HTTP response trailers to the downstream filter state. 48 : virtual void 49 : propagateResponseTrailers(Http::ResponseTrailerMapPtr&& trailers, 50 : const StreamInfo::FilterStateSharedPtr& filter_state) const PURE; 51 : }; 52 : 53 : using TunnelingConfigHelperOptConstRef = OptRef<const TunnelingConfigHelper>; 54 : 55 : // An API for wrapping either a TCP or an HTTP connection pool. 56 : class GenericConnPool : public Logger::Loggable<Logger::Id::router> { 57 : public: 58 0 : virtual ~GenericConnPool() = default; 59 : 60 : /** 61 : * Called to create a TCP connection or HTTP stream for "CONNECT" streams. 62 : * 63 : * The implementation is then responsible for calling either onGenericPoolReady or 64 : * onGenericPoolFailure on the supplied GenericConnectionPoolCallbacks. 65 : * 66 : * @param callbacks callbacks to communicate stream failure or creation on. 67 : */ 68 : virtual void newStream(GenericConnectionPoolCallbacks& callbacks) PURE; 69 : }; 70 : 71 : // An API for the UpstreamRequest to get callbacks from either an HTTP or TCP 72 : // connection pool. 73 : class GenericConnectionPoolCallbacks { 74 : public: 75 0 : virtual ~GenericConnectionPoolCallbacks() = default; 76 : 77 : /** 78 : * Called when GenericConnPool::newStream has established a new stream. 79 : * 80 : * @param info supplies the stream info object associated with the upstream connection. 81 : * @param upstream supplies the generic upstream for the stream. 82 : * @param host supplies the description of the host that will carry the request. 83 : * @param address_provider supplies the address provider of the upstream connection. 84 : * @param ssl_info supplies the ssl information of the upstream connection. 85 : */ 86 : virtual void onGenericPoolReady(StreamInfo::StreamInfo* info, 87 : std::unique_ptr<GenericUpstream>&& upstream, 88 : Upstream::HostDescriptionConstSharedPtr& host, 89 : const Network::ConnectionInfoProvider& address_provider, 90 : Ssl::ConnectionInfoConstSharedPtr ssl_info) PURE; 91 : 92 : /** 93 : * Called to indicate a failure for GenericConnPool::newStream to establish a stream. 94 : * 95 : * @param reason supplies the failure reason. 96 : * @param failure_reason failure reason string (Note: it is expected that the caller will provide 97 : * matching `reason` and `failure_reason`). 98 : * @param host supplies the description of the host that caused the failure. This may be nullptr 99 : * if no host was involved in the failure (for example overflow). 100 : */ 101 : virtual void onGenericPoolFailure(ConnectionPool::PoolFailureReason reason, 102 : absl::string_view failure_reason, 103 : Upstream::HostDescriptionConstSharedPtr host) PURE; 104 : }; 105 : 106 : // Interface for a generic Upstream, which can communicate with a TCP or HTTP 107 : // upstream. 108 : class GenericUpstream { 109 : public: 110 0 : virtual ~GenericUpstream() = default; 111 : 112 : /** 113 : * Enable/disable further data from this stream. 114 : * 115 : * @param disable true if the stream should be read disabled, false otherwise. 116 : * @return returns true if the disable is performed, false otherwise 117 : * (e.g. if the connection is closed) 118 : */ 119 : virtual bool readDisable(bool disable) PURE; 120 : 121 : /** 122 : * Encodes data upstream. 123 : * @param data supplies the data to encode. The data may be moved by the encoder. 124 : * @param end_stream supplies whether this is the last data to encode. 125 : */ 126 : virtual void encodeData(Buffer::Instance& data, bool end_stream) PURE; 127 : 128 : /** 129 : * Adds a callback to be called when the data is sent to the kernel. 130 : * @param cb supplies the callback to be called 131 : */ 132 : virtual void addBytesSentCallback(Network::Connection::BytesSentCb cb) PURE; 133 : 134 : /** 135 : * Called when an event is received on the downstream connection 136 : * @param event supplies the event which occurred. 137 : * @return the underlying ConnectionData if the event is not "Connected" and draining 138 : is supported for this upstream. 139 : */ 140 : virtual Tcp::ConnectionPool::ConnectionData* 141 : onDownstreamEvent(Network::ConnectionEvent event) PURE; 142 : 143 : /* Called to convert underlying transport socket from non-secure mode 144 : * to secure mode. Implemented only by start_tls transport socket. 145 : */ 146 : virtual bool startUpstreamSecureTransport() PURE; 147 : 148 : /** 149 : * Called when upstream starttls socket is converted to tls and upstream ssl info 150 : * needs to be set in the connection's stream_info. 151 : * @return the const SSL connection data of upstream. 152 : */ 153 : virtual Ssl::ConnectionInfoConstSharedPtr getUpstreamConnectionSslInfo() PURE; 154 : }; 155 : 156 : using GenericConnPoolPtr = std::unique_ptr<GenericConnPool>; 157 : 158 : /* 159 : * A factory for creating generic connection pools. 160 : */ 161 : class GenericConnPoolFactory : public Envoy::Config::TypedFactory { 162 : public: 163 0 : ~GenericConnPoolFactory() override = default; 164 : 165 : /* 166 : * @param thread_local_cluster the thread local cluster to use for conn pool creation. 167 : * @param config the tunneling config, if doing connect tunneling. 168 : * @param context the load balancing context for this connection. 169 : * @param upstream_callbacks the callbacks to provide to the connection if successfully created. 170 : * @param downstream_info is the downstream connection stream info. 171 : * @return may be null if there is no cluster with the given name. 172 : */ 173 : virtual GenericConnPoolPtr 174 : createGenericConnPool(Upstream::ThreadLocalCluster& thread_local_cluster, 175 : TunnelingConfigHelperOptConstRef config, 176 : Upstream::LoadBalancerContext* context, 177 : Tcp::ConnectionPool::UpstreamCallbacks& upstream_callbacks, 178 : StreamInfo::StreamInfo& downstream_info) const PURE; 179 : }; 180 : 181 : using GenericConnPoolFactoryPtr = std::unique_ptr<GenericConnPoolFactory>; 182 : 183 : } // namespace TcpProxy 184 : } // namespace Envoy