ipinfo / rust

IPinfo Rust library for IPinfo API (IP geolocation and other types of IP data)
https://ipinfo.io
Apache License 2.0
56 stars 14 forks source link

Add IPv6 support #51

Closed talhahwahla closed 10 months ago

talhahwahla commented 11 months ago

In this PR, I have moved the URL configuration from the context structure to functions. We have three public functions: get_map , lookup_batch and lookup. They will point to "https://ipinfo.io". I have added three new corresponding functions: get_map_v6 , lookup_batch_v6 and lookup_v6. They point to: "https://v6.ipinfo.io".

root@ipv6-test-server:~/rust# cat examples/lookup_batch.rs 
use ipinfo::{BatchReqOpts, IpInfo, IpInfoConfig};
use std::env;

#[tokio::main]
async fn main() {
    let token = env::args().nth(1);

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

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

    let res2 = ipinfo
        .lookup_batch_v6(&["2a00:1838:37:107::e3a6", "2a02:4780:10:5bae::1"], BatchReqOpts::default())
        .await;

    match res2 {
        Ok(r) => {
            println!("2a00:1838:37:107::e3a6: {:?}", r["2a00:1838:37:107::e3a6"]);
            println!("2a02:4780:10:5bae::1: {:?}", r["2a02:4780:10:5bae::1"]);
        }
        Err(e) => println!("error occurred: {}", &e.to_string()),
    }
}
root@ipv6-test-server:~/rust# cargo run --example lookup_batch -- <TOKEN>
   Compiling ipinfo v3.0.0 (/root/rust)
    Finished dev [unoptimized + debuginfo] target(s) in 7.99s
     Running `target/debug/examples/lookup_batch <TOKEN>`
2a00:1838:37:107::e3a6: IpDetails { ip: "2a00:1838:37:107::e3a6", hostname: None, city: "Saint Petersburg", region: "St.-Petersburg", country: "RU", country_name: Some("Russia"), is_eu: Some(false), country_flag: Some(CountryFlag { emoji: "🇷🇺", unicode: "U+1F1F7 U+1F1FA" }), country_flag_url: Some("https://cdn.ipinfo.io/static/images/countries-flags/RU.svg"), country_currency: Some(CountryCurrency { code: "RUB", symbol: "₽" }), continent: Some(Continent { code: "EU", name: "Europe" }), loc: "59.9386,30.3141", org: Some("AS43317 SIA VEESP"), postal: Some("190000"), timezone: Some("Europe/Moscow"), asn: Some(AsnDetails { asn: "AS43317", name: "SIA VEESP", domain: "veesp.com", route: "2a00:1838::/32", asn_type: "hosting" }), company: Some(CompanyDetails { name: "FNK LLC", domain: "rnet.ru", company_type: "business" }), carrier: None, privacy: Some(PrivacyDetails { vpn: true, proxy: false, tor: false, relay: false, hosting: true, service: "MaxiVPN" }), abuse: Some(AbuseDetails { address: "Tirgonju iela 17, LV3401, Liepaja, Latvia", country: "RU", email: "abuse@veesp.com", name: "Abuse-C Role", network: "2a00:1838::/32", phone: "" }), domains: Some(DomainsDetails { ip: None, total: 0, domains: [] }), bogon: None, extra: {} }
2a02:4780:10:5bae::1: IpDetails { ip: "2a02:4780:10:5bae::1", hostname: None, city: "Phoenix", region: "Arizona", country: "US", country_name: Some("United States"), is_eu: Some(false), country_flag: Some(CountryFlag { emoji: "🇺🇸", unicode: "U+1F1FA U+1F1F8" }), country_flag_url: Some("https://cdn.ipinfo.io/static/images/countries-flags/US.svg"), country_currency: Some(CountryCurrency { code: "USD", symbol: "$" }), continent: Some(Continent { code: "NA", name: "North America" }), loc: "33.4484,-112.0740", org: Some("AS47583 Hostinger International Limited"), postal: Some("85001"), timezone: Some("America/Phoenix"), asn: Some(AsnDetails { asn: "AS47583", name: "Hostinger International Limited", domain: "hostinger.com", route: "2a02:4780:10::/48", asn_type: "hosting" }), company: Some(CompanyDetails { name: "Hostinger International Limited", domain: "hostinger.com", company_type: "hosting" }), carrier: None, privacy: Some(PrivacyDetails { vpn: true, proxy: false, tor: false, relay: false, hosting: true, service: "Best Proxy Switcher" }), abuse: Some(AbuseDetails { address: "Europos pr. 32, LT-46326 Kaunas, Lithuania", country: "LT", email: "abuse@hostinger.com", name: "Hostinger Administrators", network: "2a02:4780:10::/48", phone: "+37064503378" }), domains: Some(DomainsDetails { ip: None, total: 0, domains: [] }), bogon: None, extra: {} }