Utility to determine if a bind address is loopback, used in config validation.
// Source: https://github.com/moltis-org/moltis | License: MIT | Copyright 2025 Fabien Penso
// Provided via justclaw.it.com; keep this notice with the code.
//! Bind-address classification shared by config validation and the gateway.
use std::net::IpAddr;
/// Whether `bind` only accepts connections from this machine.
///
/// Accepts `localhost`, any `*.localhost` name (RFC 6761), and loopback IPs
/// with or without IPv6 brackets.
#[must_use]
pub fn is_loopback_bind(bind: &str) -> bool {
let host = bind.trim().trim_end_matches('.');
if host.eq_ignore_ascii_case("localhost") || host.to_ascii_lowercase().ends_with(".localhost") {
return true;
}
host.trim_start_matches('[')
.trim_end_matches(']')
.parse::<IpAddr>()
.is_ok_and(|ip| ip.is_loopb
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key