In previous articles such as "Revisiting Locating User Positions Using IP Addresses (ip2region)" and "Summary of Methods for Locating User Positions Using IP Addresses", we introduced many methods and tools for IP geolocation. These include GeoIP, ipip, ip2region, as well as numerous third-party service providers like Amap and Baidu. Among them, some are online, some are offline, some are paid, and some are free. A few days ago, the editor came across another interesting open-source IP geolocation library: public-ip-address.

About public-ip-address

public-ip-address is a lightweight, easy-to-use Rust library. Its primary function goes beyond simply fetching your current public IP address. More importantly, it provides a unified interface to obtain geographic location information corresponding to an IP address from over a dozen different service providers. Instead of relying on data from a single source, it acts as a proxy for multiple IP geolocation data service providers and incorporates mechanisms like caching, making IP geolocation faster, more accurate, and more stable.

Project Repository: https://github.com/ghztomash/public-ip-address

Core Highlights

First, Multi-source aggregation. It integrates around 20+ service providers, covering different rate limits and usage scenarios. You can use providers like IfConfig and IpApiCom which require no API keys for free calls. For high-concurrency scenarios, you can use providers like IpInfo and Ip2Location that support API keys and offer higher quotas. Even for situations where only low precision is needed, providers like MyIpCom and Ipify offer unlimited queries.

Second, Smart caching + Encryption, balancing performance and privacy. Frequent IP queries in GIS projects can easily trigger service provider rate limits, while geographic information is sensitive data. This library's caching mechanism addresses these concerns perfectly. When querying the same IP address repeatedly, it directly returns the cached result, reducing API calls and improving parsing efficiency. Enabling the encryption feature flag encrypts cache files, preventing geographic information leaks and complying with data privacy regulations.

Finally, it supports both asynchronous and synchronous call methods, perfectly adapting to various GIS architectures without requiring additional wrappers. Moreover, the returned data format can cover common GIS application scenarios. Basic fields include: IP address, country/region code, province/state, city, postal code, latitude and longitude, timezone, and even information like ISP and ASN number.

Quick Start

If you are already familiar with RUST development, using it is very straightforward. Minimal example (querying geographic info of the local machine's IP):

use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // One line of code, automatically finds the fastest provider
    let result = public_ip_address::perform_lookup(None).await?;

    // Print the result
    println!("IP Address: {}", result.ip);
    println!("Country: {}", result.country.unwrap_or_default());
    println!("City: {}", result.city.unwrap_or_default());
    println!("Coordinates: ({}, {})", 
        result.latitude.unwrap_or(0.0), 
        result.longitude.unwrap_or(0.0)
    );

    Ok(())
}

Using the synchronous mode:

use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
    let result = public_ip_address::perform_lookup(None)?;
    println!("IP Geolocation Info: {}", result);
    Ok(())
}

For more advanced usage (such as specifying providers, configuring API keys, batch queries), please refer to the examples directory in the repository, which can be directly adapted for reuse in GIS projects.

List of Service Providers

Currently supported service providers are listed in the table below:

You can also add domestic (Chinese) service providers based on the source code.

Summary

While Python remains the dominant language in the GIS field, Rust is emerging in GIS core algorithms and high-performance web services due to its memory safety and impressive speed (just look at Martin server, Polars library, etc.). The public-ip-address library addresses the need for IP geolocation capabilities in Rust-based GIS development. If you are also a GIS professional who enjoys experimenting with new technologies, why not give it a try?