2023-04-27 17:53:28 +03:00
|
|
|
//! This module provides the functionality to generate random user agent string.
|
|
|
|
|
2023-08-27 20:57:33 +03:00
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
2023-05-22 13:07:37 +03:00
|
|
|
use fake_useragent::{Browsers, UserAgents, UserAgentsBuilder};
|
2023-04-25 16:30:04 +03:00
|
|
|
|
2023-09-03 19:23:34 +03:00
|
|
|
/// A static variable which stores the initially build `UserAgents` struct. So as it can be resused
|
|
|
|
/// again and again without the need of reinitializing the `UserAgents` struct.
|
2023-08-27 20:57:33 +03:00
|
|
|
static USER_AGENTS: OnceLock<UserAgents> = OnceLock::new();
|
2023-05-22 13:07:37 +03:00
|
|
|
|
|
|
|
/// A function to generate random user agent to improve privacy of the user.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// A randomly generated user agent string.
|
2023-08-27 20:57:33 +03:00
|
|
|
pub fn random_user_agent() -> &'static str {
|
|
|
|
USER_AGENTS
|
|
|
|
.get_or_init(|| {
|
|
|
|
UserAgentsBuilder::new()
|
|
|
|
.cache(false)
|
|
|
|
.dir("/tmp")
|
|
|
|
.thread(1)
|
|
|
|
.set_browsers(
|
|
|
|
Browsers::new()
|
|
|
|
.set_chrome()
|
|
|
|
.set_safari()
|
|
|
|
.set_edge()
|
|
|
|
.set_firefox()
|
|
|
|
.set_mozilla(),
|
|
|
|
)
|
|
|
|
.build()
|
|
|
|
})
|
|
|
|
.random()
|
2023-04-25 16:30:04 +03:00
|
|
|
}
|