ip-discovery 0.5.0

A lightweight, high-performance library for detecting public IP addresses via DNS, HTTP, and STUN protocols
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Synchronous, blocking IP detection engine.
//!
//! This module provides a zero-runtime-dependency synchronous API for resolving
//! public IP addresses using standard library UDP sockets and threads.
//!
//! Blocking calls return when their configured deadline is reached. Providers
//! are executed on worker threads so a non-cooperative custom provider cannot
//! block the caller indefinitely. Such provider code cannot be forcibly
//! cancelled and may finish later in the background, so custom providers should
//! still honor the timeout passed to [`BlockingProvider::get_ip`](crate::BlockingProvider::get_ip).
//!
//! # Examples
//!
//! ```rust,no_run
//! use ip_discovery::blocking::{get_ip, get_ipv4};
//!
//! // Get any public IP
//! if let Ok(result) = get_ip() {
//!     println!("Public IP: {} (via {})", result.ip, result.provider);
//! }
//!
//! // Get IPv4 specifically
//! if let Ok(result) = get_ipv4() {
//!     println!("IPv4: {}", result.ip);
//! }
//! ```

use crate::config::{Config, Strategy};
use crate::error::Error;
use crate::provider::BoxedBlockingProvider;
use crate::types::{IpVersion, Protocol, ProviderResult};
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};
use std::time::{Duration, Instant};

type WorkerMessage = (
    usize,
    String,
    Protocol,
    Result<IpAddr, crate::ProviderError>,
    Duration,
);

fn spawn_provider_worker(
    id: usize,
    provider: BoxedBlockingProvider,
    version: IpVersion,
    timeout: Duration,
    tx: Sender<WorkerMessage>,
) -> Result<(), crate::ProviderError> {
    let name = provider.name().to_string();
    let spawn_error_name = name.clone();
    let protocol = provider.protocol();
    let thread_name = format!("ip-discovery-{id}");
    std::thread::Builder::new()
        .name(thread_name)
        .spawn(move || {
            let start = Instant::now();
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                provider.get_ip(version, timeout)
            }))
            .unwrap_or_else(|_| Err(crate::ProviderError::message(&name, "provider panicked")));
            let latency = start.elapsed();
            let _ = tx.send((id, name, protocol, result, latency));
        })
        .map(|_| ())
        .map_err(|error| crate::ProviderError::new(spawn_error_name, error))
}

fn timeout_error(name: impl Into<String>) -> crate::ProviderError {
    crate::ProviderError::message(name, "timeout")
}

fn validate_ip(
    name: impl Into<String>,
    version: IpVersion,
    ip: IpAddr,
) -> Result<IpAddr, crate::ProviderError> {
    if version.matches(ip) {
        Ok(ip)
    } else {
        Err(crate::ProviderError::message(
            name,
            "provider returned unexpected IP version",
        ))
    }
}

fn receive_with_timeout(
    rx: &Receiver<WorkerMessage>,
    timeout: Duration,
) -> Result<WorkerMessage, RecvTimeoutError> {
    rx.recv_timeout(timeout)
}

/// Coordinates synchronous IP detection across configured providers.
///
/// Created via [`Resolver::new()`] with a [`Config`].
/// Call [`resolve()`](Resolver::resolve) to perform the lookup.
pub struct Resolver {
    config: Config,
}

impl Resolver {
    /// Create a new blocking resolver with the given configuration
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Return an iterator over blocking providers that support the configured IP version.
    #[inline]
    fn matching_providers(&self) -> impl Iterator<Item = &BoxedBlockingProvider> {
        self.config
            .blocking_providers
            .iter()
            .filter(|p| p.supports_version(self.config.version))
    }

    /// Resolve the public IP address synchronously using the configured strategy.
    ///
    /// # Errors
    ///
    /// - [`Error::NoProvidersForVersion`] — no provider supports the requested IP version.
    /// - [`Error::AllProvidersFailed`] — every provider either failed or timed out.
    /// - [`Error::ConsensusNotReached`] — (consensus strategy) too few providers agreed.
    pub fn resolve(&self) -> Result<ProviderResult, Error> {
        if self.matching_providers().next().is_none() {
            return Err(Error::NoProvidersForVersion);
        }

        match self.config.strategy {
            Strategy::First => self.resolve_first(),
            Strategy::Race => self.resolve_race(),
            Strategy::Consensus { min_agree } => self.resolve_consensus(min_agree),
        }
    }

