[repo] Lab relay: recorder, redacting dump, TCP/TLS-terminating/UDP relays, CLI (never compiled yet)
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
//! UDP relaying (video, control, audio, mic).
|
||||
//!
|
||||
//! Each client address gets its own upstream socket on the listen address, so Sunshine sends the stream to the
|
||||
//! relay (the address its PING or ENet connect came from) and the relay hands it to Moonlight.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::net::{SocketAddr, UdpSocket};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
|
||||
use crate::config::{Config, PortSpec};
|
||||
use crate::record::{Direction, Kind};
|
||||
use crate::recorder::Recorder;
|
||||
|
||||
/// Most client flows per port (one Moonlight uses one).
|
||||
pub const MAX_FLOWS: usize = 16;
|
||||
/// Largest datagram.
|
||||
pub const MAX_DATAGRAM: usize = 64 * 1024;
|
||||
|
||||
/// Windows reports an ICMP "port unreachable" from an earlier send as a reset on the next receive; that is not
|
||||
/// fatal for UDP.
|
||||
fn transient(e: &io::Error) -> bool {
|
||||
matches!(e.kind(), io::ErrorKind::ConnectionReset | io::ErrorKind::ConnectionRefused | io::ErrorKind::Interrupted)
|
||||
}
|
||||
|
||||
/// Relays one UDP port forever.
|
||||
///
|
||||
/// # Errors
|
||||
/// Binding fails.
|
||||
pub fn serve(config: Arc<Config>, spec: PortSpec, rec: Arc<Recorder>) -> io::Result<()> {
|
||||
let listen = Arc::new(UdpSocket::bind(config.listen_addr(&spec))?);
|
||||
let upstream_addr = config.upstream_addr(&spec);
|
||||
let flows: Mutex<HashMap<SocketAddr, (Arc<UdpSocket>, u32)>> = Mutex::new(HashMap::new());
|
||||
let mut buf = vec![0u8; MAX_DATAGRAM];
|
||||
loop {
|
||||
let (n, from) = match listen.recv_from(&mut buf) {
|
||||
Ok(v) => v,
|
||||
Err(e) if transient(&e) => continue,
|
||||
Err(e) => {
|
||||
rec.note(spec.protocol, 0, &format!("receive failed: {e}"));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let flow = {
|
||||
let Ok(mut map) = flows.lock() else { return Err(io::Error::other("flow table poisoned")) };
|
||||
if let Some(f) = map.get(&from) {
|
||||
Some(f.clone())
|
||||
} else if map.len() >= MAX_FLOWS {
|
||||
rec.note(spec.protocol, 0, &format!("dropping datagram from {from}: flow table full"));
|
||||
None
|
||||
} else {
|
||||
let up = Arc::new(UdpSocket::bind(SocketAddr::new(config.listen, 0))?);
|
||||
up.connect(upstream_addr)?;
|
||||
let conn = rec.new_connection();
|
||||
rec.log(Kind::Open, spec.protocol, Direction::None, conn, format!("{from} -> {upstream_addr}").as_bytes());
|
||||
spawn_return_path(Arc::clone(&up), Arc::clone(&listen), from, spec, Arc::clone(&rec), conn);
|
||||
map.insert(from, (Arc::clone(&up), conn));
|
||||
Some((up, conn))
|
||||
}
|
||||
};
|
||||
let Some((up, conn)) = flow else { continue };
|
||||
rec.log(Kind::Data, spec.protocol, Direction::ClientToHost, conn, &buf[..n]);
|
||||
if let Err(e) = up.send(&buf[..n]) {
|
||||
if !transient(&e) {
|
||||
rec.note(spec.protocol, conn, &format!("send to host failed: {e}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_return_path(
|
||||
up: Arc<UdpSocket>,
|
||||
listen: Arc<UdpSocket>,
|
||||
client: SocketAddr,
|
||||
spec: PortSpec,
|
||||
rec: Arc<Recorder>,
|
||||
conn: u32,
|
||||
) {
|
||||
thread::spawn(move || {
|
||||
let mut buf = vec![0u8; MAX_DATAGRAM];
|
||||
loop {
|
||||
match up.recv(&mut buf) {
|
||||
Ok(n) => {
|
||||
rec.log(Kind::Data, spec.protocol, Direction::HostToClient, conn, &buf[..n]);
|
||||
if let Err(e) = listen.send_to(&buf[..n], client) {
|
||||
if !transient(&e) {
|
||||
rec.note(spec.protocol, conn, &format!("send to client failed: {e}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) if transient(&e) => {}
|
||||
Err(e) => {
|
||||
rec.note(spec.protocol, conn, &format!("receive from host failed: {e}"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
rec.log(Kind::Close, spec.protocol, Direction::None, conn, &[]);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user