1个不稳定版本

0.1.0 2023年12月29日

#4 in #clam-av

MIT许可证

16KB
223

clamav-stream

Version License Test

ScannedStream是一个封装字节流的包装流。它将内部流发送到clamav进行扫描,同时将其传递给消费者。

此库受到了toblux/rust-clamav-client的启发。

入门指南

将依赖项添加到您的Cargo.toml

[dependencies]
clamav_stream = "0.1.0"

使用ScannedStream封装字节流并消费它。

当字节流干净时

在消费ScannedStream和其内部流之间没有区别。

use clamav_stream::ScannedStream;

use bytes::Bytes;
use std::net::TcpStream;
use tokio::fs::File;
use tokio_stream::StreamExt;
use tokio_util::io::ReaderStream;

#[tokio::main]
async fn main() {
    let file = File::open("tests/clean.txt").await.unwrap();
    let mut input = ReaderStream::new(file);

    let addr = "localhost:3310"; // tcp address to clamav server.
    let mut stream = ScannedStream::<_, TcpStream>::tcp(&mut input, addr).unwrap();

    // The result of consuming ScannedStream is equal to consuming the input stream.
    assert_eq!(stream.next().await, Some(Ok(Bytes::from("file contents 1st"))));
    assert_eq!(stream.next().await, Some(Ok(Bytes::from("file contents 2nd"))));
    // ... continue until all contents are consumed ...
    assert_eq!(stream.next().await, Some(Ok(Bytes::from("file contents last"))));
    assert_eq!(stream.next().await, None);
}

当字节流被感染时

在消费完所有内容后返回Err。

use clamav_stream::{Error, ScannedStream};

use bytes::Bytes;
use std::net::TcpStream;
use tokio::fs::File;
use tokio_stream::StreamExt;
use tokio_util::io::ReaderStream;

#[tokio::main]
async fn main() {
    let file = File::open("tests/eicar.txt").await.unwrap();
    let mut input = ReaderStream::new(file);

    let addr = "localhost:3310"; // tcp address to clamav server.
    let mut stream = ScannedStream::<_, TcpStream>::tcp(&mut input, addr).unwrap();

    // An Err is returned after all contents are consumed.
    assert_eq!(stream.next().await, Some(Ok(Bytes::from("file contents 1st"))));
    assert_eq!(stream.next().await, Some(Ok(Bytes::from("file contents 2nd"))));
    // ... continue until all contents are consumed ...
    assert_eq!(stream.next().await, Some(Ok(Bytes::from("file contents last"))));
    assert_eq!(stream.next().await, Some(Err(Error::Scan("message from clamav".into()))));
    assert_eq!(stream.next().await, None);
}

许可证

本软件在MIT许可证下发布。

依赖项

~2.6–4MB
~70K SLoC