From b6fde857a74b24cbc71631468b13656f01e370c7 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Thu, 28 May 2020 20:25:25 +0200 Subject: [PATCH] Added version check to diagnostics - Added a version check based upon the github api information. --- src/api/admin.rs | 44 +++++++++- src/static/templates/admin/diagnostics.hbs | 94 ++++++++++++++++------ 2 files changed, 112 insertions(+), 26 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 8678e0d9..3eeffe05 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -319,6 +319,24 @@ pub struct WebVaultVersion { version: String, } +fn get_github_api(url: &str) -> Result { + use reqwest::{header::USER_AGENT, blocking::Client}; + let github_api = Client::builder().build()?; + + let res = github_api + .get(url) + .header(USER_AGENT, "Bitwarden_RS") + .send()?; + + let res_status = res.status(); + if res_status != 200 { + error!("Could not retrieve '{}', response code: {}", url, res_status); + } + + let value: Value = res.error_for_status()?.json()?; + Ok(value) +} + #[get("/diagnostics")] fn diagnostics(_token: AdminToken, _conn: DbConn) -> ApiResult> { use std::net::ToSocketAddrs; @@ -331,10 +349,31 @@ fn diagnostics(_token: AdminToken, _conn: DbConn) -> ApiResult> { let github_ips = ("github.com", 0).to_socket_addrs().map(|mut i| i.next()); let dns_resolved = match github_ips { - Ok(Some(a)) => a.ip().to_string() , + Ok(Some(a)) => a.ip().to_string(), _ => "Could not resolve domain name.".to_string(), }; + let bitwarden_rs_releases = get_github_api("https://api.github.com/repos/dani-garcia/bitwarden_rs/releases/latest"); + let latest_release = match &bitwarden_rs_releases { + Ok(j) => j["tag_name"].as_str().unwrap(), + _ => "-", + }; + + let bitwarden_rs_commits = get_github_api("https://api.github.com/repos/dani-garcia/bitwarden_rs/commits/master"); + let mut latest_commit = match &bitwarden_rs_commits { + Ok(j) => j["sha"].as_str().unwrap(), + _ => "-", + }; + if latest_commit.len() >= 8 { + latest_commit = &latest_commit[..8]; + } + + let bw_web_builds_releases = get_github_api("https://api.github.com/repos/dani-garcia/bw_web_builds/releases/latest"); + let latest_web_build = match &bw_web_builds_releases { + Ok(j) => j["tag_name"].as_str().unwrap(), + _ => "-", + }; + let dt = Utc::now(); let server_time = dt.format("%Y-%m-%d %H:%M:%S").to_string(); @@ -342,6 +381,9 @@ fn diagnostics(_token: AdminToken, _conn: DbConn) -> ApiResult> { "dns_resolved": dns_resolved, "server_time": server_time, "web_vault_version": web_vault_version.version, + "latest_release": latest_release, + "latest_commit": latest_commit, + "latest_web_build": latest_web_build.replace("v", ""), }); let text = AdminTemplateData::diagnostics(diagnostics_json).render()?; diff --git a/src/static/templates/admin/diagnostics.hbs b/src/static/templates/admin/diagnostics.hbs index dbf82c1e..b5eca6ce 100644 --- a/src/static/templates/admin/diagnostics.hbs +++ b/src/static/templates/admin/diagnostics.hbs @@ -6,14 +6,30 @@
-
Server Installed
+
Server Installed + Ok + Update + Unknown +
{{version}}
-
Web Installed
+
Server Latest
+
+ {{diagnostics.latest_release}}-{{diagnostics.latest_commit}} +
+
Web Installed + Ok + Update + Unknown +
{{diagnostics.web_vault_version}}
+
Web Latest
+
+ {{diagnostics.latest_web_build}} +
@@ -45,29 +61,57 @@ \ No newline at end of file