    /// Try providers sequentially in order, returning the first success.
    fn resolve_first(&self) -> Result<ProviderResult, Error> {
        let mut errors = Vec::new();

        for (id, provider) in self.matching_providers().enumerate() {
            let provider_name = provider.name().to_string();
            if self.config.timeout.is_zero() {
                errors.push(timeout_error(provider_name));
                continue;
            }

            let (tx, rx) = std::sync::mpsc::channel();
            if let Err(error) = spawn_provider_worker(
                id,
                provider.clone(),
                self.config.version,
                self.config.timeout,
                tx,
            ) {
                errors.push(error);
                continue;
            }

            match receive_with_timeout(&rx, self.config.timeout) {
                Ok((_, name, protocol, Ok(ip), latency)) => {
                    match validate_ip(&name, self.config.version, ip) {
                        Ok(ip) => {
                            return Ok(ProviderResult {
                                ip,
                                provider: name,
                                protocol,
                                latency,
                            });
                        }
                        Err(error) => errors.push(error),
                    }
                }
                Ok((_, _, _, Err(error), _)) => errors.push(error),
                Err(RecvTimeoutError::Timeout) => errors.push(timeout_error(provider_name)),
                Err(RecvTimeoutError::Disconnected) => errors.push(crate::ProviderError::message(
                    provider_name,
                    "provider worker disconnected",
                )),
            }
        }

        Err(Error::AllProvidersFailed(errors))
    }

    /// Race all providers concurrently across threads, returning the fastest success.
    ///
    /// Workers that have already started cannot be forcibly cancelled when a
    /// winner is found. Built-in providers use bounded I/O; custom providers
    /// should honor their timeout to release background resources promptly.
    fn resolve_race(&self) -> Result<ProviderResult, Error> {
        let providers: Vec<BoxedBlockingProvider> = self.matching_providers().cloned().collect();
        if providers.is_empty() {
            return Err(Error::NoProvidersForVersion);
        }

        if self.config.timeout.is_zero() {
            return Err(Error::AllProvidersFailed(
                providers
                    .iter()
                    .map(|provider| timeout_error(provider.name()))
                    .collect(),
            ));
        }

        let (tx, rx) = std::sync::mpsc::channel();
        let mut pending = HashMap::new();
        let mut errors = Vec::new();

        for (id, provider) in providers.into_iter().enumerate() {
            pending.insert(id, provider.name().to_string());
            if let Err(error) = spawn_provider_worker(
                id,
                provider,
                self.config.version,
                self.config.timeout,
                tx.clone(),
            ) {
                pending.remove(&id);
                errors.push(error);
            }
        }
        drop(tx);

        let deadline = Instant::now().checked_add(self.config.timeout);

        while !pending.is_empty() {
            let remaining = deadline
                .and_then(|deadline| deadline.checked_duration_since(Instant::now()))
                .unwrap_or(Duration::ZERO);
            if remaining.is_zero() {
                break;
            }
            match receive_with_timeout(&rx, remaining) {
                Ok((id, name, protocol, res, latency)) => {
                    pending.remove(&id);
                    match res {
                        Ok(ip) => match validate_ip(&name, self.config.version, ip) {
                            Ok(ip) => {
                                return Ok(ProviderResult {
                                    ip,
                                    provider: name,
                                    protocol,
                                    latency,
                                });
                            }
                            Err(error) => errors.push(error),
                        },
                        Err(e) => errors.push(e),
                    }
                }
                Err(RecvTimeoutError::Timeout | RecvTimeoutError::Disconnected) => break,
            }
        }

        errors.extend(pending.into_values().map(timeout_error));

        Err(Error::AllProvidersFailed(errors))
    }

