ip-discovery
Detect your public IP address using DNS, STUN, or HTTP — with built-in fallback across trusted providers.
Features
- DNS and STUN via raw UDP sockets (zero network library dependencies)
- HTTP/HTTPS via reqwest (optional)
- Built-in providers from Google, Cloudflare, AWS, and OpenDNS
- IPv4 and IPv6
- Sequential fallback, race, or consensus strategies
- Custom providers via the
Providertrait - Async, built on tokio
Usage
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Then:
use get_ip;
async
You can also request a specific IP version:
use ;
let v4 = get_ipv4.await?;
let v6 = get_ipv6.await?;
Configuration
The defaults (Cloudflare STUN → Cloudflare DNS → Google STUN/DNS → HTTP fallback; 10s timeout) work well for most cases. If you need more control:
use ;
use Duration;
// DNS only, race all DNS providers
let config = builder
.protocols
.strategy
.timeout
.build;
let result = get_ip_with.await?;
// Pick specific providers
let config = builder
.providers
.build;
// Consensus — require at least 2 providers to agree
let config = builder
.strategy
.build;
Strategies
| Strategy | Description |
|---|---|
First (default) |
Try providers in order, return first success |
Race |
Query all concurrently, return fastest |
Consensus { min_agree } |
Require N providers to agree on the same IP |
Providers
All built-in providers are from tier-1 infrastructure companies:
| Provider | Protocol | IPv4 | IPv6 |
|---|---|---|---|
Google STUN (stun.l.google.com) |
STUN | ✅ | ✅ |
Google STUN 1 (stun1.l.google.com) |
STUN | ✅ | ✅ |
Google STUN 2 (stun2.l.google.com) |
STUN | ✅ | ✅ |
Cloudflare STUN (stun.cloudflare.com) |
STUN | ✅ | ✅ |
Google DNS (o-o.myaddr.l.google.com) |
DNS | ✅ | ✅ |
Cloudflare DNS (whoami.cloudflare) |
DNS | ✅ | ✅ |
OpenDNS (myip.opendns.com) |
DNS | ✅ | ❌ |
Cloudflare HTTP (1.1.1.1/cdn-cgi/trace) |
HTTP | ✅ | ❌ |
AWS (checkip.amazonaws.com) |
HTTP | ✅ | ❌ |
Cargo Features
| Feature | Default | Description |
|---|---|---|
dns |
✅ | DNS detection (raw UDP, no extra deps) |
stun |
✅ | STUN detection (raw UDP, no extra deps) |
http |
✅ | HTTP detection (pulls in reqwest + rustls) |
native-tls |
❌ | Use OS-native TLS instead of rustls |
If you only need DNS and STUN, you can skip the HTTP dependency entirely:
= { = "0.1", = false, = ["dns", "stun"] }
Performance
STUN and DNS use raw UDP — no TLS handshake — so they're typically 2–3× faster than HTTP. Default provider order prioritizes UDP-based protocols with IPv4 + IPv6 support first, then falls back to IPv4-only HTTP providers.
💡 Tip: Latency varies significantly by region and network environment. Run the benchmark on your own infrastructure to find the optimal provider and strategy for your use case.
# Run the benchmark to find the best config for your network
Logging
Uses tracing for diagnostics:
fmt
.with_env_filter
.init;
Examples
MSRV
Rust 1.75 or later.
License
Licensed under either of Apache License, Version 2.0 or MIT License, at your option.