Git Product home page Git Product logo

rust-ip2location's Introduction

IP2Location & IP2Proxy

Crates.io Documentation

Linux Arm7 Linux x86_64 macOS x86_64 Windows x86_64

This library reads the IP2Location DB format for both IP2Location and IP2Proxy and returns geo information for the given IP.

Requirements

  • Rust 1.60.0 and above (edition 2021)

Building

  • debug
cargo b
  • release
cargo b --release

Testing

cargo t -v

Usage

[dependencies]
ip2location = "0.5.0"

Example

use ip2location::{error, Record, DB};

const IPV4BIN: &str = "data/IP2LOCATION-LITE-DB1.BIN";
const IPV6BIN: &str = "data/IP2LOCATION-LITE-DB1.IPV6.BIN";
const IP2PROXYBIN: &str = "data/IP2PROXY-IP-COUNTRY.BIN";

// Lookup an IP v4 in the IP2Location V6 BIN Database
fn ip_lookup_in_ipv6bin() -> Result<(), error::Error> {
    let mut db = DB::from_file(IPV6BIN)?;
    let record = db.ip_lookup("43.224.159.155".parse().unwrap())?;
    let record = if let Record::LocationDb(rec) = record {
        Some(rec)
    } else {
        None
    };
    assert!(record.is_some());
    let record = record.unwrap();
    assert!(!record.country.is_none());
    assert_eq!(record.country.clone().unwrap().short_name, "IN");
    assert_eq!(record.country.unwrap().long_name, "India");
    Ok(())
}

// Lookup an IP v4 in the IP2Location V4 BIN Database
fn ip_lookup_in_ipv4bin() -> Result<(), error::Error> {
    let mut db = DB::from_file(IPV4BIN)?;
    let record = db.ip_lookup("43.224.159.155".parse().unwrap())?;
    let record = if let Record::LocationDb(rec) = record {
        Some(rec)
    } else {
        None
    };
    assert!(record.is_some());
    let record = record.unwrap();
    assert!(!record.country.is_none());
    assert_eq!(record.country.clone().unwrap().short_name, "IN");
    assert_eq!(record.country.unwrap().long_name, "India");
    Ok(())
}

// Lookup an IP in the Proxy Database
fn ip_lookup_in_proxy_bin() -> Result<(), error::Error> {
    let mut db = DB::from_file(IP2PROXYBIN)?;
    let record = db.ip_lookup("1.1.1.1".parse().unwrap())?;
    let record = if let Record::ProxyDb(rec) = record {
        Some(rec)
    } else {
        None
    };
    assert!(record.is_some());
    let record = record.unwrap();
    assert!(!record.country.is_none());
    Ok(())
}

Executing the Example

cargo b --example

# IP2Lcoation Example

./target/debug/examples/lookup data/IP2LOCATION-LITE-DB1.IPV6.BIN 2a01:cb08:8d14::
Db Path: data/IP2LOCATION-LITE-DB1.IPV6.BIN
 |- Db Type: 1
 |- Db Column: 2
 |- Db Date (YY/MM/DD): 20/12/28

Ok(
    Record {
        ip: "2a01:cb08:8d14::",
        latitude: None,
        longitude: None,
        country: Some(
            Country {
                short_name: "FR",
                long_name: "France",
            },
        ),
        region: None,
        city: None,
        isp: None,
        domain: None,
        zip_code: None,
        time_zone: None,
        net_speed: None,
        idd_code: None,
        area_code: None,
        weather_station_code: None,
        weather_station_name: None,
        mcc: None,
        mnc: None,
        mobile_brand: None,
        elevation: None,
        usage_type: None,
        address_type: None,
        category: None,
        district: None,
        asn: None,
        as_name: None,
    },
)

# IP2Proxy Example 
 
./target/debug/examples/lookup data/sample.bin.px11/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN 194.59.249.19
Db Path: data/sample.bin.px11/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN
 |- Db Type: 11
 |- Db Column: 13
 |- Db Date (YY/MM/DD): 21/5/28

ProxyDb(
    ProxyRecord {
        ip: 1.1.1.1,
        country: Some(
            Country {
                short_name: "US",
                long_name: "United States of America",
            },
        ),
        region: Some(
            "California",
        ),
        city: Some(
            "Los Angeles",
        ),
        isp: Some(
            "APNIC and CloudFlare DNS Resolver Project",
        ),
        domain: Some(
            "cloudflare.com",
        ),
        is_proxy: Some(
            IsAProxy,
        ),
        proxy_type: Some(
            "DCH",
        ),
        asn: Some(
            "13335",
        ),
        as_: Some(
            "CloudFlare Inc",
        ),
        last_seen: Some(
            "27",
        ),
        threat: Some(
            "-",
        ),
        provider: Some(
            "-",
        ),
        usage_type: Some(
            "CDN",
        ),
    },
)

License

This is free software, licensed under the MIT license.

Ip2Location Databases:


Sriram

rust-ip2location's People

Contributors

ip2location avatar lidarbtc avatar marirs avatar wezm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rust-ip2location's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.