-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
076fc27
commit 0cbf000
Showing
12 changed files
with
279 additions
and
81 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod set_dns; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Vec<Cow<'n, IpAddr>>>, | ||
} | ||
|
||
pub type NetworkSetDnsRes<'a> = R<'a, ()>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
10 changes: 8 additions & 2 deletions
10
...su_service/src/server/routing/core/log.rs → nyanpasu_service/src/server/routing/logs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<NetworkSetDnsReq<'static>>, | ||
) -> (StatusCode, Json<NetworkSetDnsRes<'static>>) { | ||
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::<Vec<_>>()); | ||
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<NetworkSetDnsReq<'static>>, | ||
) -> (StatusCode, Json<NetworkSetDnsRes<'static>>) { | ||
( | ||
StatusCode::NOT_IMPLEMENTED, | ||
Json(RBuilder::not_implemented()), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters