Lines
0 %
Functions
Branches
100 %
//! Exit streams
use std::io::ErrorKind;
use std::net::{IpAddr, SocketAddr};
use std::num::NonZero;
use std::time::Duration;
use either::Either;
use futures::io::BufReader;
use tor_cell::relaycell::msg::{BeginAddr, BeginFlags, Connected, End, EndReason};
use tor_error::{internal, trace_report, warn_report};
use tor_proto::stream::{IncomingStream, IncomingStreamRequest};
use tor_rtcompat::SleepProviderExt as _;
use tor_rtcompat::{NetStreamProvider, Runtime, TimeoutError};
use tracing::{info, trace};
use crate::stream::dns::resolver::{DnsResolver, LookupAnswers, LookupError, RecordData};
/// Handle an incoming exit stream.
///
/// This is expected to be called from a task dedicated to this stream,
/// so it may async-block (for example to establish the exit connection and proxy traffic).
/// It should not be used with a timeout.
pub(super) async fn handle_begin<R: Runtime>(
runtime: R,
incoming: IncomingStream,
resolver: DnsResolver,
) -> anyhow::Result<()> {
let connect_options = Default::default();
// It would be nice to have a better API on `IncomingStream`
// so that we don't need this extra match.
let msg = match incoming.request() {
IncomingStreamRequest::Begin(msg) => msg,
s => {
return Err(anyhow::anyhow!(
"unexpected stream request type {s:?}; should be unreachable"
));
}
};
// TODO(relay): This is temporary. Remove this once we have exit policy support.
// This is to help prevent us from accidentally running a public exit while we're testing.
match std::env::var("ARTI_EXIT") {
Ok(val) if val.trim() == "1" => {}
_ => {
info!("Rejecting incoming stream since `ARTI_EXIT` env variable is not set to '1'");
return Ok(());
// Assign these to local variables to ensure we use all of them.
let (addr, port, flags) = (msg.addr(), msg.port(), msg.flags());
let addr = match addr.decode() {
Ok(x) => x,
Err(e) => {
trace!("Rejecting incoming stream with invalid address: {e}");
incoming
.reject(End::new_with_reason(EndReason::TORPROTOCOL))
.await?;
// A future for everything we want to include within the timeout.
// Returns the TCP stream, and on error returns the END reason.
let exit_stream_fut = match addr {
BeginAddr::Ip(addr) => {
let addr = SocketAddr::new(addr, port.get());
let fut = connect_ip(&runtime, addr, flags, &connect_options);
Either::Left(fut)
BeginAddr::Hostname(ref addr) => {
let fut = connect_hostname(
&runtime,
addr.as_ref(),
port,
flags,
resolver,
&connect_options,
);
Either::Right(fut)
// TODO(relay-tuning): timeout chosen arbitrarily
let timeout = Duration::from_secs(30);
let exit_stream = match runtime.timeout(timeout, exit_stream_fut).await {
Ok(Ok(stream)) => stream,
Ok(Err(end_reason)) => {
incoming.reject(End::new_with_reason(end_reason)).await?;
Err(TimeoutError) => {
.reject(End::new_with_reason(EndReason::TIMEOUT))
let tor_stream = incoming.accept_data(Connected::new_empty()).await?;
// TODO(relay-tuning): copied from arti
const STREAM_BUF_LEN: usize = 4096;
let exit_stream = BufReader::with_capacity(STREAM_BUF_LEN, exit_stream);
let tor_stream = BufReader::with_capacity(STREAM_BUF_LEN, tor_stream);
let res = futures_copy::copy_buf_bidirectional(
tor_stream,
exit_stream,
futures_copy::eof::Close,
)
.await;
match res {
Ok((tor_to_exit, exit_to_tor)) => {
trace!(
count_fwd = tor_to_exit,
count_bwd = exit_to_tor,
"Stream bidirectional copy finished",
// TODO(relay): arti does something complicated here; see `report_proxy_error()`
warn_report!(e, "Error while proxying data on exit stream");
Ok(())
/// For a BEGIN message containing the socket address and flags,
/// attempt to connect to the address.
async fn connect_ip<R: Runtime>(
runtime: &R,
addr: SocketAddr,
_flags: BeginFlags,
connect_options: &<R as NetStreamProvider<SocketAddr>>::ConnectOptions,
) -> Result<<R as NetStreamProvider<SocketAddr>>::Stream, EndReason> {
// We ignore the `IPV4_NOT_OKAY`, `IPV6_OKAY`, and `IPV6_PREFERRED` flags.
//
// torspec:
// > Flags 1 through 3 are ignored if ADDRESS is an IP address.
// Apply the exit policy filter.
if !exit_policy_allows(addr) {
trace!("Rejecting incoming stream due to the exit policy");
return Err(EndReason::EXITPOLICY);
// TODO(relay): We need to check if the IP is in the consensus
// to prevent exit streams from reentering the Tor network.
// https://gitlab.torproject.org/tpo/core/arti/-/work_items/2841
tcp_connect(runtime, &addr, connect_options).await
/// For a BEGIN message containing a hostname and flags,
/// attempt to connect to the hostname.
async fn connect_hostname<R: Runtime>(
hostname: &[u8],
port: NonZero<u16>,
flags: BeginFlags,
mut resolver: DnsResolver,
let port = port.get();
// We need to ensure that the case of each character in the hostname is randomized
// to defend against certain DNS cache poisoning attacks,
// but the `DnsResolver` takes care of this in `resolve()`.
// `DnsResolver::resolve`:
// > The query will be transformed before resolving, as described in torspec:
// > https://spec.torproject.org/tor-spec/remote-hostname-lookup.html#dns-requests
let answers = match resolver.resolve(hostname).await {
Err(LookupError::NonAsciiHostname) => {
// `Begin::decode_from_reader()` does not allow non-ascii addresses,
// so if we get here something has gone wrong and we should probably investigate.
let bug = internal!("the `Begin` type should have prevented this");
trace_report!(bug, "Hostname for BEGIN message had non-ascii hostname");
return Err(EndReason::TORPROTOCOL);
Err(LookupError::Hickory(e)) => {
trace!("Name resolution for exit stream failed due to hickory error: {e}");
return Err(EndReason::RESOLVEFAILED);
Err(LookupError::Bug(e)) => {
trace_report!(e, "Name resolution for exit stream failed due to bug");
return Err(EndReason::INTERNAL);
// Remove any hostname answers.
// We could instead error if any answers were hostnames,
// but I don't see a reason to do that.
let answers = answers.into_iter().filter_map(|x| match x {
RecordData::Ip(x) => Some(x),
RecordData::Hostname(_) => None,
});
// Filter the addresses based on provided flags.
// I prefer it how it's written to how clippy wants it.
#[allow(clippy::needless_bool)]
let answers = answers.filter(|ip| {
if ip.is_ipv4() && flags.contains(BeginFlags::IPV4_NOT_OKAY) {
return false;
// > 3 -- IPv6 preferred. [...] This flag implies the flag at bit "1" (IPv6 okay).
let ipv6_okay = flags.intersects(BeginFlags::IPV6_OKAY | BeginFlags::IPV6_PREFERRED);
if ip.is_ipv6() && !ipv6_okay {
true
// Collect to check the length before we apply the exit policy.
let answers: LookupAnswers<IpAddr> = answers.collect();
if answers.is_empty() {
trace!("Rejecting incoming stream since the domain resolution had no usable IP answers");
let answers = answers
.into_iter()
.filter(|ip| exit_policy_allows(SocketAddr::new(*ip, port)));
// TODO(relay): We need to reject IPs that are in the consensus
// Collect so that we can sort them.
let mut answers: LookupAnswers<IpAddr> = answers.collect();
// > 3 -- IPv6 preferred. If there are both IPv4 and IPv6 addresses,
// > we want to connect to the IPv6 one. (By default, we connect
// > to the IPv4 address.)
if flags.contains(BeginFlags::IPV6_PREFERRED) {
// `false` is sorted before `true`, and `!ip.is_ipv6()` is false for ipv6 addresses
answers.sort_by_key(|ip| !ip.is_ipv6());
} else {
// `false` is sorted before `true`, and `!ip.is_ipv4()` is false for ipv4 addresses
answers.sort_by_key(|ip| !ip.is_ipv4());
// TODO: We maybe want to do happy eyeballs in the future.
// https://gitlab.torproject.org/tpo/core/arti/-/work_items/2878
// Use the first address in the list.
let addr = match answers.first() {
Some(ip) => ip,
None => {
let bug = internal!("we checked above before sorting that the list is non-empty");
trace_report!(bug, "No address answers for BEGIN hostname lookup");
let addr = SocketAddr::new(*addr, port);
/// Helper to establish the TCP connection and get the correct error "reason" to use in the END
/// message if needed.
async fn tcp_connect<R: Runtime>(
addr: &SocketAddr,
match runtime.connect(addr, connect_options).await {
Ok(x) => Ok(x),
let reason = io_error_kind_to_end_reason(e.kind());
if reason == EndReason::INTERNAL {
let bug = internal!("Unexpected io error {}: {}", e.kind(), e);
trace_report!(bug, "Unexpected {reason} when connecting to TCP address");
Err(reason)
/// Does our exit policy allow the given address?
// TODO: Later we will probably have some sort of exit policy object,
// and we can remove this function and just call `exit_policy.allows(addr)` or something.
fn exit_policy_allows(mut addr: SocketAddr) -> bool {
// Canonicalize the IP address to ensure that we handle IPv4-mapped IPv6 addresses.
// For example if an exit policy disallows `a.b.c.d`, we should also disallow `::ffff:a.b.c.d`.
addr.set_ip(addr.ip().to_canonical());
// TODO(relay): Need to support an exit policy. Currently we allow exits to anywhere (except
// basic loopback checks) for testing purposes.
!addr.ip().is_loopback() && !addr.ip().is_unspecified()
/// For a TCP error, get the reason we should use in the END message.
fn io_error_kind_to_end_reason(e: ErrorKind) -> EndReason {
match e {
ErrorKind::ConnectionRefused => EndReason::CONNECTREFUSED,
ErrorKind::ConnectionReset => EndReason::CONNRESET,
ErrorKind::HostUnreachable => EndReason::NOROUTE,
// Linux "no route to host" errors are given for EHOSTUNREACH,
// and "network is unreachable" errors are given for ENETUNREACH.
// But `EndReason` doesn't have a `ENETUNREACH`-equivalent so
// we just reuse `NOROUTE` here.
ErrorKind::NetworkUnreachable => EndReason::NOROUTE,
// We implement our own timeout in the async code and we don't expect to have a TCP timeout
// here, so this is probably a bug.
ErrorKind::TimedOut => EndReason::INTERNAL,
ErrorKind::QuotaExceeded => EndReason::RESOURCELIMIT,
ErrorKind::OutOfMemory => EndReason::RESOURCELIMIT,
// TODO(nightly: io_error_too_many_open_files): `TooManyOpenFiles` should probably return
// `RESOURCELIMIT`.
_ => EndReason::MISC,