Added some error handling to my regex lookups

Adde notes for other TLD support
This commit is contained in:
Benjamyn Love 2024-03-23 19:27:34 +11:00
parent 670eb3937f
commit 6aeaa04fce
2 changed files with 43 additions and 8 deletions

25
notes.md Normal file
View File

@ -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)(.*)"
```

View File

@ -1,7 +1,5 @@
use core::fmt; use core::fmt;
use hickory_resolver::{proto::rr::rdata::name, Name};
use regex::Regex; use regex::Regex;
use std::fmt::Write as _;
use whois_rust::{WhoIs, WhoIsLookupOptions}; use whois_rust::{WhoIs, WhoIsLookupOptions};
#[derive(Debug)] #[derive(Debug)]
@ -91,11 +89,14 @@ impl WhoisData {
let result: String = whois let result: String = whois
.lookup(WhoIsLookupOptions::from_string(domain).unwrap()) .lookup(WhoIsLookupOptions::from_string(domain).unwrap())
.unwrap(); .unwrap();
let registrar_regex = RegexQuery::new(String::from(r"(?i)(.*registrar:)(.*)")); let registrar_regex =
let domain_status_regex = RegexQuery::new(String::from(r"(?i)(.*domain status:)(.*)")); 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 // TODO: Capture the registrant info for each type
let registrant_name_regex = RegexQuery::new(String::from(r"(?i)(registrant name:)(.*)")); 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_org_regex =
RegexQuery::new(String::from(r"(?i)(registrant org.*:|registrant:)(.*)"));
let registrant_email_regex = RegexQuery::new(String::from(r"(?i)(registrant email:)(.*)")); let registrant_email_regex = RegexQuery::new(String::from(r"(?i)(registrant email:)(.*)"));
let nameserver_regex = let nameserver_regex =
@ -112,7 +113,16 @@ impl WhoisData {
registrar = String::from("None"); 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_caps = registrant_name_regex.get_matches(&result);
let reg_name: String; let reg_name: String;
match reg_name_caps.get(0) { match reg_name_caps.get(0) {
@ -155,7 +165,7 @@ impl WhoisData {
// println!("{:?}", registrar[0]); // println!("{:?}", registrar[0]);
WhoisData { WhoisData {
registrar: registrar.clone(), registrar: registrar.clone(),
domain_status: domain_status[0].clone(), domain_status: domain_status.clone(),
registrant: vec![Registrant::new( registrant: vec![Registrant::new(
reg_name.clone(), reg_name.clone(),
reg_org.clone(), reg_org.clone(),