    /// Query all providers concurrently across threads and require consensus.
    fn resolve_consensus(&self, min_agree: usize) -> Result<ProviderResult, Error> {
        let providers: Vec<BoxedBlockingProvider> = self.matching_providers().cloned().collect();
        if providers.is_empty() {
            return Err(Error::NoProvidersForVersion);
        }

        if self.config.timeout.is_zero() {
            return Err(Error::ConsensusNotReached {
                required: min_agree,
                got: 0,
                errors: providers
                    .iter()
                    .map(|provider| timeout_error(provider.name()))
                    .collect(),
            });
        }

        let (tx, rx) = std::sync::mpsc::channel();
        let mut pending = HashMap::new();
        let mut errors = Vec::new();

        for (id, provider) in providers.into_iter().enumerate() {
            pending.insert(id, provider.name().to_string());
            if let Err(error) = spawn_provider_worker(
                id,
                provider,
                self.config.version,
                self.config.timeout,
                tx.clone(),
            ) {
                pending.remove(&id);
                errors.push(error);
            }
        }
        drop(tx);

        let mut ip_results: HashMap<IpAddr, Vec<ProviderResult>> = HashMap::new();
        let deadline = Instant::now().checked_add(self.config.timeout);

        while !pending.is_empty() {
            let remaining = deadline
                .and_then(|deadline| deadline.checked_duration_since(Instant::now()))
                .unwrap_or(Duration::ZERO);
            if remaining.is_zero() {
                break;
            }
            match receive_with_timeout(&rx, remaining) {
                Ok((id, name, protocol, res, latency)) => {
                    pending.remove(&id);
                    match res {
                        Ok(ip) => match validate_ip(&name, self.config.version, ip) {
                            Ok(ip) => {
                                let pr = ProviderResult {
                                    ip,
                                    provider: name,
                                    protocol,
                                    latency,
                                };
                                ip_results.entry(ip).or_default().push(pr);
                            }
                            Err(error) => errors.push(error),
                        },
                        Err(e) => errors.push(e),
                    }
                }
                Err(RecvTimeoutError::Timeout | RecvTimeoutError::Disconnected) => break,
            }
        }

        errors.extend(pending.into_values().map(timeout_error));

        let mut best: Option<(IpAddr, usize)> = None;
        for (ip, providers) in &ip_results {
            if providers.len() >= min_agree {
                match &best {
                    None => best = Some((*ip, providers.len())),
                    Some((_, current_len)) if providers.len() > *current_len => {
                        best = Some((*ip, providers.len()))
                    }
                    _ => {}
                }
            }
        }

        match best {
            Some((ip, _)) => {
                if let Some(providers) = ip_results.remove(&ip) {
                    if let Some(fastest) = providers.into_iter().min_by_key(|p| p.latency) {
                        return Ok(fastest);
                    }
                }
                Err(Error::ConsensusNotReached {
                    required: min_agree,
                    got: 0,
                    errors,
                })
            }
            None => {
                let max_agreement = ip_results.values().map(|v| v.len()).max().unwrap_or(0);
                Err(Error::ConsensusNotReached {
                    required: min_agree,
                    got: max_agreement,
                    errors,
                })
            }
        }
    }
}

/// Get public IP address synchronously using default configuration.
///
/// # Errors
///
/// Returns [`Error::AllProvidersFailed`] if every provider fails.
pub fn get_ip() -> Result<ProviderResult, Error> {
    let config = Config::default();
    get_ip_with(config)
}

/// Get public IPv4 address synchronously using default configuration.
///
/// # Errors
///
/// Returns [`Error::AllProvidersFailed`] if no provider returns an IPv4 address.
pub fn get_ipv4() -> Result<ProviderResult, Error> {
    let config = Config::builder().version(IpVersion::V4).build();
    get_ip_with(config)
}

/// Get public IPv6 address synchronously using default configuration.
///
/// # Errors
///
/// Returns [`Error::NoProvidersForVersion`] if no provider supports IPv6.
pub fn get_ipv6() -> Result<ProviderResult, Error> {
    let config = Config::builder().version(IpVersion::V6).build();
    get_ip_with(config)
}

/// Get public IP address synchronously with a custom [`Config`].
///
/// # Errors
///
/// Returns an [`Error`] variant depending on the strategy and provider results.
pub fn get_ip_with(config: Config) -> Result<ProviderResult, Error> {
    let resolver = Resolver::new(config);
    resolver.resolve()
}