Compare commits

...
1 Commits
Author SHA1 Message Date
noahandClaude Opus 4.8 282f0b195c GUI round 12 (#13): editable per-firmware default scan credentials
Adds a per-firmware default-login editor so the operator can set the discovery
credentials each firmware is probed with, rather than a single global pair.

- credentials.rs: a per-firmware store (firmware-credentials.txt, 0600, one
  "name<TAB>user<TAB>pass" line per firmware) keyed by the asic-rs firmware
  registry's Display name -- the same key the factory uses for
  with_firmware_discovery_auth. A firmware_default_creds() table seeds the editor
  with each firmware's shipped default (mirrors the asic-rs backends' default_auth
  constants; asic-rs does not expose them at the entry level).
- configured_factory() precedence is now: a per-firmware override wins; else the
  single global scan credential (unchanged back-compat) applies to every backend;
  else the backend's own default_auth. Nothing saved -> plain MinerFactory::new().
- UI: the "Set scan credentials..." dialog gains a "Per-firmware defaults..."
  button opening an editor that lists every firmware in the compiled-in registry
  (read at runtime, not hard-coded) with editable user/pass fields prefilled from
  the saved override or the shipped default. Save persists only rows that differ
  from the shipped default (so defaults evolve with asic-rs and the file stays
  minimal); Reset to defaults clears all overrides.

