ip2c 0.1.5

Get the codes for the representation of names of countries and regions from IP address.
Documentation
  • Coverage
  • 31.48%
    17 out of 54 items documented6 out of 22 items with examples
  • Size
  • Source code size: 39.41 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.55 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • bnyu

IP2C

Get the codes for the representation of names of countries and regions from IP address.

You can directly use rir::IpCodeMap build IP to Codes for the representation of names of countries and regions.

use ip2c::rir::IpCodeMap;

let mut map = IpCodeMap::new();
map.load_from_dir("./data").expect("load rir txt info failed");
let code = map.query("127.0.0.1".parse().unwrap());
println!("{}", code.unwrap());

this library do not provide rir information files, you need download yourself.
see example.txt about download url and official txt format.

Also, you can use IpTree to build IP city location map, eg:

use ip2c::IpTree;

let mut map = IpTree::new();
map.ipv4.insert_interval("101.204.128.0-101.204.130.0".parse().unwrap(), ("sichuan", "chengdu")).unwrap();
map.ipv4.insert_interval("208.0.0.0/22".parse().unwrap(), ("beijing", "beijing")).unwrap();
map.ipv4.insert_interval("208.1.3.6".parse().unwrap(), ("beijing", "beijing")).unwrap();
assert_eq!(map.ipv4.query("101.204.129.1".parse().unwrap()), Some(&("sichuan", "chengdu")));
assert_eq!(map.ipv4.query("208.0.3.47".parse().unwrap()), Some(&("beijing", "beijing")));
assert_eq!(map.ipv4.query("208.11.0.9".parse().unwrap()), None);

doc