-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
706 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod crawler; | ||
pub mod robots; |
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,35 @@ | ||
use std::collections::HashSet; | ||
use std::time; | ||
|
||
use reqwest::header::{HeaderMap, USER_AGENT}; | ||
|
||
use crate::common; | ||
|
||
pub fn parse_robots(site: String) -> Result<HashSet<String>, Box<dyn std::error::Error>> { | ||
let site = site + "/robots.txt"; | ||
|
||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
USER_AGENT, | ||
common::user_agent::random_user_agent().parse().unwrap(), | ||
); | ||
|
||
let rsp = reqwest::blocking::Client::new() | ||
.get(site) | ||
.timeout(time::Duration::from_secs(5)) | ||
.headers(headers) | ||
.send(); | ||
assert_eq!(rsp.as_ref().unwrap().status(), 200); | ||
let txt = rsp?.text()?; | ||
let allow_values: HashSet<String> = txt | ||
.lines() | ||
.flat_map(|line| { | ||
let parts: Vec<&str> = line.split_whitespace().collect(); | ||
match parts.len() { | ||
2 if parts[0] == "Allow:" || parts[0] == "Disallow:" => Some(parts[1].to_string()), | ||
_ => None, | ||
} | ||
}) | ||
.collect(); | ||
Ok(allow_values) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,23 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use demon::handler; | ||
use handler::robots::parse_robots; | ||
|
||
fn load() { | ||
let buf = std::env::current_dir() | ||
.unwrap() | ||
.join("files/user_agent.toml"); | ||
std::env::set_var("user_agent", buf); | ||
} | ||
|
||
#[test] | ||
fn cli_test() {} | ||
fn parse_robots_test() { | ||
load(); | ||
assert_ne!( | ||
parse_robots("https://www.dvwa.co.uk".to_string()) | ||
.unwrap() | ||
.len(), | ||
0 | ||
) | ||
} | ||
} |