From 6aeaa04fcee4d3d0b29acad4d1c9e288102704c0 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sat, 23 Mar 2024 19:27:34 +1100 Subject: [PATCH] Added some error handling to my regex lookups Adde notes for other TLD support --- notes.md | 25 +++++++++++++++++++++++++ src/whois.rs | 26 ++++++++++++++++++-------- 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 notes.md diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..6bbd2ab --- /dev/null +++ b/notes.md @@ -0,0 +1,25 @@ +### UK specific regex + +Nameservers +```rs +r"(?i)(Name servers:\n)(.*\n)+" +``` + +Tag + +`This is a bad way of doing this, will use the below method instead` +```rs +r"(?i)(\[TAG = )(\w+-\w+-\w+)" +``` + +Status +```rs +r"(?i)(Registration status:\n)(.*)" +``` + +Registrar + +`This will also return the tag which we can separate that out later` +```rs +r"(?i)(Registrar:\n)(.*)" +``` \ No newline at end of file diff --git a/src/whois.rs b/src/whois.rs index 0fe800d..6fc7d4d 100644 --- a/src/whois.rs +++ b/src/whois.rs @@ -1,7 +1,5 @@ use core::fmt; -use hickory_resolver::{proto::rr::rdata::name, Name}; use regex::Regex; -use std::fmt::Write as _; use whois_rust::{WhoIs, WhoIsLookupOptions}; #[derive(Debug)] @@ -91,11 +89,14 @@ impl WhoisData { let result: String = whois .lookup(WhoIsLookupOptions::from_string(domain).unwrap()) .unwrap(); - let registrar_regex = RegexQuery::new(String::from(r"(?i)(.*registrar:)(.*)")); - let domain_status_regex = RegexQuery::new(String::from(r"(?i)(.*domain status:)(.*)")); + let registrar_regex = + RegexQuery::new(String::from(r"(?i)(.*registrar:|registrar *name:)(.*)")); + let domain_status_regex = + RegexQuery::new(String::from(r"(?i)(.*domain status:|.*status:)(.*)")); // TODO: Capture the registrant info for each type - let registrant_name_regex = RegexQuery::new(String::from(r"(?i)(registrant name:)(.*)")); - let registrant_org_regex = RegexQuery::new(String::from(r"(?i)(registrant org.*:)(.*)")); + let registrant_name_regex = RegexQuery::new(String::from(r"(?i)(registrant.*name:)(.*)")); + let registrant_org_regex = + RegexQuery::new(String::from(r"(?i)(registrant org.*:|registrant:)(.*)")); let registrant_email_regex = RegexQuery::new(String::from(r"(?i)(registrant email:)(.*)")); let nameserver_regex = @@ -112,7 +113,16 @@ impl WhoisData { registrar = String::from("None"); } } - let domain_status = domain_status_regex.get_matches(&result); + let domain_status_caps = domain_status_regex.get_matches(&result); + let domain_status: String; + match domain_status_caps.get(0) { + Some(status) => { + domain_status = status.to_string(); + } + None => { + domain_status = String::from("None"); + } + } let reg_name_caps = registrant_name_regex.get_matches(&result); let reg_name: String; match reg_name_caps.get(0) { @@ -155,7 +165,7 @@ impl WhoisData { // println!("{:?}", registrar[0]); WhoisData { registrar: registrar.clone(), - domain_status: domain_status[0].clone(), + domain_status: domain_status.clone(), registrant: vec![Registrant::new( reg_name.clone(), reg_org.clone(),