Skip to content

Commit

Permalink
chore: minor refactors on zonetransfer module
Browse files Browse the repository at this point in the history
  • Loading branch information
eredotpkfr committed Jan 11, 2025
1 parent 323959a commit bf1e37b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 16 additions & 12 deletions src/modules/zonetransfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@ impl ZoneTransfer {
zonetransfer.into()
}

pub async fn get_async_client(&self, server: SocketAddr) -> Option<AsyncClient> {
pub async fn get_async_client(&self, server: SocketAddr) -> Result<AsyncClient> {
type WrappedStream = AsyncIoTokioAsStd<TokioTcpStream>;

let (stream, sender) = TcpClientStream::<WrappedStream>::new(server);
let result = AsyncClient::new(stream, sender, None).await;

result.ok().map(|(client, bg)| {
tokio::spawn(bg);
client
})
result
.map_err(|_| Custom("client error".into()))
.map(|(client, bg)| {
tokio::spawn(bg);
Ok(client)
})?
}

pub async fn get_ns_as_ip(&self, server: SocketAddr, domain: &str) -> Option<Vec<SocketAddr>> {
let mut ips = vec![];
let mut client = self.get_async_client(server).await?;
let mut client = self.get_async_client(server).await.ok()?;

let name = Name::from_str(domain).ok()?;
let ns_response = client.query(name, DNSClass::IN, RecordType::NS);
Expand Down Expand Up @@ -100,11 +102,9 @@ impl ZoneTransfer {
domain: &str,
) -> Result<Vec<Subdomain>> {
let pattern = regex::generate_subdomain_regex(domain)?;
let mut client = self
.get_async_client(server)
.await
.ok_or(Custom("client error".into()))?;
let mut subs = vec![];

let mut subs = Vec::new();
let mut client = self.get_async_client(server).await?;

let name = Name::from_str(domain).unwrap();
let axfr_response = client.query(name, DNSClass::IN, RecordType::AXFR);
Expand Down Expand Up @@ -154,7 +154,11 @@ impl SubscanModuleInterface for ZoneTransfer {
.ok_or(Custom("connection error".into()))?;

for ip in ips {
result.extend(self.attempt_zone_transfer(ip, domain).await?);
result.extend(
self.attempt_zone_transfer(ip, domain)
.await
.unwrap_or_default(),
);
}

return Ok(result.with_finished().await);
Expand Down
4 changes: 2 additions & 2 deletions tests/components/modules/zonetransfer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn get_async_client_test() {
let zonetransfer = ZoneTransfer::dispatcher();

if let SubscanModuleDispatcher::ZoneTransfer(zonetransfer) = zonetransfer {
assert!(zonetransfer.get_async_client(server.socket).await.is_some());
assert!(zonetransfer.get_async_client(server.socket).await.is_ok());
}
}

Expand All @@ -27,7 +27,7 @@ async fn get_async_client_fail_test() {
let addr = SocketAddr::from_str(&format!("{LOCAL_HOST}:0")).unwrap();

if let SubscanModuleDispatcher::ZoneTransfer(zonetransfer) = zonetransfer {
assert!(zonetransfer.get_async_client(addr).await.is_none());
assert!(zonetransfer.get_async_client(addr).await.is_err());
}
}

Expand Down

0 comments on commit bf1e37b

Please sign in to comment.