Set up works, daemon still bugs out

This commit is contained in:
rhaskia 2024-03-10 10:40:08 +13:00 committed by Conrad Kramer
parent 982cfc39b6
commit 98ce57bba0
6 changed files with 127 additions and 58 deletions

137
Cargo.lock generated
View file

@ -956,6 +956,20 @@ dependencies = [
"tracing",
]
[[package]]
name = "handlebars"
version = "3.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4498fc115fa7d34de968184e473529abb40eeb6be8bc5f7faba3d08c316cb3e3"
dependencies = [
"log",
"pest",
"pest_derive",
"quick-error",
"serde",
"serde_json",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
@ -1159,6 +1173,22 @@ dependencies = [
"yaml-rust",
]
[[package]]
name = "interfaces"
version = "0.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6250a98af259a26fd5a4a6081fccea9ac116e4c3178acf4aeb86d32d2b7715"
dependencies = [
"bitflags 2.4.2",
"cc",
"handlebars",
"lazy_static",
"libc",
"nix 0.26.4",
"serde",
"serde_derive",
]
[[package]]
name = "ip_network"
version = "0.4.1"
@ -1453,34 +1483,6 @@ dependencies = [
"thiserror",
]
[[package]]
name = "netlink-proto"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b33524dc0968bfad349684447bfce6db937a9ac3332a1fe60c0c5a5ce63f21"
dependencies = [
"bytes",
"futures",
"log",
"netlink-packet-core",
"netlink-sys",
"thiserror",
"tokio",
]
[[package]]
name = "netlink-sys"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6471bf08e7ac0135876a9581bf3217ef0333c191c128d34878079f42ee150411"
dependencies = [
"bytes",
"futures",
"libc",
"log",
"tokio",
]
[[package]]
name = "nix"
version = "0.26.4"
@ -1686,6 +1688,51 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pest"
version = "2.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f8023d0fb78c8e03784ea1c7f3fa36e68a723138990b8d5a47d916b651e7a8"
dependencies = [
"memchr",
"thiserror",
"ucd-trie",
]
[[package]]
name = "pest_derive"
version = "2.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0d24f72393fd16ab6ac5738bc33cdb6a9aa73f8b902e8fe29cf4e67d7dd1026"
dependencies = [
"pest",
"pest_generator",
]
[[package]]
name = "pest_generator"
version = "2.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdc17e2a6c7d0a492f0158d7a4bd66cc17280308bbaff78d5bef566dca35ab80"
dependencies = [
"pest",
"pest_meta",
"proc-macro2",
"quote",
"syn 2.0.48",
]
[[package]]
name = "pest_meta"
version = "2.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "934cd7631c050f4674352a6e835d5f6711ffbfb9345c2fc0107155ac495ae293"
dependencies = [
"once_cell",
"pest",
"sha2",
]
[[package]]
name = "pin-project"
version = "1.1.4"
@ -1804,6 +1851,12 @@ dependencies = [
"prost",
]
[[package]]
name = "quick-error"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3"
[[package]]
name = "quote"
version = "1.0.35"
@ -1948,24 +2001,6 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "rtnetlink"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b684475344d8df1859ddb2d395dd3dac4f8f3422a1aa0725993cb375fc5caba5"
dependencies = [
"futures",
"log",
"netlink-packet-core",
"netlink-packet-route",
"netlink-packet-utils",
"netlink-proto",
"netlink-sys",
"nix 0.27.1",
"thiserror",
"tokio",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
@ -2618,13 +2653,15 @@ dependencies = [
"byteorder",
"fehler",
"futures",
"interfaces",
"lazy_static",
"libc",
"libloading 0.7.4",
"log",
"netlink-packet-core",
"netlink-packet-route",
"nix 0.26.4",
"reqwest",
"rtnetlink",
"schemars",
"serde",
"socket2",
@ -2643,6 +2680,12 @@ version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "ucd-trie"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9"
[[package]]
name = "unicode-bidi"
version = "0.3.15"

View file

@ -51,7 +51,7 @@ impl DaemonInstance {
}
RunState::Idle => {
let tun_if = st.tun.open()?;
tun_if.set_up(true).await?;
tun_if.set_up(true)?;
debug!("Setting tun on wg_interface");
self.wg_interface.read().await.set_tun(tun_if).await;

View file

@ -15,10 +15,10 @@ log = "0.4"
serde = { version = "1", features = ["derive"], optional = true }
schemars = { version = "0.8", optional = true }
[target.'cfg(feature = "linux")'.dev-dependencies]
rtnetlink = "0.14"
futures = { version = "0.3.28", optional = true }
netlink-packet-route = "0.19.0"
netlink-packet-core = "0.7.0"
[features]
serde = ["dep:serde", "dep:schemars"]
@ -27,6 +27,9 @@ tokio = ["tokio/net", "dep:futures"]
[target.'cfg(feature = "tokio")'.dev-dependencies]
tokio = { features = ["rt", "macros"] }
[target.'cfg(target_os = "linux")'.dependencies]
interfaces = "0.0.9"
[target.'cfg(windows)'.dependencies]
lazy_static = "1.4"
libloading = "0.7"

View file

@ -16,7 +16,7 @@ impl TunInterface {
}
#[instrument]
pub async fn set_up(&self, up: bool) -> io::Result<()> {
pub fn set_up(&self, up: bool) -> io::Result<()> {
self.inner.get_ref().set_up(up)
}

View file

@ -11,7 +11,6 @@ use std::{
use fehler::throws;
use libc::in6_ifreq;
use rtnetlink::new_connection;
use socket2::{Domain, SockAddr, Socket, Type};
use tracing::{info, instrument};
@ -99,10 +98,19 @@ impl TunInterface {
#[throws]
#[instrument]
pub fn set_up(&self, up: bool) {
let connection = new_connection()?;
let handle = connection.1;
let link = handle.link().set(self.index()? as u32);
if up { link.up() } else { link.down() }
let mut inter = interfaces::Interface::get_by_name(&self.name()?)
.unwrap()
.unwrap();
inter.set_up(up).unwrap();
}
#[throws]
#[instrument]
pub fn is_up(&self) -> bool {
let inter = interfaces::Interface::get_by_name(&self.name()?)
.unwrap()
.unwrap();
inter.is_up()
}
#[throws]

View file

@ -24,6 +24,21 @@ fn test_set_get_broadcast_addr() {
assert_eq!(broadcast_addr, result);
}
#[test]
#[throws]
#[cfg(not(any(target_os = "windows", target_vendor = "apple")))]
fn test_set_get_up() {
let tun = TunInterface::new()?;
let addr = Ipv4Addr::new(10, 0, 0, 1);
tun.set_ipv4_addr(addr)?;
let broadcast_addr = Ipv4Addr::new(255, 255, 255, 0);
tun.set_broadcast_addr(broadcast_addr)?;
tun.set_up(true)?;
assert!(tun.is_up()?);
}
#[test]
#[throws]
#[cfg(not(target_os = "windows"))]