Skip to main content

ipscanner/resources/
ip.rs

1use serde_json::json;
2
3use crate::client::segment;
4use crate::types::{
5    AsnResponse, GeolocationResponse, History, HistoryParams, LookupResult, MyIp, ProxyResponse,
6    VpnResponse, WhoisResponse,
7};
8use crate::{Client, Error};
9
10/// IP lookups. Get one with [`Client::ip`].
11#[derive(Debug, Clone, Copy)]
12pub struct Ip<'a> {
13    client: &'a Client,
14}
15
16impl<'a> Ip<'a> {
17    pub(crate) fn new(client: &'a Client) -> Self {
18        Ip { client }
19    }
20
21    /// Full lookup of an IPv4 or IPv6 address, CIDR block or hostname.
22    pub async fn lookup(&self, target: &str) -> Result<LookupResult, Error> {
23        self.client
24            .post("/ip/lookup", &json!({ "target": target }))
25            .await
26    }
27
28    /// VPN and Tor check.
29    pub async fn vpn(&self, ip: &str) -> Result<VpnResponse, Error> {
30        self.client.get(&format!("/vpn/{}", segment(ip)), &[]).await
31    }
32
33    /// Proxy and Tor check.
34    pub async fn proxy(&self, ip: &str) -> Result<ProxyResponse, Error> {
35        self.client
36            .get(&format!("/proxy/{}", segment(ip)), &[])
37            .await
38    }
39
40    /// Geolocation.
41    pub async fn geo(&self, ip: &str) -> Result<GeolocationResponse, Error> {
42        self.client.get(&format!("/geo/{}", segment(ip)), &[]).await
43    }
44
45    /// Autonomous system of an address.
46    pub async fn asn(&self, ip: &str) -> Result<AsnResponse, Error> {
47        self.client.get(&format!("/asn/{}", segment(ip)), &[]).await
48    }
49
50    /// WHOIS record of a domain.
51    pub async fn whois(&self, domain: &str) -> Result<WhoisResponse, Error> {
52        self.client
53            .get(&format!("/whois/{}", segment(domain)), &[])
54            .await
55    }
56
57    /// Your recent lookups, newest first.
58    pub async fn history(&self, params: &HistoryParams) -> Result<History, Error> {
59        let mut q = Vec::new();
60        if let Some(v) = params.limit {
61            q.push(("limit", v.to_string()));
62        }
63        if let Some(v) = params.before {
64            q.push(("before", v.to_string()));
65        }
66        if let Some(v) = &params.verdict {
67            q.push(("verdict", v.clone()));
68        }
69        self.client.get("/ip/history", &q).await
70    }
71
72    /// Keyless demo lookup.
73    pub async fn demo(&self, ip: &str) -> Result<LookupResult, Error> {
74        self.client
75            .get(&format!("/demo/{}", segment(ip)), &[])
76            .await
77    }
78
79    /// The caller's own address. Keyless.
80    pub async fn myip(&self) -> Result<MyIp, Error> {
81        self.client.get("/myip", &[]).await
82    }
83}