Expand description
§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(())
}
Re-exports§
pub use crate::cache::Cache;
pub use crate::error::Error;
pub use crate::error::Result;
pub use safebrowsing_api;
pub use safebrowsing_db;
pub use safebrowsing_hash;
pub use safebrowsing_proto;
pub use safebrowsing_url;
Modules§
- cache
- Caching for Safe Browsing API responses
- database
- Re-export database types from the safebrowsing-db crate.
- error
- Error types for the Safe Browsing library
- types
- Core types for the Safe Browsing library
Structs§
- Concurrent
Database - Thread-safe wrapper around an in-memory database
- Config
- Configuration for the Safe Browsing client
- Database
Stats - Database statistics
- Hash
Prefix - HashSet
- HashSet for fast hash prefix lookups
- InMemory
Database - In-memory database implementation
- Safe
Browser - Main Safe Browsing client
- Safe
Browsing Api - Safe Browsing API client
- Stats
- Statistics about Safe Browsing operations
- Threat
Descriptor - A threat descriptor describes a specific threat list
- URLThreat
- Information about a URL that matched a threat list
Enums§
- Database
Type - Type of database to use
- Platform
Type - Platform types in the Safe Browsing API
- Threat
Entry Type - Types of threat entries in the Safe Browsing API
- Threat
Type - Types of threats in the Safe Browsing API
Constants§
- DEFAULT_
CLIENT_ ID - Default client ID
- DEFAULT_
CLIENT_ VERSION - Default client version
- DEFAULT_
REQUEST_ TIMEOUT - Default request timeout
- DEFAULT_
SERVER_ URL - Default Safe Browsing API server URL
- DEFAULT_
UPDATE_ PERIOD - Default update period for threat lists (30 minutes)
Traits§
- Database
- Database interface for Safe Browsing
Functions§
- canonicalize_
url - Canonicalize a URL according to the Safe Browsing specification
- generate_
patterns - Generate URL patterns for lookup in the Safe Browsing database
- validate_
url - Check if a URL string is potentially valid