safebrowsing 0.1.0

Rust implementation of Google Safe Browsing Update API (v4)
Documentation

Safe Browsing API Client for Rust

This crate provides a Rust implementation of the Google Safe Browsing Update API (v4). It allows you to check URLs against Google's constantly updated lists of unsafe web resources.

Features

  • Asynchronous API using tokio
  • Pluggable database backends
  • Built-in caching with TTL support
  • URL canonicalization and pattern generation
  • Support for all Safe Browsing threat types

Example

use safebrowsing::{SafeBrowser, Config, ThreatDescriptor, ThreatType, PlatformType, ThreatEntryType};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config {
        api_key: "your-api-key".to_string(),
        client_id: "your-client-id".to_string(),
        client_version: "1.0.0".to_string(),
        update_period: Duration::from_secs(1800), // 30 minutes
        threat_lists: vec![
            ThreatDescriptor {
                threat_type: ThreatType::Malware,
                platform_type: PlatformType::AnyPlatform,
                threat_entry_type: ThreatEntryType::Url,
            },
            ThreatDescriptor {
                threat_type: ThreatType::SocialEngineering,
                platform_type: PlatformType::AnyPlatform,
                threat_entry_type: ThreatEntryType::Url,
            },
        ],
        ..Default::default()
    };

    let mut sb = SafeBrowser::new(config).await?;
    sb.wait_until_ready().await?;

    let urls = vec!["http://example.com/suspicious"];
    let threats = sb.lookup_urls(&urls).await?;

    for (url, threat_matches) in urls.iter().zip(threats.iter()) {
        if !threat_matches.is_empty() {
            println!("⚠️  {} is unsafe: {:?}", url, threat_matches);
        } else {
            println!("{} is safe", url);
        }
    }

    sb.close().await?;
    Ok(())
}