From e52d0ef7e00426dea812a25b471a51e8e78696b5 Mon Sep 17 00:00:00 2001
From: hinto-janaiyo <hinto.janaiyo@protonmail.com>
Date: Sat, 24 Dec 2022 10:28:24 -0500
Subject: [PATCH] update: be more lenient on new binary filename match

Instead of a single "true" filename when walking dirs searching
for the newly downloaded (Gupax|P2Pool|XMRig), allow for multiple
valid names as long as they _seem_ correct AND are files, e.g:

GUPAX|Gupax|gupax (.exe)
P2POOL|P2Pool|P2pool|p2pool (.exe)
XMRIG|XMRig|Xmrig|xmrig (.exe)

If the packaging naming schemes change for any of these,
the update code will be able to actually handle it.
---
 README.md     |  4 +--
 src/README.md | 50 +++++++++++++++++++++++++-----------
 src/status.rs |  2 +-
 src/update.rs | 70 ++++++++++++++++++++++++++++++++++++++++-----------
 4 files changed, 94 insertions(+), 32 deletions(-)

diff --git a/README.md b/README.md
index ca8c170..7190b17 100644
--- a/README.md
+++ b/README.md
@@ -552,7 +552,7 @@ The latest versions are downloaded using the GitHub API.
 * P2Pool [`https://github.com/SChernykh/p2pool`](https://github.com/SChernykh/p2pool)
 * XMRig [`https://github.com/xmrig/xmrig`](https://github.com/xmrig/xmrig)
 
-GitHub's API blocks request that do not have an HTTP `User-Agent` header. [Gupax uses a random recent version of a `Wget/Curl` user-agent.](https://github.com/hinto-janaiyo/gupax/blob/2b80aa027728ddd193bac2e77caa5ddb4323f8fd/src/update.rs#L134)
+GitHub's API blocks request that do not have an HTTP `User-Agent` header. [Gupax uses a random recent version of a `Wget/Curl` user-agent.](https://github.com/hinto-janaiyo/gupax/blob/e6bf49b309c64d29e50c0a1a185fcf0ebc05e7c7/src/update.rs#L134)
 
 ---
 
@@ -573,7 +573,7 @@ Although Gupax uses a temporary folder (`gupax_update_[A-Za-z0-9]`) to store tem
 ### How much memory does Gupax use?
 Gupax itself uses around 100-300 megabytes of memory.
 
-Gupax also holds up to [500,000 bytes](https://github.com/hinto-janaiyo/gupax/blob/2b80aa027728ddd193bac2e77caa5ddb4323f8fd/src/helper.rs#L63) of log data from `P2Pool/XMRig` to display in the GUI terminals. These logs are reset once over capacity which takes around 1-4 hours.
+Gupax also holds up to [500,000 bytes](https://github.com/hinto-janaiyo/gupax/blob/e6bf49b309c64d29e50c0a1a185fcf0ebc05e7c7/src/helper.rs#L59) of log data from `P2Pool/XMRig` to display in the GUI terminals. These logs are reset once over capacity which takes around 1-4 hours.
 
 Memory usage should *never* be above 500~ megabytes. If you see Gupax using more than this, please send a bug report.
 
diff --git a/src/README.md b/src/README.md
index e43d15f..98d1701 100644
--- a/src/README.md
+++ b/src/README.md
@@ -78,26 +78,46 @@ Main [App] outer frame (default: [1280.0, 960.0], 4:3 aspect ratio)
 This is the internal naming scheme used by Gupax when updating/creating default folders/etc:
 
 Windows:
-- Gupax: `Gupax.exe`
-- P2Pool: `P2Pool\p2pool.exe`
-- XMRig: `XMRig\xmrig.exe`
+```
+Gupax\
+├─ Gupax.exe
+├─ P2Pool\
+│  ├─ p2pool.exe
+├─ XMRig\
+   ├─ xmrig.exe
+```
 
-macOS:
-- Gupax: `Gupax.app/Contents/MacOS/gupax` (Gupax is packaged as an `.app` on macOS)
-- P2Pool: `p2pool/p2pool`
-- XMRig: `xmrig/xmrig`
+macOS (Gupax is packaged as an `.app` on macOS):
+```
+Gupax.app/Contents/MacOS/
+├─ gupax
+├─ p2pool/
+│  ├─ p2pool
+├─ xmrig/
+   ├─ xmrig
+```
 
 Linux:
-- Gupax: `gupax`
-- P2Pool: `p2pool/p2pool`
-- XMRig: `xmrig/xmrig`
+```
+gupax/
+├─ gupax
+├─ p2pool/
+│  ├─ p2pool
+├─ xmrig/
+   ├─ xmrig
+```
 
-These have to be packaged exactly with these names because the update code is case-sensitive. If an exact match is not found, it will error.
+When Gupax updates, it walks the directories of the extracted `zip/tar` searching for a valid file. These are the valid filenames Gupax will match against and assume is the new binary we're looking for:
+- `[GUPAX, Gupax, gupax]`
+- `[P2POOL, P2Pool, P2pool, p2pool]`
+- `[XMRIG, XMRig, Xmrig, xmrig]`
 
-Package naming schemes:
-- `gupax` - gupax-vX.X.X-(windows|macos|linux)-x64(standalone|bundle).(zip|tar.gz)
-- `p2pool` - p2pool-vX.X.X-(windows|macos|linux)-x64.(zip|tar.gz)
-- `xmrig` - xmrig-X.X.X-(msvc-win64|macos-x64|linux-static-x64).(zip|tar.gz)
+Windows versions of Gupax also need the file to end with `.exe`.
+
+The actual `zip/tar` matching is static, however. They have to be packaged exactly with the following naming scheme. If an exact match is not found, it will error:
+- `gupax-vX.X.X-(windows|macos|linux)-x64-(bundle|standalone).(zip|tar.gz)`
+- `p2pool-vX.X.X-(windows|macos|linux)-x64.(zip|tar.gz)`
+- `xmrig-X.X.X-(msvc-win64|macos-x64|linux-static-x64).(zip|tar.gz)`
 
 Exceptions (there are always exceptions...):
 - XMRig doesn't have a [v], so it is [xmrig-6.18.0-...]
diff --git a/src/status.rs b/src/status.rs
index d6699f5..ea19180 100644
--- a/src/status.rs
+++ b/src/status.rs
@@ -38,7 +38,7 @@ pub struct Status {}
 impl Status {
 pub fn show(sys: &Arc<Mutex<Sys>>, p2pool_api: &Arc<Mutex<PubP2poolApi>>, xmrig_api: &Arc<Mutex<PubXmrigApi>>, p2pool_img: &Arc<Mutex<ImgP2pool>>, xmrig_img: &Arc<Mutex<ImgXmrig>>, p2pool_alive: bool, xmrig_alive: bool, max_threads: usize, width: f32, height: f32, _ctx: &egui::Context, ui: &mut egui::Ui) {
 	let width = (width/3.0)-(SPACE*1.666);
-	let min_height = height/1.14;
+	let min_height = height/1.1;
 	let height = height/25.0;
 	ui.horizontal(|ui| {
 	// [Gupax]
diff --git a/src/update.rs b/src/update.rs
index 356e638..1f2340f 100644
--- a/src/update.rs
+++ b/src/update.rs
@@ -119,20 +119,60 @@ const XMRIG_BINARY: &str = "xmrig.exe";
 #[cfg(target_family = "unix")]
 const XMRIG_BINARY: &str = "xmrig";
 
+// --- Valid Windows Gupax
 #[cfg(target_os = "windows")]
-const ACCEPTABLE_GUPAX: [&str; 3] = ["GUPAX.exe", "Gupax.exe", "gupax.exe"];
+pub mod valid_gupax {
+	pub const VALID_GUPAX_1: &str = "GUPAX.exe";
+	pub const VALID_GUPAX_2: &str = "Gupax.exe";
+	pub const VALID_GUPAX_3: &str = "gupax.exe";
+}
+// --- Valid Unix Gupax
 #[cfg(target_family = "unix")]
-const ACCEPTABLE_GUPAX: [&str; 3] = ["GUPAX", "Gupax", "gupax"];
+pub mod valid_gupax {
+	pub const VALID_GUPAX_1: &str = "GUPAX";
+	pub const VALID_GUPAX_2: &str = "Gupax";
+	pub const VALID_GUPAX_3: &str = "gupax";
+}
+use crate::valid_gupax::*;
+const VALID_GUPAX: [&str; 3] = [VALID_GUPAX_1, VALID_GUPAX_2, VALID_GUPAX_3];
 
+// --- Valid Windows XMRig
 #[cfg(target_os = "windows")]
-const ACCEPTABLE_XMRIG: [&str; 4] = ["XMRIG.exe", "XMRig.exe", "Xmrig.exe", "xmrig.exe"];
+pub mod valid_xmrig {
+	pub const VALID_XMRIG_1: &str = "XMRIG.exe";
+	pub const VALID_XMRIG_2: &str = "XMRig.exe";
+	pub const VALID_XMRIG_3: &str = "Xmrig.exe";
+	pub const VALID_XMRIG_4: &str = "xmrig.exe";
+}
+// --- Valid Unix XMRig
 #[cfg(target_family = "unix")]
-const ACCEPTABLE_XMRIG: [&str; 4] = ["XMRIG", "XMRig", "Xmrig", "xmrig"];
+pub mod valid_xmrig {
+	pub const VALID_XMRIG_1: &str = "XMRIG";
+	pub const VALID_XMRIG_2: &str = "XMRig";
+	pub const VALID_XMRIG_3: &str = "Xmrig";
+	pub const VALID_XMRIG_4: &str = "xmrig";
+}
+use crate::valid_xmrig::*;
+const VALID_XMRIG: [&str; 4] = [VALID_XMRIG_1, VALID_XMRIG_2, VALID_XMRIG_3, VALID_XMRIG_4];
 
+// --- Valid Windows P2Pool
 #[cfg(target_os = "windows")]
-const ACCEPTABLE_P2POOL: [&str; 4] = ["P2POOL.exe", "P2Pool.exe", "P2pool.exe", "p2pool.exe"];
+pub mod valid_p2pool {
+	pub const VALID_P2POOL_1: &str = "P2POOL.exe";
+	pub const VALID_P2POOL_2: &str = "P2Pool.exe";
+	pub const VALID_P2POOL_3: &str = "P2pool.exe";
+	pub const VALID_P2POOL_4: &str = "p2pool.exe";
+}
+// --- Valid Unix P2Pool
 #[cfg(target_family = "unix")]
-const ACCEPTABLE_P2POOL: [&str; 4] = ["P2POOL", "P2Pool", "P2pool", "p2pool"];
+pub mod valid_p2pool {
+	pub const VALID_P2POOL_1: &str = "P2POOL";
+	pub const VALID_P2POOL_2: &str = "P2Pool";
+	pub const VALID_P2POOL_3: &str = "P2pool";
+	pub const VALID_P2POOL_4: &str = "p2pool";
+}
+use crate::valid_p2pool::*;
+const VALID_P2POOL: [&str; 4] = [VALID_P2POOL_1, VALID_P2POOL_2, VALID_P2POOL_3, VALID_P2POOL_4];
 
 // Some fake Curl/Wget user-agents because GitHub API requires one and a Tor browser
 // user-agent might be fingerprintable without all the associated headers.
@@ -178,7 +218,7 @@ pub fn check_p2pool_path(path: &str) -> bool {
 		Some(p) => p,
 		None => { error!("Couldn't get P2Pool file name"); return false; },
 	};
-	path == ACCEPTABLE_P2POOL[0] || path == ACCEPTABLE_P2POOL[1] || path == ACCEPTABLE_P2POOL[2] || path == ACCEPTABLE_P2POOL[3]
+	path == VALID_P2POOL[0] || path == VALID_P2POOL[1] || path == VALID_P2POOL[2] || path == VALID_P2POOL[3]
 }
 
 pub fn check_xmrig_path(path: &str) -> bool {
@@ -190,7 +230,7 @@ pub fn check_xmrig_path(path: &str) -> bool {
 		Some(p) => p,
 		None => { error!("Couldn't get XMRig file name"); return false; },
 	};
-	path == ACCEPTABLE_XMRIG[0] || path == ACCEPTABLE_XMRIG[1] || path == ACCEPTABLE_XMRIG[2] || path == ACCEPTABLE_XMRIG[3]
+	path == VALID_XMRIG[0] || path == VALID_XMRIG[1] || path == VALID_XMRIG[2] || path == VALID_XMRIG[3]
 }
 
 //---------------------------------------------------------------------------------------------------- Update struct/impl
@@ -323,7 +363,7 @@ impl Update {
 			info!("Update | Using P2Pool path: [{}]", p2pool_path.display());
 		} else {
 			warn!("Update | Aborting update, incorrect P2Pool path: [{}]", file);
-			let text = format!("Provided P2Pool path seems incorrect. Not starting update for safety.\nTry one of these: {:?}", ACCEPTABLE_P2POOL);
+			let text = format!("Provided P2Pool path seems incorrect. Not starting update for safety.\nTry one of these: {:?}", VALID_P2POOL);
 			error_state.set(text, ErrorFerris::Error, ErrorButtons::Okay);
 			return;
 		}
@@ -347,7 +387,7 @@ impl Update {
 			info!("Update | Using XMRig path: [{}]", xmrig_path.display());
 		} else {
 			warn!("Update | Aborting update, incorrect XMRig path: [{}]", file);
-			let text = format!("Provided XMRig path seems incorrect. Not starting update for safety.\nTry one of these: {:?}", ACCEPTABLE_XMRIG);
+			let text = format!("Provided XMRig path seems incorrect. Not starting update for safety.\nTry one of these: {:?}", VALID_XMRIG);
 			error_state.set(text, ErrorFerris::Error, ErrorButtons::Okay);
 			return;
 		}
@@ -665,12 +705,14 @@ impl Update {
 			if ! entry.file_type().is_file() { continue }
 			let basename = entry.file_name().to_str().ok_or_else(|| anyhow!("WalkDir basename failed"))?;
 			match basename {
-				GUPAX_BINARY|P2POOL_BINARY|XMRIG_BINARY => {
+				VALID_GUPAX_1|VALID_GUPAX_2|VALID_GUPAX_3|
+				VALID_P2POOL_1|VALID_P2POOL_2|VALID_P2POOL_3|VALID_P2POOL_4|
+				VALID_XMRIG_1|VALID_XMRIG_2|VALID_XMRIG_3|VALID_XMRIG_4 => {
 					found = true;
 					let name = match basename {
-						GUPAX_BINARY  => Gupax,
-						P2POOL_BINARY => P2pool,
-						_  => Xmrig,
+						VALID_GUPAX_1|VALID_GUPAX_2|VALID_GUPAX_3 => Gupax,
+						VALID_P2POOL_1|VALID_P2POOL_2|VALID_P2POOL_3|VALID_P2POOL_4 => P2pool,
+						_ => Xmrig,
 					};
 					let path = match name {
 						Gupax  => update.lock().unwrap().path_gupax.clone(),