#ip-address #external #dns #source #http #service #retrieve

external-ip

Asynchronous library to retrieve the system external IP

13 stable releases (5 major)

5.2.0 Apr 26, 2024
4.3.0 Mar 31, 2024
4.2.0 Jan 30, 2023
4.1.0 Jan 10, 2021
0.1.0 Dec 30, 2019

#1247 in Network programming

Download history 280/week @ 2024-02-20 312/week @ 2024-02-27 471/week @ 2024-03-05 226/week @ 2024-03-12 178/week @ 2024-03-19 300/week @ 2024-03-26 527/week @ 2024-04-02 155/week @ 2024-04-09 219/week @ 2024-04-16 229/week @ 2024-04-23 68/week @ 2024-04-30 15/week @ 2024-05-07 176/week @ 2024-05-14 310/week @ 2024-05-21 141/week @ 2024-05-28 183/week @ 2024-06-04

816 downloads per month
Used in 2 crates

MIT license

30KB
708 lines

external-ip

Build Status

Finds the current external IP address contacting http and dns external services.

If at least one of the sources replies the reply with the highest occurrences will be reported as the IP address.

Three functions provides sets of known working sources.

  • get_http_sources Returns all known http sources
  • get_dns_sources Returns all known dns sources
  • get_sources Returns all sources combined

Additionally a single igd source can be instantiated if the feature is enabled (discover_igd), to retrieve the IP from an home router. If the feature is enabled get_sources will return it as a source too.

Runtime

It requires to run with Tokio runtime due to the dependency on hyper if you use the HTTP resolver. The DNS resolver can work with other executors at the moment. (tested with futures)

Extend

It's possible to extend how the sources dynamically via the API as long as the Source interface is implemented and it's passed as a boxed trait object.

Example

For ease of use a single async function is enough to obtain the IP trying with all the default sources enabled

  let result = external_ip::get_ipv4();
  let value : Option<IpAddr> = block_on(result);

  let result = external_ip::get_ipv6();
  let value : Option<IpAddr> = block_on(result);

Policies

The library supports 3 consensus policies. The default policy is Random

  • All Query all sources in parallel and return the most common response
  • First Query the sources one by one and return the first success
  • Random Query the sources one by one in random order and return the first success

Families

It's possible to select a specific address family to resolve to and all resolver will try to resolve to that or fail.

  • All
  • IPv4
  • IPv6

Changelog

v1

  • Initial release

v2

  • Based on trust dns instead of c-ares. Now requires less external dependencies (only ssl at the moment)

v3

  • The default policy is now random

v4

  • Consensus is now not consumed when used

v5

  • Added ip family selection option
  • Support http sources with IPv6
  • Updated http and dns sources

Dependencies

~9–23MB
~317K SLoC