Verified under Xvfb: the editor lists all 16 registry firmwares with correct
default logins (AntMiner root/root, WhatsMiner admin/admin, VNish blank/admin,
UMC OS blank/letmein, ...). Round-trip/permissions/precedence covered by a unit
test (folded into the existing HOME-scoped credentials test to avoid a parallel
HOME race); self-test still passes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y8h447BPyJR4fioYZvQ9bh
2026-09-20 00:06:37 -06:00
2 changed files with 278 additions and 9 deletions
+141 -8
View File
@@ -5,6 +5,7 @@
//! is the one place that fans a saved username/password out across the whole
//! firmware registry, so `scan`'s callers never have to think about it.
use std::collections::HashMap;
use std::io::Write;
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
@@ -14,6 +15,10 @@ use asic_rs::core::traits::miner::MinerAuth;
use crate::scan::config_path;
pub const CREDENTIALS_FILE: &str = "scan-credentials.txt";
/// Per-firmware default logins the operator can override (round 12 #13). One
/// `firmware-name<TAB>user<TAB>pass` line per firmware, 0600 like the others.
/// Keyed by the firmware registry's Display name (see `firmware_default_creds`).
pub const FIRMWARE_CREDENTIALS_FILE: &str = "firmware-credentials.txt";
/// SSH login for the control board (used by the PIC probe; distinct from the
/// web/API scan credentials above -- miners' SSH root login is usually not the
/// same account as the web digest-auth login).
@@ -87,16 +92,115 @@ pub fn clear_credentials() -> std::io::Result<()> {
}
}
/// Build a `MinerFactory` with the saved credentials (if any) applied as
/// discovery auth for every firmware backend, so scans, refreshes, config
/// actions, and firmware flashes all authenticate against a non-default
/// login the same way. With no saved credentials this is plain `MinerFactory::new()`
/// and every backend falls back to its own default auth.
/// The stock/default login each firmware ships with, keyed by the firmware
/// registry's Display name (round 12 #13). These seed the per-firmware editor and
/// let it tell an override from an unchanged default; they mirror the
/// `default_auth()` constants in the vendored asic-rs backends (a single
/// best-guess per registry entry, since a stock entry can span several backend
/// versions). asic-rs does not expose these at the entry level, so they are
/// duplicated here; a firmware absent from this table defaults to blank.
pub fn firmware_default_creds() -> &'static [(&'static str, &'static str, &'static str)] {
&[
("AntMiner Stock", "root", "root"),
("WhatsMiner Stock", "admin", "admin"),
("Braiins", "root", "root"),
("VNish", "", "admin"),
("LuxOS", "root", "root"),
("UMC OS", "", "letmein"),
("Marathon", "root", "root"),
("VolcMiner Stock", "root", "ltc@dog"),
("Elphapex Stock", "root", "root"),
("FutureBit Stock", "futurebit", "futurebit123"),
("SealMiner Stock", "seal", "seal"),
("Auradine Stock", "admin", "admin"),
("AvalonMiner Stock", "", ""),
("Nerdaxe Stock", "", ""),
("Proto Stock", "", ""),
("Bitaxe Stock", "", ""),
]
}
/// The shipped default login for a firmware, or `("","")` if unknown.
pub fn firmware_default_for(name: &str) -> (String, String) {
firmware_default_creds()
.iter()
.find(|(n, _, _)| *n == name)
.map(|(_, u, p)| (u.to_string(), p.to_string()))
.unwrap_or_default()
}
/// Load the operator's per-firmware credential overrides, keyed by firmware
/// Display name. Malformed lines are skipped.
pub fn load_firmware_credentials() -> HashMap<String, (String, String)> {
let mut map = HashMap::new();
let Ok(s) = std::fs::read_to_string(config_path(FIRMWARE_CREDENTIALS_FILE)) else {
return map;
};
for line in s.lines() {
let mut parts = line.splitn(3, '\t');
let (Some(name), user) = (parts.next(), parts.next()) else {
continue;
};
if name.is_empty() {
continue;
}
let user = user.unwrap_or("").to_string();
let pass = parts.next().unwrap_or("").to_string();
map.insert(name.to_string(), (user, pass));
}
map
}
/// Save the per-firmware credential overrides (0600), sorted by name for a
/// stable file. An empty map clears the file.
pub fn save_firmware_credentials(map: &HashMap<String, (String, String)>) -> std::io::Result<()> {
if map.is_empty() {
return clear_firmware_credentials();
}
let p = config_path(FIRMWARE_CREDENTIALS_FILE);
if let Some(dir) = p.parent() {
std::fs::create_dir_all(dir)?;
}
let mut f = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o600)
.open(&p)?;
f.set_permissions(std::fs::Permissions::from_mode(0o600))?;
let mut names: Vec<&String> = map.keys().collect();
names.sort();
for name in names {
let (user, pass) = &map[name];
f.write_all(format!("{name}\t{user}\t{pass}\n").as_bytes())?;
}
Ok(())
}
/// Remove the per-firmware overrides file, if any. Not an error if none exists.
pub fn clear_firmware_credentials() -> std::io::Result<()> {
clear_creds_file(FIRMWARE_CREDENTIALS_FILE)
}
/// Build a `MinerFactory` with saved credentials applied as discovery auth.
/// Precedence per firmware backend: a per-firmware override (round 12 #13) wins;
/// otherwise the single global scan credential (if set) applies to every backend;
/// otherwise the backend falls back to its own default auth. With nothing saved
/// this is plain `MinerFactory::new()`.
pub fn configured_factory() -> MinerFactory {
let mut factory = MinerFactory::new();
if let Some((user, pass)) = load_credentials() {
for fw in asic_rs::factory::default_firmware_registry() {
factory = factory.with_firmware_discovery_auth(fw.as_ref(), MinerAuth::new(user.clone(), pass.clone()));
let global = load_credentials();
let per_fw = load_firmware_credentials();
if global.is_none() && per_fw.is_empty() {
return factory;
}
for fw in asic_rs::factory::default_firmware_registry() {
let cred = per_fw
.get(&fw.to_string())
.cloned()
.or_else(|| global.clone());
if let Some((user, pass)) = cred {
factory = factory.with_firmware_discovery_auth(fw.as_ref(), MinerAuth::new(user, pass));
}
}
factory
@@ -149,6 +253,35 @@ mod tests {
assert_eq!(load_credentials(), None, "cleared -> None");
clear_credentials().unwrap(); // idempotent: clearing an already-clear store is not an error
// Per-firmware overrides (round 12 #13). Kept in this same test because
// HOME is process-global: a second #[test] mutating it would race under
// cargo's default parallel runner.
assert!(load_firmware_credentials().is_empty(), "no file -> empty map");
let mut fw = HashMap::new();
fw.insert("WhatsMiner Stock".to_string(), ("super".to_string(), "sup3r".to_string()));
fw.insert("VNish".to_string(), ("".to_string(), "hunter2".to_string()));
save_firmware_credentials(&fw).unwrap();
// 0600, and it round-trips (an empty username survives the tab format).
let fwpath = config_path(FIRMWARE_CREDENTIALS_FILE);
assert_eq!(
std::fs::metadata(&fwpath).unwrap().permissions().mode() & 0o777,
0o600,
"per-firmware creds file must be 0600"
);
let back = load_firmware_credentials();
assert_eq!(back.get("WhatsMiner Stock"), Some(&("super".into(), "sup3r".into())));
assert_eq!(back.get("VNish"), Some(&("".into(), "hunter2".into())));
assert_eq!(back.len(), 2);
// Shipped-default lookup: known firmware resolves, unknown blanks.
assert_eq!(firmware_default_for("AntMiner Stock"), ("root".into(), "root".into()));
assert_eq!(firmware_default_for("nonexistent"), (String::new(), String::new()));
// Overrides + no global still builds a factory.
let _ = configured_factory();
// Saving an empty map clears the file (no stale overrides linger).
save_firmware_credentials(&HashMap::new()).unwrap();
assert!(load_firmware_credentials().is_empty(), "empty save -> cleared");
clear_firmware_credentials().unwrap(); // idempotent
match prev {
Some(v) => std::env::set_var("HOME", v),
None => std::env::remove_var("HOME"),
+137 -1
View File
@@ -72,6 +72,12 @@ const ID_FLASH_CANCEL: i32 = 745;
const ID_CREDS_SAVE: i32 = 746;
const ID_CREDS_CLEAR: i32 = 747;
const ID_CREDS_CANCEL: i32 = 748;
// Per-firmware default-credentials editor (round 12 #13), opened from the scan
// credentials dialog.
const ID_FWCREDS_OPEN: i32 = 749;
const ID_FWCREDS_SAVE: i32 = 750;
const ID_FWCREDS_RESET: i32 = 751;
const ID_FWCREDS_CANCEL: i32 = 752;
// Column-header right-click menu (Excel-style).
const ID_HDR_SORT_ASC: i32 = 760;
const ID_HDR_SORT_DESC: i32 = 761;
@@ -569,6 +575,10 @@ pub struct App {
creds_dialog: Widget,
creds_user_input: Widget,
creds_pass_input: Widget,
// Per-firmware default-credentials editor (round 12 #13): the dialog plus one
// (firmware name, user field, pass field) per registry firmware.
fwcreds_dialog: Widget,
fwcreds_rows: Vec<(String, Widget, Widget)>,
// Background access (lock-state) probe.
access_probe: Option<scan::AccessProbeHandle>,
// Background SSH PIC probe + its credentials dialog.
@@ -840,6 +850,8 @@ pub fn build(frame: Widget) -> App {
creds_dialog: frame,
creds_user_input: frame,
creds_pass_input: frame,
fwcreds_dialog: frame,
fwcreds_rows: Vec::new(),
access_probe: None,
pic_probe: None,
pending_pic_targets: Vec::new(),
@@ -1814,6 +1826,19 @@ impl App {
pass_input.set_min_size(200, -1);
pass_row.add(pass_input, 1, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 6);
root.add_sizer(pass_row, 0, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 6);
// The single credential above is applied to every firmware that has no
// per-firmware override; the button below edits those per-firmware default
// logins (round 12 #13).
let fwrow = Widget::box_sizer(false);
fwrow.add(
Widget::static_text(dlg, "Or set a login per firmware:"),
0,
sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL,
6,
);
fwrow.add_stretch(1);
fwrow.add(Widget::button(dlg, "Per-firmware defaults...", ID_FWCREDS_OPEN), 0, sf::BORDER_ALL, 6);
root.add_sizer(fwrow, 0, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT | sf::BORDER_TOP, 6);
let btns = Widget::box_sizer(false);
btns.add_stretch(1);
btns.add(Widget::button(dlg, "Save", ID_CREDS_SAVE), 0, sf::BORDER_ALL, 6);
@@ -1821,7 +1846,7 @@ impl App {
btns.add(Widget::button(dlg, "Cancel", ID_CREDS_CANCEL), 0, sf::BORDER_ALL, 6);
root.add_sizer(btns, 0, sf::EXPAND | sf::BORDER_ALL, 6);
root.set_sizer_on(dlg);
dlg.set_min_size(360, 190);
dlg.set_min_size(460, 220);
dlg.fit(dlg);
self.creds_dialog = dlg;
self.creds_user_input = user_input;
@@ -1829,6 +1854,113 @@ impl App {
dlg
}
/// Build (but do not show) the per-firmware default-credentials editor
/// (round 12 #13): one row per firmware in the asic-rs registry, each with a
/// username and password field prefilled from the saved override or, failing
/// that, the firmware's shipped default. Editing a field away from the default
/// makes it an override; setting it back to the default clears the override.
fn build_firmware_creds_dialog(&mut self) -> Widget {
let frame = Widget::main_frame();
let dlg = Widget::dialog(frame, "Per-firmware default logins");
let root = Widget::box_sizer(true);
root.add(
Widget::static_text(
dlg,
"Default login used when auto-discovering each firmware. Blank fields mean no login.\nValues left at the firmware's shipped default are not saved as overrides.",
),
0,
sf::BORDER_ALL,
8,
);
// Column headers.
let head = Widget::box_sizer(false);
let h_fw = Widget::static_text(dlg, "Firmware");
h_fw.set_min_size(150, -1);
head.add(h_fw, 0, sf::BORDER_LEFT | sf::BORDER_RIGHT, 6);
let h_user = Widget::static_text(dlg, "Username");
h_user.set_min_size(150, -1);
head.add(h_user, 0, sf::BORDER_LEFT | sf::BORDER_RIGHT, 6);
head.add(Widget::static_text(dlg, "Password"), 0, sf::BORDER_LEFT | sf::BORDER_RIGHT, 6);
root.add_sizer(head, 0, sf::BORDER_LEFT | sf::BORDER_RIGHT | sf::BORDER_TOP, 6);
let saved = credentials::load_firmware_credentials();
let mut rows: Vec<(String, Widget, Widget)> = Vec::new();
for fw in asic_rs::factory::default_firmware_registry() {
let name = fw.to_string();
let (def_user, def_pass) = credentials::firmware_default_for(&name);
let (user, pass) = saved
.get(&name)
.cloned()
.unwrap_or((def_user, def_pass));
let row = Widget::box_sizer(false);
let label = Widget::static_text(dlg, &name);
label.set_min_size(150, -1);
row.add(label, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_LEFT | sf::BORDER_RIGHT, 6);
let user_ctrl = Widget::text_ctrl(dlg, false);
user_ctrl.set_min_size(150, -1);
user_ctrl.set_value(&user);
row.add(user_ctrl, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_LEFT | sf::BORDER_RIGHT, 6);
let pass_ctrl = Widget::text_ctrl(dlg, false);
pass_ctrl.set_min_size(150, -1);
pass_ctrl.set_value(&pass);
row.add(pass_ctrl, 1, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_LEFT | sf::BORDER_RIGHT, 6);
root.add_sizer(row, 0, sf::EXPAND | sf::BORDER_TOP, 4);
rows.push((name, user_ctrl, pass_ctrl));
}
let btns = Widget::box_sizer(false);
btns.add_stretch(1);
btns.add(Widget::button(dlg, "Save", ID_FWCREDS_SAVE), 0, sf::BORDER_ALL, 6);
btns.add(Widget::button(dlg, "Reset to defaults", ID_FWCREDS_RESET), 0, sf::BORDER_ALL, 6);
btns.add(Widget::button(dlg, "Cancel", ID_FWCREDS_CANCEL), 0, sf::BORDER_ALL, 6);
root.add_sizer(btns, 0, sf::EXPAND | sf::BORDER_ALL, 6);
root.set_sizer_on(dlg);
dlg.set_min_size(520, 200);
dlg.fit(dlg);
self.fwcreds_dialog = dlg;
self.fwcreds_rows = rows;
dlg
}
/// Persist the per-firmware editor: a row that differs from the firmware's
/// shipped default becomes an override; a row equal to the default clears it.
fn apply_firmware_creds_save(&mut self) {
let mut map: std::collections::HashMap<String, (String, String)> = std::collections::HashMap::new();
for (name, user_ctrl, pass_ctrl) in &self.fwcreds_rows {
let user = user_ctrl.get_value();
let pass = pass_ctrl.get_value();
let default = credentials::firmware_default_for(name);
if (user.clone(), pass.clone()) != default {
map.insert(name.clone(), (user, pass));
}
}
match credentials::save_firmware_credentials(&map) {
Ok(()) => {
self.fwcreds_dialog.close(1);
let n = map.len();
wx_rs::set_status_text(&format!(
"per-firmware logins saved ({n} override{})",
if n == 1 { "" } else { "s" }
));
}
Err(e) => wx_rs::set_status_text(&format!("failed to save per-firmware logins: {e}")),
}
}
/// Reset every editor row to the firmware's shipped default and clear the
/// saved overrides.
fn apply_firmware_creds_reset(&mut self) {
for (name, user_ctrl, pass_ctrl) in &self.fwcreds_rows {
let (user, pass) = credentials::firmware_default_for(name);
user_ctrl.set_value(&user);
pass_ctrl.set_value(&pass);
}
match credentials::clear_firmware_credentials() {
Ok(()) => wx_rs::set_status_text("per-firmware logins reset to defaults"),
Err(e) => wx_rs::set_status_text(&format!("failed to clear per-firmware logins: {e}")),
}
}
fn apply_creds_save(&mut self) {
let user = self.creds_user_input.get_value();
let mut pass = self.creds_pass_input.get_value();
@@ -2915,6 +3047,10 @@ impl App {
ID_CREDS_SAVE => self.apply_creds_save(),
ID_CREDS_CLEAR => self.apply_creds_clear(),
ID_CREDS_CANCEL => self.creds_dialog.close(0),
ID_FWCREDS_OPEN => return Some(self.build_firmware_creds_dialog()),
ID_FWCREDS_SAVE => self.apply_firmware_creds_save(),
ID_FWCREDS_RESET => self.apply_firmware_creds_reset(),
ID_FWCREDS_CANCEL => self.fwcreds_dialog.close(0),
ID_KPI_HASHING => self.toggle_kpi_filter("hashing"),
ID_KPI_SLOW => self.toggle_kpi_filter("slow"),
ID_KPI_NOT => self.toggle_kpi_filter("not hashing"),