-
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.
- Loading branch information
Showing
6 changed files
with
178 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Get-NetNeighbor -AddressFamily IPv6 | Select-Object -Property InterfaceIndex, IPAddress, State | ConvertTo-Csv |
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,114 @@ | ||
use super::IpNeighbor; | ||
use super::IpNeighborScanError; | ||
use super::IpNeighborScanner; | ||
use csv::Reader; | ||
use futures_util::future::BoxFuture; | ||
use futures_util::FutureExt; | ||
use serde::Deserialize; | ||
use std::net::Ipv6Addr; | ||
|
||
pub struct PowerShellIpNeighborScanner; | ||
|
||
impl PowerShellIpNeighborScanner { | ||
async fn scan() -> Result<Vec<IpNeighbor>, IpNeighborScanError> { | ||
let stdout = crate::process::eval( | ||
"pwsh", | ||
&["-NonInteractive", "-Command", "-"], | ||
include_bytes!("./Print-IpNeighbors.ps1"), | ||
) | ||
.await?; | ||
Self::parse_output(&stdout).map_err(Into::into) | ||
} | ||
|
||
fn parse_output(output: &[u8]) -> Result<Vec<IpNeighbor>, csv::Error> { | ||
let neighbors = Reader::from_reader(output) | ||
.deserialize() | ||
.collect::<Result<Vec<NetNeighbor>, _>>()?; | ||
neighbors | ||
.iter() | ||
.for_each(|n| log::debug!("Scanned IP neighbor: {:?}", n)); | ||
|
||
let neighbors: Vec<_> = neighbors | ||
.into_iter() | ||
.filter(|n| n.State != "Unreachable") | ||
.filter(|n| n.IPAddress.segments().starts_with(&[0xFE80, 0, 0, 0])) | ||
.map(Into::into) | ||
.collect(); | ||
neighbors | ||
.iter() | ||
.for_each(|n| log::info!("Valid IP neighbor: {:?}", n)); | ||
|
||
Ok(neighbors) | ||
} | ||
} | ||
|
||
impl IpNeighborScanner for PowerShellIpNeighborScanner { | ||
fn scan(&self) -> BoxFuture<'static, Result<Vec<IpNeighbor>, IpNeighborScanError>> { | ||
Self::scan().boxed() | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[allow(non_snake_case)] | ||
struct NetNeighbor { | ||
InterfaceIndex: u32, | ||
IPAddress: Ipv6Addr, | ||
State: String, | ||
} | ||
|
||
impl From<NetNeighbor> for IpNeighbor { | ||
fn from(value: NetNeighbor) -> Self { | ||
Self { | ||
address: value.IPAddress, | ||
network_interface_index: value.InterfaceIndex, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::os::OperatingSystem; | ||
|
||
#[tokio::test] | ||
async fn scan() { | ||
crate::test::init(); | ||
|
||
if crate::os::detect_operating_system().await.unwrap() != OperatingSystem::Windows { | ||
println!("Operating system is not Windows, skipping."); | ||
return; | ||
} | ||
|
||
let neighbors = PowerShellIpNeighborScanner.scan().await.unwrap(); | ||
assert!(!neighbors.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn parse_output() { | ||
let output = r#" | ||
"InterfaceIndex","IPAddress","State" | ||
"1","fe80::1:abcd","Reachable" | ||
"2","fe80::2:abcd","Permanent" | ||
"3","fe80::3:abcd","Unreachable" | ||
"4","ff02::4:abcd","Reachable" | ||
"# | ||
.trim(); | ||
let expected_neighbors = vec![ | ||
IpNeighbor { | ||
network_interface_index: 1, | ||
address: "fe80::1:abcd".parse().unwrap(), | ||
}, | ||
IpNeighbor { | ||
network_interface_index: 2, | ||
address: "fe80::2:abcd".parse().unwrap(), | ||
}, | ||
]; | ||
|
||
// When | ||
let actual_neighbors = | ||
PowerShellIpNeighborScanner::parse_output(output.as_bytes()).unwrap(); | ||
|
||
// Then | ||
assert_eq!(actual_neighbors, expected_neighbors); | ||
} | ||
} |
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,46 @@ | ||
use std::process::Stdio; | ||
use thiserror::Error; | ||
use tokio::io::AsyncReadExt; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::process::Command; | ||
|
||
pub async fn eval(command: &str, args: &[&str], stdin: &[u8]) -> Result<Vec<u8>, ProcessError> { | ||
let mut process = Command::new(command) | ||
.args(args) | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::inherit()) | ||
.spawn()?; | ||
|
||
let mut stdin_pipe = process | ||
.stdin | ||
.take() | ||
.ok_or_else(|| ProcessError::StdioRedirection)?; | ||
stdin_pipe.write_all(stdin).await?; | ||
drop(stdin_pipe); | ||
|
||
if !process.wait().await?.success() { | ||
return Err(ProcessError::ExternalCommand); | ||
} | ||
|
||
let mut stdout = process | ||
.stdout | ||
.take() | ||
.ok_or_else(|| ProcessError::StdioRedirection)?; | ||
|
||
let mut buffer = Default::default(); | ||
stdout.read_to_end(&mut buffer).await?; | ||
Ok(buffer) | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ProcessError { | ||
#[error("Failed in create a child process")] | ||
ChildProcessCreation(#[from] std::io::Error), | ||
|
||
#[error("Failed to redirect standard I/O")] | ||
StdioRedirection, | ||
|
||
#[error("External command failed")] | ||
ExternalCommand, | ||
} |