forked from svenstaro/genact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweblog.rs
67 lines (59 loc) · 2.09 KB
/
weblog.rs
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
/// Module that pretends to tail a web server log.
use rand::prelude::*;
use crate::parse_args::AppConfig;
use chrono::prelude::*;
use crate::EXTENSIONS_LIST;
use crate::PACKAGES_LIST;
use crate::utils::{csleep, dprint, gen_file_path};
static HTTP_CODES: &'static [u16] = &[200, 201, 400, 401, 403, 404, 500, 502, 503];
pub fn run(appconfig: &AppConfig) {
let mut rng = thread_rng();
let num_lines = rng.gen_range(50, 200);
let mut burst_mode = false;
let mut count_burst_lines = 0;
for _ in 1..num_lines {
let ip = if rng.gen_bool(0.5) {
fake!(Internet.ipv4)
} else {
fake!(Internet.ipv6).to_lowercase()
};
let date = Local::now().format("%e/%b/%Y:%T %z");
let method = "GET";
let dir_candidates = fake!(Lorem.words(20));
let path = gen_file_path(&mut rng, &PACKAGES_LIST, &EXTENSIONS_LIST, &dir_candidates);
let http_code = HTTP_CODES.choose(&mut rng).unwrap_or(&200);
let size = fake!(Number.between(99, 5_000_000));
let referrer = "-";
let user_agent = fake!(Internet.user_agent);
let line = format!(
"{ip} - - [{date}] \"{method} {path} HTTP/1.0\" {http_code} {size} \"{referrer}\" \"{user_agent}\"",
ip=ip,
date=date,
method=method,
path=path,
http_code=http_code,
size=size,
referrer=referrer,
user_agent=user_agent
);
let mut line_sleep_length = rng.gen_range(10, 1000);
let burst_lines = rng.gen_range(10, 50);
if burst_mode && count_burst_lines < burst_lines {
line_sleep_length = 30;
} else if count_burst_lines == burst_lines {
burst_mode = false;
count_burst_lines = 0;
} else if !burst_mode {
burst_mode = rng.gen_bool(1.0 / 20.0);
}
dprint(line.to_string(), 0);
println!();
if burst_mode {
count_burst_lines += 1;
}
csleep(line_sleep_length);
if appconfig.should_exit() {
return;
}
}
}