Skip to main content

ipscanner/resources/
bulk.rs

1use std::collections::VecDeque;
2use std::fmt;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5use std::time::Duration;
6
7use futures_util::stream::{self, Stream, StreamExt};
8
9use crate::types::{BulkCheck, BulkEvent, BulkRequest};
10use crate::{Client, Error};
11
12const STREAM_TIMEOUT: Duration = Duration::from_secs(300);
13
14/// Bulk checks. Get one with [`Client::bulk`].
15#[derive(Debug, Clone, Copy)]
16pub struct Bulk<'a> {
17    client: &'a Client,
18}
19
20impl<'a> Bulk<'a> {
21    pub(crate) fn new(client: &'a Client) -> Self {
22        Bulk { client }
23    }
24
25    /// Checks a list of IPs and returns every result in one response.
26    pub async fn check(&self, req: &BulkRequest) -> Result<BulkCheck, Error> {
27        self.client.post("/bulk/check", req).await
28    }
29
30    /// Streams one event per IP as NDJSON; 400 and 429 before the stream starts are returned here.
31    pub async fn stream(&self, req: &BulkRequest) -> Result<BulkStream, Error> {
32        let res = self
33            .client
34            .post_raw("/ip/bulk", req, Some(STREAM_TIMEOUT))
35            .await?;
36        Ok(BulkStream::new(res.bytes_stream()))
37    }
38}
39
40/// A stream of [`BulkEvent`]s parsed from NDJSON.
41pub struct BulkStream {
42    inner: Pin<Box<dyn Stream<Item = Result<BulkEvent, Error>> + Send>>,
43}
44
45impl fmt::Debug for BulkStream {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        f.debug_struct("BulkStream").finish_non_exhaustive()
48    }
49}
50
51impl BulkStream {
52    pub(crate) fn new<S, B, E>(body: S) -> Self
53    where
54        S: Stream<Item = Result<B, E>> + Send + 'static,
55        B: AsRef<[u8]> + 'static,
56        E: Into<Error> + 'static,
57    {
58        BulkStream {
59            inner: Box::pin(ndjson(body)),
60        }
61    }
62}
63
64impl Stream for BulkStream {
65    type Item = Result<BulkEvent, Error>;
66
67    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
68        self.inner.as_mut().poll_next(cx)
69    }
70}
71
72struct LineState<S> {
73    body: Pin<Box<S>>,
74    buf: Vec<u8>,
75    ready: VecDeque<Result<BulkEvent, Error>>,
76    done: bool,
77}
78
79impl<S> LineState<S> {
80    fn drain_lines(&mut self) {
81        while let Some(pos) = self.buf.iter().position(|&b| b == b'\n') {
82            let line: Vec<u8> = self.buf.drain(..=pos).collect();
83            self.push_line(&line);
84        }
85        if self.done && !self.buf.is_empty() {
86            let line = std::mem::take(&mut self.buf);
87            self.push_line(&line);
88        }
89    }
90
91    fn push_line(&mut self, line: &[u8]) {
92        let line = line.trim_ascii();
93        if !line.is_empty() {
94            self.ready
95                .push_back(serde_json::from_slice(line).map_err(Error::from));
96        }
97    }
98}
99
100fn ndjson<S, B, E>(body: S) -> impl Stream<Item = Result<BulkEvent, Error>> + Send
101where
102    S: Stream<Item = Result<B, E>> + Send + 'static,
103    B: AsRef<[u8]> + 'static,
104    E: Into<Error> + 'static,
105{
106    let state = LineState {
107        body: Box::pin(body),
108        buf: Vec::new(),
109        ready: VecDeque::new(),
110        done: false,
111    };
112    stream::unfold(state, |mut st| async move {
113        loop {
114            if let Some(item) = st.ready.pop_front() {
115                return Some((item, st));
116            }
117            if st.done {
118                return None;
119            }
120            match st.body.next().await {
121                Some(Ok(chunk)) => {
122                    st.buf.extend_from_slice(chunk.as_ref());
123                    st.drain_lines();
124                }
125                Some(Err(e)) => {
126                    st.done = true;
127                    st.buf.clear();
128                    return Some((Err(e.into()), st));
129                }
130                None => {
131                    st.done = true;
132                    st.drain_lines();
133                }
134            }
135        }
136    })
137}