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 with service types
30//! let mut config = DiscoveryConfig::new();
31//! config.add_service_type(ServiceType::new("_http._tcp")?);
32//!
33//! // Initialize service discovery
34//! let discovery = ServiceDiscovery::new(config).await?;
35//!
36//! // Discover services using all protocols
37//! let services = discovery.discover_services(None).await?;
38//!
39//! println!("Found {} services", services.len());
40//! Ok(())
41//! }
42//! ```
43//! - Protocol manager with selective protocol enabling
44//! - Cross-platform implementation (Windows, Linux, macOS)
45//! - Asynchronous API with tokio support
46//! - Builder patterns for type-safe configuration
47//! - Secure service verification with cryptographic signatures
48//! - Protocol-agnostic service interface
49//!
50//! ## Advanced Usage
51//!
52//! ```rust
53//! use auto_discovery::{
54//! config::DiscoveryConfig,
55//! discovery::ServiceDiscovery,
56//! service::ServiceInfo,
57//! types::ServiceType,
58//! };
59//! use std::time::Duration;
60//!
61//! #[tokio::main]
62//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
63//! // Configure discovery with service types
64//! let mut config = DiscoveryConfig::new();
65//! config.add_service_type(ServiceType::new("_myservice._tcp")?);
66//!
67//! // Initialize service discovery
68//! let mut discovery = ServiceDiscovery::new(config).await?;
69//!
70//! // Register our service
71//! let service = ServiceInfo::new(
72//! "My Service Instance",
73//! "_myservice._tcp",
74//! 8080,
75//! Some(vec![("version", "1.0"), ("feature", "basic")])
76//! )?;
77//! discovery.register_service(service).await?;
78//!
79//! // Discover services using all protocols
80//! let services = discovery.discover_services(None).await?;
81//!
82//! for service in services {
83//! println!("Found service: {} at {}:{}",
84//! service.name,
85//! service.address,
86//! service.port);
87//! }
88//!
89//! Ok(())
90//! }
91//! ```
92//!
93//! ## Protocol Manager
94//!
95//! The library uses a protocol manager to handle multiple discovery protocols:
96//!
97//! ```rust
98//! use auto_discovery::protocols::ProtocolManager;
99//! use auto_discovery::config::DiscoveryConfig;
100//!
101//! #[tokio::main]
102//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
103//! let config = DiscoveryConfig::new();
104//! let manager = ProtocolManager::new(config).await?;
105//! Ok(())
106//! }
107//! ```
108//! ```
109//!
110//! ## Error Handling
111//!
112//! The library provides detailed error context for each protocol:
113//!
114//! ```rust
115//! use auto_discovery::error::DiscoveryError;
116//!
117//! // Protocol-specific errors include the protocol name
118//! let err = DiscoveryError::protocol("Service registration failed");
119//! ```
120//!
121//! ## Security Features
122//!
123//! ### TSIG Authentication
124//!
125//! The library provides comprehensive TSIG (Transaction SIGnature) support for secure DNS updates:
126//!
127//! ```rust,ignore
128//! use auto_discovery::{
129//! config::DiscoveryConfig,
130//! security::tsig::{TsigKey, TsigAlgorithm, TsigKeyManager},
131//! discovery::ServiceDiscovery,
132//! };
133//! use std::time::Duration;
134//!
135//! #[tokio::main]
136//! async fn main() {
137//! // Create TSIG key with expiry
138//! let key = TsigKey::new(
139//! "update.example.com.",
140//! TsigAlgorithm::HmacSha256,
141//! b"secret-key-data",
142//! Some(Duration::from_secs(3600)), // 1 hour validity
143//! ).unwrap();
144//!
145//! // Create key manager with rotation
146//! let key_manager = TsigKeyManager::new(Duration::from_secs(300)); // 5 minute rotation check
147//! key_manager.add_key(key).await;
148//!
149//! // Start key rotation
150//! key_manager.clone().start_key_rotation().await;
151//!
152//! // Create discovery instance with TSIG support
153//! let config = DiscoveryConfig::new();
154//!
155//! let discovery = ServiceDiscovery::new(config).await.unwrap();
156//! }
157//! ```
158//!
159//! Features:
160//! - Multiple HMAC algorithms (SHA1, SHA256, SHA384, SHA512)
161//! - Automatic key rotation
162//! - Key expiry management
163//! - Metrics collection
164//! - Prometheus integration
165//!
166//! ### Metrics
167//!
168//! TSIG-related metrics available:
169//! - `autodiscovery_tsig_keys_total`: Number of active TSIG keys
170//! - `autodiscovery_tsig_keys_expired_total`: Number of expired keys removed
171//! - `autodiscovery_tsig_sign_duration_seconds`: Time taken to sign messages
172//! - `autodiscovery_tsig_verify_duration_seconds`: Time taken to verify messages
173//! - `autodiscovery_tsig_sign_errors_total`: Number of signing failures
174//! - `autodiscovery_tsig_verify_errors_total`: Number of verification failures
175//!
176//! See the `examples/` directory for more complete examples.
177
178#![warn(missing_docs)]
179#![forbid(unsafe_code)]
180
181pub mod config;
182pub mod discovery;
183pub mod error;
184pub mod protocols;
185pub mod registry; // Service registry for managing discovered and registered services
186pub mod service;
187pub mod simple; // Simple API for common use cases
188pub mod types;
189pub mod utils;
190#[cfg(feature = "secure")]
191pub mod security;
192
193// Re-export main types for convenience
194pub use config::DiscoveryConfig;
195pub use discovery::ServiceDiscovery;
196pub use error::{DiscoveryError, Result};
197pub use service::{ServiceInfo, ServiceEvent};
198pub use types::{ServiceType, ProtocolType};