Add base subcrates

This commit is contained in:
DaZuo0122
2026-01-16 00:38:03 +08:00
commit 240107e00f
17 changed files with 5081 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
[package]
name = "wtfnet-platform"
version = "0.1.0"
edition = "2024"
[dependencies]
async-trait = "0.1"
serde = { version = "1", features = ["derive"] }
wtfnet-core = { path = "../wtfnet-core" }

View File

@@ -0,0 +1,118 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use wtfnet_core::ErrorCode;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetInterface {
pub name: String,
pub index: Option<u32>,
pub is_up: Option<bool>,
pub mtu: Option<u32>,
pub mac: Option<String>,
pub addresses: Vec<NetAddress>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetAddress {
pub ip: String,
pub prefix_len: Option<u8>,
pub scope: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DnsConfigSnapshot {
pub servers: Vec<String>,
pub search_domains: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RouteEntry {
pub destination: String,
pub gateway: Option<String>,
pub interface: Option<String>,
pub metric: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListenSocket {
pub proto: String,
pub local_addr: String,
pub state: Option<String>,
pub pid: Option<u32>,
pub ppid: Option<u32>,
pub process_name: Option<String>,
pub process_path: Option<String>,
pub owner: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RootCert {
pub subject: String,
pub issuer: String,
pub not_before: String,
pub not_after: String,
pub serial_number: String,
pub sha1: String,
pub sha256: String,
pub key_algorithm: String,
pub key_size: Option<u32>,
pub store: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NeighborEntry {
pub ip: String,
pub mac: Option<String>,
pub interface: Option<String>,
pub state: Option<String>,
}
#[derive(Debug, Clone)]
pub struct PlatformError {
pub code: ErrorCode,
pub message: String,
}
impl PlatformError {
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn not_supported(message: impl Into<String>) -> Self {
Self::new(ErrorCode::NotSupported, message)
}
}
#[async_trait]
pub trait SysProvider: Send + Sync {
async fn interfaces(&self) -> Result<Vec<NetInterface>, PlatformError>;
async fn routes(&self) -> Result<Vec<RouteEntry>, PlatformError>;
async fn dns_config(&self) -> Result<DnsConfigSnapshot, PlatformError>;
}
#[async_trait]
pub trait PortsProvider: Send + Sync {
async fn listening(&self) -> Result<Vec<ListenSocket>, PlatformError>;
async fn who_owns(&self, port: u16) -> Result<Vec<ListenSocket>, PlatformError>;
}
#[async_trait]
pub trait CertProvider: Send + Sync {
async fn trusted_roots(&self) -> Result<Vec<RootCert>, PlatformError>;
}
#[async_trait]
pub trait NeighProvider: Send + Sync {
async fn neighbors(&self) -> Result<Vec<NeighborEntry>, PlatformError>;
}
pub struct Platform {
pub sys: Arc<dyn SysProvider>,
pub ports: Arc<dyn PortsProvider>,
pub cert: Arc<dyn CertProvider>,
pub neigh: Arc<dyn NeighProvider>,
}