From bb79748cce14311bc600932e8e5c4444274c6f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Sun, 27 Oct 2019 16:19:40 +0100 Subject: [PATCH 1/2] added device_id and exposed it through /api/0/info --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 1 + src/endpoints/mod.rs | 23 ++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index fcfed585..13d544a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -88,6 +88,7 @@ dependencies = [ "serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1211,6 +1212,9 @@ dependencies = [ name = "serde" version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "serde_derive" @@ -1448,6 +1452,15 @@ dependencies = [ "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "vcpkg" version = "0.2.7" @@ -1727,6 +1740,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unsafe-any 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f30360d7979f5e9c6e6cea48af192ea8fab4afb3cf72597154b8f08935bc9c7f" "checksum untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f" "checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" +"checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" "checksum vcpkg 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "33dd455d0f96e90a75803cfeb7f948768c08d70a6de9a8d2362461935698bf95" "checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" diff --git a/Cargo.toml b/Cargo.toml index 8c7b506c..d8d91066 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ fern = { version = "0.5", features = ["colored"] } toml = "0.5" gethostname = "0.2" regex = "1.0" +uuid = { version = "0.8", features = ["serde", "v4"] } [target.'cfg(target_os="android")'.dependencies] jni = { version = "0.5", default-features = false } diff --git a/src/endpoints/mod.rs b/src/endpoints/mod.rs index aee4d05b..2fce1f7a 100644 --- a/src/endpoints/mod.rs +++ b/src/endpoints/mod.rs @@ -1,3 +1,4 @@ +use std::fs; use std::path::PathBuf; use std::sync::Mutex; @@ -6,7 +7,9 @@ use rocket::response::{NamedFile}; use rocket::State; use rocket_contrib::json::JsonValue; use gethostname::gethostname; +use uuid::Uuid; +use crate::dirs; use crate::config::AWConfig; #[macro_export] @@ -60,6 +63,23 @@ fn root_favicon(state: State) -> Option { NamedFile::open(state.asset_path.join("favicon.ico")).ok() } +/// Retrieves the device ID, if none exists it generates one (using UUID v4) +fn get_device_id() -> String { + // TODO: Cache to avoid retrieving on every /info call + // TODO: How should these unwraps be removed? + // Should this be propagated into a 500 Internal Server Error? How? + // I chose get_data_dir over get_config_dir since the latter isn't yet supported on Android. + let mut path = dirs::get_data_dir().unwrap(); + path.push("device_id"); + if path.exists() { + fs::read_to_string(path).unwrap() + } else { + let uuid = Uuid::new_v4().to_hyphenated().to_string(); + fs::write(path, &uuid).unwrap(); + uuid + } +} + #[get("/")] fn server_info() -> JsonValue { let testing : bool; @@ -78,7 +98,8 @@ fn server_info() -> JsonValue { json!({ "hostname": hostname, "version": format!("aw-server-rust v{}", VERSION.unwrap_or("(unknown)")), - "testing": testing + "testing": testing, + "device_id": get_device_id(), }) } From 3f04d6b4826a04ca42dc21d0b66396176dbf86a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Sun, 27 Oct 2019 17:07:16 +0100 Subject: [PATCH 2/2] bumped version number to v0.8 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d8d91066..dc8cc244 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aw_server" -version = "0.1.0" +version = "0.8.0" authors = ["Johan Bjäreholt ", "Erik Bjäreholt "] edition = "2018" default-run = "aw-server-rust"