use tokio::net::TcpStream;
use tracing::info;
use crate::error::{Aria2Error, Result};
use crate::network::OutboundNetworkPolicy;
use super::config::HttpProxyConfig;
use super::io::{connect_to_proxy, connect_to_proxy_with_policy};
pub struct HttpProxyTunnel {
config: HttpProxyConfig,
}
impl HttpProxyTunnel {
pub fn new(config: HttpProxyConfig) -> Self {
Self { config }
}
pub async fn connect(&self) -> Result<TcpStream> {
let stream = connect_to_proxy(&self.config).await?;
self.finish_connect(stream).await
}
pub async fn connect_with_policy(&self, policy: &OutboundNetworkPolicy) -> Result<TcpStream> {
let stream = connect_to_proxy_with_policy(&self.config, policy).await?;
self.finish_connect(stream).await
}
async fn finish_connect(&self, mut stream: TcpStream) -> Result<TcpStream> {
let target = self.config.target_host_port();
let options = aria2_protocol::http::connect::HttpConnectOptions {
target_host: &self.config.target_host,
target_port: self.config.target_port,
user_agent: "",
username: self.config.proxy_username.as_deref(),
password: self.config.proxy_password.as_deref(),
preemptive_basic_auth: false,
write_timeout: self.config.write_timeout,
read_timeout: self.config.read_timeout,
};
let retry_policy = aria2_protocol::http::connect::HttpConnectRetryPolicy {
max_auth_retries: super::io::MAX_AUTH_RETRIES,
prefer_digest: true,
fallback_to_basic: true,
proxy_connection_keep_alive: true,
};
aria2_protocol::http::connect::establish_connect_tunnel_with_retry_policy(
&mut stream,
&options,
&retry_policy,
)
.await
.map_err(|error| map_connect_error(error, &self.config))?;
info!("CONNECT tunnel established to {} via proxy", target);
Ok(stream)
}
}
fn map_connect_error(
error: aria2_protocol::http::connect::HttpConnectError,
config: &HttpProxyConfig,
) -> Aria2Error {
use aria2_protocol::http::connect::HttpConnectError;
match error {
HttpConnectError::Timeout => Aria2Error::Network("Timeout during proxy CONNECT".into()),
HttpConnectError::InvalidRequestField(field) => {
Aria2Error::InvalidArgument(format!("invalid HTTP CONNECT request field: {field}"))
}
HttpConnectError::Io(error) => {
Aria2Error::Network(format!("Error communicating with proxy: {error}"))
}
HttpConnectError::InvalidResponse(message) => {
Aria2Error::Parse(format!("Error parsing proxy response: {message}"))
}
HttpConnectError::CredentialsRequired => {
Aria2Error::Network("Proxy requires authentication but no credentials provided".into())
}
HttpConnectError::UnsupportedAuthentication(_) => {
Aria2Error::Network("Proxy requires auth but no supported scheme found".into())
}
HttpConnectError::AuthenticationFailed => {
Aria2Error::Network("Proxy authentication failed after max retries".into())
}
HttpConnectError::Rejected {
status_code,
reason_phrase,
} => Aria2Error::Network(format!(
"Proxy returned error {status_code} {reason_phrase} for CONNECT to {}",
config.target_host_port()
)),
}
}