1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::fmt;
mod browser;
mod engin;
mod platform;
mod semver;
mod utils;
pub struct UserAgent {
version: String,
browser: Option<browser::Browser>,
platform: Option<platform::Platform>,
}
impl UserAgent {
pub fn to_string(&self) -> String {
let b = self.browser.as_ref().unwrap();
let p = &self.platform.as_ref().unwrap();
if b.name.contains("irefox") {
return format!(
"{} ({}; rv:{}) {}",
self.version,
p.to_string().replace("_", "."),
b.semver.to_string('.'),
b.to_string()
);
}
format!("{} ({}) {}", self.version, p.to_string(), b.to_string())
}
}
impl std::fmt::Display for UserAgent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_string())
}
}
pub fn new() -> UserAgent {
let mozilla_with_version = "Mozilla/5.0";
let mut ua = UserAgent {
version: String::from(mozilla_with_version),
browser: None,
platform: None,
};
if let None = ua.browser {
ua.browser = Some(browser::Browser::random())
}
if let None = ua.platform {
ua.platform = Some(platform::Platform::random(true));
}
return ua;
}
#[test]
fn test_user_agent_for_firefox() {
let ua = new().firefox().phone().to_string();
assert!(ua.contains("Firefox"));
assert!(ua.to_string().contains("iPhone") || ua.to_string().contains("Android"));
}