feat: WHIP support
This commit is contained in:
@@ -36,19 +36,31 @@ impl WebrtcProxy {
|
||||
let sock = UdpSocket::bind(format!("0.0.0.0:{}", config.proxy_port)).await?;
|
||||
let port = sock.local_addr()?.port();
|
||||
|
||||
let public_ip = match env::var("PUBLIC_DOMAIN") {
|
||||
Ok(domain) => {
|
||||
let public_ip = match env::var("PUBLIC_DOMAIN")
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
{
|
||||
Some(domain) => {
|
||||
let ip = resolve_domain(&domain).await?;
|
||||
info!(%domain, %ip, "resolved PUBLIC_DOMAIN for WebRTC candidates");
|
||||
ip
|
||||
}
|
||||
Err(_) => {
|
||||
None => {
|
||||
if cfg!(debug_assertions) {
|
||||
// For testing
|
||||
info!(
|
||||
"using ip 0.0.0.0 for WebRTC candidates (because we are in a debug build)"
|
||||
);
|
||||
IpAddr::from([127, 0, 0, 1])
|
||||
// For testing — advertise a real interface IP. Loopback is
|
||||
// unreachable from clients whose ICE stack pins its UDP
|
||||
// sockets to a specific interface (OBS sets
|
||||
// IP_UNICAST_IF), which makes checks to 127.0.0.1 vanish.
|
||||
match default_iface_ipv4() {
|
||||
Some(ip) => {
|
||||
info!(%ip, "using default interface IP for WebRTC candidates (debug build)");
|
||||
ip
|
||||
}
|
||||
None => {
|
||||
warn!("no non-loopback IPv4 interface found, falling back to 127.0.0.1");
|
||||
IpAddr::from([127, 0, 0, 1])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let ip = stun_public_ip().await?;
|
||||
info!(%ip, "discovered public IP via STUN for WebRTC candidates");
|
||||
@@ -83,9 +95,14 @@ impl WebrtcProxy {
|
||||
};
|
||||
let data = Bytes::copy_from_slice(&buf[..b]);
|
||||
|
||||
// By addr
|
||||
if let Some(tx) = by_addr.get(&from) {
|
||||
debug!("proxy: routing {} bytes by addr {}:{} → channel", b, from.ip(), from.port());
|
||||
// By addr
|
||||
if let Some(tx) = by_addr.get(&from) {
|
||||
debug!(
|
||||
"proxy: routing {} bytes by addr {}:{} → channel",
|
||||
b,
|
||||
from.ip(),
|
||||
from.port()
|
||||
);
|
||||
match tx.try_send((data, from)) {
|
||||
Ok(_) => continue,
|
||||
Err(e) => {
|
||||
@@ -110,16 +127,22 @@ impl WebrtcProxy {
|
||||
// might be a response to OUR STUN request (remote:local)
|
||||
// or an incoming request from the remote peer (local:remote).
|
||||
let part2_lookup = part2.clone();
|
||||
let entry = by_ufrag.remove(&part1).or_else(|| {
|
||||
part2_lookup.and_then(|p2| by_ufrag.remove(&p2))
|
||||
});
|
||||
let entry = by_ufrag
|
||||
.remove(&part1)
|
||||
.or_else(|| part2_lookup.and_then(|p2| by_ufrag.remove(&p2)));
|
||||
let Some((_, tx)) = entry else {
|
||||
warn!("STUN packet ({}/{:?}), isnt registored", part1, part2);
|
||||
continue;
|
||||
};
|
||||
|
||||
by_addr.insert(from, tx.clone());
|
||||
info!("proxy: STUN match → promoted {} → ufrag={} (match was {}/{})", from, part1, part1, part2.as_deref().unwrap_or("-"));
|
||||
info!(
|
||||
"proxy: STUN match → promoted {} → ufrag={} (match was {}/{})",
|
||||
from,
|
||||
part1,
|
||||
part1,
|
||||
part2.as_deref().unwrap_or("-")
|
||||
);
|
||||
debug!("sending data");
|
||||
if let Err(e) = tx.try_send((data, from)) {
|
||||
match e {
|
||||
@@ -148,33 +171,40 @@ impl WebrtcProxy {
|
||||
self.public_addr
|
||||
}
|
||||
pub fn ufrag_pair(b: &Bytes) -> Option<(String, Option<String>)> {
|
||||
if b.len() <= 20 {
|
||||
return None;
|
||||
}
|
||||
let magic = u32::from_be_bytes(b[4..8].try_into().ok()?);
|
||||
if magic != STUN_MAGIC {
|
||||
return None;
|
||||
}
|
||||
// attribies start at 20
|
||||
let mut pos = 20usize;
|
||||
while (pos + 4) <= b.len() {
|
||||
let attr_type: u16 = u16::from_be_bytes(b[pos..pos + 2].try_into().ok()?);
|
||||
let attr_len: u16 = u16::from_be_bytes(b[pos + 2..pos + 4].try_into().ok()?);
|
||||
pos += 4;
|
||||
if attr_type == 0x0006 {
|
||||
let value = std::str::from_utf8(
|
||||
b[pos..pos + (attr_len as usize)].try_into().ok()?,
|
||||
)
|
||||
.ok()?;
|
||||
let mut parts = value.split(':');
|
||||
let first = parts.next()?.to_string();
|
||||
let second = parts.next().map(|s| s.to_string());
|
||||
return Some((first, second));
|
||||
}
|
||||
pos += (attr_len as usize + 3) & !3;
|
||||
}
|
||||
None
|
||||
if b.len() <= 20 {
|
||||
return None;
|
||||
}
|
||||
let magic = u32::from_be_bytes(b[4..8].try_into().ok()?);
|
||||
if magic != STUN_MAGIC {
|
||||
return None;
|
||||
}
|
||||
// attribies start at 20
|
||||
let mut pos = 20usize;
|
||||
while (pos + 4) <= b.len() {
|
||||
let attr_type: u16 = u16::from_be_bytes(b[pos..pos + 2].try_into().ok()?);
|
||||
let attr_len: u16 = u16::from_be_bytes(b[pos + 2..pos + 4].try_into().ok()?);
|
||||
pos += 4;
|
||||
if attr_type == 0x0006 {
|
||||
let value =
|
||||
std::str::from_utf8(b[pos..pos + (attr_len as usize)].try_into().ok()?).ok()?;
|
||||
let mut parts = value.split(':');
|
||||
let first = parts.next()?.to_string();
|
||||
let second = parts.next().map(|s| s.to_string());
|
||||
return Some((first, second));
|
||||
}
|
||||
pos += (attr_len as usize + 3) & !3;
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// IP of the interface holding the default route, via a UDP connect() trick:
|
||||
/// connect() only does a route lookup (no packets sent), so the kernel binds
|
||||
/// the source address the OS would use for outbound traffic.
|
||||
fn default_iface_ipv4() -> Option<IpAddr> {
|
||||
let sock = std::net::UdpSocket::bind("0.0.0.0:0").ok()?;
|
||||
sock.connect("8.8.8.8:9").ok()?;
|
||||
sock.local_addr().ok().map(|a| a.ip())
|
||||
}
|
||||
|
||||
async fn resolve_domain(domain: &str) -> Result<std::net::IpAddr, Box<dyn Error>> {
|
||||
|
||||
Reference in New Issue
Block a user