-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from gongzhengyang/feature-udp
Feature udp
- Loading branch information
Showing
12 changed files
with
260 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::net::{IpAddr, SocketAddr}; | ||
use std::time::Duration; | ||
|
||
use async_trait::async_trait; | ||
|
||
use crate::err::APPError; | ||
use crate::opts::ScanOpts; | ||
use crate::sockets_iter::SocketIterator; | ||
|
||
#[async_trait] | ||
pub trait SocketScanner { | ||
async fn socket_success(socket: SocketAddr, timeout: u64); | ||
|
||
async fn socket_connect(socket: SocketAddr, timeout: u64) -> anyhow::Result<()>; | ||
|
||
async fn pre_scan(_scan_opts: &ScanOpts) -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
async fn pre_send_socket(_socket: &SocketAddr) -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
async fn scan(scan_opts: ScanOpts) -> anyhow::Result<()> { | ||
Self::pre_scan(&scan_opts).await?; | ||
let ips = scan_opts | ||
.hosts | ||
.iter() | ||
.map(|x| IpAddr::V4(*x)) | ||
.collect::<Vec<IpAddr>>(); | ||
let ports = scan_opts.ports.ok_or(APPError::PortIsEmpty)?; | ||
let socket_iter = SocketIterator::new(&ips, &ports); | ||
for socket_addr in socket_iter { | ||
let per_timeout = scan_opts.per_timeout; | ||
Self::pre_send_socket(&socket_addr) | ||
.await | ||
.unwrap_or_else(|e| tracing::error!("pre send socket error with {e:?}")); | ||
tokio::spawn(async move { Self::socket_success(socket_addr, per_timeout).await }); | ||
} | ||
tokio::time::sleep(Duration::from_secs(scan_opts.timeout)).await; | ||
Self::after_scan().await?; | ||
Ok(()) | ||
} | ||
|
||
async fn after_scan() -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod common; | ||
pub mod icmp; | ||
pub mod tcp; | ||
pub mod udp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,24 @@ | ||
use std::net::{IpAddr, SocketAddr}; | ||
use std::net::SocketAddr; | ||
use std::time::Duration; | ||
|
||
use async_trait::async_trait; | ||
use tokio::net::TcpStream; | ||
|
||
use crate::err::APPError; | ||
use crate::opts::ScanOpts; | ||
use crate::sockets_iter::SocketIterator; | ||
use super::common::SocketScanner; | ||
|
||
pub async fn tcp_success(socket: SocketAddr, timeout: u64) { | ||
tracing::debug!("trying connect socket {socket} with timeout {timeout}"); | ||
if tcp_connect(socket, timeout).await.is_ok() { | ||
println!("rscan|tcp|{socket}|"); | ||
} | ||
} | ||
pub struct TcpSocketScanner; | ||
|
||
pub async fn tcp_connect(socket: SocketAddr, timeout: u64) -> anyhow::Result<TcpStream> { | ||
Ok(tokio::time::timeout(Duration::from_secs(timeout), TcpStream::connect(socket)).await??) | ||
} | ||
#[async_trait] | ||
impl SocketScanner for TcpSocketScanner { | ||
async fn socket_success(socket: SocketAddr, timeout: u64) { | ||
tracing::debug!("trying connect socket {socket} with timeout {timeout}"); | ||
if Self::socket_connect(socket, timeout).await.is_ok() { | ||
println!("rscan|tcp|{socket}|"); | ||
} | ||
} | ||
|
||
pub async fn scan(scan_opts: ScanOpts) -> anyhow::Result<()> { | ||
let ips = scan_opts | ||
.hosts | ||
.iter() | ||
.map(|x| IpAddr::V4(*x)) | ||
.collect::<Vec<IpAddr>>(); | ||
let ports = scan_opts.ports.ok_or(APPError::PortIsEmpty)?; | ||
let socket_iter = SocketIterator::new(&ips, &ports); | ||
for socket_addr in socket_iter { | ||
let per_timeout = scan_opts.per_timeout; | ||
tokio::spawn(async move { tcp_success(socket_addr, per_timeout).await }); | ||
async fn socket_connect(socket: SocketAddr, timeout: u64) -> anyhow::Result<()> { | ||
tokio::time::timeout(Duration::from_secs(timeout), TcpStream::connect(socket)).await??; | ||
Ok(()) | ||
} | ||
tokio::time::sleep(Duration::from_secs(scan_opts.timeout)).await; | ||
Ok(()) | ||
} |
Oops, something went wrong.