From 534fadd6d44fbc1aebe5b31230b1c57fce3761e2 Mon Sep 17 00:00:00 2001 From: Albrecht Lohofener Date: Wed, 26 Mar 2025 15:23:08 +0100 Subject: [PATCH] Add API call get_openwrt_ip_addresses --- web-backend/Cargo.lock | 7 +++++++ web-backend/Cargo.toml | 3 ++- web-backend/src/main.rs | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/web-backend/Cargo.lock b/web-backend/Cargo.lock index a8fb8cf..b4c70bd 100644 --- a/web-backend/Cargo.lock +++ b/web-backend/Cargo.lock @@ -982,6 +982,12 @@ dependencies = [ "log", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.159" @@ -1178,6 +1184,7 @@ name = "qemu-openwrt-web-backend" version = "0.1.0" dependencies = [ "async-std", + "lazy_static", "serde", "serde_json", "tide", diff --git a/web-backend/Cargo.toml b/web-backend/Cargo.toml index 449dcb8..5e55401 100644 --- a/web-backend/Cargo.toml +++ b/web-backend/Cargo.toml @@ -7,4 +7,5 @@ edition = '2021' tide = "0.16.0" async-std = "1.10" serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" +lazy_static = "1.4" diff --git a/web-backend/src/main.rs b/web-backend/src/main.rs index 6176ac5..5603be5 100644 --- a/web-backend/src/main.rs +++ b/web-backend/src/main.rs @@ -2,6 +2,8 @@ // TODO: I expect that this code needs some refactoring use std::process::Command; +use std::sync::{Mutex}; +use lazy_static::lazy_static; use tide::{Request, Response, StatusCode, Next}; use serde::Serialize; use std::str; @@ -14,6 +16,9 @@ struct JsonResponse { combined_output: String, } +lazy_static! { + static ref run_mutex: Mutex = Mutex::new(0i32); +} // Middleware for adding CORS headers fn cors_middleware<'a>( req: Request<()>, @@ -40,6 +45,8 @@ async fn handle_options(_req: Request<()>) -> tide::Result { } async fn run_command(command: &str, args: &[&str]) -> JsonResponse { + let my_lock = run_mutex.lock().unwrap(); + let output = Command::new(command) .args(args) .output() @@ -96,6 +103,14 @@ async fn endpoint_factory_reset(_req: Request<()>) -> tide::Result { Ok(res) } +async fn endpoint_get_ip_addr(_req: Request<()>) -> tide::Result { + let response_data = run_command("/run/qemu_qmp.sh", &["-c", "ip -json addr"]).await; + let mut res = Response::new(StatusCode::Ok); + res.set_body(serde_json::to_string(&response_data)?); + res.insert_header("Content-Type", "application/json"); + Ok(res) +} + fn main() -> tide::Result<()> { async_std::task::block_on(async { println!("*******************************************"); @@ -113,6 +128,7 @@ fn main() -> tide::Result<()> { app.at("/get_openwrt_info").get(endpoint_get_openwrt_info).options(handle_options); app.at("/get_container_info").get(endpoint_get_container_info).options(handle_options); app.at("/factory_reset").get(endpoint_factory_reset).options(handle_options); + app.at("/get_openwrt_ip_addresses").get(endpoint_get_ip_addr).options(handle_options); // Start the server at localhost:8080 app.listen("127.0.0.1:8080").await?;