Skip to main content

ipscanner/types/
bulk.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use super::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    pub invalid: Vec<String>,
46    pub summary: HashMap<String, i64>,
47    pub results: Vec<BulkResult>,
48}
49
50/// One row of a bulk check.
51#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
52#[serde(default, rename_all = "camelCase")]
53pub struct BulkResult {
54    pub input: String,
55    pub ip: String,
56    pub port: Option<u16>,
57    pub score: i64,
58    pub grade: String,
59    pub verdict: String,
60    pub classification: String,
61    pub confidence: f64,
62    pub anonymized: bool,
63    pub is_tor_exit: bool,
64    pub in_vpn_range: bool,
65    pub in_datacenter_range: bool,
66    pub asn: Option<String>,
67    pub asn_name: Option<String>,
68    pub asn_type: Option<String>,
69    pub country: Option<String>,
70    pub deductions: Vec<Deduction>,
71}
72
73/// One line of the `POST /v1/ip/bulk` NDJSON stream. Missing fields are zero values.
74#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
75#[serde(default, rename_all = "camelCase")]
76pub struct BulkEvent {
77    /// `meta`, `result`, `error` or `done`.
78    #[serde(rename = "type")]
79    pub kind: String,
80    pub index: i64,
81    pub total: i64,
82    pub input: String,
83    pub reason: String,
84    pub message: String,
85    pub error: String,
86    pub ip: String,
87    pub verdict: String,
88    pub classification: String,
89    pub confidence: f64,
90    pub anonymized: bool,
91    pub score: i64,
92    pub grade: String,
93    pub is_tor_exit: bool,
94    pub asn: String,
95    pub asn_name: String,
96    pub asn_type: String,
97    pub country: String,
98    pub processed: i64,
99    pub failed: i64,
100    pub metered: i64,
101}
102
103impl BulkEvent {
104    /// True for a `done` event with reason `complete` where `processed + failed >= total`.
105    pub fn complete(&self) -> bool {
106        self.kind == "done"
107            && self.reason == "complete"
108            && self.processed + self.failed >= self.total
109    }
110}