From b32cc7a9174811d6b6d8db79ec7209a2a0596308 Mon Sep 17 00:00:00 2001 From: Xiaopeng Han Date: Wed, 29 Mar 2023 02:14:39 +0800 Subject: [PATCH] Add simple dashboard to ztunnel admin (#475) * add dashboard * make gen * include file with macro --- src/admin.rs | 45 +++++++++++++++++++++++++++- src/assets/dashboard.html | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/assets/dashboard.html diff --git a/src/admin.rs b/src/admin.rs index 5a17380ed..f038869b1 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -25,7 +25,7 @@ use drain::Watch; use gperftools::heap_profiler::HEAP_PROFILER; #[cfg(feature = "gperftools")] use gperftools::profiler::PROFILER; -use hyper::{Body, Request, Response}; +use hyper::{header::HeaderValue, header::CONTENT_TYPE, Body, Request, Response}; use pprof::protos::Message; #[cfg(feature = "gperftools")] use tokio::fs::File; @@ -128,12 +128,55 @@ impl Service { ) .await), "/logging" => Ok(handle_logging(req).await), + "/" => Ok(handle_dashboard(req).await), _ => Ok(empty_response(hyper::StatusCode::NOT_FOUND)), } }) } } +async fn handle_dashboard(_req: Request) -> Response { + let apis = &[ + ( + "debug/pprof/profile", + "build profile using the pprof profiler (if supported)", + ), + ( + "debug/gprof/profile", + "build profile using the gperftools profiler (if supported)", + ), + ( + "debug/gprof/heap", + "collect heap profiling data (if supported)", + ), + ("quitquitquit", "shut down the server"), + ("config_dump", "dump the current Ztunnel configuration"), + ("logging", "query/changing logging levels"), + ]; + + let mut api_rows = String::new(); + + for (index, (path, description)) in apis.iter().enumerate() { + api_rows.push_str(&format!( + "{path}{description}\n", + row_class = if index % 2 == 1 { "gray" } else { "vert-space" }, + path = path, + description = description + )); + } + + let html_str = include_str!("./assets/dashboard.html"); + let html_str = html_str.replace("", &api_rows); + + let mut response = Response::new(Body::from(html_str)); + response.headers_mut().insert( + CONTENT_TYPE, + HeaderValue::from_static("text/html; charset=utf-8"), + ); + + response +} + fn x509_to_pem(x509: &X509) -> String { match x509.to_pem() { Err(e) => format!(""), diff --git a/src/assets/dashboard.html b/src/assets/dashboard.html new file mode 100644 index 000000000..9c4ebd569 --- /dev/null +++ b/src/assets/dashboard.html @@ -0,0 +1,62 @@ + + + +Ztunnel Admin + + + + + + + + + + + + +
CommandDescription
+ +