Skip to content

Commit ed55cc7

Browse files
committed
feat(agents): support dispatch on FormFactor in spoof_by
Add functions to collate static user agents into groups by form factor, and then dispatch on them in spoof_by. Addresses #7: [1] [1]: #7
1 parent 7ac2b74 commit ed55cc7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

ua_generator/src/ua.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,43 @@ pub fn spoof_chrome_linux_ua_with_randomizer(thread_rng: &mut Rng) -> &'static s
107107
pick_rand(Some(thread_rng), STATIC_CHROME_LINUX_AGENTS)
108108
}
109109

110+
/// Desktop agents.
111+
pub fn desktop_agents() -> &'static [&'static str] {
112+
let v = [
113+
STATIC_CHROME_WINDOWS_AGENTS,
114+
STATIC_CHROME_MAC_AGENTS,
115+
STATIC_CHROME_LINUX_AGENTS,
116+
STATIC_FIREFOX_WINDOWS_AGENTS,
117+
STATIC_FIREFOX_MAC_AGENTS,
118+
STATIC_FIREFOX_LINUX_AGENTS,
119+
STATIC_SAFARI_MAC_AGENTS,
120+
]
121+
.concat();
122+
Box::leak(v.into_boxed_slice())
123+
}
124+
125+
/// Mobile agents.
126+
pub fn mobile_agents() -> &'static [&'static str] {
127+
let v = [
128+
STATIC_CHROME_MOBILE_AGENTS,
129+
STATIC_FIREFOX_MOBILE_AGENTS,
130+
STATIC_SAFARI_MOBILE_AGENTS,
131+
]
132+
.concat();
133+
Box::leak(v.into_boxed_slice())
134+
}
135+
136+
/// Tablet agents.
137+
pub fn tablet_agents() -> &'static [&'static str] {
138+
let v = [
139+
STATIC_CHROME_TABLET_AGENTS,
140+
STATIC_FIREFOX_TABLET_AGENTS,
141+
STATIC_SAFARI_TABLET_AGENTS,
142+
]
143+
.concat();
144+
Box::leak(v.into_boxed_slice())
145+
}
146+
110147
/// Slices for each generated family (zero-copy, no alloc).
111148
#[inline]
112149
pub fn chrome_agents() -> &'static [&'static str] {
@@ -367,6 +404,15 @@ pub fn spoof_by(
367404
// Any Safari fallback → global Safari list
368405
(_, _, Some(Browser::Safari)) => pick_rand(rng, STATIC_SAFARI_AGENTS),
369406

407+
// --- FormFactor match only ---
408+
// Desktop
409+
(_, Some(FormFactor::Desktop), _) => pick_rand(rng, desktop_agents()),
410+
// Mobile
411+
(_, Some(FormFactor::Mobile), _) => pick_rand(rng, mobile_agents()),
412+
// Tablet
413+
(_, Some(FormFactor::Tablet), _) => pick_rand(rng, tablet_agents()),
414+
415+
// --- Fall Back ---
370416
// IE: until IE lists are generated, fall back to mixed static
371417
_ => pick_rand(rng, STATIC_AGENTS),
372418
}
@@ -633,5 +679,10 @@ mod tests {
633679
Some(Browser::Ie),
634680
None,
635681
);
682+
683+
// FormFactor-only
684+
let _ = spoof_by(None, Some(FormFactor::Desktop), None, None);
685+
let _ = spoof_by(None, Some(FormFactor::Mobile), None, None);
686+
let _ = spoof_by(None, Some(FormFactor::Tablet), None, None);
636687
}
637688
}

0 commit comments

Comments
 (0)