-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// Module that pretends to tail a web server log. | ||
use rand::{thread_rng, Rng}; | ||
|
||
use parse_args::AppConfig; | ||
use chrono::prelude::*; | ||
use EXTENSIONS_LIST; | ||
use PACKAGES_LIST; | ||
use 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_weighted_bool(2) { | ||
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 = rng.choose(HTTP_CODES).unwrap_or(&200); | ||
let size = fake!(Number.between(99, 5000000)); | ||
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 == false { | ||
burst_mode = rng.gen_weighted_bool(20); | ||
} | ||
|
||
dprint(format!("{}", line), 0); | ||
|
||
println!(); | ||
if burst_mode { | ||
count_burst_lines += 1; | ||
} | ||
|
||
csleep(line_sleep_length); | ||
|
||
if appconfig.should_exit() { | ||
return; | ||
} | ||
} | ||
} |