#ip-address #ip-lookup #country #check #csv #city #looker

bin+lib ip-check

Check in what country an IP address is located

6 releases

new 0.1.5 Jan 19, 2025
0.1.4 Sep 28, 2024

#1415 in Network programming

Download history 337/week @ 2024-09-22 94/week @ 2024-09-29 5/week @ 2024-10-06 24/week @ 2024-10-13 41/week @ 2024-10-20 20/week @ 2024-10-27 37/week @ 2024-11-03 45/week @ 2024-11-10 58/week @ 2024-11-17 8/week @ 2024-11-24 1/week @ 2024-12-01 2/week @ 2024-12-08

407 downloads per month

MIT license

9KB
170 lines

This crate exposes a struct called Looker that you need to instantiate like this:

use ip_check::{Looker, IpLookup};

let file_path = PathBuf::from_str("locationv4.csv").expect("Path not correct");
let looker = Looker::new_(file_path);

You can then use the looker variable to call the look_up method like this to get a IP result with Country, Region and City:

    let ip = Ipv4Addr::new(12, 22, 104, 13);
    let result = looker.look_up_ipv4(ip);
    match result {
        Some(ip_range) => {
            println!("Country: {}", ip_range.country);
            println!("Region: {}", ip_range.region);
            println!("City: {}", ip_range.city);
        },
        None => {
            println!("No match found");
        }
    }

Or if you only have a string as an IP:

    let ip = "12.22.104.13";
    let result = looker.look_up(ip);
    match result {
        Some(ip_range) => {
            println!("Country: {}", ip_range.country);
            println!("Region: {}", ip_range.region);
            println!("City: {}", ip_range.city);
        },
        None => {
            println!("No match found");
        }
    }

You can download the ip .csv file from here: https://cable.ayra.ch/ip/data/locationv4.gz

And then unzip it with:

gzip -d locationv4.gz

The file needs to have the .csv extension. If it doesn't have it after you unzip it, add it.

History

  • 0.1.5 - Breaking change: Needs a PathBuf instead of a string to load the csv. New function called look_up_ipv4 that takes an Ipv4Addr instead of a string.

Dependencies

~1.2–1.7MB
~21K SLoC