mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-24 01:20:29 +01:00
dd3a94e2e5
This consolidates the proxy code in `client/src/proxy` into a new `proxy_handshake` crate that implements the client side of HTTP CONNECT and SOCKS4/4a/5/5h handshakes sans-IO: the protocol logic is a pure state machine that never touches a socket, with thin drivers behind `futures-io` and `tokio` feature flags. `client` now drives the websocket's proxy tunnel through the tokio driver, and the `tokio-socks` dependency goes away. The main win is testability: the handshakes are now covered by synchronous byte-level tests on both sides of each exchange, plus a property test that the outcome is invariant under how the proxy's bytes are chunked across reads — the class of partial-read bug that's hard to hit with hand-written tests but easy to hit in production behind a buffering proxy. The crate is written as if it were standalone (no zed-internal dependencies, structured errors instead of `anyhow`), which is also why it grows both driver flavors rather than just the one `client` needs today. Two deliberate behavior changes, both converging on spec/curl semantics: - Proxy credentials embedded in URLs are now percent-decoded before use, per RFC 3986 §3.2.1. Previously they were sent in their encoded form, which only worked for credentials with no reserved characters. A password that literally contains something like `%40` and relied on the old behavior now needs the RFC-correct `%2540` spelling. - `NO_PROXY` is now honored for the collaboration connection, matching the exclusions reqwest already applies to the rest of Zed's HTTP traffic. TLS to the proxy itself (for `https://` proxy URLs) and DNS policy stay in `client`, since those are I/O concerns the sans-IO crate deliberately doesn't own. Release Notes: - Improved proxy support: the collaboration connection now respects `NO_PROXY` and percent-encoded proxy credentials.
93 lines
3.4 KiB
Rust
93 lines
3.4 KiB
Rust
//! Proxied connections for the collaboration WebSocket.
|
|
//!
|
|
//! Proxy URLs come from settings or the environment (see
|
|
//! `ProxySettings::proxy_url`); this module dials the proxy, wraps the
|
|
//! connection in TLS when an `https://` proxy asks for it, and tunnels
|
|
//! through it with `proxy_handshake`.
|
|
|
|
use anyhow::{Context as _, Result};
|
|
use http_client::Url;
|
|
use proxy_handshake::{ProxyScheme, ProxySpec, Target};
|
|
use tokio::net::TcpStream;
|
|
|
|
pub(crate) trait AsyncReadWrite:
|
|
tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static
|
|
{
|
|
}
|
|
impl<T: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static> AsyncReadWrite
|
|
for T
|
|
{
|
|
}
|
|
|
|
/// Whether `NO_PROXY` in the environment excludes `host` from proxying,
|
|
/// matching the exclusions the HTTP client already applies to its own
|
|
/// requests.
|
|
pub(crate) fn excluded_from_proxy(host: &str) -> bool {
|
|
http_client::read_no_proxy_from_env()
|
|
.is_some_and(|no_proxy| proxy_handshake::no_proxy_matches(&no_proxy, host))
|
|
}
|
|
|
|
pub(crate) async fn connect_proxy_stream(
|
|
proxy: &Url,
|
|
rpc_host: (&str, u16),
|
|
) -> Result<Box<dyn AsyncReadWrite>> {
|
|
// If parsing the proxy URL fails, we must avoid falling back to an
|
|
// insecure connection. Proxies are often used in contexts where security
|
|
// and privacy are critical, so any fallback could expose users to
|
|
// significant risks.
|
|
let spec = ProxySpec::parse(proxy).context("parsing proxy URL")?;
|
|
|
|
let target = if spec.remote_dns() {
|
|
Target::Domain(rpc_host.0.to_string(), rpc_host.1)
|
|
} else {
|
|
// SOCKS4 requests carry a raw IPv4 address, so the target must
|
|
// resolve to one.
|
|
let requires_ipv4 = matches!(spec.scheme, ProxyScheme::Socks4 { .. });
|
|
let address = tokio::net::lookup_host(rpc_host)
|
|
.await
|
|
.with_context(|| format!("failed to lookup domain {}", rpc_host.0))?
|
|
.find(|address| !requires_ipv4 || address.is_ipv4())
|
|
.with_context(|| format!("failed to lookup domain {}", rpc_host.0))?;
|
|
Target::Address(address)
|
|
};
|
|
|
|
let stream = TcpStream::connect((spec.host.as_str(), spec.port))
|
|
.await
|
|
.context("Failed to connect to proxy")?;
|
|
|
|
let stream: Box<dyn AsyncReadWrite> = if spec.tls() {
|
|
Box::new(connect_tls_to_proxy(stream, &spec.host).await?)
|
|
} else {
|
|
Box::new(stream)
|
|
};
|
|
|
|
let stream = proxy_handshake::tokio::establish(stream, &spec, &target)
|
|
.await
|
|
.context("error connecting through proxy")?;
|
|
Ok(Box::new(stream))
|
|
}
|
|
|
|
#[cfg(any(target_os = "windows", target_os = "macos"))]
|
|
async fn connect_tls_to_proxy(
|
|
stream: TcpStream,
|
|
proxy_domain: &str,
|
|
) -> Result<tokio_native_tls::TlsStream<TcpStream>> {
|
|
use tokio_native_tls::{TlsConnector, native_tls};
|
|
|
|
let tls_connector = TlsConnector::from(native_tls::TlsConnector::new()?);
|
|
Ok(tls_connector.connect(proxy_domain, stream).await?)
|
|
}
|
|
|
|
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
|
|
async fn connect_tls_to_proxy(
|
|
stream: TcpStream,
|
|
proxy_domain: &str,
|
|
) -> Result<tokio_rustls::client::TlsStream<TcpStream>> {
|
|
let proxy_domain = rustls_pki_types::ServerName::try_from(proxy_domain)
|
|
.context("invalid DNS name for proxy TLS")?
|
|
.to_owned();
|
|
let tls_connector =
|
|
tokio_rustls::TlsConnector::from(std::sync::Arc::new(http_client_tls::tls_config()));
|
|
Ok(tls_connector.connect(proxy_domain, stream).await?)
|
|
}
|