2023-01-30 15:23:01 -05:00
|
|
|
//! # Rdsys Backend Distributor API
|
|
|
|
//!
|
|
|
|
//! `rdsys_backend` is an implementation of the rdsys backend API
|
|
|
|
//! https://gitlab.torproject.org/tpo/anti-censorship/rdsys/-/blob/main/doc/backend-api.md
|
|
|
|
|
2023-01-30 15:08:30 -05:00
|
|
|
use bytes::{self, Buf, Bytes};
|
2023-03-21 17:37:15 -04:00
|
|
|
use core::pin::Pin;
|
2023-03-22 15:05:05 -04:00
|
|
|
use core::task::{Poll, ready};
|
2023-03-21 17:37:15 -04:00
|
|
|
use futures_util::{Stream, StreamExt};
|
2023-01-30 15:08:30 -05:00
|
|
|
use reqwest::Client;
|
|
|
|
use std::io::{self, BufRead};
|
2023-03-21 17:37:15 -04:00
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
2023-03-22 15:05:05 -04:00
|
|
|
use tokio_util::sync::ReusableBoxFuture;
|
2023-01-20 16:01:03 -05:00
|
|
|
|
2023-01-30 15:08:30 -05:00
|
|
|
pub mod proto;
|
2023-01-20 16:01:03 -05:00
|
|
|
|
2023-01-30 15:08:30 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Reqwest(reqwest::Error),
|
|
|
|
Io(io::Error),
|
|
|
|
JSON(serde_json::Error),
|
|
|
|
}
|
2023-01-29 14:18:39 -05:00
|
|
|
|
2023-01-30 15:08:30 -05:00
|
|
|
impl From<serde_json::Error> for Error {
|
|
|
|
fn from(value: serde_json::Error) -> Self {
|
|
|
|
Self::JSON(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for Error {
|
|
|
|
fn from(value: reqwest::Error) -> Self {
|
|
|
|
Self::Reqwest(value)
|
|
|
|
}
|
2023-01-20 16:01:03 -05:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:08:30 -05:00
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(value: io::Error) -> Self {
|
|
|
|
Self::Io(value)
|
|
|
|
}
|
2023-01-20 16:01:03 -05:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:23:01 -05:00
|
|
|
/// An iterable wrapper of ResourceDiff items for the streamed chunks of Bytes
|
|
|
|
/// received from the connection to the rdsys backend
|
2023-01-30 15:08:30 -05:00
|
|
|
pub struct ResourceStream {
|
2023-03-22 15:05:05 -04:00
|
|
|
inner: ReusableBoxFuture<'static, (Option<Bytes>, ReceiverStream<Bytes>)>,
|
2023-01-30 15:08:30 -05:00
|
|
|
buf: Vec<u8>,
|
|
|
|
partial: Option<bytes::buf::Reader<Bytes>>,
|
|
|
|
}
|
|
|
|
|
2023-03-21 17:37:15 -04:00
|
|
|
impl ResourceStream {
|
|
|
|
pub fn new(rx: mpsc::Receiver<Bytes>) -> ResourceStream {
|
|
|
|
ResourceStream {
|
2023-03-22 15:05:05 -04:00
|
|
|
inner: ReusableBoxFuture::new(make_future(ReceiverStream::new(rx))),
|
2023-03-21 17:37:15 -04:00
|
|
|
buf: vec![],
|
|
|
|
partial: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:05:05 -04:00
|
|
|
async fn make_future(mut rx: ReceiverStream<Bytes>) -> (Option<Bytes>, ReceiverStream<Bytes>) {
|
|
|
|
let result = rx.next().await;
|
|
|
|
(result, rx)
|
|
|
|
}
|
|
|
|
|
2023-03-21 17:37:15 -04:00
|
|
|
impl Stream for ResourceStream {
|
2023-01-30 15:08:30 -05:00
|
|
|
type Item = proto::ResourceDiff;
|
2023-03-21 17:37:15 -04:00
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut core::task::Context) -> Poll<Option<Self::Item>> {
|
|
|
|
let parse =
|
|
|
|
|buffer: &mut bytes::buf::Reader<Bytes>, buf: &mut Vec<u8>| -> Result<Option<Self::Item>, Error> {
|
|
|
|
match buffer.read_until(b'\r', buf) {
|
|
|
|
Ok(_) => match buf.pop() {
|
|
|
|
Some(b'\r') => match serde_json::from_slice(buf) {
|
2023-01-30 15:08:30 -05:00
|
|
|
Ok(diff) => {
|
2023-03-21 17:37:15 -04:00
|
|
|
buf.clear();
|
2023-01-30 17:05:38 -05:00
|
|
|
Ok(Some(diff))
|
2023-01-30 15:08:30 -05:00
|
|
|
}
|
2023-01-30 17:05:38 -05:00
|
|
|
Err(e) => Err(Error::JSON(e)),
|
2023-01-30 15:08:30 -05:00
|
|
|
},
|
|
|
|
Some(n) => {
|
2023-03-21 17:37:15 -04:00
|
|
|
buf.push(n);
|
2023-01-30 17:05:38 -05:00
|
|
|
Ok(None)
|
2023-01-30 15:08:30 -05:00
|
|
|
}
|
2023-01-30 17:05:38 -05:00
|
|
|
None => Ok(None),
|
2023-01-30 15:08:30 -05:00
|
|
|
},
|
|
|
|
Err(e) => Err(Error::Io(e)),
|
|
|
|
}
|
|
|
|
};
|
2023-03-21 17:37:15 -04:00
|
|
|
// This clone is here to avoid having multiple mutable references to self
|
|
|
|
// it's not optimal performance-wise but given that these resource streams aren't large
|
|
|
|
// this feels like an acceptable trade-off to the complexity of interior mutability
|
|
|
|
let mut buf = self.buf.clone();
|
2023-01-30 15:08:30 -05:00
|
|
|
if let Some(p) = &mut self.partial {
|
2023-03-21 17:37:15 -04:00
|
|
|
match parse(p, &mut buf) {
|
|
|
|
Ok(Some(diff)) => return Poll::Ready(Some(diff)),
|
2023-01-30 15:08:30 -05:00
|
|
|
Ok(None) => self.partial = None,
|
2023-03-21 17:37:15 -04:00
|
|
|
Err(_) => return Poll::Ready(None),
|
2023-01-30 15:08:30 -05:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 17:37:15 -04:00
|
|
|
self.buf = buf;
|
2023-03-22 15:05:05 -04:00
|
|
|
println!("HERE");
|
|
|
|
let (result, rx) = ready!(self.inner.poll(cx));
|
|
|
|
println!("HERE2");
|
|
|
|
self.inner.set(make_future(rx));
|
|
|
|
match result {
|
|
|
|
Some(chunk) => {
|
2023-03-21 17:37:15 -04:00
|
|
|
let mut buffer = chunk.reader();
|
|
|
|
match parse(&mut buffer, &mut self.buf) {
|
|
|
|
Ok(Some(diff)) => {
|
|
|
|
self.partial = Some(buffer);
|
|
|
|
return Poll::Ready(Some(diff));
|
|
|
|
}
|
|
|
|
Ok(None) => Poll::Pending, //maybe loop here?
|
|
|
|
Err(_) => return Poll::Ready(None),
|
2023-01-30 15:08:30 -05:00
|
|
|
}
|
2023-03-21 17:37:15 -04:00
|
|
|
}
|
2023-03-22 15:05:05 -04:00
|
|
|
None => Poll::Ready(None),
|
2023-01-30 15:08:30 -05:00
|
|
|
}
|
|
|
|
}
|
2023-01-20 16:01:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2023-03-21 17:37:15 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn parse_resource() {
|
|
|
|
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
|
2023-01-30 15:08:30 -05:00
|
|
|
let chunk = Bytes::from_static(
|
|
|
|
b"{\"new\": null,\"changed\": null,\"gone\": null,\"full_update\": true}\r",
|
2023-01-20 16:12:29 -05:00
|
|
|
);
|
2023-03-21 17:37:15 -04:00
|
|
|
let (tx, rx) = mpsc::channel(100);
|
|
|
|
tx.send(chunk).await.unwrap();
|
|
|
|
let mut diffs = ResourceStream::new(rx);
|
|
|
|
let res = Pin::new(&mut diffs).poll_next(&mut cx);
|
|
|
|
assert_ne!(res, Poll::Ready(None));
|
|
|
|
assert_ne!(res, Poll::Pending);
|
|
|
|
if let Poll::Ready(Some(diff)) = res {
|
2023-01-30 15:08:30 -05:00
|
|
|
assert_eq!(diff.new, None);
|
|
|
|
assert_eq!(diff.full_update, true);
|
2023-01-23 12:58:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 17:37:15 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn parse_across_chunks() {
|
|
|
|
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
|
2023-01-30 15:08:30 -05:00
|
|
|
let chunk1 = Bytes::from_static(b"{\"new\": null,\"changed\": null,");
|
|
|
|
let chunk2 = Bytes::from_static(b"\"gone\": null,\"full_update\": true}\r");
|
2023-03-21 17:37:15 -04:00
|
|
|
let (tx, rx) = mpsc::channel(100);
|
|
|
|
tx.send(chunk1).await.unwrap();
|
|
|
|
tx.send(chunk2).await.unwrap();
|
|
|
|
let mut diffs = ResourceStream::new(rx);
|
|
|
|
let mut res = Pin::new(&mut diffs).poll_next(&mut cx);
|
|
|
|
while let Poll::Pending = res {
|
|
|
|
res = Pin::new(&mut diffs).poll_next(&mut cx);
|
|
|
|
}
|
|
|
|
assert_ne!(res, Poll::Ready(None));
|
|
|
|
assert_ne!(res, Poll::Pending);
|
|
|
|
if let Poll::Ready(Some(diff)) = res {
|
2023-01-30 15:08:30 -05:00
|
|
|
assert_eq!(diff.new, None);
|
|
|
|
assert_eq!(diff.full_update, true);
|
|
|
|
}
|
2023-01-20 16:01:03 -05:00
|
|
|
}
|
2023-01-30 10:22:32 -05:00
|
|
|
|
2023-03-21 17:37:15 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn parse_multi_diff_partial_chunks() {
|
|
|
|
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
|
2023-01-30 15:08:30 -05:00
|
|
|
let chunk1 = Bytes::from_static(b"{\"new\": null,\"changed\": null,");
|
|
|
|
let chunk2 =
|
|
|
|
Bytes::from_static(b"\"gone\": null,\"full_update\": true}\r{\"new\": null,\"changed");
|
|
|
|
let chunk3 = Bytes::from_static(b"\": null,\"gone\": null,\"full_update\": true}");
|
|
|
|
let chunk4 = Bytes::from_static(b"\r");
|
2023-03-21 17:37:15 -04:00
|
|
|
let (tx, rx) = mpsc::channel(100);
|
|
|
|
tx.send(chunk1).await.unwrap();
|
|
|
|
tx.send(chunk2).await.unwrap();
|
|
|
|
tx.send(chunk3).await.unwrap();
|
|
|
|
tx.send(chunk4).await.unwrap();
|
|
|
|
let mut diffs = ResourceStream::new(rx);
|
|
|
|
let mut res = Pin::new(&mut diffs).poll_next(&mut cx);
|
|
|
|
while let Poll::Pending = res {
|
|
|
|
res = Pin::new(&mut diffs).poll_next(&mut cx);
|
|
|
|
}
|
|
|
|
assert_ne!(res, Poll::Ready(None));
|
|
|
|
assert_ne!(res, Poll::Pending);
|
|
|
|
if let Poll::Ready(Some(diff)) = res {
|
2023-01-30 15:08:30 -05:00
|
|
|
assert_eq!(diff.new, None);
|
|
|
|
assert_eq!(diff.full_update, true);
|
|
|
|
}
|
2023-03-21 17:37:15 -04:00
|
|
|
res = Pin::new(&mut diffs).poll_next(&mut cx);
|
|
|
|
while let Poll::Pending = res {
|
|
|
|
res = Pin::new(&mut diffs).poll_next(&mut cx);
|
|
|
|
}
|
|
|
|
assert_ne!(res, Poll::Ready(None));
|
|
|
|
assert_ne!(res, Poll::Pending);
|
|
|
|
if let Poll::Ready(Some(diff)) = res {
|
2023-01-30 15:08:30 -05:00
|
|
|
assert_eq!(diff.new, None);
|
|
|
|
assert_eq!(diff.full_update, true);
|
|
|
|
}
|
2023-01-30 10:22:32 -05:00
|
|
|
}
|
2023-01-20 16:01:03 -05:00
|
|
|
}
|
2023-01-30 15:08:30 -05:00
|
|
|
|
2023-01-30 15:23:01 -05:00
|
|
|
/// Makes an http connection to the rdsys backend api endpoint and returns a ResourceStream
|
|
|
|
/// if successful
|
|
|
|
///
|
|
|
|
/// # Examples
|
2023-01-30 17:05:38 -05:00
|
|
|
///
|
|
|
|
/// ```ignore
|
2023-01-30 15:23:01 -05:00
|
|
|
/// use rdsys_backend::start_stream;
|
|
|
|
///
|
|
|
|
/// let endpoint = String::from("http://127.0.0.1:7100/resource-stream");
|
|
|
|
/// let name = String::from("https");
|
|
|
|
/// let token = String::from("HttpsApiTokenPlaceholder");
|
|
|
|
/// let types = vec![String::from("obfs2"), String::from("scramblesuit")];
|
2023-03-21 17:37:15 -04:00
|
|
|
/// let stream = start_stream(endpoint, name, token, types).await.unwrap();
|
|
|
|
/// loop {
|
|
|
|
/// match Pin::new(&mut stream).poll_next(&mut cx) {
|
|
|
|
/// Poll::Ready(Some(diff)) => println!("Received diff: {:?}", diff),
|
|
|
|
/// Poll::Ready(None) => break,
|
|
|
|
/// Poll::Pending => continue,
|
|
|
|
/// }
|
2023-01-30 15:23:01 -05:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2023-01-30 15:08:30 -05:00
|
|
|
pub async fn start_stream(
|
|
|
|
api_endpoint: String,
|
|
|
|
name: String,
|
|
|
|
token: String,
|
|
|
|
resource_types: Vec<String>,
|
|
|
|
) -> Result<ResourceStream, Error> {
|
2023-03-21 17:37:15 -04:00
|
|
|
let (tx, rx) = mpsc::channel(100);
|
2023-01-30 15:08:30 -05:00
|
|
|
|
|
|
|
let req = proto::ResourceRequest {
|
|
|
|
request_origin: name,
|
2023-01-30 17:05:38 -05:00
|
|
|
resource_types,
|
2023-01-30 15:08:30 -05:00
|
|
|
};
|
|
|
|
let json = serde_json::to_string(&req)?;
|
|
|
|
|
|
|
|
let auth_value = format!("Bearer {}", token);
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
let mut stream = client
|
|
|
|
.get(api_endpoint)
|
|
|
|
.header("Authorization", &auth_value)
|
|
|
|
.body(json)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.bytes_stream();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Some(chunk) = stream.next().await {
|
|
|
|
let bytes = match chunk {
|
|
|
|
Ok(b) => b,
|
|
|
|
Err(_e) => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2023-03-21 17:37:15 -04:00
|
|
|
tx.send(bytes).await.unwrap();
|
2023-01-30 15:08:30 -05:00
|
|
|
}
|
|
|
|
});
|
2023-03-21 17:37:15 -04:00
|
|
|
Ok(ResourceStream::new(rx))
|
2023-01-30 15:08:30 -05:00
|
|
|
}
|