Git Product home page Git Product logo

rust-geoip's Introduction

rust-geoip

GeoIP bindings for Rust.

Currently only supports:

Installation: use Cargo.

Tested with GeoIP v1.6.9.

Usage:

City Database:

// Open by DBType
let geoip = GeoIp::open_type(DBType::CityEditionRev1, Options::MemoryCache).unwrap();
// Open by Path
let geoip = GeoIp::open(&Path::new("/opt/geoip/GeoLiteCity.dat"),
						Options::MemoryCache)
	.unwrap();
/*
GeoIp {
	info: Ok(
		"GEO-133 20160621 Build 1 Copyright (c) 2016 MaxMind Inc All Rights Re"
	)
}
*/

// Query by IP
let ip = IpAddr::V4("8.8.8.8".parse().unwrap());
let res = geoip.city_info_by_ip(ip).unwrap();
/*
CityInfo {
	country_code: Some(
		"US"
	),
	country_code3: Some(
		"USA"
	),
	country_name: Some(
		"United States"
	),
	region: Some(
		"CA"
	),
	city: Some(
		"Mountain View"
	),
	postal_code: Some(
		"94035"
	),
	latitude: 37.386,
	longitude: -122.0838,
	dma_code: Some(
		807
	),
	area_code: Some(
		650
	),
	continent_code: Some(
		"NA"
	),
	netmask: 24
}
*/

// Get additional information (as compiled in the C library)
let region_name = GeoIp::region_name_by_code("US", "CA");
// Some("California")

// Get time zone inforamtion (as compiled in the C library)
let time_zone = GeoIp::time_zone_by_country_and_region("US", "CA");
// Some("America/Los_Angeles")

AS Database:

// Open by Path
let geoip = GeoIp::open(&Path::new("/opt/geoip/GeoIPASNum.dat"),
						Options::MemoryCache)
	.unwrap();
/*
GeoIp {
	info: Ok(
		"GEO-117 20160627 Build 1 Copyright (c) 2016 MaxMind Inc All Rights Re"
	)
}
*/

// Query by IP
let ip = IpAddr::V4("8.8.8.8".parse().unwrap());
let res = geoip.as_info_by_ip(ip).unwrap();
/*
ASInfo {
	asn: 15169,
	name: "Google Inc.",
	netmask: 24
}
*/

rust-geoip's People

Contributors

jedisct1 avatar jpastuszek avatar ryman avatar

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.