51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
use itertools::Itertools;
|
|
use std::{net::Ipv4Addr, sync::Arc};
|
|
|
|
use warp::Filter;
|
|
|
|
const IP2ASN_DATA: &str = include_str!("../data/ip2asn-v4.tsv");
|
|
|
|
#[derive(Debug)]
|
|
struct RangeRecord {
|
|
ip_start: Ipv4Addr,
|
|
ip_end: Ipv4Addr,
|
|
asn: u64,
|
|
country_code: &'static str,
|
|
asn_description: &'static str,
|
|
}
|
|
|
|
fn parse_ranges() -> Vec<RangeRecord> {
|
|
let mut ranges = Vec::with_capacity(IP2ASN_DATA.lines().count());
|
|
|
|
IP2ASN_DATA
|
|
.lines()
|
|
.filter(|s| !s.is_empty())
|
|
.map(|l| {
|
|
l.splitn(5, '\t')
|
|
.next_tuple::<(&str, &str, &str, &str, &str)>()
|
|
.unwrap()
|
|
})
|
|
.for_each(|(ip_start, ip_end, asn, country_code, asn_description)| {
|
|
ranges.push(RangeRecord {
|
|
ip_start: ip_start.parse().unwrap(),
|
|
ip_end: ip_end.parse().unwrap(),
|
|
asn: asn.parse().unwrap(),
|
|
country_code,
|
|
asn_description,
|
|
})
|
|
});
|
|
|
|
ranges
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
eprintln!("[i] Parsing IP ASN data...");
|
|
let _ranges = Arc::new(parse_ranges());
|
|
eprintln!("[i] Done!");
|
|
|
|
let hello = warp::any().map(|| "Hello!".to_string());
|
|
println!("\nListening on http://127.0.0.1:8000 ...");
|
|
warp::serve(hello).bind(([127, 0, 0, 1], 8000)).await;
|
|
}
|