Skip to main content

ipscanner/types/
bulk.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use super::{null_default, Deduction};
6
7/// Input for the bulk endpoints: a list of IPs, or a pasted blob the server splits.
8#[derive(Debug, Clone, Default, PartialEq, Serialize)]
9pub struct BulkRequest {
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub ips: Option<Vec<String>>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub input: Option<String>,
14}
15
16impl BulkRequest {
17    /// A request from a list of IPs.
18    pub fn ips<I, S>(ips: I) -> Self
19    where
20        I: IntoIterator<Item = S>,
21        S: Into<String>,
22    {
23        BulkRequest {
24            ips: Some(ips.into_iter().map(Into::into).collect()),
25            input: None,
26        }
27    }
28
29    /// A request from free text, one or more IPs per line.
30    pub fn input(input: impl Into<String>) -> Self {
31        BulkRequest {
32            ips: None,
33            input: Some(input.into()),
34        }
35    }
36}
37
38/// Response of `POST /v1/bulk/check`.
39#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
40#[serde(default, rename_all = "camelCase")]
41pub struct BulkCheck {
42    pub submitted: i64,
43    pub unique: i64,
44    pub duplicates: i64,
45    #[serde(deserialize_with = "null_default")]
46    pub invalid: Vec<String>,
47    /// Empty on the Free plan.
48    #[serde(deserialize_with = "null_default")]
49    pub summary: HashMap<String, i64>,
50    #[serde(deserialize_with = "null_default")]
51    pub results: Vec<BulkResult>,
52    pub plan_required: Option<String>,
53}
54
55/// One row of a bulk check. Score, grade, verdict, deductions and VPN brand are null on the Free plan.
56#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
57#[serde(default, rename_all = "camelCase")]
58pub struct BulkResult {
59    pub input: String,
60    pub ip: String,
61    pub port: Option<u16>,
62    pub score: Option<i64>,
63    pub grade: Option<String>,
64    pub verdict: Option<String>,
65    pub classification: String,
66    pub confidence: f64,
67    pub anonymized: bool,
68    pub vpn_provider: Option<String>,
69    pub is_tor_exit: bool,
70    pub in_vpn_range: bool,
71    pub in_datacenter_range: bool,
72    pub asn: Option<String>,
73    pub asn_name: Option<String>,
74    pub asn_type: Option<String>,
75    pub country: Option<String>,
76    #[serde(deserialize_with = "null_default")]
77    pub deductions: Vec<Deduction>,
78    pub locked: Vec<String>,
79}
80
81/// One line of the `POST /v1/ip/bulk` NDJSON stream. Missing and null fields are zero values.
82#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
83#[serde(default, rename_all = "camelCase")]
84pub struct BulkEvent {
85    /// `meta`, `result`, `error` or `done`.
86    #[serde(rename = "type")]
87    pub kind: String,
88    pub index: i64,
89    pub total: i64,
90    pub input: String,
91    pub reason: String,
92    pub message: String,
93    pub error: String,
94    pub ip: String,
95    #[serde(deserialize_with = "null_default")]
96    pub verdict: String,
97    pub classification: String,
98    pub confidence: f64,
99    pub anonymized: bool,
100    #[serde(deserialize_with = "null_default")]
101    pub vpn_provider: String,
102    #[serde(deserialize_with = "null_default")]
103    pub score: i64,
104    #[serde(deserialize_with = "null_default")]
105    pub grade: String,
106    pub is_tor_exit: bool,
107    pub asn: String,
108    pub asn_name: String,
109    pub asn_type: String,
110    pub country: String,
111    pub processed: i64,
112    pub failed: i64,
113    pub metered: i64,
114    /// Set on the `meta` line when result lines are locked.
115    pub plan_required: String,
116    /// Fields of a `result` line nulled because of the plan.
117    pub locked: Vec<String>,
118}
119
120impl BulkEvent {
121    /// True for a `done` event with reason `complete` where `processed + failed >= total`.
122    pub fn complete(&self) -> bool {
123        self.kind == "done"
124            && self.reason == "complete"
125            && self.processed + self.failed >= self.total
126    }
127}