From 864e8ea394de6c5d9a3ebc7bc5140a5789be4e55 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 25 Dec 2023 09:45:55 -0500 Subject: [PATCH 1/3] helper: strip ANSI escape sequences at beginning of PTY start --- src/helper.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/helper.rs b/src/helper.rs index ababd9b..2666307 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -271,6 +271,18 @@ impl Helper { fn read_pty_xmrig(output_parse: Arc>, output_pub: Arc>, reader: Box) { use std::io::BufRead; let mut stdout = std::io::BufReader::new(reader).lines(); + + // Run a ANSI escape sequence filter for the first few lines. + for (i, line) in stdout.next().enumerate() { + let Some(Ok(line)) = line else { continue; } + let line = strip_ansi_escapes::strip_str(line); + if let Err(e) = writeln!(lock!(output_parse), "{}", line) { error!("XMRig PTY Parse | Output error: {}", e); } + if let Err(e) = writeln!(lock!(output_pub), "{}", line) { error!("XMRig PTY Pub | Output error: {}", e); } + if i > 20 { + break; + } + } + while let Some(Ok(line)) = stdout.next() { // println!("{}", line); // For debugging. if let Err(e) = writeln!(lock!(output_parse), "{}", line) { error!("XMRig PTY Parse | Output error: {}", e); } @@ -281,6 +293,18 @@ impl Helper { fn read_pty_p2pool(output_parse: Arc>, output_pub: Arc>, reader: Box, gupax_p2pool_api: Arc>) { use std::io::BufRead; let mut stdout = std::io::BufReader::new(reader).lines(); + + // Run a ANSI escape sequence filter for the first few lines. + for (i, line) in stdout.next().enumerate() { + let Some(Ok(line)) = line else { continue; } + let line = strip_ansi_escapes::strip_str(line); + if let Err(e) = writeln!(lock!(output_parse), "{}", line) { error!("P2Pool PTY Parse | Output error: {}", e); } + if let Err(e) = writeln!(lock!(output_pub), "{}", line) { error!("P2Pool PTY Pub | Output error: {}", e); } + if i > 20 { + break; + } + } + while let Some(Ok(line)) = stdout.next() { // println!("{}", line); // For debugging. if P2POOL_REGEX.payout.is_match(&line) { From 91e5cddb8b8ce333790f577eddf2546d4b7ae070 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 25 Dec 2023 09:52:24 -0500 Subject: [PATCH 2/3] `for` -> `while` --- Cargo.lock | 36 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/helper.rs | 14 ++++++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a3a801b..3e52081 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2238,6 +2238,7 @@ dependencies = [ "serde", "serde_json", "static_vcruntime", + "strip-ansi-escapes", "strsim", "sudo", "sysinfo", @@ -4640,6 +4641,15 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e08d8363704e6c71fc928674353e6b7c23dcea9d82d7012c8faf2a3a025f8d0" +[[package]] +name = "strip-ansi-escapes" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55ff8ef943b384c414f54aefa961dd2bd853add74ec75e7ac74cf91dba62bcfa" +dependencies = [ + "vte", +] + [[package]] name = "strsim" version = "0.10.0" @@ -5839,6 +5849,12 @@ dependencies = [ "log", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "vcpkg" version = "0.2.15" @@ -5869,6 +5885,26 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +[[package]] +name = "vte" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5022b5fbf9407086c180e9557be968742d839e68346af7792b8592489732197" +dependencies = [ + "utf8parse", + "vte_generate_state_changes", +] + +[[package]] +name = "vte_generate_state_changes" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d257817081c7dffcdbab24b9e62d2def62e2ff7d00b1c20062551e6cccc145ff" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "waker-fn" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 83bcb7b..ef603aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,7 @@ tor-rtcompat = "0.9.0" walkdir = "2.3.3" zeroize = "1.6.0" strsim = "0.10.0" +strip-ansi-escapes = "0.2.0" # Unix dependencies [target.'cfg(unix)'.dependencies] diff --git a/src/helper.rs b/src/helper.rs index 2666307..4a6b6bc 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -273,15 +273,18 @@ impl Helper { let mut stdout = std::io::BufReader::new(reader).lines(); // Run a ANSI escape sequence filter for the first few lines. - for (i, line) in stdout.next().enumerate() { - let Some(Ok(line)) = line else { continue; } + let mut i = 0; + while let Some(Ok(line)) = stdout.next() { let line = strip_ansi_escapes::strip_str(line); if let Err(e) = writeln!(lock!(output_parse), "{}", line) { error!("XMRig PTY Parse | Output error: {}", e); } if let Err(e) = writeln!(lock!(output_pub), "{}", line) { error!("XMRig PTY Pub | Output error: {}", e); } if i > 20 { break; + } else { + i += 1; } } + drop(i); while let Some(Ok(line)) = stdout.next() { // println!("{}", line); // For debugging. @@ -295,15 +298,18 @@ impl Helper { let mut stdout = std::io::BufReader::new(reader).lines(); // Run a ANSI escape sequence filter for the first few lines. - for (i, line) in stdout.next().enumerate() { - let Some(Ok(line)) = line else { continue; } + let mut i = 0; + while let Some(Ok(line)) = stdout.next() { let line = strip_ansi_escapes::strip_str(line); if let Err(e) = writeln!(lock!(output_parse), "{}", line) { error!("P2Pool PTY Parse | Output error: {}", e); } if let Err(e) = writeln!(lock!(output_pub), "{}", line) { error!("P2Pool PTY Pub | Output error: {}", e); } if i > 20 { break; + } else { + i += 1; } } + drop(i); while let Some(Ok(line)) = stdout.next() { // println!("{}", line); // For debugging. From 8919e2dd573c19307ceb822b9baebbf7cae29d6e Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 25 Dec 2023 12:22:49 -0500 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b487b8..8be7e76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v1.3.4 # Fixes * Domain parsing is more relaxed, allows subdomains with longer TLDs (thanks @soupslurpr [#67](https://github.com/hinto-janai/gupax/pull/67)) +* ANSI escape sequences in Windows P2Pool/XMRig terminal output ([#71](https://github.com/hinto-janai/gupax/pull/71)) ## Bundled Versions * [`P2Pool v3.9`](https://github.com/SChernykh/p2pool/releases/tag/v3.9)