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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use chrono::Datelike;
pub use ipaddress::*;
use super::dependencies::*;
use super::{Datetime, File, Text};
#[derive(Debug)]
pub enum StockType {
URL(String),
Image(Vec<u8>),
}
pub struct Internet;
impl Internet {
pub fn content_type(mime_type: Option<MimeType>) -> String {
format!("Content-Type: {}", File::mime_type(mime_type))
}
pub fn http_status_message() -> &'static str {
get_random_element(HTTP_STATUS_MSGS.iter())
}
pub fn http_status_code() -> &'static str {
get_random_element(HTTP_STATUS_CODES.iter())
}
pub fn http_method() -> &'static str {
get_random_element(HTTP_METHODS.iter())
}
pub fn port(port_range: Option<PortRange>) -> u16 {
let range = validate_enum(port_range, Some(PortRange::ALL));
randint(range.0, range.1)
}
pub fn ip_v4_object() -> IPAddress {
ipv4::from_u32(randint(0, 4294967295), 32).expect("Cant create v4 IPAddress!")
}
pub fn ip_v4() -> String {
Self::ip_v4_object().to_s()
}
pub fn ip_v4_with_port(port_range: Option<PortRange>) -> String {
format!("{}:{}", Self::ip_v4(), Self::port(port_range))
}
pub fn ip_v6_object() -> IPAddress {
ipv6::from_int(randbigint(u128::MIN, u128::MAX), 32).expect("Cant create v6 IPAddress!")
}
pub fn ip_v6() -> String {
Self::ip_v6_object().to_s()
}
pub fn mac() -> String {
vec![
0x00,
0x16,
0x3E,
randint(0x00, 0x7F),
randint(0x00, 0xFF),
randint(0x00, 0xFF),
].iter().map(|i| format!("{:02x}", i))
.join(":")
}
pub fn emoji() -> &'static str {
get_random_element(EMOJI.iter())
}
pub fn stock_image(width: u32, height: u32, keywords: Vec<&str>, to_image: bool) -> StockType {
let keywords_str = keywords.join(",");
let url = format!("https://source.unsplash.com/{width}x{height}?{keywords_str}");
match to_image {
true => {
let r = reqwest::blocking::get(url).expect("Cant fetch images from unsplash!")
.bytes().expect("Cant get output!");
StockType::Image(r.to_vec())
},
false => StockType::URL(url),
}
}
pub fn hashtags(quantity: i32) -> Vec<String> {
let locale = Text(Locale::EN);
(0..quantity).map(|_| format!("#{}", locale.word())).collect()
}
pub fn top_level_domain(tld_type: Option<TLDType>) -> &'static str {
let tld = validate_enum(tld_type, None);
get_random_element(TLD.get(tld).expect("Cant get TLD type!").iter())
}
pub fn tld(tld_type: Option<TLDType>) -> &'static str {
Self::top_level_domain(tld_type)
}
pub fn user_agent() -> &'static str {
get_random_element(USER_AGENTS.iter())
}
pub fn public_dns() -> &'static str {
get_random_element(PUBLIC_DNS.iter())
}
pub fn slug(parts_count: Option<usize>) -> String {
let parts = parts_count.unwrap_or_else(|| randint(2, 12));
if parts > 12 {
panic!("Slug's parts count must be <= 12");
}
if parts < 2 {
panic!("Slug must contain more than 2 parts");
}
Text(Locale::EN).words(parts).iter().join("-")
}
pub fn hostname(tld_type: Option<TLDType>, subdomains: Option<Vec<&str>>) -> String {
let tld = Self::top_level_domain(tld_type);
let host = get_random_element(USERNAMES.iter());
let mut url = format!("{host}{tld}");
if let Some(v) = subdomains {
url = format!("{}.{url}", get_random_element(v.iter()));
}
url
}
pub fn url(scheme: Option<URLScheme>, port_range: Option<PortRange>, tld_type: Option<TLDType>, subdomains: Option<Vec<&str>>) -> String {
let hostname = Self::hostname(tld_type, subdomains);
let scheme = validate_enum(scheme, None);
let mut url = format!("{scheme}://{hostname}");
if port_range.is_some() {
url = format!("{url}:{}", Self::port(port_range));
}
format!("{url}/")
}
pub fn uri(scheme: Option<URLScheme>, port_range: Option<PortRange>, tld_type: Option<TLDType>, subdomains: Option<Vec<&str>>, query_params_count: Option<usize>) -> String {
let directory = Datetime::date(2010, chrono::Local::now().year()).format("%Y/%m/%d");
let mut url = format!("{}{directory}/{}", Self::url(scheme, port_range, tld_type, subdomains), Self::slug(None));
if query_params_count.is_some() {
url = format!("{url}?{}", Self::query_string(query_params_count));
}
url
}
pub fn query_string(length: Option<usize>) -> String {
let formated = Self::query_parameters(length).iter().map(|p| {
format!("{}={}", p.0, p.1)
}).join("&");
urlencoding::encode(&formated).into_owned()
}
pub fn query_parameters(length: Option<usize>) -> Vec<(&'static str, &'static String)> {
let text = &Text(Locale::RU);
let length = length.unwrap_or_else(|| randint(1, 10));
if length > 32 {
panic!("Length should be less than 32!")
}
let mut unique_words: Vec<&'static str> = vec![];
while unique_words.len() < length {
let word = text.word();
if !unique_words.contains(&word) {
unique_words.push(word)
}
}
unique_words.into_iter().zip(text.words(length)).collect()
}
}