rust_web_utils/uautil/
bot.rs1use http::HeaderMap;
2
3use super::patterns::{self, Kind, PatternRemover};
4
5pub fn is_bot(headers: &HeaderMap, allow_legitimate: bool) -> bool {
7 is_bot_user_agent(&patterns::user_agent(headers), allow_legitimate)
8}
9
10pub fn is_bot_user_agent(user_agent: &str, allow_legitimate: bool) -> bool {
13 if user_agent.is_empty() {
14 return true;
15 }
16 let ua = user_agent.to_lowercase();
17 let p = patterns::read();
18
19 if allow_legitimate && p.legitimate.iter().any(|pat| ua.contains(pat.as_str())) {
20 return false;
21 }
22 p.bot.iter().any(|pat| ua.contains(pat.as_str()))
23}
24
25pub fn add_custom_bot_pattern(pattern: &str) -> PatternRemover {
27 patterns::add(Kind::Bot, pattern)
28}
29
30pub fn add_legitimate_bot(pattern: &str) -> PatternRemover {
32 patterns::add(Kind::Legitimate, pattern)
33}
34
35pub fn bot_patterns() -> Vec<String> {
37 patterns::snapshot(Kind::Bot)
38}
39
40pub fn legitimate_patterns() -> Vec<String> {
42 patterns::snapshot(Kind::Legitimate)
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn detects_bots() {
51 let cases = [
52 ("", false, true),
53 ("python-requests/2.31.0", false, true),
54 ("curl/8.4.0", false, true),
55 ("Wget/1.21", false, true),
56 ("Go-http-client/1.1", false, true),
57 ("sqlmap/1.7", false, true),
58 ("Mozilla/5.0 (X11; Linux x86_64) HeadlessChrome/120.0", false, true),
59 (
60 "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
61 false,
62 true,
63 ),
64 (
65 "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
66 true,
67 false,
68 ),
69 ("Mozilla/5.0 (compatible; Baiduspider/2.0)", true, false),
70 ("TelegramBot (like TwitterBot)", true, false),
71 ("SomeRandomCrawler/1.0", true, true),
72 (
73 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/130.0 Safari/537.36",
74 false,
75 false,
76 ),
77 ];
78 for (ua, allow, want) in cases {
79 assert_eq!(is_bot_user_agent(ua, allow), want, "{ua:?} allow={allow}");
80 }
81 }
82
83 #[test]
84 fn reads_header() {
85 let mut h = HeaderMap::new();
86 assert!(is_bot(&h, true));
87 h.insert("user-agent", "curl/8".parse().unwrap());
88 assert!(is_bot(&h, true));
89 }
90
91 #[test]
92 fn custom_patterns() {
93 let ua = "MyInternalMonitorXYZ/1.0";
94 assert!(!is_bot_user_agent(ua, false));
95 let remover = add_custom_bot_pattern("InternalMonitorXYZ");
96 assert!(bot_patterns().contains(&"internalmonitorxyz".to_owned()));
97 assert!(is_bot_user_agent(ua, false));
98 remover.remove();
99 assert!(!is_bot_user_agent(ua, false));
100
101 let legit_ua = "PartnerFetchBotQRS/2.0";
102 assert!(is_bot_user_agent(legit_ua, true));
103 let remover = add_legitimate_bot("partnerfetchbotqrs");
104 assert!(legitimate_patterns().contains(&"partnerfetchbotqrs".to_owned()));
105 assert!(!is_bot_user_agent(legit_ua, true));
106 assert!(is_bot_user_agent(legit_ua, false));
107 remover.remove();
108 assert!(is_bot_user_agent(legit_ua, true));
109 }
110
111 #[test]
112 fn concurrent_add_and_detect() {
113 let handles: Vec<_> = (0..8)
114 .map(|i| {
115 std::thread::spawn(move || {
116 for j in 0..200 {
117 let r = add_custom_bot_pattern(&format!("concurrency-{i}-{j}"));
118 let _ = is_bot_user_agent("Mozilla/5.0 Chrome/130.0", true);
119 r.remove();
120 }
121 })
122 })
123 .collect();
124 for h in handles {
125 h.join().unwrap();
126 }
127 assert!(!bot_patterns().iter().any(|p| p.starts_with("concurrency-")));
128 }
129}