use std::net::IpAddr;
use std::str::FromStr;
use std::time::Duration;
use crate::interface::Interface;
use crate::{Error, Result};
#[derive(Debug, Clone, Copy)]
pub enum NatDetectionMethod {
Stun,
SocketIntrospection,
Both,
}
#[derive(Debug, Clone)]
pub struct NatDetector {
method: NatDetectionMethod,
stun_servers: Vec<String>,
timeout: Duration,
}
impl Default for NatDetector {
fn default() -> Self {
Self {
method: NatDetectionMethod::Both,
stun_servers: vec![
"stun.l.google.com:19302".to_string(),
"stun1.l.google.com:19302".to_string(),
"stun2.l.google.com:19302".to_string(),
],
timeout: Duration::from_secs(5),
}
}
}
impl NatDetector {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn method(mut self, method: NatDetectionMethod) -> Self {
self.method = method;
self
}
#[must_use]
pub fn stun_servers(mut self, servers: Vec<String>) -> Self {
self.stun_servers = servers;
self
}
#[must_use]
pub const fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
#[allow(clippy::unused_self)]
fn detect_via_stun(&self) -> Result<bool> {
Err(Error::unsupported_platform("STUN detection"))
}
#[allow(clippy::unused_self)]
fn detect_via_socket(&self) -> Result<bool> {
let interfaces = Interface::list()?;
let mut has_private_ip = false;
let mut has_public_ip = false;
for interface in interfaces {
if !interface.is_up() || interface.is_loopback() {
continue;
}
for ip_str in interface.ip_addresses() {
if let Ok(ip) = IpAddr::from_str(&ip_str) {
if is_private_ip(ip) {
has_private_ip = true;
} else if is_public_ip(ip) {
has_public_ip = true;
}
}
}
}
Ok(has_private_ip && has_public_ip)
}
pub fn is_behind_nat(&self) -> Result<bool> {
match self.method {
NatDetectionMethod::Stun => self.detect_via_stun(),
NatDetectionMethod::SocketIntrospection => self.detect_via_socket(),
NatDetectionMethod::Both => {
self.detect_via_stun()
.map_or_else(|_| self.detect_via_socket(), Ok)
}
}
}
}
const fn is_private_ip(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(ip) => {
ip.octets()[0] == 10
|| (ip.octets()[0] == 172 && ip.octets()[1] >= 16 && ip.octets()[1] <= 31)
|| (ip.octets()[0] == 192 && ip.octets()[1] == 168)
|| (ip.octets()[0] == 169 && ip.octets()[1] == 254)
}
IpAddr::V6(ip) => {
ip.segments()[0] & 0xfe00 == 0xfc00
|| ip.segments()[0] & 0xffc0 == 0xfe80
}
}
}
const fn is_public_ip(ip: IpAddr) -> bool {
!is_private_ip(ip) && !ip.is_loopback() && !ip.is_unspecified()
}
pub fn get_public_ip() -> Result<Option<IpAddr>> {
let interfaces = Interface::list()?;
for interface in interfaces {
if !interface.is_up() || interface.is_loopback() {
continue;
}
for ip_str in interface.ip_addresses() {
if let Ok(ip) = IpAddr::from_str(&ip_str) {
if is_public_ip(ip) {
return Ok(Some(ip));
}
}
}
}
Ok(None)
}
pub fn get_local_ip() -> Result<Option<IpAddr>> {
let interfaces = Interface::list()?;
for interface in interfaces {
if !interface.is_up() || interface.is_loopback() {
continue;
}
for ip_str in interface.ip_addresses() {
if let Ok(ip) = IpAddr::from_str(&ip_str) {
if is_private_ip(ip) {
return Ok(Some(ip));
}
}
}
}
Ok(None)
}