init
This commit is contained in:
commit
da40e48b19
20 changed files with 4150 additions and 0 deletions
59
src/controllers/home.rs
Normal file
59
src/controllers/home.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use askama::Template;
|
||||
use time::Date;
|
||||
|
||||
use crate::sources;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
pub struct HomeTemplate {
|
||||
time_since: TimeSince,
|
||||
}
|
||||
|
||||
pub struct TimeSince {
|
||||
days: i32,
|
||||
}
|
||||
|
||||
impl TimeSince {
|
||||
fn from_interval(leak: &Date, now: &Date) -> Self {
|
||||
Self {
|
||||
days: now.to_julian_day() - leak.to_julian_day(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn get() -> HomeTemplate {
|
||||
let mut t = vec![];
|
||||
|
||||
for source in sources::sources() {
|
||||
let url = source.url();
|
||||
let Ok(res) = (reqwest::get(url)).await else {
|
||||
tracing::error!("fetch error");
|
||||
continue;
|
||||
};
|
||||
|
||||
let Ok(text) = res.text().await else {
|
||||
tracing::error!("fetch decode text error");
|
||||
continue;
|
||||
};
|
||||
|
||||
let Ok(last) = source.latest_leak(text) else {
|
||||
tracing::error!("source decode error");
|
||||
continue;
|
||||
};
|
||||
|
||||
t.push(last);
|
||||
}
|
||||
|
||||
let last = t
|
||||
.into_iter()
|
||||
.max()
|
||||
.unwrap_or(time::Date::from_calendar_date(2021, time::Month::July, 14).unwrap());
|
||||
|
||||
let now = time::OffsetDateTime::now_utc();
|
||||
let now = time::Date::from_calendar_date(now.year(), now.month(), now.day()).unwrap();
|
||||
|
||||
HomeTemplate {
|
||||
time_since: TimeSince::from_interval(&last, &now),
|
||||
}
|
||||
}
|
1
src/controllers/mod.rs
Normal file
1
src/controllers/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod home;
|
51
src/lib.rs
Normal file
51
src/lib.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use std::{future::Future, pin::Pin};
|
||||
|
||||
use anyhow::Result;
|
||||
use axum::{routing::get, Router};
|
||||
use tokio::net::TcpListener;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
mod controllers;
|
||||
mod sources;
|
||||
|
||||
fn routes() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(controllers::home::get))
|
||||
.fallback_service(ServeDir::new("./static"))
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
pub struct RunningServer {
|
||||
pub port: u16,
|
||||
pub server: Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>>,
|
||||
}
|
||||
|
||||
pub async fn run(config: Config) -> Result<RunningServer> {
|
||||
setup_tracing();
|
||||
|
||||
let router = routes().layer(tower_http::trace::TraceLayer::new_for_http());
|
||||
|
||||
let tcp_listener = TcpListener::bind(format!("0.0.0.0:{}", config.port)).await?;
|
||||
|
||||
let port = tcp_listener.local_addr()?.port();
|
||||
|
||||
tracing::info!("Listening on http://localhost:{port}");
|
||||
let server = Box::pin(async move {
|
||||
axum::serve(tcp_listener, router).await?;
|
||||
Ok(())
|
||||
});
|
||||
|
||||
Ok(RunningServer { port, server })
|
||||
}
|
||||
|
||||
pub fn setup_tracing() {
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.try_init()
|
||||
.ok();
|
||||
}
|
16
src/main.rs
Normal file
16
src/main.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
use anyhow::Result;
|
||||
use warthunder_confidential_document_leak_counter::{run, Config};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let config = Config {
|
||||
port: std::env::var("WARTHUNDER_LEAK_SERVE_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(8000u16),
|
||||
};
|
||||
|
||||
run(config).await?.server.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
14
src/sources/mod.rs
Normal file
14
src/sources/mod.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use anyhow::Result;
|
||||
|
||||
mod wikipedia;
|
||||
|
||||
pub trait Source {
|
||||
/// Return the URL to query
|
||||
fn url(&self) -> String;
|
||||
/// Given the content of the url figure out the date of the latest leak
|
||||
fn latest_leak(&self, html: String) -> Result<time::Date>;
|
||||
}
|
||||
|
||||
pub fn sources() -> Vec<Box<dyn Source + Send>> {
|
||||
vec![Box::new(wikipedia::Wikipedia)]
|
||||
}
|
115
src/sources/wikipedia/mod.rs
Normal file
115
src/sources/wikipedia/mod.rs
Normal file
|
@ -0,0 +1,115 @@
|
|||
use std::{str::FromStr, time::Instant};
|
||||
|
||||
use super::Source;
|
||||
use anyhow::{bail, Context, Result};
|
||||
use regex::Regex;
|
||||
use soup::{NodeExt, QueryBuilderExt};
|
||||
|
||||
pub struct Wikipedia;
|
||||
|
||||
impl Source for Wikipedia {
|
||||
fn url(&self) -> String {
|
||||
"https://en.wikipedia.org/wiki/War_Thunder".to_string()
|
||||
}
|
||||
|
||||
fn latest_leak(&self, html: String) -> Result<time::Date> {
|
||||
let soup = soup::Soup::new(&html);
|
||||
|
||||
let tables = soup.tag("table").find_all();
|
||||
|
||||
let tables_with_classified = tables
|
||||
.into_iter()
|
||||
.filter(|t| t.text().contains("Classified"))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let table = match &tables_with_classified[..] {
|
||||
[table] => table,
|
||||
_ => bail!("Cannot reliably find leaks table"),
|
||||
};
|
||||
|
||||
let lines: Vec<String> = table
|
||||
.tag("tbody")
|
||||
.find()
|
||||
.context("Could not find table body")?
|
||||
.tag("tr")
|
||||
.find_all()
|
||||
.flat_map(|line| line.tag("td").find())
|
||||
.map(|td| td.text())
|
||||
.collect();
|
||||
|
||||
lines
|
||||
.iter()
|
||||
.flat_map(|txt| parse_wikipedia_date(txt))
|
||||
.max()
|
||||
.context("Could not find any date?")
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_wikipedia_date(text: &str) -> Result<time::Date> {
|
||||
let full_regex = Regex::new(r"(\w+)\s+(\d+),?\s+(\d+)").unwrap();
|
||||
|
||||
if let Some(cap) = full_regex.captures(text) {
|
||||
let (_, [month, day, year]) = cap.extract();
|
||||
|
||||
let month = time::Month::from_str(month);
|
||||
|
||||
return time::Date::from_calendar_date(
|
||||
year.parse().context("Failed to parse year")?,
|
||||
month.context("Failed to parse month")?,
|
||||
day.parse().context("Failed to parse day")?,
|
||||
)
|
||||
.context("Failed to create date from provided text");
|
||||
}
|
||||
|
||||
let small_regex = Regex::new(r"(\w+) (\d+)").unwrap();
|
||||
if let Some(cap) = small_regex.captures(text) {
|
||||
let (_, [month, year]) = cap.extract();
|
||||
|
||||
let month = time::Month::from_str(month);
|
||||
|
||||
return time::Date::from_calendar_date(
|
||||
year.parse().context("Failed to parse year")?,
|
||||
month.context("Failed to parse month")?,
|
||||
1,
|
||||
)
|
||||
.context("Failed to create date from provided text");
|
||||
}
|
||||
|
||||
bail!("Failed to parse wikipedia date")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wikipedia_html_parse() {
|
||||
let html = std::fs::read_to_string("./data/wikipedia.html").unwrap();
|
||||
|
||||
let real = Wikipedia.latest_leak(html).unwrap();
|
||||
let expected = time::Date::from_calendar_date(2023, time::Month::December, 12).unwrap();
|
||||
|
||||
assert_eq!(expected, real);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wikipedia_date_parse() {
|
||||
assert!(parse_wikipedia_date("testing 123, 1234").is_err());
|
||||
assert_eq!(
|
||||
parse_wikipedia_date("July 14, 2021").unwrap(),
|
||||
time::Date::from_calendar_date(2021, time::Month::July, 14).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
parse_wikipedia_date(" July 14, 2021 ").unwrap(),
|
||||
time::Date::from_calendar_date(2021, time::Month::July, 14).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
parse_wikipedia_date("July 14 2021").unwrap(),
|
||||
time::Date::from_calendar_date(2021, time::Month::July, 14).unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
parse_wikipedia_date("October 2021").unwrap(),
|
||||
time::Date::from_calendar_date(2021, time::Month::October, 1).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
parse_wikipedia_date("october 2021").unwrap(),
|
||||
time::Date::from_calendar_date(2021, time::Month::October, 1).unwrap()
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue