auto_discovery/lib.rs
1//! # Auto Discovery
2//!
3//! A production-ready service discovery library for Rust applications.
4//!
5//! This crate provides a robust, secure, and extensible service discovery solution
6//! supporting multiple protocols (mDNS, DNS-SD, UPnP) with production-grade features
7//! including caching, health monitoring, metrics, and security.
8//!
9//! ## Features
10//!
11//! - Multiple protocol support (mDNS, DNS-SD, UPnP)
12//! - Async-first design using Tokio
13//! - Production safety features (caching, rate limiting, health checks)
14//! - Comprehensive security (TSIG, TLS, certificate pinning)
15//! - Prometheus metrics integration
16//! - Cross-platform support (Windows, Linux, macOS)
17//!
18//! ## Quick Start
19//!
20//! ```rust
21//! use auto_discovery::{
22//! config::DiscoveryConfig,
23//! discovery::ServiceDiscovery,
24//! types::{ProtocolType, ServiceType},
25//! };
26//!
27//! #[tokio::main]
28//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
29//! // Create configuration
30//! let config = DiscoveryConfig::default();
31//!
32//! // Initialize service discovery
33//! let discovery = ServiceDiscovery::new(config)?;
34//!
35//! // Discover HTTP services
36//! let services = discovery
37//! .discover_services(vec![
38//! ServiceType::new("_http._tcp")
39//! ])
40//! .await?;
41//!
42//! println!("Found {} services", services.len());
43//! Ok(())
44//! }
45//! ```
46//! - Protocol manager with selective protocol enabling
47//! - Cross-platform implementation (Windows, Linux, macOS)
48//! - Asynchronous API with tokio support
49//! - Builder patterns for type-safe configuration
50//! - Secure service verification with cryptographic signatures
51//! - Protocol-agnostic service interface
52//!
53//! ## Advanced Usage
54//!
55//! ```rust
56//! use auto_discovery::{
57//! config::DiscoveryConfig,
58//! service::ServiceInfo,
59//! types::ServiceType,
60//! };
61//! use std::time::Duration;
62//!
63//! #[tokio::main]
64//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
65//! // Configure discovery with builder pattern
66//! let config = DiscoveryConfig::builder()
67//! .discovery_interval(Duration::from_secs(5))
68//! .verify_services(true)
69//! .build();
70//!
71//! // Initialize service discovery
72//! let mut discovery = ServiceDiscovery::new(config)?;
73//!
74//! // Register our service
75//! let service = ServiceInfo::new(
76//! "My Service Instance",
77//! "_myservice._tcp",
78//! 8080,
79//! Some(vec![("version", "1.0"), ("feature", "basic")])
80//! );
81//! discovery.register_service(service).await?;
82//!
83//! // Discover services
84//! let service_types = vec![ServiceType::new("_myservice._tcp")];
85//! let services = discovery
86//! .discover_services(service_types)
87//! .await?;
88//!
89//! for service in services {
90//! println!("Found service: {} at {}:{}",
91//! service.name,
92//! service.address,
93//! service.port);
94//! }
95//!
96//! Ok(())
97//! }
98//! ```
99//!
100//! ## Protocol Manager
101//!
102//! The library uses a protocol manager to handle multiple discovery protocols:
103//!
104//! ```rust
105//! use auto_discovery::protocols::ProtocolManagerBuilder;
106//!
107//! let manager = ProtocolManagerBuilder::new(config)
108//! .with_mdns(true) // Enable mDNS
109//! .with_upnp(true) // Enable UPnP
110//! .with_dns_sd(false) // Disable DNS-SD
111//! .build()
112//! .await?;
113//! ```
114//!
115//! ## Error Handling
116//!
117//! The library provides detailed error context for each protocol:
118//!
119//! ```rust
120//! use auto_discovery::error::DiscoveryError;
121//!
122//! // Protocol-specific errors include the protocol name
123//! let err = DiscoveryError::protocol("mdns", "Service registration failed");
124//! ```
125//!
126//! ## Security Features
127//!
128//! ### TSIG Authentication
129//!
130//! The library provides comprehensive TSIG (Transaction SIGnature) support for secure DNS updates:
131//!
132//! ```rust
133//! use auto_discovery::{
134//! config::DiscoveryConfig,
135//! security::tsig::{TsigKey, TsigAlgorithm, TsigKeyManager},
136//! discovery::ServiceDiscovery,
137//! };
138//! use std::time::Duration;
139//!
140//! #[tokio::main]
141//! async fn main() {
142//! // Create TSIG key with expiry
143//! let key = TsigKey::new(
144//! "update.example.com.",
145//! TsigAlgorithm::HmacSha256,
146//! b"secret-key-data",
147//! Some(Duration::from_secs(3600)), // 1 hour validity
148//! ).unwrap();
149//!
150//! // Create key manager with rotation
151//! let key_manager = TsigKeyManager::new(Duration::from_secs(300)); // 5 minute rotation check
152//! key_manager.add_key(key).await;
153//!
154//! // Start key rotation
155//! key_manager.clone().start_key_rotation().await;
156//!
157//! // Create discovery instance with TSIG support
158//! let config = DiscoveryConfig::builder()
159//! .enable_tsig(true)
160//! .tsig_key_manager(key_manager)
161//! .build();
162//!
163//! let discovery = ServiceDiscovery::new(config).await.unwrap();
164//! }
165//! ```
166//!
167//! Features:
168//! - Multiple HMAC algorithms (SHA1, SHA256, SHA384, SHA512)
169//! - Automatic key rotation
170//! - Key expiry management
171//! - Metrics collection
172//! - Prometheus integration
173//!
174//! ### Metrics
175//!
176//! TSIG-related metrics available:
177//! - `autodiscovery_tsig_keys_total`: Number of active TSIG keys
178//! - `autodiscovery_tsig_keys_expired_total`: Number of expired keys removed
179//! - `autodiscovery_tsig_sign_duration_seconds`: Time taken to sign messages
180//! - `autodiscovery_tsig_verify_duration_seconds`: Time taken to verify messages
181//! - `autodiscovery_tsig_sign_errors_total`: Number of signing failures
182//! - `autodiscovery_tsig_verify_errors_total`: Number of verification failures
183//!
184//! See the `examples/` directory for more complete examples.
185
186#![warn(missing_docs)]
187#![forbid(unsafe_code)]
188
189pub mod config;
190pub mod discovery;
191pub mod error;
192pub mod protocols;
193pub mod service;
194pub mod types;
195pub mod utils;
196pub mod security;
197
198// Re-export main types for convenience
199pub use config::DiscoveryConfig;
200pub use discovery::ServiceDiscovery;
201pub use error::{DiscoveryError, Result};
202pub use service::{ServiceInfo, ServiceEvent};
203pub use types::{ServiceType, ProtocolType};