stalkermap/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! # StalkerMap
3//!
4//! A comprehensive Rust library for building CLI network scanner applications with robust input validation,
5//! terminal interaction, and URL parsing capabilities.
6//!
7//! ## Features
8//!
9//! - For advanced users: see docs for **dns::resolver::agnostic** and **dns::compressor::agnostic**
10//!
11//! ### Currently Available
12//!
13//! (All feature versions)
14//! - **Input Sanitization & Validation** - Type-safe input validation with composable filters
15//! - **Interactive Terminal Interface** - User-friendly CLI input with validation loops
16//! - **URL Parsing** - Comprehensive HTTP/HTTPS URL parsing with host validation
17//!
18//! ("std" feature)
19//! - **DNS Resolver** - Blocking DNS queries with support for multiple record types (A, MX, TXT, SOA, PTR, WKS, etc.)
20//!
21//! ("tokio-dep" feature)
22//! - **DNS Resolver** - Async DNS queries with support for multiple record types (A, MX, TXT, SOA, PTR, WKS, etc.)
23//! - **Asynchronous Scanner Engine** -
24//! - concurrent TCP scanning
25//! - modular per-connection Actions
26//! - real-time log streaming
27//! - bounded concurrency
28//! - idle detection & graceful shutdown
29//! - customizable log formatter
30//! - customizable actions
31//!
32//! ("Agnostic" feature)
33//! - **DNS message structure** - With encoder helpers (RFC1035 compliant)
34//! - **DNS message compressor** - For hostnames (RFC1035 compliant)
35//!
36//! ### Planned Features
37//!
38//! - **Directory Enumeration** - Web directory and file discovery
39//! - **Report Generation** - Export scan results to various formats
40//!
41//! ## Feature Variants
42//!
43//! - **Agnostic version**
44//! - Only parsing, encoding/decoding of DNS messages and helpers.
45//! - No executor or transport included — user chooses their own.
46//! - Fast to integrate, lightweight, perfect for advanced/custom usage.
47//!
48//! - **Default (`std`) version**
49//! - Blocking TCP/UDP transport using `std::net`.
50//! - Fully functional DNS resolver for blocking queries
51//!
52//! - **Tokio (`async`) version**
53//! - Replaces blocking transport with `tokio::net` for async TCP/UDP.
54//! - Supports non-blocking behaviors inspired by RFCs (TCP fallback, EDNS, retries, etc.). (Planned)
55//! - Can include opinionated helpers to accelerate scanner development.
56//! - Scanner Engine
57//!
58//! ## Quick Start
59//!
60//! Add this to your `Cargo.toml`:
61//!
62//! ```toml
63//! [dependencies]
64//! stalkermap = { version = "0.1.50", features = ["std"]}
65//! stalkermap = { version = "0.1.50", features = ["tokio-dep"]}
66//! stalkermap = { version = "0.1.50", default-features = false, features = ["agnostic"]}
67//! ```
68//!
69//! ## Usage Examples
70//!
71//! ### Scanner Engine (tokio-dep)
72//!
73//! ```rust,ignore
74//! use stalkermap::scanner::*;
75//! use stalkermap::actions;
76//! use stalkermap::utils::UrlParser;
77//! use tokio_stream::StreamExt;
78//!
79//! #[tokio::main]
80//! async fn main() {
81//! // Create the scanner (requires feature = "tokio-dep")
82//! let scanner = Scanner::<StructuredFormatter>::new().build();
83//!
84//! // Subscribe to log events
85//! let mut logs = scanner.get_logs_stream().await.unwrap();
86//!
87//! // Add a simple task using the built-in ActionIsPortOpen
88//! scanner.add_task(
89//! actions!(ActionIsPortOpen {}),
90//! UrlParser::from_str("https://127.0.0.1:80").unwrap()
91//! );
92//!
93//! // Start executing tasks
94//! scanner.execute_tasks();
95//!
96//! // Consume logs in real-time
97//! tokio::spawn(async move {
98//! while let Some(event) = logs.next().await {
99//! println!("{event:?}");
100//!
101//! // Pause until new tasks arrive when scanner goes idle
102//! if StructuredFormatter::is_idle_signal(&event) {
103//! logs.notify_when_new_tasks().await;
104//! }
105//! }
106//! });
107//!
108//! // Wait until scanner finishes all tasks
109//! scanner.await_idle().await;
110//!
111//! // Graceful shutdown
112//! scanner.shutdown_graceful().await;
113//! }
114//!
115//! ```
116//!
117//! ### DNS Resolver Example (std)
118//!
119//! ```rust,ignore
120//! # #[cfg(not(feature = "agnostic"))]
121//! # {
122//! use stalkermap::dns::resolver::{resolve_ipv4, resolve_mx};
123//!
124//! let a_records = resolve_ipv4("example.com").unwrap();
125//! let mx_records = resolve_mx("example.com").unwrap();
126//!
127//! println!("A records: {:#?}", a_records);
128//! println!("MX records: {:#?}", mx_records);
129//! # }
130//! ```
131//!
132//! ### Basic Input & Range Validation
133//!
134//! ```rust,no_run
135//! use stalkermap::utils::{Terminal, Sanitize, DesiredType};
136//!
137//! // Get validated user input with range checking
138//! let threads = Terminal::ask(
139//! "Enter scan threads (1-16):",
140//! &[Sanitize::IsBetween(1, 16)],
141//! );
142//! println!("Threads: {}", threads.answer);
143//! ```
144//!
145//! ### URL Parsing and Validation
146//!
147//! ```rust,no_run
148//! use stalkermap::utils::UrlParser;
149//!
150//! // Parse and validate URLs
151//! match UrlParser::new("https://example.com:8080/api") {
152//! Ok(url) => {
153//! println!("Scheme: {}", url.scheme);
154//! println!("Target: {}", url.target);
155//! println!("Port: {}", url.port);
156//! println!("Path: {}", url.subdirectory);
157//! }
158//! Err(e) => eprintln!("Invalid URL: {}", e),
159//! }
160//! ```
161//!
162//! ### Complex Input Validation
163//!
164//! ```rust,no_run
165//! use stalkermap::utils::{Terminal, Sanitize, DesiredType};
166//!
167//! // Multiple validation rules
168//! let choice = Terminal::ask(
169//! "Choose scan type (quick/deep/custom):",
170//! &[
171//! Sanitize::IsType(DesiredType::String),
172//! Sanitize::MatchStrings(vec![
173//! "quick".to_string(),
174//! "deep".to_string(),
175//! "custom".to_string(),
176//! ]),
177//! ],
178//! );
179//! ```
180//!
181//! ### Complete Interactive URL Input Example
182//!
183//! This example demonstrates the complete workflow of getting user input, validating it,
184//! and parsing URLs - perfect for network scanner applications:
185//!
186//! ```rust,no_run
187//! use stalkermap::utils::{Terminal, Sanitize, DesiredType, UrlParser};
188//!
189//! // Get URL from user with validation
190//! let url: UrlParser = loop {
191//! let input = Terminal::ask(
192//! "Enter a URL (http:// or https://):",
193//! &[Sanitize::IsType(DesiredType::String)],
194//! );
195//!
196//! // Parse and validate the URL
197//! match UrlParser::new(&input.answer) {
198//! Ok(url) => break url,
199//! Err(e) => println!("{}", e)
200//! }
201//! };
202//!
203//! println!("{}", url);
204//! ```
205//!
206//! ### "Agnostic" Only Feature DNS Compressor Example
207//!
208//! Users can either build a DNS message manually using their own structures
209//! or use the `DnsMessage` struct provided by this library. Example with a raw buffer:
210//!
211//! ```rust,no_run
212//! # #[cfg(feature = "agnostic")]
213//! # {
214//! use std::collections::HashMap;
215//! use stalkermap::dns::compressor::MessageCompressor;
216//!
217//! let mut message = Vec::new();
218//! let mut pointer_map = HashMap::new();
219//!
220//! // Compress a domain name
221//! MessageCompressor::compress("www.example.com", &mut message, &mut pointer_map).unwrap();
222//!
223//! // Reusing the same domain (or suffix) inserts a pointer instead of repeating bytes
224//! MessageCompressor::compress("mail.example.com", &mut message, &mut pointer_map).unwrap();
225//! # }
226//! ```
227//!
228//! ### "Agnostic" Only Feature DNS Message Query Builder Example
229//!
230//! ```rust,no_run
231//! # #[cfg(feature = "agnostic")]
232//! # {
233//! use stalkermap::dns::resolver::agnostic::{DnsMessage, RecordType, OpCodeOptions};
234//!
235//! // Build a query for example.com A record
236//! let msg = DnsMessage::new_query("example.com", RecordType::A, OpCodeOptions::StandardQuery);
237//!
238//! // Encode into raw bytes, ready to send via UDP/TCP
239//! let bytes = msg.encode_query();
240//!
241//! assert!(!bytes[12..].is_empty()); // includes header + question
242//! # }
243//! ```
244//!
245//! ## Architecture
246//!
247//! The library is designed with modularity and composability in mind:
248//!
249//! - **`utils`** - Core utilities for input handling and URL parsing
250//! - **`dns`** - DNS resolution and query utilities
251//! - **`scanner`** - Port scanning and network discovery
252//! - **`reporter`** - Report generation and export (planned)
253//!
254//! ## Design Principles
255//!
256//! - **Type Safety** - Leverages Rust's type system for compile-time guarantees
257//! - **Error Handling** - Comprehensive error types with descriptive messages
258//! - **Performance** - Optimized for speed and memory efficiency
259//! - **Usability** - Intuitive APIs with excellent documentation
260//!
261//! ## Supported URL Formats
262//!
263//! - HTTP/HTTPS schemes
264//! - IPv4 and IPv6 addresses
265//! - DNS hostnames
266//! - Custom ports
267//! - Paths and query strings
268//!
269//! ## Error Handling
270//!
271//! All operations return `Result<T, E>` types for safe error handling:
272//!
273//! ```rust,no_run
274//! use stalkermap::utils::{UrlParser, UrlParserErrors};
275//!
276//! match UrlParser::new("invalid-url") {
277//! Ok(url) => println!("Valid URL: {}", url),
278//! Err(UrlParserErrors::InvalidScheme) => eprintln!("Only HTTP/HTTPS supported"),
279//! Err(UrlParserErrors::InvalidTargetType) => eprintln!("Invalid hostname or IP"),
280//! Err(e) => eprintln!("Other error: {}", e),
281//! }
282//! ```
283//!
284//! ## Contributing
285//!
286//! ## License
287//!
288//! This project is licensed under the MIT License - see the [LICENSE](https://github.com/seakerOner/stalkermap-rs/blob/master/LICENSE) file for details.
289//!
290//! ## Changelog
291//!
292//! See [CHANGELOG.md](https://github.com/seakerOner/stalkermap-rs/blob/master/CHANGELOG.md) for a list of changes and version history.
293
294pub mod dns;
295
296pub mod utils;
297
298#[cfg_attr(docsrs, doc(cfg(feature = "tokio-dep")))]
299#[cfg(feature = "tokio-dep")]
300pub mod scanner;