Coder Social home page Coder Social logo

ipinfo / rust Goto Github PK

View Code? Open in Web Editor NEW
51.0 12.0 13.0 174 KB

IPinfo Rust library for IPinfo API (IP geolocation and other types of IP data)

Home Page: https://ipinfo.io

License: Apache License 2.0

Rust 99.86% Shell 0.14%
ipinfo rust ip-address ip-database ip-geolocation

rust's Introduction

IPinfo IPinfo Rust Client Library

This is the Rust client library for the IPinfo.io IP address API. It allows you to look up your own IP address, or get any of the following details for an IP:

  • IP Geolocation (city, region, country, postal code, latitude, and longitude)
  • ASN (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
  • Company data (the name and domain of the business that uses the IP address)
  • Carrier details (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)

Check all the data we have for your IP address here.

Usage

To use IPinfo, add the following to your Cargo.toml file.

[dependencies]
ipinfo = "3.0.0"

Getting Started

An access token is required, which can be acquired by signing up for a free account at https://ipinfo.io/signup.

The free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as the IP type and company information. To get the complete list of information on an IP address and make more requests per day see https://ipinfo.io/pricing.

Examples

There are several ready-to-run examples located in the /examples directory. These can be run directly, replacing <token> with your access token

cargo run --example lookup -- <token>
cargo run --example lookup_batch -- <token>
cargo run --example get_map

The lookup example above looks more or less like

use ipinfo::{IpInfo, IpInfoConfig};
#[tokio::main]
async fn main() {
    let config = IpInfoConfig {
        token: Some("<token>".to_string()),
        ..Default::default()
    };

    let mut ipinfo = IpInfo::new(config)
        .expect("should construct");

    let res = ipinfo.lookup("8.8.8.8").await;
    match res {
        Ok(r) => {
            println!("{} lookup result: {:?}", "8.8.8.8", r);
        },
        Err(e) => println!("error occurred: {}", &e.to_string()),
    }
}

Features

  • Smart LRU cache for cost and quota savings.
  • Structured and type-checked query results.
  • Bulk IP address lookup using IPinfo batch API.
  • Locate IPs on a World Map.

Internationalization

When looking up an IP address, the response includes country_name which is the country name based on American English, is_eu which returns true if the country is a member of the European Union (EU), country_flag which includes the emoji and Unicode of a country's flag, country_currency which includes the code and symbol of a country's currency, country_flag_url which returns a public link to the country's flag image as an SVG which can be used anywhere. and continent which includes the code and name of the continent.

let r = ipinfo.lookup("8.8.8.8");
println!("{}: {}", "8.8.8.8", r.country_name) // United States
println!("{}: {:?}", "8.8.8.8", r.is_eu) // Some(false)
println!("{}: {:?}", "8.8.8.8", r.country_flag) // Some(CountryFlag { emoji: "πŸ‡ΊπŸ‡Έ", unicode: "U+1F1FA U+1F1F8" })
println!("{}: {:?}", "8.8.8.8", r.country_flag_url) // Some(https://cdn.ipinfo.io/static/images/countries-flags/US.svg)
println!("{}: {:?}", "8.8.8.8", r.country_currency) // Some(CountryCurrency { code: "USD", symbol: "$" })
println!("{}: {:?}", "8.8.8.8", r.continent) // Some(Continent { code: "NA", name: "North America" })

It is possible to return the country name in other languages, change the EU countries and change the flag emoji or unicode by setting the default_countries, default_eu, default_country_flags, default_currencies and default_continents when creating the IPinfo client.

let countries = {
    let json_data = r#"
        {
            "US": "United States"
        }
    "#;
    serde_json::from_str(json_data).expect("error parsing user-defined JSON!")
};

let config = IpInfoConfig {
    default_countries: countries,
    ..Default::default()
};

Other Libraries

There are official IPinfo client libraries available for many languages including PHP, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.

Contributing

Thought of something you'd like to see? You can visit the issue tracker to check if it was reported or proposed before, and if not please feel free to create an issue or feature request. Ready to start contributing? The contributing guide is a good place to start. If you have questions please feel free to ask.

About IPinfo

Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, Reverse IP, hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for 100,000 businesses and developers.

image

rust's People

Contributors

abdullahdevrel avatar abu-usama avatar adamchalmers avatar alextopher avatar amrali avatar awaismslm avatar deltwalrus avatar fayzanx avatar rm-umar avatar st-polina avatar talhahwahla avatar thesurlydev avatar umanshahzad 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  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  avatar

Watchers

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

rust's Issues

lookup for my current IP

Hello,

I would like to obtain information about my current IP. It works, accidentally, by passing an empty string to lookup:

let config = ipinfo::IpInfoConfig::default();
let mut ipinfo = ipinfo::IpInfo::new(config)?;

let location = ipinfo.lookup("").await?;

This works fine because the resulting url matches the good API endpoint.

Is it possible to clarify this feature ? An Option is probably clearer or a dedicated function.

Add single IP lookup API

Currently there's a single lookup call that accepts a slice of IP strings and returns a hashmap from IP to results, matching the response of the batch API.

This is generally OK in most other languages. But Rust users would probably want something a bit more efficient for the more common use case of a single IP lookup, e.g. per each request they get in their web service. The API is then going to be:

  1. more ergonomic for the common use case.
  2. more efficient for the common use case.

Add a single IP lookup API which accepts only a single IP and goes the traditional route for making the request, returning a single struct as a response.

Fix test cases

assert-json-diff v1.1.0 doesn't seem compatible with mockito v0.21.0:

   Compiling assert-json-diff v1.1.0
   Compiling mockito v0.21.0
error[E0432]: unresolved imports `assert_json_diff::Comparison`, `assert_json_diff::Actual`, `assert_json_diff::Expected`
   --> /.cargo/registry/src/github.com-1ecc6299db9ec823/mockito-0.21.0/src/lib.rs:527:24
    |
527 | use assert_json_diff::{Comparison, assert_json_no_panic, Actual, Expected};
    |                        ^^^^^^^^^^                        ^^^^^^  ^^^^^^^^ no `Expected` in the root
    |                        |                                 |
    |                        |                                 no `Actual` in the root
    |                        no `Comparison` in the root

error[E0603]: function `assert_json_no_panic` is private
   --> /.cargo/registry/src/github.com-1ecc6299db9ec823/mockito-0.21.0/src/lib.rs:527:36
    |
527 | use assert_json_diff::{Comparison, assert_json_no_panic, Actual, Expected};
    |                                    ^^^^^^^^^^^^^^^^^^^^ private function
    |
note: the function `assert_json_no_panic` is defined here
   --> /.cargo/registry/src/github.com-1ecc6299db9ec823/assert-json-diff-1.1.0/src/lib.rs:240:1
    |
240 | / fn assert_json_no_panic<Lhs, Rhs>(lhs: &Lhs, rhs: &Rhs, mode: Mode) -> Result<(), String>
241 | | where
242 | |     Lhs: Serialize,
243 | |     Rhs: Serialize,
    | |___________________^

error: aborting due to 2 previous errors

We should just upgrade to the latest mockito which is v0.27.0.

Support an async/await impl

Doesn't seem like the current handler is actually async in any way at all. Given Rust has native async/await support, and that the HTTP client library we use most definitely supports that as well, we should either make an async version of the current handler, or a new subpackage that assumes a totally async environment.

Proposal: Make `IpInfo` generic over localization provider.

Proposed Change

Create a new abstraction for "localization providers" and make IpInfo generic over that type. If the spirit of this change is accepted we can also begin to improve the status-quo regarding excessive use of .clone() in IpInfo. This could look like making the following change (I'm working through experiments at https://github.com/Alextopher/ipinfo/tree/memory)

pub trait Localization {
    fn country_name(&self, country_code: &str) -> Option<&str>;
    fn is_eu(&self, country_code: &str) -> bool;
    fn flag(&self, country_code: &str) -> Option<&CountryFlag>;
    fn currency(&self, country_code: &str) -> Option<&CountryCurrency>;
    fn continent(&self, country_code: &str) -> Option<&Continent>;
}

// Where `StaticTables` is a crate provided Localization. It is a good default and improvement on the status-quo 
// See: https://github.com/Alextopher/ipinfo/blob/memory/src/localization.rs#L78
pub struct IpInfo<T: Localization = StaticTables> {
    url: String,
    token: Option<String>,
    client: reqwest::Client,
    cache: LruCache<String, IpDetails>,
    localization: T,
}

Motivation

There are some key API guidelines that inspires this kind of change.

Using a trait and generics here minimizes our assumptions on how localization data can be provided. #42 called for the replacement of remote resources with tables baked into the library. If IpInfo was generic then we could support both workflows. C-GENERIC

IpInfo only uses shared references to localization data. Because of this we should only require users to provide shared references. Then we can empower users to handle memory management themselves. C-CALLER-CONTROL

Downsides

This proposal is suggesting making a breaking change. Using a good default type parameter (StaticTables) means anyone who hasn't used localization (and just used ..Default::default() won't have a trouble updating.

Inline all data files

We have several files like eu.json, countries.json, continents.json and so on, which are loaded during initialization / startup of the client.

Instead of loading these as such, which has risks such as the asset not appearing in a production environment properly, and has a performance penalty during init of loading an on-disk file, we should inline the files into a static, in-memory map / dictionary or similar.

Batch lookup doesn't properly respect `timeout_total`

timeout_total and timeout_per_batch are timing out the same scopes. Both behave as timeout_per_batch. In addition, there is a subtle error inconsistency. A reqwest timeout should probably be an IpErrorKind::TimeOutError. At the moment all reqwest errors are IpErrorKind::HTTPClientError.

match timeout(

I'm willing to provide fixes to both issues as a PR!

Use versioned cache key

Make sure the cache key contains a number to indicate the version of the cached data. Data changes that change what's expected in cached data require a version change.

Add optional IP selection handler

Add an optional IP selection handler to the SDK client initialization step which accepts the request context and expects returning an IP.

Add a default handler for this which looks at the X-Forwarded-For header and falls back to the source IP.

The resulting IP is the IP for which details are fetched.

New Release

I think now would be a good time for a 3.1.0 release.

missing field `hostname` in ipinfo.io response

It seems that hostname is not included for the free accounts. The lookup function returns the below error which is caused by serde deserialization.

error: IpError { kind: ParseError, description: Some("missing field `hostname` at line 10 column 3") }

I use curl to make a request and the response does not contain hostname either.

curl http://ipinfo.io/47.89.252.15
{
  "ip": "47.89.252.15",
  "city": "Santa Clara",
  "region": "California",
  "country": "US",
  "loc": "37.3541,-121.9552",
  "org": "AS45102 Alibaba (US) Technology Co., Ltd.",
  "postal": "95052",
  "timezone": "America/Los_Angeles",
  "readme": "https://ipinfo.io/missingauth"
}

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.