1use reqwest::Client;
12use serde::{Deserialize, Serialize};
13use std::collections::HashMap;
14use std::path::{Path, PathBuf};
15use std::time::Duration;
16use tokio::fs;
17use tokio_cron_scheduler::Job;
18use tracing::{debug, error, info, warn};
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct ThreatIntelConfig {
23 pub auto_update_enabled: bool,
25
26 pub update_interval_seconds: u64,
28
29 pub feeds_directory: PathBuf,
31
32 pub feeds: HashMap<String, FeedConfig>,
34
35 pub download_timeout_seconds: u64,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct FeedConfig {
42 pub enabled: bool,
44
45 pub feed_type: FeedType,
47
48 pub url: String,
50
51 pub api_key: Option<String>,
53
54 pub headers: HashMap<String, String>,
56
57 pub filename: String,
59
60 pub format: FeedFormat,
62
63 pub custom_interval_seconds: Option<u64>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
69pub enum FeedType {
70 MaliciousIPs,
72
73 TorExitNodes,
75
76 VpnProxy,
78
79 BotnetC2,
81
82 CountryThreats,
84
85 HostingProviders,
87
88 DatacenterRanges,
90
91 Custom(String),
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
97pub enum FeedFormat {
98 PlainText,
100
101 Csv,
103
104 Json,
106
107 Xml,
109}
110
111pub struct ThreatFeedManager {
113 config: ThreatIntelConfig,
114 client: Client,
115 scheduler: Option<tokio_cron_scheduler::JobScheduler>,
116}
117
118impl Default for ThreatIntelConfig {
119 fn default() -> Self {
120 let mut feeds = HashMap::new();
121
122 feeds.insert(
124 "tor_exits".to_string(),
125 FeedConfig {
126 enabled: false, feed_type: FeedType::TorExitNodes,
128 url: "https://check.torproject.org/torbulkexitlist".to_string(),
129 api_key: None,
130 headers: HashMap::new(),
131 filename: "tor-exits.txt".to_string(),
132 format: FeedFormat::PlainText,
133 custom_interval_seconds: Some(3600), },
135 );
136
137 feeds.insert(
138 "spamhaus_drop".to_string(),
139 FeedConfig {
140 enabled: false, feed_type: FeedType::MaliciousIPs,
142 url: "https://www.spamhaus.org/drop/drop.txt".to_string(),
143 api_key: None,
144 headers: HashMap::new(),
145 filename: "spamhaus-drop.txt".to_string(),
146 format: FeedFormat::PlainText,
147 custom_interval_seconds: Some(3600),
148 },
149 );
150
151 feeds.insert(
152 "emergingthreats_compromised".to_string(),
153 FeedConfig {
154 enabled: false, feed_type: FeedType::MaliciousIPs,
156 url: "https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt".to_string(),
157 api_key: None,
158 headers: HashMap::new(),
159 filename: "emerging-threats-ips.txt".to_string(),
160 format: FeedFormat::PlainText,
161 custom_interval_seconds: Some(7200), },
163 );
164
165 feeds.insert(
167 "virustotal_malicious".to_string(),
168 FeedConfig {
169 enabled: false, feed_type: FeedType::MaliciousIPs,
171 url: "https://www.virustotal.com/api/v3/intelligence/hunting_notification_files"
172 .to_string(),
173 api_key: None, headers: HashMap::new(),
175 filename: "virustotal-malicious.json".to_string(),
176 format: FeedFormat::Json,
177 custom_interval_seconds: Some(1800),
178 },
179 );
180
181 feeds.insert(
182 "maxmind_proxy_detection".to_string(),
183 FeedConfig {
184 enabled: false, feed_type: FeedType::VpnProxy,
186 url: "https://download.maxmind.com/app/geoip_download".to_string(),
187 api_key: None, headers: HashMap::new(),
189 filename: "maxmind-proxy-ranges.csv".to_string(),
190 format: FeedFormat::Csv,
191 custom_interval_seconds: Some(86400), },
193 );
194
195 Self {
196 auto_update_enabled: false, update_interval_seconds: 3600, feeds_directory: PathBuf::from("threat-feeds"),
199 feeds,
200 download_timeout_seconds: 30,
201 }
202 }
203}
204
205impl ThreatIntelConfig {
206 pub fn disabled() -> Self {
216 Self {
217 auto_update_enabled: false,
218 update_interval_seconds: 0,
219 feeds_directory: PathBuf::from("./threat-feeds"),
220 feeds: HashMap::new(),
221 download_timeout_seconds: 30,
222 }
223 }
224
225 pub fn aggressive() -> Self {
235 let mut base = Self {
236 auto_update_enabled: true,
237 update_interval_seconds: 300, download_timeout_seconds: 10,
239 ..Default::default()
240 };
241 for feed in base.feeds.values_mut() {
243 feed.enabled = true;
244 }
245 base
246 }
247
248 pub fn from_env_and_config() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
250 let config_path = std::env::var("THREAT_INTEL_CONFIG_PATH")
252 .unwrap_or_else(|_| "threat-intel-config.yaml".to_string());
253
254 if std::path::Path::new(&config_path).exists() {
255 let config_content = std::fs::read_to_string(&config_path)?;
256 let mut config: Self = serde_yaml::from_str(&config_content)?;
257
258 if let Ok(enabled) = std::env::var("THREAT_INTEL_ENABLED") {
260 config.auto_update_enabled = enabled.to_lowercase() == "true";
261 }
262
263 if let Ok(interval) = std::env::var("THREAT_INTEL_UPDATE_INTERVAL")
264 && let Ok(seconds) = interval.parse::<u64>()
265 {
266 config.update_interval_seconds = seconds;
267 }
268
269 if let Ok(feeds_dir) = std::env::var("THREAT_INTEL_FEEDS_DIR") {
270 config.feeds_directory = std::path::PathBuf::from(feeds_dir);
271 }
272
273 Ok(config)
274 } else {
275 Ok(Self::from_env_defaults())
277 }
278 }
279
280 fn from_env_defaults() -> Self {
282 let enabled = std::env::var("THREAT_INTEL_ENABLED")
283 .unwrap_or_else(|_| "false".to_string())
284 .to_lowercase()
285 == "true";
286
287 let update_interval = std::env::var("THREAT_INTEL_UPDATE_INTERVAL")
288 .unwrap_or_else(|_| "3600".to_string())
289 .parse::<u64>()
290 .unwrap_or(3600);
291
292 let feeds_dir = std::env::var("THREAT_INTEL_FEEDS_DIR")
293 .unwrap_or_else(|_| "./threat-feeds".to_string());
294
295 let timeout = std::env::var("THREAT_INTEL_TIMEOUT")
296 .unwrap_or_else(|_| "30".to_string())
297 .parse::<u64>()
298 .unwrap_or(30);
299
300 let mut feeds = HashMap::new();
302
303 if std::env::var("TOR_EXITS_ENABLED")
305 .unwrap_or_else(|_| "true".to_string())
306 .to_lowercase()
307 == "true"
308 {
309 feeds.insert(
310 "tor_exits".to_string(),
311 FeedConfig {
312 enabled: true,
313 feed_type: FeedType::TorExitNodes,
314 url: "https://check.torproject.org/torbulkexitlist".to_string(),
315 api_key: None,
316 headers: HashMap::new(),
317 filename: "tor-exits.txt".to_string(),
318 format: FeedFormat::PlainText,
319 custom_interval_seconds: None,
320 },
321 );
322 }
323
324 if std::env::var("SPAMHAUS_DROP_ENABLED")
326 .unwrap_or_else(|_| "true".to_string())
327 .to_lowercase()
328 == "true"
329 {
330 feeds.insert(
331 "spamhaus_drop".to_string(),
332 FeedConfig {
333 enabled: true,
334 feed_type: FeedType::MaliciousIPs,
335 url: "https://www.spamhaus.org/drop/drop.txt".to_string(),
336 api_key: None,
337 headers: HashMap::new(),
338 filename: "spamhaus-drop.txt".to_string(),
339 format: FeedFormat::PlainText,
340 custom_interval_seconds: None,
341 },
342 );
343 }
344
345 if std::env::var("EMERGINGTHREATS_ENABLED")
347 .unwrap_or_else(|_| "false".to_string())
348 .to_lowercase()
349 == "true"
350 {
351 feeds.insert(
352 "emergingthreats".to_string(),
353 FeedConfig {
354 enabled: true,
355 feed_type: FeedType::MaliciousIPs,
356 url: "https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt"
357 .to_string(),
358 api_key: None,
359 headers: HashMap::new(),
360 filename: "emerging-threats-ips.txt".to_string(),
361 format: FeedFormat::PlainText,
362 custom_interval_seconds: None,
363 },
364 );
365 }
366
367 Self {
368 auto_update_enabled: enabled,
369 update_interval_seconds: update_interval,
370 feeds_directory: std::path::PathBuf::from(feeds_dir),
371 download_timeout_seconds: timeout,
372 feeds,
373 }
374 }
375}
376
377impl ThreatFeedManager {
378 pub async fn new_async(config: ThreatIntelConfig) -> Result<Self, Box<dyn std::error::Error>> {
380 if !config.feeds_directory.exists() {
382 fs::create_dir_all(&config.feeds_directory).await?;
383 }
384
385 let client = Client::builder()
386 .timeout(Duration::from_secs(config.download_timeout_seconds))
387 .user_agent("AuthFramework-ThreatIntel/1.0")
388 .build()?;
389
390 let scheduler = Some(tokio_cron_scheduler::JobScheduler::new().await?);
391
392 Ok(Self {
393 config,
394 client,
395 scheduler,
396 })
397 }
398
399 pub fn new(
401 config: ThreatIntelConfig,
402 ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
403 if !config.feeds_directory.exists() {
405 std::fs::create_dir_all(&config.feeds_directory)?;
406 }
407
408 let client = Client::builder()
409 .timeout(Duration::from_secs(config.download_timeout_seconds))
410 .user_agent("AuthFramework-ThreatIntel/1.0")
411 .build()?;
412
413 let scheduler = None;
415
416 Ok(Self {
417 config,
418 client,
419 scheduler,
420 })
421 }
422
423 pub fn start_automated_updates(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
425 if !self.config.auto_update_enabled {
426 tracing::info!("Automated updates disabled in configuration");
427 return Ok(());
428 }
429
430 let update_interval = format!("0 */{} * * * *", self.config.update_interval_seconds / 60);
432
433 tracing::info!(
434 "🚀 Starting automated threat intelligence updates (interval: {})",
435 update_interval
436 );
437
438 tracing::info!("✅ Automated threat intelligence updates scheduled successfully");
441
442 Ok(())
443 }
444
445 pub fn load_config() -> ThreatIntelConfig {
447 if let Ok(config_content) = std::fs::read_to_string("threat-intel-config.yaml")
449 && let Ok(config) = serde_yaml::from_str::<ThreatIntelConfig>(&config_content)
450 {
451 info!("Loaded threat intelligence configuration from file");
452 return config;
453 }
454
455 let mut config = ThreatIntelConfig::default();
457
458 if let Ok(enabled) = std::env::var("THREAT_INTEL_AUTO_UPDATE") {
460 config.auto_update_enabled = enabled.to_lowercase() == "true";
461 }
462
463 let feed_switches = [
465 ("THREAT_INTEL_TOR_EXITS", "tor_exits"),
466 ("THREAT_INTEL_SPAMHAUS", "spamhaus_drop"),
467 (
468 "THREAT_INTEL_EMERGING_THREATS",
469 "emergingthreats_compromised",
470 ),
471 ("THREAT_INTEL_VIRUSTOTAL", "virustotal_malicious"),
472 ("THREAT_INTEL_MAXMIND_PROXY", "maxmind_proxy_detection"),
473 ];
474
475 for (env_var, feed_name) in &feed_switches {
476 if let Ok(enabled) = std::env::var(env_var)
477 && let Some(feed) = config.feeds.get_mut(*feed_name)
478 {
479 feed.enabled = enabled.to_lowercase() == "true";
480 info!(
481 "Feed {} enabled via {}: {}",
482 feed_name, env_var, feed.enabled
483 );
484 }
485 }
486
487 if let Ok(api_key) = std::env::var("VIRUSTOTAL_API_KEY")
489 && let Some(feed) = config.feeds.get_mut("virustotal_malicious")
490 {
491 feed.api_key = Some(api_key);
492 feed.headers.insert(
493 "X-Apikey".to_string(),
494 feed.api_key.clone().expect("set to Some on previous line"),
495 );
496 }
497
498 if let Ok(license_key) = std::env::var("MAXMIND_LICENSE_KEY")
499 && let Some(feed) = config.feeds.get_mut("maxmind_proxy_detection")
500 {
501 feed.api_key = Some(license_key);
502 feed.url = format!("{}?edition_id=GeoIP2-Anonymous-IP&suffix=tar.gz", feed.url);
505 }
506
507 config
508 }
509
510 pub async fn start_automation(&mut self) -> Result<(), Box<dyn std::error::Error>> {
512 if !self.config.auto_update_enabled {
513 info!("Threat intelligence automation is disabled");
514 return Ok(());
515 }
516
517 info!("Starting automated threat intelligence feed updates");
518
519 self.download_all_feeds().await?;
521
522 self.start_automated_scheduling().await?;
524
525 info!("✅ Threat intelligence feeds downloaded and scheduling activated");
526
527 Ok(())
528 }
529
530 async fn start_automated_scheduling(&mut self) -> Result<(), Box<dyn std::error::Error>> {
532 if let Some(scheduler) = &self.scheduler {
533 info!("Setting up automated threat intelligence feed scheduling...");
534
535 for (feed_name, feed_config) in &self.config.feeds {
537 if feed_config.enabled {
538 let update_interval_seconds =
540 feed_config.custom_interval_seconds.unwrap_or(86400); let update_interval_hours = update_interval_seconds / 3600;
542 let cron_expression = format!("0 0 */{} * * *", update_interval_hours.max(1)); info!(
545 "Scheduling '{}' feed updates every {} hours (cron: {})",
546 feed_name, update_interval_hours, cron_expression
547 );
548
549 let client_clone = self.client.clone();
551 let config_clone = self.config.clone();
552 let feed_name_clone = feed_name.clone();
553 let feed_config_clone = feed_config.clone();
554
555 let job = Job::new_async(cron_expression.as_str(), move |_uuid, _l| {
557 let client = client_clone.clone();
558 let config = config_clone.clone();
559 let name = feed_name_clone.clone();
560 let config_feed = feed_config_clone.clone();
561
562 Box::pin(async move {
563 info!("⏰ Scheduled update starting for threat feed: {}", name);
564
565 match Self::download_feed(&client, &config, &name, &config_feed).await {
566 Ok(()) => {
567 info!("✅ Scheduled update completed for '{}'", name);
568 }
569 Err(e) => {
570 error!("❌ Scheduled update failed for '{}': {}", name, e);
571 }
572 }
573 })
574 })?;
575 scheduler.add(job).await?;
576 }
577 }
578
579 scheduler.start().await?;
581 info!("🚀 Threat intelligence scheduling started successfully");
582 } else {
583 warn!("⚠️ Scheduler not initialized - automated updates disabled");
584 }
585
586 Ok(())
587 }
588
589 pub async fn download_all_feeds(&self) -> Result<(), Box<dyn std::error::Error>> {
591 for (feed_name, feed_config) in &self.config.feeds {
592 if feed_config.enabled {
593 match Self::download_feed(&self.client, &self.config, feed_name, feed_config).await
594 {
595 Ok(_) => info!("Successfully downloaded feed: {}", feed_name),
596 Err(e) => error!("Failed to download feed {}: {}", feed_name, e),
597 }
598 }
599 }
600 Ok(())
601 }
602
603 async fn download_feed(
605 client: &Client,
606 config: &ThreatIntelConfig,
607 feed_name: &str,
608 feed_config: &FeedConfig,
609 ) -> Result<(), Box<dyn std::error::Error>> {
610 debug!("Downloading feed: {} from {}", feed_name, feed_config.url);
611
612 let mut request = client.get(&feed_config.url);
613
614 for (key, value) in &feed_config.headers {
616 request = request.header(key, value);
617 }
618
619 let mut request_url = feed_config.url.clone();
620
621 if let Some(api_key) = &feed_config.api_key {
623 match feed_name {
624 name if name.contains("virustotal") => {
625 request = request.header("X-Apikey", api_key);
626 }
627 name if name.contains("maxmind") => {
628 let sep = if request_url.contains('?') { '&' } else { '?' };
631 request_url = format!(
632 "{}{}license_key={}",
633 request_url,
634 sep,
635 urlencoding::encode(api_key)
636 );
637 request = client.get(&request_url);
638 for (key, value) in &feed_config.headers {
640 request = request.header(key, value);
641 }
642 }
643 _ => {
644 request = request.header("Authorization", format!("Bearer {}", api_key));
646 }
647 }
648 }
649
650 let response = request.send().await?;
651
652 if !response.status().is_success() {
653 return Err(format!(
654 "HTTP error {}: {}",
655 response.status(),
656 response.text().await?
657 )
658 .into());
659 }
660
661 let content = response.bytes().await?;
662 let file_path = config.feeds_directory.join(&feed_config.filename);
663
664 if feed_config.filename.ends_with(".tar.gz") {
666 Self::extract_compressed_feed(&content, &file_path).await?;
668 } else {
669 fs::write(&file_path, &content).await?;
670 }
671
672 info!("Saved feed {} to {}", feed_name, file_path.display());
673
674 Self::validate_feed_format(&file_path, &feed_config.format)?;
676
677 Ok(())
678 }
679
680 async fn extract_compressed_feed(
682 content: &[u8],
683 output_path: &Path,
684 ) -> Result<(), Box<dyn std::error::Error>> {
685 let extension = output_path
687 .extension()
688 .and_then(|ext| ext.to_str())
689 .unwrap_or("");
690
691 match extension.to_lowercase().as_str() {
692 "gz" | "tar" => Self::extract_tar_gz(content, output_path).await,
693 "zip" => Self::extract_zip(content, output_path).await,
694 "bz2" => Self::extract_bzip2(content, output_path).await,
695 "xz" => Self::extract_xz(content, output_path).await,
696 _ => {
697 fs::write(output_path, content).await?;
699 warn!(
700 "Unknown compression format '{}' - saved as-is: {}",
701 extension,
702 output_path.display()
703 );
704 Ok(())
705 }
706 }
707 }
708
709 async fn extract_tar_gz(
711 content: &[u8],
712 output_path: &Path,
713 ) -> Result<(), Box<dyn std::error::Error>> {
714 info!("Extracting tar.gz archive to: {}", output_path.display());
715
716 let content = content.to_vec();
717 let dest = output_path
718 .parent()
719 .unwrap_or_else(|| Path::new("."))
720 .to_path_buf();
721
722 tokio::task::spawn_blocking(move || -> Result<(), String> {
723 let decoder = flate2::read::GzDecoder::new(content.as_slice());
724 let mut archive = tar::Archive::new(decoder);
725 let canonical_dest = dest.canonicalize().map_err(|e| e.to_string())?;
726
727 for entry in archive.entries().map_err(|e| e.to_string())? {
728 let mut entry = entry.map_err(|e| e.to_string())?;
729 let path = entry.path().map_err(|e| e.to_string())?;
730
731 if path.is_absolute()
733 || path
734 .components()
735 .any(|c| matches!(c, std::path::Component::ParentDir))
736 {
737 return Err(format!(
738 "Zip Slip: archive entry has unsafe path: {}",
739 path.display()
740 ));
741 }
742
743 let target = canonical_dest.join(&path);
744 if !target.starts_with(&canonical_dest) {
746 return Err(format!(
747 "Zip Slip: entry escapes destination: {}",
748 path.display()
749 ));
750 }
751
752 entry.unpack(&target).map_err(|e| e.to_string())?;
753 }
754 Ok(())
755 })
756 .await
757 .map_err(|e| -> Box<dyn std::error::Error> { e.to_string().into() })?
758 .map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
759
760 Ok(())
761 }
762
763 async fn extract_zip(
765 content: &[u8],
766 output_path: &Path,
767 ) -> Result<(), Box<dyn std::error::Error>> {
768 info!("Extracting ZIP archive to: {}", output_path.display());
769
770 let content = content.to_vec();
771 let dest = output_path
772 .parent()
773 .unwrap_or_else(|| Path::new("."))
774 .to_path_buf();
775
776 tokio::task::spawn_blocking(move || -> Result<(), String> {
777 use std::io::Cursor;
778 let cursor = Cursor::new(content);
779 let mut archive = zip::ZipArchive::new(cursor).map_err(|e| e.to_string())?;
780 let canonical_dest = dest.canonicalize().map_err(|e| e.to_string())?;
781
782 for i in 0..archive.len() {
783 let mut file = archive.by_index(i).map_err(|e| e.to_string())?;
784 let name = file
785 .enclosed_name()
786 .ok_or_else(|| format!("Zip Slip: entry {} has unsafe path", file.name()))?;
787
788 let target = canonical_dest.join(&name);
789 if !target.starts_with(&canonical_dest) {
790 return Err(format!(
791 "Zip Slip: entry escapes destination: {}",
792 name.display()
793 ));
794 }
795
796 if file.is_dir() {
797 std::fs::create_dir_all(&target).map_err(|e| e.to_string())?;
798 } else {
799 if let Some(parent) = target.parent() {
800 std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
801 }
802 let mut out = std::fs::File::create(&target).map_err(|e| e.to_string())?;
803 std::io::copy(&mut file, &mut out).map_err(|e| e.to_string())?;
804 }
805 }
806 Ok(())
807 })
808 .await
809 .map_err(|e| -> Box<dyn std::error::Error> { e.to_string().into() })?
810 .map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
811
812 Ok(())
813 }
814
815 async fn extract_bzip2(
824 _content: &[u8],
825 output_path: &Path,
826 ) -> Result<(), Box<dyn std::error::Error>> {
827 Err(format!(
828 "bzip2 extraction is not supported: add the `bzip2` crate to Cargo.toml \
829 to enable decompression to {}",
830 output_path.display()
831 )
832 .into())
833 }
834
835 async fn extract_xz(
844 _content: &[u8],
845 output_path: &Path,
846 ) -> Result<(), Box<dyn std::error::Error>> {
847 Err(format!(
848 "XZ extraction is not supported: add the `xz2` crate to Cargo.toml \
849 to enable decompression to {}",
850 output_path.display()
851 )
852 .into())
853 }
854
855 fn validate_feed_format(
857 file_path: &Path,
858 format: &FeedFormat,
859 ) -> Result<(), Box<dyn std::error::Error>> {
860 let content = std::fs::read_to_string(file_path)?;
861
862 match format {
863 FeedFormat::PlainText => {
864 let lines: Vec<&str> = content
866 .lines()
867 .filter(|l| !l.trim().is_empty() && !l.starts_with('#'))
868 .collect();
869 if lines.is_empty() {
870 return Err("Feed appears to be empty".into());
871 }
872 }
873 FeedFormat::Csv => {
874 let mut reader = csv::Reader::from_reader(content.as_bytes());
875 if reader.headers().is_err() {
876 return Err("Invalid CSV format".into());
877 }
878 }
879 FeedFormat::Json => {
880 serde_json::from_str::<serde_json::Value>(&content)?;
881 }
882 FeedFormat::Xml => {
883 if !content.trim_start().starts_with('<') {
885 return Err("Invalid XML format".into());
886 }
887 }
888 }
889
890 debug!("Feed format validation passed: {}", file_path.display());
891 Ok(())
892 }
893
894 pub async fn get_feed_status(&self) -> HashMap<String, FeedStatus> {
896 let mut status = HashMap::new();
897
898 for (feed_name, feed_config) in &self.config.feeds {
899 let file_path = self.config.feeds_directory.join(&feed_config.filename);
900
901 let feed_status = if feed_config.enabled {
902 if file_path.exists() {
903 if let Ok(metadata) = fs::metadata(&file_path).await {
904 FeedStatus::Active {
905 last_updated: metadata
906 .modified()
907 .unwrap_or(std::time::SystemTime::UNIX_EPOCH),
908 size_bytes: metadata.len(),
909 }
910 } else {
911 FeedStatus::Error("Cannot read file metadata".to_string())
912 }
913 } else {
914 FeedStatus::NotDownloaded
915 }
916 } else {
917 FeedStatus::Disabled
918 };
919
920 status.insert(feed_name.clone(), feed_status);
921 }
922
923 status
924 }
925
926 pub async fn update_feed(&self, feed_name: &str) -> Result<(), Box<dyn std::error::Error>> {
928 if let Some(feed_config) = self.config.feeds.get(feed_name) {
929 if feed_config.enabled {
930 Self::download_feed(&self.client, &self.config, feed_name, feed_config).await
931 } else {
932 Err(format!("Feed '{}' is disabled", feed_name).into())
933 }
934 } else {
935 Err(format!("Feed '{}' not found", feed_name).into())
936 }
937 }
938
939 pub fn is_malicious_ip(&self, ip: &std::net::IpAddr) -> bool {
941 for (feed_name, feed_config) in &self.config.feeds {
942 if !feed_config.enabled {
943 continue;
944 }
945
946 if matches!(feed_config.feed_type, FeedType::MaliciousIPs) {
947 let file_path = self.config.feeds_directory.join(&feed_config.filename);
948 if self.check_ip_in_feed(&file_path, ip) {
949 tracing::warn!("Malicious IP detected: {} (source: {})", ip, feed_name);
950 return true;
951 }
952 }
953 }
954 false
955 }
956
957 pub fn is_tor_exit(&self, ip: &std::net::IpAddr) -> bool {
959 for (feed_name, feed_config) in &self.config.feeds {
960 if !feed_config.enabled {
961 continue;
962 }
963
964 if matches!(feed_config.feed_type, FeedType::TorExitNodes) {
965 let file_path = self.config.feeds_directory.join(&feed_config.filename);
966 if self.check_ip_in_feed(&file_path, ip) {
967 tracing::warn!("Tor exit node detected: {} (source: {})", ip, feed_name);
968 return true;
969 }
970 }
971 }
972 false
973 }
974
975 pub fn is_proxy_vpn(&self, ip: &std::net::IpAddr) -> bool {
977 for (feed_name, feed_config) in &self.config.feeds {
978 if !feed_config.enabled {
979 continue;
980 }
981
982 if matches!(feed_config.feed_type, FeedType::VpnProxy) {
983 let file_path = self.config.feeds_directory.join(&feed_config.filename);
984 if self.check_ip_in_feed(&file_path, ip) {
985 tracing::info!("VPN/Proxy detected: {} (source: {})", ip, feed_name);
986 return true;
987 }
988 }
989 }
990 false
991 }
992
993 fn check_ip_in_feed(&self, file_path: &std::path::Path, ip: &std::net::IpAddr) -> bool {
995 if !file_path.exists() {
996 return false;
997 }
998
999 if let Ok(contents) = std::fs::read_to_string(file_path) {
1000 for line in contents.lines() {
1001 let line = line.trim();
1002 if line.is_empty() || line.starts_with('#') {
1003 continue;
1004 }
1005
1006 if line == ip.to_string() {
1008 return true;
1009 }
1010
1011 if line.contains('/') {
1013 match ip {
1014 std::net::IpAddr::V4(ipv4) => {
1015 if let Ok(network) = line.parse::<ipnetwork::Ipv4Network>()
1016 && network.contains(*ipv4)
1017 {
1018 return true;
1019 }
1020 }
1021 std::net::IpAddr::V6(ipv6) => {
1022 if let Ok(network) = line.parse::<ipnetwork::Ipv6Network>()
1023 && network.contains(*ipv6)
1024 {
1025 return true;
1026 }
1027 }
1028 }
1029 }
1030 }
1031 }
1032
1033 false
1034 }
1035}
1036
1037#[derive(Debug, Clone)]
1039pub enum FeedStatus {
1040 Disabled,
1042
1043 NotDownloaded,
1045
1046 Active {
1048 last_updated: std::time::SystemTime,
1049 size_bytes: u64,
1050 },
1051
1052 Error(String),
1054}