Add: Public hostname resolution (needed for testing)

This commit is contained in:
ghost 2025-12-28 18:38:28 +01:00
parent a42af38cfe
commit 8fc5450dc0

View File

@ -1,11 +1,11 @@
//! Binary entry point that wires configuration, UDP listener, optional TLS listener, and allocation handling.
//! Backlog: graceful shutdown signals, structured metrics, and coordinated lifecycle management across listeners.
use std::net::SocketAddr;
use std::net::{IpAddr, Ipv4Addr};
use std::net::{IpAddr, Ipv4Addr, ToSocketAddrs};
use std::sync::Arc;
use std::time::Duration;
use tokio::net::UdpSocket;
use tracing::{error, info};
use tracing::{error, info, warn};
// Use the library crate's public modules instead of local `mod` declarations.
use niom_turn::alloc::AllocationManager;
@ -14,6 +14,25 @@ use niom_turn::auth::{AuthManager, InMemoryStore};
use niom_turn::config::{AuthOptions, Config};
use niom_turn::rate_limit::RateLimiters;
// Workaround: allow relay.* config fields to be hostnames by resolving them once at startup.
fn resolve_host_or_ip(label: &str, value: &str) -> Option<IpAddr> {
if let Ok(ip) = value.parse() {
return Some(ip);
}
let target = format!("{}:0", value);
match target.to_socket_addrs() {
Ok(mut addrs) => addrs.next().map(|addr| addr.ip()).or_else(|| {
warn!("workaround: {}='{}' resolved to no addresses", label, value);
None
}),
Err(e) => {
warn!("workaround: failed to resolve {}='{}': {:?}", label, value, e);
None
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Bootstrap configuration: prefer appsettings.json, otherwise rely on baked-in demo defaults.
@ -90,15 +109,14 @@ async fn main() -> anyhow::Result<()> {
.relay
.relay_bind_ip
.as_deref()
.unwrap_or("0.0.0.0")
.parse()
.and_then(|s| resolve_host_or_ip("relay.relay_bind_ip", s))
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
let advertised_ip: Option<IpAddr> = cfg
.relay
.advertised_ip
.as_deref()
.and_then(|s| s.parse().ok());
.and_then(|s| resolve_host_or_ip("relay.advertised_ip", s));
let alloc_mgr = AllocationManager::new_with_options(AllocationOptions {
relay_bind_ip,