From 0cbf000945c9f42be8cce950899e1fcbb52f54a6 Mon Sep 17 00:00:00 2001 From: Petard Jonson Date: Thu, 9 Jan 2025 21:38:01 +0800 Subject: [PATCH] fix(macos): support set dns --- Cargo.lock | 205 +++++++++++++----- nyanpasu_ipc/src/api/mod.rs | 1 + nyanpasu_ipc/src/api/network/mod.rs | 1 + nyanpasu_ipc/src/api/network/set_dns.rs | 12 + nyanpasu_ipc/src/client/shortcuts.rs | 16 ++ nyanpasu_service/Cargo.toml | 1 + nyanpasu_service/src/cmds/rpc/mod.rs | 17 +- .../src/server/routing/core/mod.rs | 13 +- .../server/routing/{core/log.rs => logs.rs} | 10 +- nyanpasu_service/src/server/routing/mod.rs | 25 +-- .../src/server/routing/network.rs | 50 +++++ nyanpasu_service/src/server/routing/status.rs | 9 +- 12 files changed, 279 insertions(+), 81 deletions(-) create mode 100644 nyanpasu_ipc/src/api/network/mod.rs create mode 100644 nyanpasu_ipc/src/api/network/set_dns.rs rename nyanpasu_service/src/server/routing/{core/log.rs => logs.rs} (62%) create mode 100644 nyanpasu_service/src/server/routing/network.rs diff --git a/Cargo.lock b/Cargo.lock index 69556a2..489721e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -163,9 +163,9 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.84" +version = "0.1.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b1244b10dcd56c92219da4e14caa97e312079e185f04ba3eea25061561dc0a0" +checksum = "3f934833b4b7233644e5848f235df3f57ed8c80f1528a26c3dfa13d2147fa056" dependencies = [ "proc-macro2", "quote", @@ -799,12 +799,12 @@ dependencies = [ [[package]] name = "dirs-utils" version = "0.1.0" -source = "git+https://github.com/LibNyanpasu/nyanpasu-utils.git#82a1c73621e4d94e5db3075e32e06e190cc01d37" +source = "git+https://github.com/LibNyanpasu/nyanpasu-utils.git#f3a4ca29800484524643c02b96b76004384d5ec6" dependencies = [ "dirs 5.0.1", - "thiserror 1.0.69", + "thiserror 2.0.10", "tracing", - "windows 0.58.0", + "windows 0.59.0", ] [[package]] @@ -855,6 +855,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + [[package]] name = "fixedbitset" version = "0.4.2" @@ -1378,7 +1384,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -1393,9 +1399,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.14" +version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" [[package]] name = "lock_api" @@ -1472,6 +1478,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "network-utils" +version = "0.1.0" +source = "git+https://github.com/LibNyanpasu/nyanpasu-utils.git#f3a4ca29800484524643c02b96b76004384d5ec6" +dependencies = [ + "log", + "tempfile", +] + [[package]] name = "nix" version = "0.27.1" @@ -1607,7 +1622,7 @@ dependencies = [ [[package]] name = "nyanpasu-utils" version = "0.1.0" -source = "git+https://github.com/LibNyanpasu/nyanpasu-utils.git#82a1c73621e4d94e5db3075e32e06e190cc01d37" +source = "git+https://github.com/LibNyanpasu/nyanpasu-utils.git#f3a4ca29800484524643c02b96b76004384d5ec6" dependencies = [ "constcat", "derive_builder", @@ -1615,13 +1630,14 @@ dependencies = [ "encoding_rs", "kill_tree", "memchr", + "network-utils", "os-utils", "os_pipe", "parking_lot", "serde", "shared_child", "sysinfo", - "thiserror 1.0.69", + "thiserror 2.0.10", "tokio", "tracing", "tracing-attributes", @@ -1657,11 +1673,11 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" [[package]] name = "os-utils" version = "0.1.0" -source = "git+https://github.com/LibNyanpasu/nyanpasu-utils.git#82a1c73621e4d94e5db3075e32e06e190cc01d37" +source = "git+https://github.com/LibNyanpasu/nyanpasu-utils.git#f3a4ca29800484524643c02b96b76004384d5ec6" dependencies = [ "nix 0.29.0", "shared_child", - "windows 0.58.0", + "windows 0.59.0", ] [[package]] @@ -1724,36 +1740,36 @@ dependencies = [ [[package]] name = "phf" -version = "0.11.2" +version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" dependencies = [ "phf_shared", ] [[package]] name = "phf_shared" -version = "0.11.2" +version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" dependencies = [ "siphasher", ] [[package]] name = "pin-project" -version = "1.1.7" +version = "1.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be57f64e946e500c8ee36ef6331845d40a93055567ec57e8fae13efd33759b95" +checksum = "1e2ec53ad785f4d35dac0adea7f7dc6f1bb277ad84a680c7afefeae05d1f5916" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.7" +version = "1.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c" +checksum = "d56a66c0c55993aa927429d0f8a0abfd74f084e4d9c192cffed01e418d83eefb" dependencies = [ "proc-macro2", "quote", @@ -1802,9 +1818,9 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.25" +version = "0.2.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" +checksum = "483f8c21f64f3ea09fe0f30f5d48c3e8eefe5dac9129f0075f76593b4c1da705" dependencies = [ "proc-macro2", "syn", @@ -2032,9 +2048,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.42" +version = "0.38.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85" +checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6" dependencies = [ "bitflags", "errno", @@ -2089,9 +2105,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.134" +version = "1.0.135" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d" +checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9" dependencies = [ "itoa", "memchr", @@ -2203,9 +2219,9 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" [[package]] name = "siphasher" -version = "0.3.11" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" [[package]] name = "slab" @@ -2240,9 +2256,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.94" +version = "2.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "987bc0be1cdea8b10216bd06e2ca407d40b9543468fafd3ddfb02f36e77f71f3" +checksum = "46f71c0377baf4ef1cc3e3402ded576dccc315800fbc62dfc7fe04b009773b4a" dependencies = [ "proc-macro2", "quote", @@ -2257,9 +2273,9 @@ checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" [[package]] name = "sysinfo" -version = "0.32.1" +version = "0.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c33cd241af0f2e9e3b5c32163b873b29956890b5342e6745b917ce9d490f4af" +checksum = "4fc858248ea01b66f19d8e8a6d55f41deaf91e9d495246fd01368d99935c6c01" dependencies = [ "core-foundation-sys", "libc", @@ -2269,6 +2285,20 @@ dependencies = [ "windows 0.57.0", ] +[[package]] +name = "tempfile" +version = "3.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8a559c81686f576e8cd0290cd2a24a2a9ad80c98b3478856500fcbd7acd704" +dependencies = [ + "cfg-if", + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + [[package]] name = "thiserror" version = "1.0.69" @@ -2890,12 +2920,12 @@ dependencies = [ [[package]] name = "windows" -version = "0.58.0" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +checksum = "7f919aee0a93304be7f62e8e5027811bbba96bcb1de84d6618be56e43f8a32a1" dependencies = [ - "windows-core 0.58.0", - "windows-targets 0.52.6", + "windows-core 0.59.0", + "windows-targets 0.53.0", ] [[package]] @@ -2930,15 +2960,15 @@ dependencies = [ [[package]] name = "windows-core" -version = "0.58.0" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +checksum = "810ce18ed2112484b0d4e15d022e5f598113e220c53e373fb31e67e21670c1ce" dependencies = [ - "windows-implement 0.58.0", - "windows-interface 0.58.0", - "windows-result 0.2.0", + "windows-implement 0.59.0", + "windows-interface 0.59.0", + "windows-result 0.3.0", "windows-strings", - "windows-targets 0.52.6", + "windows-targets 0.53.0", ] [[package]] @@ -2954,9 +2984,9 @@ dependencies = [ [[package]] name = "windows-implement" -version = "0.58.0" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +checksum = "83577b051e2f49a058c308f17f273b570a6a758386fc291b5f6a934dd84e48c1" dependencies = [ "proc-macro2", "quote", @@ -2976,9 +3006,9 @@ dependencies = [ [[package]] name = "windows-interface" -version = "0.58.0" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +checksum = "cb26fd936d991781ea39e87c3a27285081e3c0da5ca0fcbc02d368cc6f52ff01" dependencies = [ "proc-macro2", "quote", @@ -2996,11 +3026,11 @@ dependencies = [ [[package]] name = "windows-result" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +checksum = "d08106ce80268c4067c0571ca55a9b4e9516518eaa1a1fe9b37ca403ae1d1a34" dependencies = [ - "windows-targets 0.52.6", + "windows-targets 0.53.0", ] [[package]] @@ -3016,12 +3046,11 @@ dependencies = [ [[package]] name = "windows-strings" -version = "0.1.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +checksum = "b888f919960b42ea4e11c2f408fadb55f78a9f236d5eef084103c8ce52893491" dependencies = [ - "windows-result 0.2.0", - "windows-targets 0.52.6", + "windows-targets 0.53.0", ] [[package]] @@ -3075,13 +3104,29 @@ dependencies = [ "windows_aarch64_gnullvm 0.52.6", "windows_aarch64_msvc 0.52.6", "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm", + "windows_i686_gnullvm 0.52.6", "windows_i686_msvc 0.52.6", "windows_x86_64_gnu 0.52.6", "windows_x86_64_gnullvm 0.52.6", "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows-targets" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e4c7e8ceaaf9cb7d7507c974735728ab453b67ef8f18febdd7c11fe59dca8b" +dependencies = [ + "windows_aarch64_gnullvm 0.53.0", + "windows_aarch64_msvc 0.53.0", + "windows_i686_gnu 0.53.0", + "windows_i686_gnullvm 0.53.0", + "windows_i686_msvc 0.53.0", + "windows_x86_64_gnu 0.53.0", + "windows_x86_64_gnullvm 0.53.0", + "windows_x86_64_msvc 0.53.0", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" @@ -3094,6 +3139,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" @@ -3106,6 +3157,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" + [[package]] name = "windows_i686_gnu" version = "0.48.5" @@ -3118,12 +3175,24 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" +[[package]] +name = "windows_i686_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" + [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" + [[package]] name = "windows_i686_msvc" version = "0.48.5" @@ -3136,6 +3205,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_i686_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" @@ -3148,6 +3223,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" @@ -3160,6 +3241,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" @@ -3172,11 +3259,17 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" + [[package]] name = "xml-rs" -version = "0.8.24" +version = "0.8.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea8b391c9a790b496184c29f7f93b9ed5b16abb306c05415b68bcc16e4d06432" +checksum = "c5b940ebc25896e71dd073bad2dbaa2abfe97b0a391415e22ad1326d9c54e3c4" [[package]] name = "zerocopy" diff --git a/nyanpasu_ipc/src/api/mod.rs b/nyanpasu_ipc/src/api/mod.rs index 9edd329..c245620 100644 --- a/nyanpasu_ipc/src/api/mod.rs +++ b/nyanpasu_ipc/src/api/mod.rs @@ -1,5 +1,6 @@ pub mod core; pub mod log; +pub mod network; pub mod status; use serde::{de::DeserializeOwned, Deserialize, Serialize}; diff --git a/nyanpasu_ipc/src/api/network/mod.rs b/nyanpasu_ipc/src/api/network/mod.rs new file mode 100644 index 0000000..d0d3909 --- /dev/null +++ b/nyanpasu_ipc/src/api/network/mod.rs @@ -0,0 +1 @@ +pub mod set_dns; diff --git a/nyanpasu_ipc/src/api/network/set_dns.rs b/nyanpasu_ipc/src/api/network/set_dns.rs new file mode 100644 index 0000000..c83e55a --- /dev/null +++ b/nyanpasu_ipc/src/api/network/set_dns.rs @@ -0,0 +1,12 @@ +use crate::api::R; +use serde::{Deserialize, Serialize}; +use std::{borrow::Cow, net::IpAddr}; + +pub const NETWORK_SET_DNS_ENDPOINT: &str = "/network/set_dns"; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct NetworkSetDnsReq<'n> { + pub dns_servers: Option>>, +} + +pub type NetworkSetDnsRes<'a> = R<'a, ()>; diff --git a/nyanpasu_ipc/src/client/shortcuts.rs b/nyanpasu_ipc/src/client/shortcuts.rs index bec948c..a4a7813 100644 --- a/nyanpasu_ipc/src/client/shortcuts.rs +++ b/nyanpasu_ipc/src/client/shortcuts.rs @@ -89,4 +89,20 @@ impl<'a> Client<'a> { let data = response.data.unwrap(); Ok(data) } + + pub async fn set_dns( + &self, + payload: &api::network::set_dns::NetworkSetDnsReq<'_>, + ) -> Result<()> { + let payload = simd_json::serde::to_string(payload)?; + let request = Request::post(api::network::set_dns::NETWORK_SET_DNS_ENDPOINT) + .header(CONTENT_TYPE, "application/json") + .body(Body::from(payload))?; + let response = send_request(&self.0, request) + .await? + .cast_body::() + .await?; + response.ok()?; + Ok(()) + } } diff --git a/nyanpasu_service/Cargo.toml b/nyanpasu_service/Cargo.toml index 9ce2210..b481897 100644 --- a/nyanpasu_service/Cargo.toml +++ b/nyanpasu_service/Cargo.toml @@ -22,6 +22,7 @@ hardware-lock-elision = ["parking_lot/hardware-lock-elision"] nyanpasu-utils = { workspace = true, default-features = false, features = [ "dirs", "os", + "network", ] } nyanpasu-ipc = { path = "../nyanpasu_ipc", default-features = false, features = [ "server", diff --git a/nyanpasu_service/src/cmds/rpc/mod.rs b/nyanpasu_service/src/cmds/rpc/mod.rs index 4a78253..ed2f0f9 100644 --- a/nyanpasu_service/src/cmds/rpc/mod.rs +++ b/nyanpasu_service/src/cmds/rpc/mod.rs @@ -1,9 +1,9 @@ -use std::borrow::Cow; +use std::{borrow::Cow, net::IpAddr}; /// This module is a shortcut for client rpc calls. /// It is useful for testing and debugging service rpc calls. use clap::Subcommand; -use nyanpasu_ipc::client::shortcuts::Client; +use nyanpasu_ipc::{api::network::set_dns::NetworkSetDnsReq, client::shortcuts::Client}; fn core_type_parser(s: &str) -> Result { let mut s = s.to_string(); @@ -32,6 +32,8 @@ pub enum RpcCommand { RestartCore, /// Get the logs of the service InspectLogs, + /// Set the dns servers + SetDns { dns_servers: Option> }, } pub async fn rpc(commands: RpcCommand) -> Result<(), crate::cmds::CommandError> { @@ -76,6 +78,17 @@ pub async fn rpc(commands: RpcCommand) -> Result<(), crate::cmds::CommandError> println!("{}", log.trim_matches('\n')); } } + RpcCommand::SetDns { dns_servers } => { + let client = Client::service_default(); + client + .set_dns(&NetworkSetDnsReq { + dns_servers: dns_servers + .as_ref() + .map(|v| v.into_iter().map(|v| Cow::Borrowed(v)).collect()), + }) + .await + .map_err(|e| crate::cmds::CommandError::Other(e.into()))?; + } } Ok(()) } diff --git a/nyanpasu_service/src/server/routing/core/mod.rs b/nyanpasu_service/src/server/routing/core/mod.rs index d2e438d..00d0e85 100644 --- a/nyanpasu_service/src/server/routing/core/mod.rs +++ b/nyanpasu_service/src/server/routing/core/mod.rs @@ -1,4 +1,15 @@ -pub mod log; +use axum::{routing::post, Router}; +use nyanpasu_ipc::api::core::{ + restart::CORE_RESTART_ENDPOINT, start::CORE_START_ENDPOINT, stop::CORE_STOP_ENDPOINT, +}; + pub mod restart; pub mod start; pub mod stop; + +pub fn setup() -> Router { + Router::new() + .route(CORE_START_ENDPOINT, post(start::start)) + .route(CORE_STOP_ENDPOINT, post(stop::stop)) + .route(CORE_RESTART_ENDPOINT, post(restart::restart)) +} diff --git a/nyanpasu_service/src/server/routing/core/log.rs b/nyanpasu_service/src/server/routing/logs.rs similarity index 62% rename from nyanpasu_service/src/server/routing/core/log.rs rename to nyanpasu_service/src/server/routing/logs.rs index cf83956..365bfc6 100644 --- a/nyanpasu_service/src/server/routing/core/log.rs +++ b/nyanpasu_service/src/server/routing/logs.rs @@ -1,9 +1,15 @@ -use axum::{http::StatusCode, Json}; +use axum::{http::StatusCode, routing::get, Json, Router}; use nyanpasu_ipc::api::{ - log::{LogsRes, LogsResBody}, + log::{LogsRes, LogsResBody, LOGS_INSPECT_ENDPOINT, LOGS_RETRIEVE_ENDPOINT}, RBuilder, }; +pub fn setup() -> Router { + Router::new() + .route(LOGS_RETRIEVE_ENDPOINT, get(retrieve_logs)) + .route(LOGS_INSPECT_ENDPOINT, get(inspect_logs)) +} + pub async fn retrieve_logs() -> (StatusCode, Json>) { let logs = crate::server::logger::Logger::global().retrieve_logs(); let res = RBuilder::success(LogsResBody { logs }); diff --git a/nyanpasu_service/src/server/routing/mod.rs b/nyanpasu_service/src/server/routing/mod.rs index d6ae69a..cb4520e 100644 --- a/nyanpasu_service/src/server/routing/mod.rs +++ b/nyanpasu_service/src/server/routing/mod.rs @@ -1,29 +1,18 @@ -use core::log; - -use axum::{ - routing::{get, post}, - Router, -}; - -use nyanpasu_ipc::api::{ - core::{restart::CORE_RESTART_ENDPOINT, start::CORE_START_ENDPOINT, stop::CORE_STOP_ENDPOINT}, - log::{LOGS_INSPECT_ENDPOINT, LOGS_RETRIEVE_ENDPOINT}, - status::STATUS_ENDPOINT, -}; +use axum::Router; use tracing_attributes::instrument; pub mod core; +pub mod logs; +pub mod network; pub mod status; #[instrument] pub fn apply_routes(app: Router) -> Router { tracing::info!("Applying routes..."); let tracing_layer = tower_http::trace::TraceLayer::new_for_http(); - app.route(STATUS_ENDPOINT, get(status::status)) - .route(CORE_START_ENDPOINT, post(core::start::start)) - .route(CORE_STOP_ENDPOINT, post(core::stop::stop)) - .route(CORE_RESTART_ENDPOINT, post(core::restart::restart)) - .route(LOGS_RETRIEVE_ENDPOINT, get(log::retrieve_logs)) - .route(LOGS_INSPECT_ENDPOINT, get(log::inspect_logs)) + app.nest("/", status::setup()) + .nest("/", core::setup()) + .nest("/", logs::setup()) + .nest("/", network::setup()) .layer(tracing_layer) } diff --git a/nyanpasu_service/src/server/routing/network.rs b/nyanpasu_service/src/server/routing/network.rs new file mode 100644 index 0000000..52ff37a --- /dev/null +++ b/nyanpasu_service/src/server/routing/network.rs @@ -0,0 +1,50 @@ +use axum::{http::StatusCode, routing::post, Json, Router}; +use nyanpasu_ipc::api::network::set_dns::{ + NetworkSetDnsReq, NetworkSetDnsRes, NETWORK_SET_DNS_ENDPOINT, +}; +#[cfg(target_os = "macos")] +use nyanpasu_utils::network::macos::{get_default_network_hardware_port, set_dns}; + +pub fn setup() -> Router { + Router::new().route(NETWORK_SET_DNS_ENDPOINT, post(network)) +} + +#[cfg(target_os = "macos")] +pub async fn network( + Json(mut req): Json>, +) -> (StatusCode, Json>) { + use std::borrow::Cow; + + use nyanpasu_ipc::api::RBuilder; + + let default_interface = match get_default_network_hardware_port() { + Ok(interface) => interface, + Err(e) => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(RBuilder::other_error(Cow::Owned(e.to_string()))), + ) + } + }; + let dns_servers = req + .dns_servers + .take() + .map(|v| v.into_iter().map(|v| v.into_owned()).collect::>()); + match set_dns(&default_interface, dns_servers) { + Ok(_) => (StatusCode::OK, Json(RBuilder::success(()))), + Err(e) => ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(RBuilder::other_error(Cow::Owned(e.to_string()))), + ), + } +} + +#[cfg(not(target_os = "macos"))] +pub async fn network( + Json(_req): Json>, +) -> (StatusCode, Json>) { + ( + StatusCode::NOT_IMPLEMENTED, + Json(RBuilder::not_implemented()), + ) +} diff --git a/nyanpasu_service/src/server/routing/status.rs b/nyanpasu_service/src/server/routing/status.rs index 2dc4b5e..de48c05 100644 --- a/nyanpasu_service/src/server/routing/status.rs +++ b/nyanpasu_service/src/server/routing/status.rs @@ -1,12 +1,17 @@ use std::borrow::Cow; -use axum::{http::StatusCode, Json}; +use axum::{http::StatusCode, routing::get, Json, Router}; use nyanpasu_ipc::api::{ - status::{RuntimeInfos, StatusRes, StatusResBody}, + status::{RuntimeInfos, StatusRes, StatusResBody, STATUS_ENDPOINT}, RBuilder, }; +pub fn setup() -> Router { + let router = Router::new(); + router.route(STATUS_ENDPOINT, get(status)) +} + pub async fn status() -> (StatusCode, Json>) { let instance = crate::server::CoreManager::global(); let status = instance.status();