5 releases
0.1.5 | Mar 8, 2023 |
---|---|
0.1.4 | Mar 7, 2023 |
0.1.3 | Oct 28, 2022 |
0.1.1 | Oct 25, 2022 |
0.1.0 | Oct 24, 2022 |
#7 in #countries
28KB
572 lines
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);