Expand description
§ip-discovery
A lightweight, high-performance Rust library for detecting public IP addresses via DNS, HTTP, and STUN protocols with fallback support.
§Features
- Multi-protocol support: DNS, HTTP/HTTPS, STUN (RFC 5389)
- Trusted providers: Google, Cloudflare, AWS, OpenDNS
- Fallback mechanism: Automatic retry with different providers
- Flexible strategies: First success, race (fastest), or consensus
- Zero-dependency protocols: DNS and STUN use raw UDP sockets
- Async-first: Built on tokio for high performance
§Quick Start
use ip_discovery::{get_ip, get_ipv4, get_ipv6};
#[tokio::main]
async fn main() {
// Get any IP address (IPv4 or IPv6)
if let Ok(result) = get_ip().await {
println!("Public IP: {} (via {})", result.ip, result.provider);
}
// Get IPv4 specifically
if let Ok(result) = get_ipv4().await {
println!("IPv4: {}", result.ip);
}
}§Custom Configuration
use ip_discovery::{Config, Strategy, Protocol, BuiltinProvider, get_ip_with};
use std::time::Duration;
#[tokio::main]
async fn main() {
// Use only DNS providers with race strategy
let config = Config::builder()
.protocols(&[Protocol::Dns])
.strategy(Strategy::Race)
.timeout(Duration::from_secs(5))
.build();
if let Ok(result) = get_ip_with(config).await {
println!("IP: {} (latency: {:?})", result.ip, result.latency);
}
// Or pick specific providers
let config = Config::builder()
.providers(&[
BuiltinProvider::CloudflareDns,
BuiltinProvider::GoogleStun,
])
.build();
if let Ok(result) = get_ip_with(config).await {
println!("IP: {}", result.ip);
}
}Modules§
- dns
- DNS protocol implementation for public IP detection
- http
- HTTP/HTTPS protocol implementation for public IP detection
- stun
- STUN protocol implementation for public IP detection
Structs§
- Config
- Configuration for IP detection
- Config
Builder - Builder for
Config. - Provider
Error - Error from a specific provider
- Provider
Result - Result from a successful IP lookup
- Resolver
- Coordinates IP detection across configured providers.
Enums§
- Builtin
Provider - Built-in IP detection providers
- Error
- Main error type for IP detection
- IpVersion
- IP version preference
- Protocol
- Protocol used to detect public IP
- Strategy
- Strategy for resolving the public IP across multiple providers.
Traits§
- Provider
- Trait for IP detection providers
Functions§
- get_ip
- Get public IP address using default configuration.
- get_
ip_ with - Get public IP address with a custom
Config. - get_
ipv4 - Get public IPv4 address using default configuration.
- get_
ipv6 - Get public IPv6 address using default configuration.