-
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.
WIP Detect OS before choosing
IpNeighborScanner
WIP: In unit test, neighbors are empty.
- Loading branch information
Showing
8 changed files
with
193 additions
and
5 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,4 @@ | ||
Write-Output "if_index,ip_address,state" | ||
foreach ($row in (Get-NetNeighbor -AddressFamily IPv6)) { | ||
Write-Output "$($row.ifIndex),$($row.IPAddress),$($row.State)" | ||
} |
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,72 @@ | ||
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 WindowsIpNeighborScanner; | ||
|
||
impl WindowsIpNeighborScanner { | ||
async fn scan() -> Result<Vec<IpNeighbor>, IpNeighborScanError> { | ||
let stdout = crate::process::eval( | ||
"powershell", | ||
&["-Command", "-"], | ||
include_bytes!("./Print-IpNeighbors.ps1"), | ||
) | ||
.await?; | ||
let neighbors: Vec<_> = Reader::from_reader(stdout.as_slice()) | ||
.deserialize::<NetNeighbor>() | ||
.collect::<Result<Vec<NetNeighbor>, _>>()?; | ||
let neighbors = neighbors | ||
.into_iter() | ||
.filter(|n| n.state != "Unreachable") | ||
.filter(|n| n.ip_address.segments().starts_with(&[0xF, 0xE, 0x8, 0x0])) | ||
.map(Into::into) | ||
.collect(); | ||
Ok(neighbors) | ||
} | ||
} | ||
|
||
impl IpNeighborScanner for WindowsIpNeighborScanner { | ||
fn scan(&self) -> BoxFuture<'static, Result<Vec<IpNeighbor>, IpNeighborScanError>> { | ||
Self::scan().boxed() | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct NetNeighbor { | ||
if_index: u32, | ||
ip_address: Ipv6Addr, | ||
state: String, | ||
} | ||
|
||
impl From<NetNeighbor> for IpNeighbor { | ||
fn from(value: NetNeighbor) -> Self { | ||
Self { | ||
address: value.ip_address, | ||
network_interface_index: value.if_index, | ||
} | ||
} | ||
} | ||
|
||
#[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 = WindowsIpNeighborScanner.scan().await.unwrap(); | ||
assert!(!neighbors.is_empty()); | ||
} | ||
} |
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,42 @@ | ||
use std::path::PathBuf; | ||
use thiserror::Error; | ||
|
||
#[derive(PartialEq, Eq)] | ||
pub enum OperatingSystem { | ||
Windows, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum OperatingSystemDetectError { | ||
#[error("Error in file system")] | ||
FileSystem(#[from] std::io::Error), | ||
|
||
#[error("Unknown operating system")] | ||
UnknownOperatingSystem, | ||
} | ||
|
||
pub async fn detect_operating_system() -> Result<OperatingSystem, OperatingSystemDetectError> { | ||
if is_file(&[r"C:\", "Windows", "System32", "ntoskrnl.exe"]).await? { | ||
return Ok(OperatingSystem::Windows); | ||
} | ||
if is_file(&["/", "usr", "bin", "systemctl"]).await? { | ||
return Ok(OperatingSystem::Windows); | ||
} | ||
|
||
Err(OperatingSystemDetectError::UnknownOperatingSystem) | ||
} | ||
|
||
async fn is_file(path: &[&'static str]) -> std::io::Result<bool> { | ||
let path_buf: PathBuf = path.iter().collect(); | ||
Ok(tokio::fs::try_exists(&path_buf).await? && tokio::fs::metadata(&path_buf).await?.is_file()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn can_detect() { | ||
detect_operating_system().await.expect("OS must be known"); | ||
} | ||
} |
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, | ||
} |
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