2024-02-09 08:04:23 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use futures::TryStreamExt;
|
|
|
|
use log::{error, info, warn};
|
|
|
|
use netlink_packet_route::link::LinkAttribute;
|
2024-02-06 10:28:39 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2024-02-05 19:10:02 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::time::sleep;
|
2024-02-06 14:35:55 +00:00
|
|
|
|
2024-02-09 08:04:23 +00:00
|
|
|
use crate::backend::NetworkBackend;
|
2024-02-05 12:45:45 +00:00
|
|
|
|
2024-02-09 08:04:23 +00:00
|
|
|
mod backend;
|
2024-02-09 13:06:00 +00:00
|
|
|
mod chandev;
|
|
|
|
mod nat;
|
|
|
|
mod proxynat;
|
2024-02-09 08:04:23 +00:00
|
|
|
mod raw_socket;
|
2024-02-08 12:17:51 +00:00
|
|
|
|
2024-02-05 19:10:02 +00:00
|
|
|
pub struct NetworkService {
|
|
|
|
pub network: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkService {
|
|
|
|
pub fn new(network: String) -> Result<NetworkService> {
|
|
|
|
Ok(NetworkService { network })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkService {
|
|
|
|
pub async fn watch(&mut self) -> Result<()> {
|
2024-02-06 10:28:39 +00:00
|
|
|
let spawned: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
|
2024-02-05 19:10:02 +00:00
|
|
|
let (connection, handle, _) = rtnetlink::new_connection()?;
|
|
|
|
tokio::spawn(connection);
|
|
|
|
loop {
|
|
|
|
let mut stream = handle.link().get().execute();
|
|
|
|
while let Some(message) = stream.try_next().await? {
|
|
|
|
let mut name: Option<String> = None;
|
|
|
|
for attribute in &message.attributes {
|
|
|
|
if let LinkAttribute::IfName(if_name) = attribute {
|
|
|
|
name = Some(if_name.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if name.is_none() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = name.unwrap();
|
|
|
|
if !name.starts_with("vif") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-02-06 10:28:39 +00:00
|
|
|
if let Ok(spawns) = spawned.lock() {
|
|
|
|
if spawns.contains(&name) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-05 19:10:02 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 10:28:39 +00:00
|
|
|
if let Err(error) = self.add_network_backend(&name, spawned.clone()).await {
|
2024-02-05 19:10:02 +00:00
|
|
|
warn!(
|
|
|
|
"failed to initialize network backend for interface {}: {}",
|
|
|
|
name, error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-06 10:28:39 +00:00
|
|
|
if let Ok(mut spawns) = spawned.lock() {
|
|
|
|
spawns.push(name.clone());
|
|
|
|
}
|
2024-02-05 19:10:02 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 06:19:36 +00:00
|
|
|
sleep(Duration::from_secs(2)).await;
|
2024-02-05 19:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 10:28:39 +00:00
|
|
|
async fn add_network_backend(
|
|
|
|
&mut self,
|
|
|
|
interface: &str,
|
|
|
|
spawned: Arc<Mutex<Vec<String>>>,
|
|
|
|
) -> Result<()> {
|
2024-02-05 19:10:02 +00:00
|
|
|
let interface = interface.to_string();
|
2024-02-08 12:17:51 +00:00
|
|
|
let mut network = NetworkBackend::new(&self.network, &interface)?;
|
2024-02-06 06:19:36 +00:00
|
|
|
info!("initializing network backend for interface {}", interface);
|
2024-02-05 19:10:02 +00:00
|
|
|
network.init().await?;
|
2024-02-06 06:19:36 +00:00
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
2024-02-05 19:10:02 +00:00
|
|
|
info!("spawning network backend for interface {}", interface);
|
2024-02-06 14:35:55 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
if let Err(error) = network.run().await {
|
2024-02-05 19:10:02 +00:00
|
|
|
error!(
|
2024-02-06 14:35:55 +00:00
|
|
|
"network backend for interface {} has been stopped: {}",
|
2024-02-05 19:10:02 +00:00
|
|
|
interface, error
|
|
|
|
);
|
|
|
|
}
|
2024-02-06 10:28:39 +00:00
|
|
|
|
|
|
|
if let Ok(mut spawns) = spawned.lock() {
|
|
|
|
if let Some(position) = spawns.iter().position(|x| *x == interface) {
|
|
|
|
spawns.remove(position);
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 19:10:02 +00:00
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|