Compare commits

...
12 Commits
Author SHA1 Message Date
noah 086c883c4a Merge pull request 'Media icons: 3:1 against the button colour macOS actually paints' (#28) from media-icon-contrast into main
build-test-release / windows (push) Successful in 5m32s
build-test-release / release (push) Successful in 13m30s
2026-11-04 13:31:17 -07:00
noahandClaude Opus 5.5 4324d3046e Media icons: 3:1 against the button colour macOS actually paints
source / check (pull_request) Skipped
verify / verify (pull_request) Successful in 7m29s
verify / verify-windows (pull_request) Successful in 5m16s
The play/pause icons on macOS light buttons were about 2.6:1, because
native macOS buttons paint #ebebeb on a white window, not the system
button-face colour the check used. Check the icons against the button
face, the window colour and a shade 20 steps toward the opposite end.
They now measure 3.1:1 on the Mac (light and dark checked in real
system appearance), and stay above 3:1 on Linux and Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdKQBRg6YCM1XXQCTnD5GF
2026-11-04 14:17:52 -06:00
noah de5c1a5a15 Merge pull request 'Keep every coloured text readable in light and dark themes' (#27) from dark-mode-contrast into main
build-test-release / windows (push) Successful in 5m13s
build-test-release / release (push) Successful in 13m38s
2026-11-04 12:52:46 -07:00
noahandClaude Opus 5.5 d22ee79159 Keep every coloured text readable in light and dark themes
verify / verify (pull_request) Successful in 7m18s
verify / verify-windows (pull_request) Successful in 4m48s
source / check (pull_request) Skipped
Checked in real system dark and light mode on macOS (SkyLight theme
switch on the M1, restored afterwards), Windows 11 (the app-mode
setting, run on the VM's desktop) and Linux (Adwaita and Adwaita:dark),
measuring each screenshot's text against its background with the WCAG
contrast formula.

The app's own colours fell short on every platform. The fixed firmware
hues were 2.6 to 3.9:1 (LuxOS gold and ePIC blue on light, Alkane navy,
Braiins purple and Bixbit red on dark). The hashrate greens and
temperature purples were about 4:1, and the play/pause icons about 2:1
on light buttons.

- readable()/readable_at(): nudge a colour toward black (light theme)
  or white (dark theme), keeping its hue, until it reaches 4.5:1 for
  text (3:1 for icons) against every given background.
- palette(): green/amber/red/accent/label are made readable against the
  window, the zebra row and the KPI card; Palette::text() guards every
  computed cell colour (ratio and temperature blends, firmware hues).
- Media button icons: at least 3:1 against the theme's button face.
- macOS snapshot (wx-rs patch 0011): capture the window's frame view.
  The content view alone left unpainted areas transparent, which read
  as a white toolbar strip in dark mode. On screen the area was always
  the dark window colour.

After the change, no app-coloured text is below 4.5:1 in any of the
six theme/platform combinations. What the checker still flags is theme
chrome (grid lines, GTK header labels, button outlines, the zoom
slider) and anti-aliasing on thin fonts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdKQBRg6YCM1XXQCTnD5GF
2026-11-04 13:40:10 -06:00
noah 3d1fd36811 Merge pull request 'Fix macOS table cells hiding their text; Bug report button and report relay' (#26) from macos-table-and-bug-report into main
build-test-release / windows (push) Successful in 5m30s
build-test-release / release (push) Successful in 13m53s
2026-11-04 10:20:14 -07:00
noahandClaude Opus 5.5 7f2ea8a8dc Manual: reporting a problem with the Bug report button
verify / verify (pull_request) Successful in 7m39s
source / check (pull_request) Skipped
verify / verify-windows (pull_request) Successful in 5m30s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdKQBRg6YCM1XXQCTnD5GF
2026-11-04 11:04:29 -06:00
noahandClaude Opus 5.5 b26f389460 Report relay: files the app's bug reports as Gitea issues
tools/report-relay is a dependency-free Python service. It takes the
JSON the app's Bug report button sends, validates it (type, text,
context and log sizes), scrubs the log a second time, rate-limits (5
per client IP and 60 in all per hour, 4 in flight), neutralises
@mentions, and files one issue in blueflare/alkane-toolkit with the
labels bug/ui/feature and from-app. It stores nothing and logs one
line per report without its text.

It runs on the Gitea host behind the shared nginx at
https://gitea.bluefla.re/alkane-report/ as the scoped alkane-reporter
bot (team alkane-reporters: issues write, code read, alkane-toolkit
only). See tools/report-relay/README.md. Its tests run in verify's
unit-test step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdKQBRg6YCM1XXQCTnD5GF
2026-11-04 11:04:15 -06:00
noahandClaude Opus 5.5 b46f15b340 Bug report button: file bugs and feature requests from the app
A Bug report button between Export and Settings opens a dialog: a type
(Bug, UI change, Feature request), one text box whose first line
becomes the issue title, and "Include the recent activity log
(passwords removed)", ticked by default. Send posts the report to the
report relay (next commit), which files it as an issue in
blueflare/alkane-toolkit. The dialog then shows the issue number, and
Open issue opens it, or shows why the report was not sent with the text
kept.

The app holds no token. A report carries the app version, platform, OS
version, appearance, language and window size, plus the newest 80 log
lines (at most 16 KiB). Before the log leaves the machine,
bug_report::scrub removes values of password/token/secret-like names,
Authorization and Cookie headers, URL passwords and PEM private keys.
The request goes through the system curl, as the other HTTP actions
do. ALKANE_REPORT_URL overrides the relay address for testing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdKQBRg6YCM1XXQCTnD5GF
2026-11-04 10:59:02 -06:00
noahandClaude Opus 5.5 c9d32f9793 Fix macOS table cells hiding their text; add a self-test snapshot
On macOS several table columns showed no values until a row was
clicked. wxOSX draws custom cells without clipping to the cell, and the
segment renderer paints its zebra background over an oversized rectangle
(a column width either side, 40 px up and down) that GTK and MSW clip
to the cell. On the Mac each cell's fill painted over its neighbours.
Selected rows skip the fill, so their text came back.

On macOS the zebra is now the table's native alternating rows. AppKit
paints them under every cell in the theme's colours, and no row
carries a background attribute, so the renderer never fills. Plain
segment text uses the theme-aware list text colour instead of the
control's foreground colour.

To check rendering without screen-recording permission, the self-test
takes ALKANE_SNAPSHOT=path (macOS) and writes a PNG of the window as
the app draws it (osx_native.mm: cacheDisplayInRect). Before/after
snapshots on the M1 confirm the fix in light and dark appearance.

wx-rs patch 0010. Also folds in PR #24 (build-gui-macos without
--locked).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdKQBRg6YCM1XXQCTnD5GF
2026-11-04 10:40:43 -06:00
noahandClaude Opus 5.5 5cf9d44d7e build-gui-macos: build like CI, without --locked
A release sets the crate version first, which changes the workspace
entries in Cargo.lock; --locked then refuses to build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 10:37:31 -06:00
noah d9dc60918c Merge pull request 'macOS build: universal app for Apple Silicon and Intel, macOS 11+' (#23) from macos-build into main
build-test-release / release (push) Successful in 13m52s
build-test-release / windows (push) Successful in 6m2s
2026-11-03 14:41:23 -07:00
noahandClaude Opus 5.5 bb31a7befc macOS build: universal app for Apple Silicon and Intel, macOS 11+
verify / verify (pull_request) Successful in 7m33s
source / check (pull_request) Skipped
verify / verify-windows (pull_request) Successful in 5m48s
- scripts/build-wx-macos.sh: static, monolithic, universal (arm64 +
  x86_64) wxOSX/Cocoa 3.3.3 from the vendored tree, with wx's bundled
  image/zlib/regex/expat so the app links only system frameworks.
  Minimum macOS 11.0: current SDKs' libc++ supports nothing older.
- scripts/build-gui-macos.sh: builds both Rust targets against that
  prefix and merges them with lipo.
- ci/package-macos.py: "Alkane Toolkit.app" (Info.plist, icon, local
  network usage string), ad-hoc signed, zipped with ditto as
  alkane-toolkit-vX.Y.Z-macos-universal.zip with LICENSE and README.
- crates/alkane-gui/macos: app icon (.icns) from the same glyph as the
  Windows icon, plus the script that makes it.
- wx-rs patch 0009: macOS goes through wx-config (drops the universal
  -arch flags, maps -framework), reports logical sizes like GTK/MSW
  instead of narrowing floats to ints, and links clang's compiler
  runtime for wxOSX's @available checks.
- docs/macos.md, README and the manual's install chapter.

Verified on an M1 running macOS 27.0.1: both slices build, all
workspace unit tests pass, and the headless UI self-test passes 52/52
on arm64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 15:26:56 -06:00
28 changed files with 2174 additions and 46 deletions
+1
View File
@@ -159,6 +159,7 @@ jobs:
run: |
cargo test -p alkane-gui --offline
cargo test --workspace --exclude alkane-gui --exclude wx-rs --offline
python3 tools/report-relay/test_relay.py
- name: Microbenchmarks
if: steps.surface.outputs.surface == 'full'
+3 -1
View File
@@ -32,7 +32,9 @@ uses offline fixtures and does not contact hardware.
Windows 10/11 builds are cross-compiled on Linux (`scripts/build-wx-windows.sh`,
then `scripts/build-gui-windows.sh`) and attached to every release as a
self-contained `alkane-toolkit.exe`; see [docs/windows.md](docs/windows.md).
macOS is not built.
macOS builds run on a Mac (`scripts/build-wx-macos.sh`, then
`scripts/build-gui-macos.sh`) and produce one universal app for Apple Silicon
and Intel, macOS 11 or later; see [docs/macos.md](docs/macos.md).
## Current scope
+109
View File
@@ -0,0 +1,109 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Package the universal macOS build as an app bundle, for release.
package-macos.py VERSION BINARY OUT_DIR
BINARY is the universal (arm64 + x86_64) alkane-toolkit executable made by
scripts/build-gui-macos.sh. Writes
dist/alkane-toolkit-vVERSION-macos-universal.zip holding "Alkane Toolkit.app",
LICENSE and README.txt, and prints its path. Runs on macOS (lipo, codesign,
ditto).
The bundle is ad-hoc signed only (Apple Silicon refuses to run unsigned
code); it is not signed with a Developer ID or notarized, so Gatekeeper asks
the user to confirm the first launch. README.txt says how.
"""
import pathlib
import plistlib
import shutil
import subprocess
import sys
import tempfile
ROOT = pathlib.Path(__file__).resolve().parents[1]
APP = "Alkane Toolkit.app"
BUNDLE_ID = "re.bluefla.alkane-toolkit"
MIN_MACOS = "11.0"
def run(*args: str) -> str:
return subprocess.run(args, check=True, capture_output=True, text=True).stdout
def build_app(version: str, binary: pathlib.Path, dest: pathlib.Path) -> pathlib.Path:
archs = run("lipo", "-archs", str(binary)).split()
if sorted(archs) != ["arm64", "x86_64"]:
sys.exit(f"{binary} is not a universal arm64 + x86_64 binary: {archs}")
app = dest / APP
macos = app / "Contents" / "MacOS"
resources = app / "Contents" / "Resources"
macos.mkdir(parents=True)
resources.mkdir()
shutil.copy2(binary, macos / "alkane-toolkit")
shutil.copy2(ROOT / "crates/alkane-gui/macos/alkane-toolkit.icns", resources)
info = {
"CFBundleName": "Alkane Toolkit",
"CFBundleDisplayName": "Alkane Toolkit",
"CFBundleIdentifier": BUNDLE_ID,
"CFBundleExecutable": "alkane-toolkit",
"CFBundleIconFile": "alkane-toolkit",
"CFBundlePackageType": "APPL",
"CFBundleShortVersionString": version,
"CFBundleVersion": version,
"CFBundleInfoDictionaryVersion": "6.0",
"LSMinimumSystemVersion": MIN_MACOS,
"NSHighResolutionCapable": True,
"NSPrincipalClass": "NSApplication",
# The scanner talks to miners on the local network.
"NSLocalNetworkUsageDescription":
"Alkane Toolkit scans your local network to find and manage miners.",
"NSHumanReadableCopyright":
"Copyright (c) 2026 BlueFlare Energy Solutions Inc. (MIT License)",
}
with open(app / "Contents" / "Info.plist", "wb") as fh:
plistlib.dump(info, fh)
(app / "Contents" / "PkgInfo").write_text("APPL????")
# Ad-hoc signature over the whole bundle (the linker's per-slice
# signatures do not cover Info.plist or the resources).
run("codesign", "--force", "--sign", "-", "--timestamp=none", str(app))
run("codesign", "--verify", "--strict", str(app))
return app
def release(version: str, binary: str, out_dir: str) -> None:
name = f"alkane-toolkit-v{version}-macos-universal"
out = pathlib.Path(out_dir).resolve() / f"{name}.zip"
out.parent.mkdir(parents=True, exist_ok=True)
readme = (
f"alkane-toolkit v{version} (macOS, Apple Silicon and Intel)\n"
"\n"
f"Runs on macOS {MIN_MACOS} (Big Sur) or later. Drag \"Alkane Toolkit\"\n"
"to Applications, then open it.\n"
"\n"
"The app is not notarized by Apple, so the first launch is blocked\n"
"with a warning. To allow it once:\n"
" - macOS 15 or later: try to open it, then System Settings >\n"
" Privacy & Security > \"Open Anyway\" for Alkane Toolkit.\n"
" - macOS 11 to 14: Control-click the app, choose Open, then Open.\n"
"Or, in Terminal: xattr -dr com.apple.quarantine \"/Applications/Alkane Toolkit.app\"\n"
"\n"
"Settings are stored in ~/.config/alkane-toolkit/.\n"
"Manual: https://blueflare.gitea.bluefla.re/alkane-toolkit/manual/\n"
)
with tempfile.TemporaryDirectory(prefix="alkane-macos-") as tmp:
folder = pathlib.Path(tmp) / name
folder.mkdir()
build_app(version, pathlib.Path(binary), folder)
shutil.copy2(ROOT / "LICENSE", folder / "LICENSE")
(folder / "README.txt").write_text(readme)
out.unlink(missing_ok=True)
run("ditto", "-c", "-k", "--keepParent", str(folder), str(out))
print(out)
if __name__ == "__main__":
if len(sys.argv) != 4:
sys.exit(__doc__)
release(*sys.argv[1:])
Binary file not shown.
+34
View File
@@ -0,0 +1,34 @@
#!/bin/sh
# Regenerate alkane-toolkit.icns (the macOS app icon) from the in-repo
# microchip glyph: the same white chip on a rounded amber tile as the Windows
# icon, at the sizes macOS asks for (16-1024 px). Needs rsvg-convert,
# ImageMagick and python3; the .icns is committed, so a build never runs this.
set -e
cd "$(dirname "$0")"
GLYPH=../assets/icons/microchip-solid-full.svg
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' 0
sed 's/currentColor/#ffffff/g; s/<svg /<svg fill="#ffffff" /' "$GLYPH" > "$TMP/glyph.svg"
for s in 16 32 64 128 256 512 1024; do
# Apple's grid: the tile fills ~80% of the canvas, centred.
t=$((s * 80 / 100)); m=$(((s - t) / 2)); r=$((t * 22 / 100))
g=$((t * 60 / 100)); o=$(((s - g) / 2))
rsvg-convert -w "$g" -h "$g" "$TMP/glyph.svg" -o "$TMP/g$s.png"
convert -size "${s}x${s}" xc:none -fill '#d9822b' \
-draw "roundrectangle $m,$m $((m + t - 1)),$((m + t - 1)) $r,$r" \
"$TMP/g$s.png" -geometry "+$o+$o" -composite "PNG32:$TMP/i$s.png"
done
python3 - "$TMP" alkane-toolkit.icns <<'PY'
import pathlib, struct, sys
tmp, out = pathlib.Path(sys.argv[1]), sys.argv[2]
# ICNS element types that hold PNG data, by pixel size.
types = [("icp4", 16), ("icp5", 32), ("icp6", 64), ("ic07", 128), ("ic08", 256),
("ic09", 512), ("ic10", 1024), ("ic11", 32), ("ic12", 64), ("ic13", 256),
("ic14", 512)]
body = b""
for kind, size in types:
data = (tmp / f"i{size}.png").read_bytes()
body += kind.encode() + struct.pack(">I", len(data) + 8) + data
pathlib.Path(out).write_bytes(b"icns" + struct.pack(">I", len(body) + 8) + body)
PY
echo "wrote $(pwd)/alkane-toolkit.icns"
+437
View File
@@ -0,0 +1,437 @@
//! Bug reports and feature requests from the toolbar's Bug report button.
//!
//! The app posts the report to the report relay (see tools/report-relay),
//! which holds the Gitea token and files the issue in blueflare/alkane-toolkit.
//! The app itself holds no secret. A report carries its type, the user's text,
//! what the app knows about itself (version, OS, appearance, language, window
//! size) and, if the user leaves it ticked, the recent activity log with
//! credentials scrubbed. Nothing is stored locally.
use std::io::Write;
use std::process::Stdio;
use std::sync::mpsc::{channel, Receiver};
/// Where reports go. `ALKANE_REPORT_URL` overrides it (tests, staging).
pub const DEFAULT_URL: &str = "https://gitea.bluefla.re/alkane-report/v1/report";
/// Longest report text the relay accepts.
pub const MAX_TEXT_CHARS: usize = 8000;
/// Log sent with a report: at most this many of the newest lines...
const LOG_LINES: usize = 80;
/// ...cut to this many bytes on a line boundary.
const LOG_BYTES: usize = 16 * 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
Bug,
Ui,
Feature,
}
impl Kind {
/// In the order the dialog's type choice lists them.
pub const ALL: [Kind; 3] = [Kind::Bug, Kind::Ui, Kind::Feature];
pub fn label(self) -> &'static str {
match self {
Kind::Bug => "Bug",
Kind::Ui => "UI change",
Kind::Feature => "Feature request",
}
}
/// The wire value, which is also the issue label.
pub fn as_str(self) -> &'static str {
match self {
Kind::Bug => "bug",
Kind::Ui => "ui",
Kind::Feature => "feature",
}
}
}
/// What the app knows about itself, sent with every report.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Context {
pub app_version: String,
pub platform: String,
pub os: String,
pub appearance: String,
pub language: String,
pub window: String,
}
impl Context {
/// The running app's context. `window` is the main window's size.
pub fn current(dark: bool, language: &str, window: (i32, i32)) -> Context {
Context {
app_version: env!("CARGO_PKG_VERSION").to_string(),
platform: format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH),
os: os_version(),
appearance: if dark { "dark" } else { "light" }.to_string(),
language: language.to_string(),
window: format!("{}x{}", window.0, window.1),
}
}
}
/// A human-readable OS name and version, best effort.
fn os_version() -> String {
#[cfg(target_os = "linux")]
{
if let Ok(s) = std::fs::read_to_string("/etc/os-release") {
for line in s.lines() {
if let Some(v) = line.strip_prefix("PRETTY_NAME=") {
return v.trim_matches('"').to_string();
}
}
}
"Linux".to_string()
}
#[cfg(target_os = "macos")]
{
let out = alkane_core::platform::quiet_command("sw_vers")
.arg("-productVersion")
.output();
match out {
Ok(o) if o.status.success() => {
format!("macOS {}", String::from_utf8_lossy(&o.stdout).trim())
}
_ => "macOS".to_string(),
}
}
#[cfg(windows)]
{
let out = alkane_core::platform::quiet_command("cmd")
.args(["/C", "ver"])
.output();
match out {
Ok(o) if o.status.success() => {
let v = String::from_utf8_lossy(&o.stdout).trim().to_string();
if v.is_empty() {
"Windows".to_string()
} else {
v
}
}
_ => "Windows".to_string(),
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
{
std::env::consts::OS.to_string()
}
}
/// Names whose values are secrets: `name=value`, `name: value` and
/// `"name": "value"` have the value replaced.
const SECRET_NAMES: [&str; 12] = [
"password",
"passwd",
"pass",
"pwd",
"secret",
"token",
"apikey",
"api_key",
"authorization",
"cookie",
"psk",
"credential",
];
/// Remove credentials from log text before it leaves the machine: values of
/// secret-looking names, `user:password@` in URLs, and PEM private keys.
/// What is removed becomes `[redacted]`.
pub fn scrub(text: &str) -> String {
let mut out = Vec::new();
let mut in_pem = false;
for line in text.lines() {
if line.contains("-----BEGIN") && line.contains("PRIVATE KEY") {
in_pem = true;
out.push("[redacted]".to_string());
continue;
}
if in_pem {
if line.contains("-----END") {
in_pem = false;
}
continue;
}
out.push(scrub_line(line));
}
out.join("\n")
}
fn scrub_line(line: &str) -> String {
let mut s = scrub_url_userinfo(line);
let lower = s.to_ascii_lowercase();
// Walk every occurrence of every secret name; redact the value after the
// next `=` or `:` (quotes and spaces skipped) up to whitespace, a quote, a
// comma, `&` or `;`.
let mut cuts: Vec<(usize, usize)> = Vec::new();
for name in SECRET_NAMES {
let mut from = 0;
while let Some(pos) = lower[from..].find(name) {
let start = from + pos;
from = start + name.len();
let bytes = s.as_bytes();
let mut i = start + name.len();
// allow a closing quote and spaces between the name and the separator
while i < bytes.len() && (bytes[i] == b'"' || bytes[i] == b'\'' || bytes[i] == b' ') {
i += 1;
}
if i >= bytes.len() || (bytes[i] != b'=' && bytes[i] != b':') {
continue;
}
i += 1;
while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'"' || bytes[i] == b'\'') {
i += 1;
}
let vstart = i;
if name == "authorization" || name == "cookie" {
// "Authorization: Bearer <token>", "Cookie: a=b; c=d": the whole
// rest of the line is the secret.
i = bytes.len();
}
while i < bytes.len()
&& !matches!(bytes[i], b' ' | b'"' | b'\'' | b',' | b'&' | b';' | b'\t')
{
i += 1;
}
if i > vstart {
cuts.push((vstart, i));
}
}
}
if cuts.is_empty() {
return s;
}
cuts.sort();
let mut merged: Vec<(usize, usize)> = Vec::new();
for (a, b) in cuts {
match merged.last_mut() {
Some(last) if a <= last.1 => last.1 = last.1.max(b),
_ => merged.push((a, b)),
}
}
for (a, b) in merged.into_iter().rev() {
if s.is_char_boundary(a) && s.is_char_boundary(b) {
s.replace_range(a..b, "[redacted]");
}
}
s
}
/// `scheme://user:password@host` -> `scheme://user:[redacted]@host`.
fn scrub_url_userinfo(line: &str) -> String {
let mut s = line.to_string();
let mut from = 0;
while let Some(p) = s[from..].find("://") {
let auth_start = from + p + 3;
let rest = &s[auth_start..];
let end = rest
.find(|c: char| c == '/' || c.is_whitespace())
.unwrap_or(rest.len());
let authority = &rest[..end];
if let Some(at) = authority.rfind('@') {
if let Some(colon) = authority[..at].find(':') {
let a = auth_start + colon + 1;
let b = auth_start + at;
s.replace_range(a..b, "[redacted]");
}
}
from = auth_start;
if from >= s.len() {
break;
}
}
s
}
/// The newest activity-log lines, scrubbed and cut to the size limits.
pub fn recent_log(full_log: &str) -> String {
let lines: Vec<&str> = full_log.lines().collect();
let tail = &lines[lines.len().saturating_sub(LOG_LINES)..];
let mut text = scrub(&tail.join("\n"));
if text.len() > LOG_BYTES {
let mut cut = text.len() - LOG_BYTES;
while !text.is_char_boundary(cut) {
cut += 1;
}
text = text[cut..].to_string();
if let Some(nl) = text.find('\n') {
text = text[nl + 1..].to_string();
}
}
text
}
/// The JSON body the relay takes.
pub fn payload(kind: Kind, text: &str, ctx: &Context, log: Option<&str>) -> String {
serde_json::json!({
"kind": kind.as_str(),
"text": text,
"app_version": ctx.app_version,
"platform": ctx.platform,
"os": ctx.os,
"appearance": ctx.appearance,
"language": ctx.language,
"window": ctx.window,
"log": log.unwrap_or(""),
})
.to_string()
}
/// A filed issue: its number and web link.
#[derive(Clone, Debug, PartialEq)]
pub struct Filed {
pub number: u64,
pub url: String,
}
/// Read the relay's answer: curl's body, then the HTTP status on the last line.
pub fn parse_response(curl_stdout: &str) -> Result<Filed, String> {
let trimmed = curl_stdout.trim_end();
let (body, code) = match trimmed.rfind('\n') {
Some(i) => (&trimmed[..i], trimmed[i + 1..].trim()),
None => ("", trimmed),
};
let json: Option<serde_json::Value> = serde_json::from_str(body).ok();
if code == "201" || code == "200" {
let number = json.as_ref().and_then(|j| j["number"].as_u64());
let url = json.as_ref().and_then(|j| j["url"].as_str()).unwrap_or("");
return match number {
Some(number) => Ok(Filed {
number,
url: url.to_string(),
}),
None => Err("the report service answered without an issue number; \
check the issue list before sending again"
.into()),
};
}
let reason = json
.as_ref()
.and_then(|j| j["error"].as_str())
.map(str::to_string);
Err(match (code, reason) {
(_, Some(r)) => r,
("000", None) | ("", None) => "could not reach the report service".into(),
(c, None) => format!("the report service answered HTTP {c}"),
})
}
/// Send a report on a worker thread; the receiver gets one result.
pub fn send(body: String) -> Receiver<Result<Filed, String>> {
let url = std::env::var("ALKANE_REPORT_URL").unwrap_or_else(|_| DEFAULT_URL.to_string());
let (tx, rx) = channel();
std::thread::spawn(move || {
let result = (|| {
let mut child = alkane_core::platform::quiet_command("curl")
.args([
"-sS",
"--max-time",
"40",
"-X",
"POST",
"-H",
"Content-Type: application/json",
"--data-binary",
"@-",
"-w",
"\n%{http_code}",
&url,
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| format!("could not run curl: {e}"))?;
if let Some(mut si) = child.stdin.take() {
let _ = si.write_all(body.as_bytes());
}
let out = child
.wait_with_output()
.map_err(|e| format!("curl failed: {e}"))?;
parse_response(&String::from_utf8_lossy(&out.stdout))
})();
let _ = tx.send(result);
});
rx
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scrub_removes_credentials_and_keeps_the_rest() {
let log = "12:00:01 set pools on 192.168.1.20: url=stratum+tcp://u:hunter2@pool:3333 password=hunter2\n\
12:00:02 login {\"user\": \"root\", \"pass\": \"s3cret\"}\n\
12:00:03 Authorization: Bearer abc.def\n\
-----BEGIN OPENSSH PRIVATE KEY-----\nAAAA\n-----END OPENSSH PRIVATE KEY-----\n\
12:00:04 scan finished: 9 miners";
let s = scrub(log);
assert!(!s.contains("hunter2"), "{s}");
assert!(!s.contains("s3cret"), "{s}");
assert!(!s.contains("abc.def"), "{s}");
assert!(!s.contains("AAAA"), "{s}");
assert!(s.contains("192.168.1.20"));
assert!(s.contains("\"user\": \"root\""));
assert!(s.contains("scan finished: 9 miners"));
assert_eq!(s.matches("[redacted]").count(), 5, "{s}");
}
#[test]
fn recent_log_keeps_the_newest_lines_within_limits() {
let log: Vec<String> = (0..500)
.map(|i| format!("line {i} {}", "x".repeat(300)))
.collect();
let tail = recent_log(&log.join("\n"));
assert!(tail.len() <= LOG_BYTES);
assert!(tail.ends_with(&log[499]));
assert!(tail.starts_with("line "), "cut on a line boundary");
assert!(!tail.contains("line 400 "), "only the newest lines");
}
#[test]
fn payload_carries_kind_text_and_context() {
let ctx = Context {
app_version: "1.2.3".into(),
platform: "macos-aarch64".into(),
os: "macOS 15.1".into(),
appearance: "dark".into(),
language: "en".into(),
window: "1280x800".into(),
};
let v: serde_json::Value =
serde_json::from_str(&payload(Kind::Ui, "Text \"quoted\"", &ctx, None)).unwrap();
assert_eq!(v["kind"], "ui");
assert_eq!(v["text"], "Text \"quoted\"");
assert_eq!(v["app_version"], "1.2.3");
assert_eq!(v["log"], "");
}
#[test]
fn response_parsing() {
assert_eq!(
parse_response("{\"number\":42,\"url\":\"https://x/42\"}\n201"),
Ok(Filed {
number: 42,
url: "https://x/42".into()
})
);
assert_eq!(
parse_response("{\"error\":\"Too many reports; try again later\"}\n429"),
Err("Too many reports; try again later".into())
);
assert_eq!(
parse_response("\n000"),
Err("could not reach the report service".into())
);
assert_eq!(
parse_response("<html>\n502"),
Err("the report service answered HTTP 502".into())
);
}
}
+8 -1
View File
@@ -34,7 +34,7 @@ impl Lang {
Lang::Pt => "Portugu\u{ea}s",
}
}
fn code(self) -> &'static str {
pub fn code(self) -> &'static str {
match self {
Lang::En => "en",
Lang::Zh => "zh",
@@ -137,6 +137,13 @@ const TABLE: &[(&str, [&str; 6])] = &[
("Credentials", ["Credentials", "\u{51ed}\u{636e}", "\u{0423}\u{0447}\u{0451}\u{0442}\u{043d}\u{044b}\u{0435} \u{0434}\u{0430}\u{043d}\u{043d}\u{044b}\u{0435}", "Identifiants", "Credenciales", "Credenciais"]),
("Language", ["Language", "\u{8bed}\u{8a00}", "\u{042f}\u{0437}\u{044b}\u{043a}", "Langue", "Idioma", "Idioma"]),
("Logs", ["Logs", "\u{65e5}\u{5fd7}", "\u{0416}\u{0443}\u{0440}\u{043d}\u{0430}\u{043b}", "Journaux", "Registros", "Registos"]),
("Bug report", ["Bug report", "\u{95ee}\u{9898}\u{53cd}\u{9988}", "\u{421}\u{43e}\u{43e}\u{431}\u{449}\u{438}\u{442}\u{44c} \u{43e}\u{431} \u{43e}\u{448}\u{438}\u{431}\u{43a}\u{435}", "Signaler un bug", "Informar de un error", "Relatar um erro"]),
("Type", ["Type", "\u{7c7b}\u{578b}", "\u{422}\u{438}\u{43f}", "Type", "Tipo", "Tipo"]),
("Bug", ["Bug", "\u{7f3a}\u{9677}", "\u{41e}\u{448}\u{438}\u{431}\u{43a}\u{430}", "Bug", "Error", "Erro"]),
("UI change", ["UI change", "\u{754c}\u{9762}\u{4fee}\u{6539}", "\u{418}\u{437}\u{43c}\u{435}\u{43d}\u{435}\u{43d}\u{438}\u{435} \u{438}\u{43d}\u{442}\u{435}\u{440}\u{444}\u{435}\u{439}\u{441}\u{430}", "Modification de l'interface", "Cambio de interfaz", "Altera\u{e7}\u{e3}o da interface"]),
("Feature request", ["Feature request", "\u{529f}\u{80fd}\u{5efa}\u{8bae}", "\u{41f}\u{440}\u{435}\u{434}\u{43b}\u{43e}\u{436}\u{435}\u{43d}\u{438}\u{435} \u{444}\u{443}\u{43d}\u{43a}\u{446}\u{438}\u{438}", "Demande de fonctionnalit\u{e9}", "Solicitud de funci\u{f3}n", "Pedido de recurso"]),
("Send", ["Send", "\u{53d1}\u{9001}", "\u{41e}\u{442}\u{43f}\u{440}\u{430}\u{432}\u{438}\u{442}\u{44c}", "Envoyer", "Enviar", "Enviar"]),
("Open issue", ["Open issue", "\u{6253}\u{5f00}\u{95ee}\u{9898}", "\u{41e}\u{442}\u{43a}\u{440}\u{44b}\u{442}\u{44c} \u{437}\u{430}\u{434}\u{430}\u{447}\u{443}", "Ouvrir le ticket", "Abrir incidencia", "Abrir issue"]),
// KPI captions
("Total", ["Total", "\u{603b}\u{6570}", "\u{0412}\u{0441}\u{0435}\u{0433}\u{043e}", "Total", "Total", "Total"]),
("Hashing", ["Hashing", "\u{6b63}\u{5728}\u{6316}\u{77ff}", "\u{0425}\u{0435}\u{0448}\u{0438}\u{0440}\u{043e}\u{0432}\u{0430}\u{043d}\u{0438}\u{0435}", "Minage", "Minando", "Minerando"]),
+12
View File
@@ -7,6 +7,7 @@
mod access_probe;
mod askpass;
mod bug_report;
mod credentials;
mod export;
mod fixtures;
@@ -99,6 +100,17 @@ fn run_selftest() -> i32 {
let frame = wx_rs::Widget::main_frame();
let mut app = ui::build(frame);
let ok = app.run_fixture_selftest();
// ALKANE_SNAPSHOT=path (macOS): also write a PNG of the window as the app
// draws it, to check rendering without screen-recording permission.
if let Some(path) = std::env::var_os("ALKANE_SNAPSHOT") {
let path = path.to_string_lossy();
let wrote = wx_rs::snapshot_png(&frame, &path);
println!(
"SNAPSHOT {} {}",
if wrote { "wrote" } else { "unavailable" },
path
);
}
println!("SELFTEST {}", if ok { "PASS" } else { "FAIL" });
if ok {
0
+317 -10
View File
@@ -22,6 +22,12 @@ pub const ID_PRESETS: i32 = 108;
pub const ID_EXPORT: i32 = 109;
// Settings menu (round 15 #13): the toolbar button + its menu items.
pub const ID_SETTINGS: i32 = 110;
// Bug report / feature request dialog (toolbar, between Export and Settings).
pub const ID_BUG_REPORT: i32 = 111;
const ID_REPORT_SEND: i32 = 240;
const ID_REPORT_CLOSE: i32 = 241;
const ID_REPORT_OPEN: i32 = 242;
const ID_REPORT_KIND: i32 = 243;
const ID_LANG_BASE: i32 = 790; // one id per language (790 + Lang index); reserves 790..790+Lang::all().len()
const ID_LOGS_OPEN: i32 = 796; // open the Logs page
const ID_LOGS_REFRESH: i32 = 797;
@@ -170,6 +176,68 @@ struct Palette {
accent: (u8, u8, u8),
/// Odd-row (zebra) background: a subtle tint of the window colour.
zebra: (u8, u8, u8),
/// Backgrounds coloured text is drawn on (window, zebra row, KPI card).
/// `text()` keeps every computed text colour readable on all of them.
text_bgs: Vec<(u8, u8, u8)>,
}
/// WCAG relative luminance of an sRGB colour.
fn wcag_luminance(c: (u8, u8, u8)) -> f64 {
let ch = |v: u8| {
let v = v as f64 / 255.0;
if v <= 0.03928 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
};
0.2126 * ch(c.0) + 0.7152 * ch(c.1) + 0.0722 * ch(c.2)
}
/// WCAG contrast ratio of two colours (1.0 to 21.0).
fn contrast_ratio(a: (u8, u8, u8), b: (u8, u8, u8)) -> f64 {
let (la, lb) = (wcag_luminance(a), wcag_luminance(b));
(la.max(lb) + 0.05) / (la.min(lb) + 0.05)
}
/// Minimum contrast for text: WCAG AA for normal-size text.
const MIN_TEXT_CONTRAST: f64 = 4.5;
/// Minimum contrast for icons and other graphics (WCAG non-text contrast).
const MIN_ICON_CONTRAST: f64 = 3.0;
/// `c`, darkened (on light backgrounds) or lightened (on dark ones) just
/// enough to reach MIN_TEXT_CONTRAST against every background in `bgs`,
/// keeping its hue. Colours that already pass are returned unchanged.
fn readable(c: (u8, u8, u8), bgs: &[(u8, u8, u8)]) -> (u8, u8, u8) {
readable_at(c, bgs, MIN_TEXT_CONTRAST)
}
/// `readable` with an explicit minimum ratio (MIN_ICON_CONTRAST for icons).
fn readable_at(c: (u8, u8, u8), bgs: &[(u8, u8, u8)], min: f64) -> (u8, u8, u8) {
let passes = |c| bgs.iter().all(|&bg| contrast_ratio(c, bg) >= min);
if bgs.is_empty() || passes(c) {
return c;
}
let mean = bgs.iter().map(|&bg| wcag_luminance(bg)).sum::<f64>() / bgs.len() as f64;
let target = if mean > 0.18 {
(0, 0, 0)
} else {
(255, 255, 255)
};
for step in 1..=50 {
let d = tint_toward(c, target, step as f64 / 50.0);
if passes(d) {
return d;
}
}
target
}
impl Palette {
/// A text colour adjusted to stay readable on this theme's backgrounds.
fn text(&self, c: (u8, u8, u8)) -> (u8, u8, u8) {
readable(c, &self.text_bgs)
}
}
fn luminance(c: (u8, u8, u8)) -> f32 {
@@ -206,11 +274,11 @@ fn tint_toward(base: (u8, u8, u8), target: (u8, u8, u8), f: f64) -> (u8, u8, u8)
/// output reads as clearly bad, and 100% reads green.
fn ratio_color(pal: &Palette, ratio: f64) -> (u8, u8, u8) {
let t = ((ratio - 0.5) / 0.5).clamp(0.0, 1.0);
if t <= 0.5 {
pal.text(if t <= 0.5 {
tint_toward(pal.red, pal.amber, t / 0.5)
} else {
tint_toward(pal.amber, pal.green, (t - 0.5) / 0.5)
}
})
}
/// Whether a firmware family has a supported restore-to-stock path (round 12
@@ -246,11 +314,11 @@ fn firmware_color(family: &str) -> Option<(u8, u8, u8)> {
/// Colour for a chip-temperature reading: cool blue at/below 40 C fading to hot
/// red at/above 90 C, so a hot miner reads red at a glance.
fn temp_color(pal: &Palette, temp_c: f64) -> (u8, u8, u8) {
tint_toward(
pal.text(tint_toward(
pal.accent,
pal.red,
((temp_c - 40.0) / 50.0).clamp(0.0, 1.0),
)
))
}
/// Build the segment-encoded cell for a combined actual/expected column
@@ -618,7 +686,7 @@ fn palette() -> Palette {
let win = wx_rs::system_color(0); // window background
let text = wx_rs::system_color(1); // window text
let dark = luminance(win) < 128.0;
Palette {
let mut p = Palette {
// Cards read as slightly elevated vs the window: lighter in dark mode,
// pure white on a light-grey window.
card_bg: if dark {
@@ -647,7 +715,17 @@ fn palette() -> Palette {
} else {
nudge(win, -10)
},
}
text_bgs: Vec::new(),
};
// Every coloured text must read on the window, the zebra rows and the
// KPI cards in both themes; tune the fixed hues to those backgrounds.
p.text_bgs = vec![win, p.zebra, p.card_bg];
p.label = p.text(p.label);
p.green = p.text(p.green);
p.amber = p.text(p.amber);
p.red = p.text(p.red);
p.accent = p.text(p.accent);
p
}
pub struct App {
@@ -791,6 +869,15 @@ pub struct App {
// Settings > Logs page (round 15 #13).
logs_dialog: Widget,
logs_text: Widget,
// Bug report dialog (bug_report.rs): its controls, the send in flight and
// the last filed issue's link.
report_dialog: Widget,
report_kind: Widget,
report_text: Widget,
report_log: Widget,
report_status: Widget,
report_rx: Option<std::sync::mpsc::Receiver<Result<crate::bug_report::Filed, String>>>,
report_url: Option<String>,
// Restore-to-stock (round 12 #8a): background job + its typed-confirm dialog.
restore: Option<restore::RestoreHandle>,
restore_dialog: Widget,
@@ -1003,11 +1090,20 @@ pub fn build(frame: Widget) -> App {
ctl.add_spacer(8);
// Play / pause / stop as coloured media icons (green / amber / red). Vivid
// fixed colours read well on both light and dark themes.
let play_btn = Widget::media_button(panel, ID_PLAY, 0, (56, 190, 108));
// The icon colours are kept to at least 3:1 against this theme's button
// face (the fixed hues alone were about 2:1 on light buttons). Native
// buttons do not all paint the system button-face colour (macOS draws
// #ebebeb on a white window), so also check the window colour and a shade
// 20 steps toward the opposite end.
let win = wx_rs::system_color(0);
let shade = if luminance(win) < 128.0 { 20 } else { -20 };
let face = [wx_rs::system_color(3), win, nudge(win, shade)];
let icon = |c| readable_at(c, &face, MIN_ICON_CONTRAST);
let play_btn = Widget::media_button(panel, ID_PLAY, 0, icon((56, 190, 108)));
ctl.add(play_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 2);
let pause_btn = Widget::media_button(panel, ID_PAUSE, 1, (240, 172, 40));
let pause_btn = Widget::media_button(panel, ID_PAUSE, 1, icon((240, 172, 40)));
ctl.add(pause_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 2);
let stop_btn = Widget::media_button(panel, ID_STOP, 2, (226, 72, 60));
let stop_btn = Widget::media_button(panel, ID_STOP, 2, icon((226, 72, 60)));
ctl.add(stop_btn, 0, sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_ALL, 2);
ctl.add_stretch(1);
// Toolbar buttons; labels are localized and tracked for live re-labelling
@@ -1024,6 +1120,7 @@ pub fn build(frame: Widget) -> App {
let _filter_btn = tbtn("Filter", ID_FILTER);
let _columns_btn = tbtn("Columns", ID_COLUMNS);
let _export_btn = tbtn("Export", ID_EXPORT);
let _report_btn = tbtn("Bug report", ID_BUG_REPORT);
// The old Credentials button is now a Settings menu (round 15 #13).
let _creds_btn = tbtn("Settings", ID_SETTINGS);
root.add_sizer(ctl, 0, sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT, 4);
@@ -1149,6 +1246,13 @@ pub fn build(frame: Widget) -> App {
logview_dialog: frame,
logs_dialog: frame,
logs_text: frame,
report_dialog: frame,
report_kind: frame,
report_text: frame,
report_log: frame,
report_status: frame,
report_rx: None,
report_url: None,
restore: None,
restore_dialog: frame,
pending_restore: Vec::new(),
@@ -1417,7 +1521,10 @@ impl App {
let color = (!r.errors.is_empty()).then_some(self.pal.red);
encode_flag_cell(&c.cell(r), color)
}
Column::Firmware => encode_flag_cell(&c.cell(r), firmware_color(&r.firmware)),
Column::Firmware => encode_flag_cell(
&c.cell(r),
firmware_color(&r.firmware).map(|fc| self.pal.text(fc)),
),
Column::Status => {
// The status reads as its KPI icon (round 12 #11), drawn
// in the status colour so it matches the dashboard card.
@@ -3628,6 +3735,7 @@ impl App {
self.drain_refresh();
self.drain_access_probe();
self.drain_restore();
self.drain_report();
// A completed background log fetch yields a viewer dialog to show; return
// it so `dispatch` presents it modally after the App borrow is released
// (round 18 #12).
@@ -4348,6 +4456,168 @@ impl App {
/// Build (but do not show) the Logs page (round 15 #13): the in-app activity
/// log with Refresh / Clear / Close.
/// The Bug report dialog: a type (Bug, UI change, Feature request), one text
/// box whose first line becomes the issue title, and whether to include the
/// recent activity log (scrubbed). Send files it through the report relay
/// (bug_report.rs) and shows the issue number, or the reason, in the dialog.
fn build_report_dialog(&mut self) -> Widget {
let frame = Widget::main_frame();
let t = crate::i18n::t;
let dlg = Widget::dialog(frame, &t("Bug report"));
let root = Widget::box_sizer(true);
let kindrow = Widget::box_sizer(false);
kindrow.add(
Widget::static_text(dlg, &t("Type")),
0,
sf::ALIGN_CENTRE_VERTICAL | sf::BORDER_RIGHT,
8,
);
let kind = Widget::choice(dlg, ID_REPORT_KIND);
for k in crate::bug_report::Kind::ALL {
kind.choice_append(&t(k.label()));
}
kind.choice_set_selection(0);
kindrow.add(kind, 0, sf::ALIGN_CENTRE_VERTICAL, 0);
root.add_sizer(kindrow, 0, sf::BORDER_ALL, 8);
root.add(
Widget::static_text(
dlg,
&t("Describe the problem or the idea. The first line becomes the title."),
),
0,
sf::BORDER_LEFT | sf::BORDER_RIGHT,
8,
);
let text = Widget::text_ctrl(dlg, true);
text.set_min_size(560, 220);
root.add(text, 1, sf::EXPAND | sf::BORDER_ALL, 8);
let log = Widget::check_box(
dlg,
&t("Include the recent activity log (passwords removed)"),
);
log.set_checked(true);
root.add(log, 0, sf::BORDER_LEFT | sf::BORDER_RIGHT, 8);
root.add(
Widget::static_text(
dlg,
&t("Reports are public issues on gitea.bluefla.re. The app version and OS are included."),
),
0,
sf::BORDER_ALL,
8,
);
let status = Widget::static_text(dlg, "");
root.add(
status,
0,
sf::EXPAND | sf::BORDER_LEFT | sf::BORDER_RIGHT,
8,
);
let btns = Widget::box_sizer(false);
let open = Widget::button(dlg, &t("Open issue"), ID_REPORT_OPEN);
btns.add(open, 0, sf::BORDER_ALL, 6);
btns.add_stretch(1);
btns.add(
Widget::button(dlg, &t("Close"), ID_REPORT_CLOSE),
0,
sf::BORDER_ALL,
6,
);
btns.add(
Widget::button(dlg, &t("Send"), ID_REPORT_SEND),
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(600, 440);
dlg.fit(dlg);
self.report_dialog = dlg;
self.report_kind = kind;
self.report_text = text;
self.report_log = log;
self.report_status = status;
self.report_url = None;
dlg
}
/// Validate the Bug report dialog and send it on a worker thread.
fn send_report(&mut self) {
let t = crate::i18n::t;
if self.report_rx.is_some() {
return; // one send at a time
}
let text = self.report_text.get_value();
let text = text.trim();
if text.is_empty() {
self.report_status
.set_label(&t("Write what happened or what you would like first."));
return;
}
if text.chars().count() > crate::bug_report::MAX_TEXT_CHARS {
self.report_status.set_label(&format!(
"{} ({} / {})",
t("The text is too long"),
text.chars().count(),
crate::bug_report::MAX_TEXT_CHARS
));
return;
}
let kinds = crate::bug_report::Kind::ALL;
let kind = usize::try_from(self.report_kind.choice_get_selection())
.ok()
.and_then(|i| kinds.get(i).copied())
.unwrap_or(crate::bug_report::Kind::Bug);
let size = wx_rs::get_client_size();
let ctx = crate::bug_report::Context::current(
wx_rs::is_dark(),
crate::i18n::current().code(),
(size.width as i32, size.height as i32),
);
let log = self
.report_log
.is_checked()
.then(|| crate::bug_report::recent_log(&crate::logs::dump()));
let body = crate::bug_report::payload(kind, text, &ctx, log.as_deref());
self.report_status.set_label(&t("Sending..."));
self.report_rx = Some(crate::bug_report::send(body));
}
/// Show the Bug report result in the dialog once the send finishes.
fn drain_report(&mut self) {
let Some(rx) = &self.report_rx else { return };
let result = match rx.try_recv() {
Ok(r) => r,
Err(std::sync::mpsc::TryRecvError::Empty) => return,
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
Err("the report was not sent".to_string())
}
};
self.report_rx = None;
let t = crate::i18n::t;
match result {
Ok(filed) => {
crate::logs::log(format!("bug report filed as issue #{}", filed.number));
self.report_status.set_label(&format!(
"{} #{}",
t("Thank you. Filed as issue"),
filed.number
));
self.report_text.set_value("");
self.report_kind.choice_set_selection(0);
if !filed.url.is_empty() {
self.report_url = Some(filed.url);
}
}
Err(e) => {
crate::logs::log(format!("bug report not sent: {e}"));
self.report_status
.set_label(&format!("{} {e}", t("Not sent:")));
}
}
}
fn build_logs_dialog(&mut self) -> Widget {
let frame = Widget::main_frame();
let dlg = Widget::dialog(frame, &crate::i18n::t("Logs"));
@@ -4933,6 +5203,17 @@ impl App {
ID_EXPORT_ALL => self.export_check_all(true),
ID_EXPORT_NONE => self.export_check_all(false),
ID_SETTINGS => self.open_settings_menu(),
ID_BUG_REPORT => return Some(self.build_report_dialog()),
ID_REPORT_SEND => self.send_report(),
ID_REPORT_CLOSE => self.report_dialog.close(0),
ID_REPORT_OPEN => match &self.report_url {
Some(url) => {
wx_rs::launch_browser(url);
}
None => self.report_status.set_label(&crate::i18n::t(
"Send a report first; its issue opens here.",
)),
},
ID_LOGS_REFRESH => self.logs_text.set_value(&crate::logs::dump()),
ID_LOGS_CLEAR => {
crate::logs::clear();
@@ -5390,9 +5671,35 @@ mod tests {
red: (255, 0, 0),
accent: (0, 0, 255),
zebra: (245, 245, 245),
text_bgs: Vec::new(),
}
}
#[test]
fn readable_reaches_aa_contrast_on_every_background() {
let light = [(255, 255, 255), (245, 245, 245)];
let dark = [(30, 30, 30), (41, 41, 41), (44, 44, 44)];
for fw in [
"braiins", "luxos", "vnish", "bixbit", "epic", "hiveon", "alkane",
] {
let c = firmware_color(fw).unwrap();
for bgs in [&light[..], &dark[..]] {
let r = readable(c, bgs);
for &bg in bgs {
assert!(
contrast_ratio(r, bg) >= MIN_TEXT_CONTRAST,
"{fw} {r:?} on {bg:?}"
);
}
}
}
// Already-readable colours are left alone; hue survives the nudge.
assert_eq!(readable((0, 0, 0), &light), (0, 0, 0));
let gold = readable((185, 150, 20), &light);
assert!(gold.0 > gold.1 && gold.1 > gold.2, "still gold: {gold:?}");
assert!((contrast_ratio((255, 255, 255), (0, 0, 0)) - 21.0).abs() < 0.01);
}
#[test]
fn tint_toward_endpoints_and_midpoint() {
let a = (10, 20, 30);
+62
View File
@@ -0,0 +1,62 @@
# macOS
The macOS build is one universal app, `Alkane Toolkit.app`, that runs natively
on Apple Silicon and Intel Macs with macOS 11 (Big Sur) or later. It ships as
`alkane-toolkit-vX.Y.Z-macos-universal.zip`, holding the app, `LICENSE` and a
`README.txt`.
## Using it
- Settings live in `~/.config/alkane-toolkit`, the same files as on Linux.
- SSH actions use the system `ssh`; HTTP actions use the system `curl`.
- The app is ad-hoc signed but not notarized, so macOS blocks the first launch.
On macOS 15 or later, try to open it, then choose **Open Anyway** in
System Settings > Privacy & Security. On macOS 11 to 14, Control-click the
app and choose **Open**. Or clear the quarantine flag once:
`xattr -dr com.apple.quarantine "/Applications/Alkane Toolkit.app"`.
- The first scan asks for permission to find devices on the local network
(macOS 15 and later). Scanning needs it.
- Offline demo: `ALKANE_FAKE_SCAN=1 "/Applications/Alkane Toolkit.app/Contents/MacOS/alkane-toolkit"`.
## Building
macOS builds only on a Mac. It needs the Xcode Command Line Tools
(`xcode-select --install`) and rustup with both Apple targets:
```sh
rustup target add aarch64-apple-darwin x86_64-apple-darwin
scripts/build-wx-macos.sh # once: universal static wxOSX 3.3.3 in ~/.cache/wx-3.3.3-macos
scripts/build-gui-macos.sh # both slices, merged with lipo
python3 ci/package-macos.py X.Y.Z target/universal-apple-darwin/release/alkane-toolkit dist
```
`build-wx-macos.sh` builds wxWidgets once for arm64 and x86_64 together, with
wx's bundled image, zlib, regex and expat libraries, so the app links only
against system frameworks. `build-gui-macos.sh` builds each Rust target
against that prefix and merges them. `package-macos.py` writes the bundle
(Info.plist, icon), ad-hoc signs it and zips it with `ditto`.
To test on the Mac:
```sh
ALKANE_SELFTEST=1 target/universal-apple-darwin/release/alkane-toolkit # headless UI self-test
cargo test --release --offline --target aarch64-apple-darwin --workspace --exclude wx-rs
```
The self-test opens a window briefly, so it needs a logged-in desktop
session. Running the x86_64 slice on Apple Silicon needs Rosetta 2.
## Why macOS 11
Current Xcode SDKs ship a libc++ that supports macOS 11 and later only;
building for an older target compiles, but the app could fail at run time on
the older system. macOS 11 is also the first release for Apple Silicon. Older
Intel Macs would need a build with an older SDK.
## Notes for the bridge
wxOSX reports logical (point) sizes, like the GTK and MSW bridges; the
bridge never scales to physical pixels. wxOSX's Objective-C uses
`@available`, so `build.rs` links clang's compiler runtime
(`libclang_rt.osx.a`), which rustc's link does not include by itself. Both
are wx-rs patch `0009`.
+11
View File
@@ -14,6 +14,7 @@ Get the latest release from the
|---|---|---|
| Windows 10 and 11 (x86_64) | `alkane-toolkit-vX.Y.Z-windows-x86_64.zip` | One signed `alkane-toolkit.exe`; nothing to install |
| Linux (x86_64) | `alkane-toolkit-vX.Y.Z-linux-x86_64.tar.gz` | One binary; needs GTK 3 (`sudo apt install libgtk-3-0`) |
| macOS 11 or later (Apple Silicon and Intel) | `alkane-toolkit-vX.Y.Z-macos-universal.zip` | One app; not notarized, so confirm the first launch |
### Windows
@@ -23,6 +24,16 @@ Get the latest release from the
the verified publisher. To check the signature yourself, right-click the exe,
choose **Properties** and open **Digital Signatures**.
### macOS
1. Download the zip and open it; drag **Alkane Toolkit** to Applications.
2. Open it. The app is not notarized, so macOS blocks the first launch:
- macOS 15 or later: open **System Settings > Privacy & Security** and
choose **Open Anyway** for Alkane Toolkit.
- macOS 11 to 14: Control-click the app, choose **Open**, then **Open**.
3. When asked, allow it to find devices on your local network. Scanning
needs this.
### Linux
```sh
+18
View File
@@ -26,3 +26,21 @@ report them. Try **Check access** and the Credentials dialog.
**The window is too small or too large.** It opens at most as large as your
screen's work area. Use the zoom slider in the status bar to scale the table.
## Reporting a problem
**Bug report** in the toolbar, between Export and Settings, files a bug or a
feature request with the developers. Choose the type (Bug, UI change or
Feature request) and describe it; the first line becomes the title. **Send**
shows the issue number when it is filed, and **Open issue** opens it in the
browser. If it was not sent, the reason is shown and the text stays in the
box.
A report includes the app version, the operating system, the light or dark
appearance, the language and the window size. **Include the recent activity
log** adds the newest lines of the Settings > Logs page, with passwords,
tokens and similar values removed first; untick it to leave the log out.
Reports become public issues on
[gitea.bluefla.re](https://gitea.bluefla.re/blueflare/alkane-toolkit/issues),
so do not put passwords or customer details in the text. A computer can send
five reports an hour.
+25 -6
View File
@@ -1,9 +1,28 @@
#!/bin/sh
# Build the alkane-toolkit GUI on macOS.
# wx-rs upstream supports macOS: its build.rs clones/builds wx 3.2.2.1 into
# ./dist (osx-release-build) on first build. Requires Xcode Command Line
# Tools. Run this ON a Mac - macOS cannot be linked from Linux.
# Build the alkane-toolkit GUI on macOS as one universal binary.
#
# scripts/build-gui-macos.sh [extra cargo build args]
# -> target/universal-apple-darwin/release/alkane-toolkit (arm64 + x86_64)
#
# Needs the Xcode Command Line Tools, rustup with the aarch64-apple-darwin and
# x86_64-apple-darwin targets, and the universal static wx prefix from
# scripts/build-wx-macos.sh (WX_MACOS_PREFIX, default ~/.cache/wx-3.3.3-macos).
# Each target is built separately against that prefix, then the two slices
# are merged with lipo. MACOSX_DEPLOYMENT_TARGET matches the wx build.
set -e
cd "$(dirname -- "$0")/.."
cargo build --release -p alkane-gui
echo "binary: target/release/alkane-toolkit (app bundle packaging is follow-up)"
PREFIX="${WX_MACOS_PREFIX:-$HOME/.cache/wx-3.3.3-macos}"
[ -x "$PREFIX/bin/wx-config" ] || {
echo "no wx prefix at $PREFIX; run scripts/build-wx-macos.sh first" >&2; exit 1; }
export WX_RS_WXCONFIG="$PREFIX/bin/wx-config"
export MACOSX_DEPLOYMENT_TARGET="${MACOSX_MIN:-11.0}"
for target in aarch64-apple-darwin x86_64-apple-darwin; do
cargo build --release --offline -p alkane-gui --target "$target" "$@"
done
out=target/universal-apple-darwin/release
mkdir -p "$out"
lipo -create -output "$out/alkane-toolkit" \
target/aarch64-apple-darwin/release/alkane-toolkit \
target/x86_64-apple-darwin/release/alkane-toolkit
lipo -info "$out/alkane-toolkit"
echo "binary: $out/alkane-toolkit"
+43
View File
@@ -0,0 +1,43 @@
#!/bin/sh
# Build the vendored wxWidgets 3.3.3 for macOS: wxOSX/Cocoa, static,
# monolithic, and universal (arm64 + x86_64) so one prefix serves both Rust
# targets. Run on a Mac with the Xcode Command Line Tools.
#
# scripts/build-wx-macos.sh -> ~/.cache/wx-3.3.3-macos
#
# Dependency tracking is off: its bootstrap fails with several -arch flags.
# MACOSX_MIN (default 11.0) is the oldest macOS the app runs on, for both
# slices: the libc++ in current SDKs supports nothing older, and macOS 11 is
# also where Apple Silicon starts. Image/zlib/regex/expat libraries are
# wx's bundled copies, so the app links only against system frameworks.
# The repo's vendor tree stays pristine; the build lives in ~/.cache/wxbuild.
set -e
REPO=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
SRC="$REPO/thirdparty/wxwidgets/wxWidgets-3.3.3"
BUILD="$HOME/.cache/wxbuild/wxWidgets-3.3.3-macos"
PREFIX="${WX_MACOS_PREFIX:-$HOME/.cache/wx-3.3.3-macos}"
MACOSX_MIN="${MACOSX_MIN:-11.0}"
JOBS="${JOBS:-$(sysctl -n hw.ncpu)}"
[ -d "$SRC" ] || { echo "thirdparty wxWidgets tree missing: $SRC" >&2; exit 1; }
rm -rf "$BUILD"
mkdir -p "$HOME/.cache/wxbuild"
cp -R "$SRC" "$BUILD"
trap 'rm -rf "$BUILD"' 0
chmod +x "$BUILD/configure" "$BUILD/config.guess" "$BUILD/config.sub" "$BUILD/install-sh" 2>/dev/null || true
mkdir -p "$BUILD/build-macos"
cd "$BUILD/build-macos"
sh ../configure --prefix="$PREFIX" --with-osx_cocoa \
--disable-shared --enable-monolithic \
--enable-universal_binary=arm64,x86_64 \
--with-macosx-version-min="$MACOSX_MIN" \
--disable-dependency-tracking \
--with-libpng=builtin --with-libjpeg=builtin --without-libtiff \
--with-zlib=builtin --with-expat=builtin --with-regex=builtin \
--without-liblzma --without-libcurl \
--with-opengl=no --disable-stc --disable-webview --disable-mediactrl \
--disable-tests
make -j"$JOBS"
make install
lib=$(ls "$PREFIX"/lib/libwx_osx_cocoau-3.3*.a | head -1)
lipo -info "$lib"
echo "installed: $PREFIX ($lib)"
+140
View File
@@ -0,0 +1,140 @@
Subject: [PATCH 0009] macOS: wxOSX through wx-config, logical sizes, clang runtime
--- a/build.rs
+++ b/build.rs
@@ -42,12 +42,12 @@
target_os() == "linux"
}
-// alkane-toolkit patch: Linux (wxGTK) and Windows/GNU (wxMSW built with
-// mingw-w64, natively or cross from Linux) both build against a wx prefix
-// described by its wx-config script, so they share one code path. Upstream's
-// MSVC/nmake and macOS download-and-build paths are left as they were.
+// alkane-toolkit patch: Linux (wxGTK), macOS (wxOSX/Cocoa, universal static
+// build) and Windows/GNU (wxMSW built with mingw-w64, natively or cross from
+// Linux) all build against a wx prefix described by its wx-config script, so
+// they share one code path. Upstream's MSVC/nmake path is left as it was.
fn uses_wx_config() -> bool {
- is_linux() || (is_windows() && !is_msvc())
+ is_linux() || is_osx() || (is_windows() && !is_msvc())
}
fn is_stub() -> bool {
@@ -278,7 +278,14 @@
build.cpp(true)
.file("cpp_src/wxbridge.cpp")
.file("cpp_src/widgets_bridge.cpp");
- for f in cxxflags.split_whitespace() {
+ let mut flags = cxxflags.split_whitespace();
+ while let Some(f) = flags.next() {
+ // A universal wx prefix reports `-arch arm64 -arch x86_64`; cargo builds
+ // one target at a time and cc adds that target's own -arch.
+ if is_osx() && f == "-arch" {
+ flags.next();
+ continue;
+ }
build.flag(f);
}
if is_linux() {
@@ -308,7 +315,53 @@
}
build.compile("wxbridge");
- for flag in libs.split_whitespace() {
+ if is_osx() {
+ // wxOSX's Objective-C uses @available, which calls
+ // ___isPlatformVersionAtLeast from clang's compiler runtime. clang
+ // links that runtime itself, but rustc drives the link without it.
+ let resource_dir = Command::new("clang")
+ .arg("-print-resource-dir")
+ .output()
+ .ok()
+ .filter(|o| o.status.success())
+ .and_then(|o| String::from_utf8(o.stdout).ok())
+ .map(|s| s.trim().to_string())
+ .ok_or("wx-rs: cannot ask clang for its resource directory")?;
+ let rt = Path::new(&resource_dir).join("lib").join("darwin");
+ if !rt.join("libclang_rt.osx.a").exists() {
+ return Err(format!("wx-rs: no libclang_rt.osx.a in {}", rt.display()).into());
+ }
+ println!("cargo:rustc-link-search=native={}", rt.display());
+ println!("cargo:rustc-link-lib=static=clang_rt.osx");
+ }
+
+ let mut lib_flags = libs.split_whitespace();
+ while let Some(flag) = lib_flags.next() {
+ if is_osx() {
+ match flag {
+ "-arch" => {
+ lib_flags.next();
+ continue;
+ }
+ "-framework" => {
+ if let Some(name) = lib_flags.next() {
+ println!("cargo:rustc-link-lib=framework={}", name);
+ }
+ continue;
+ }
+ "-weak_framework" => {
+ if let Some(name) = lib_flags.next() {
+ println!("cargo:rustc-link-arg=-Wl,-weak_framework,{}", name);
+ }
+ continue;
+ }
+ _ if flag.starts_with("-Wl,") => {
+ println!("cargo:rustc-link-arg={}", flag);
+ continue;
+ }
+ _ => {}
+ }
+ }
if flag.ends_with(".a") && Path::new(flag).is_absolute() {
// static wx builds: wx-config emits the archive as a full path
let file = Path::new(flag).file_name().and_then(|f| f.to_str()).unwrap_or(flag);
--- a/cpp_src/wxbridge.cpp
+++ b/cpp_src/wxbridge.cpp
@@ -492,21 +492,12 @@
// The logical window size
Size get_client_size() {
-#ifdef __APPLE__
- int status_bar_height;
- if (wxGetApp().frame->GetStatusBar()) {
- status_bar_height = wxGetApp().frame->GetStatusBar()->GetRect().height;
- } else {
- status_bar_height = 0;
- }
- float scale_factor = wxGetApp().frame->GetDPIScaleFactor();
- wxSize s = wxGetApp().frame->GetClientSize();
- struct Size sz = {s.x * scale_factor, (s.y + status_bar_height) * scale_factor};
- return sz;
-#elif defined(__linux__)
- // alkane-toolkit patch: wxGTK3 path (upstream only knew Apple vs Windows)
+#if defined(__linux__) || defined(__APPLE__)
+ // alkane-toolkit patch: wxGTK3 and wxOSX both report logical (DIP) sizes,
+ // like every other bridge size. Upstream's Apple branch scaled to physical
+ // pixels through a float -> int narrowing current clang rejects.
wxSize s = wxGetApp().frame->GetClientSize();
- return Size {(unsigned int)s.x, (unsigned int)s.y};
+ return Size {s.x, s.y};
#else
HWND hwnd = (HWND) wxGetApp().frame->GetHandle();
RECT rect;
@@ -559,15 +550,10 @@
}
Size get_mouse_position(wxMouseEvent &event) {
-#ifdef __APPLE__
- wxPoint p = event.GetPosition();
- float scale_factor = wxGetApp().frame->GetDPIScaleFactor();
- struct Size s = {p.x * scale_factor, p.y * scale_factor};
- return s;
-#else
+ // alkane-toolkit patch: logical (DIP) coordinates on every platform,
+ // matching get_client_size (upstream scaled them on Apple).
wxPoint p = event.GetPosition();
return Size {p.x, p.y};
-#endif
}
int get_mouse_wheel_rotation(wxMouseEvent &event) {
+162
View File
@@ -0,0 +1,162 @@
Subject: [PATCH 0010] macOS: native alternating rows, theme text colour, self-test snapshot
--- a/build.rs
+++ b/build.rs
@@ -278,6 +278,12 @@
build.cpp(true)
.file("cpp_src/wxbridge.cpp")
.file("cpp_src/widgets_bridge.cpp");
+ if is_osx() {
+ // Cocoa calls wx does not wrap (native alternating rows, self-test
+ // snapshot); clang compiles .mm as Objective-C++.
+ println!("cargo:rerun-if-changed=cpp_src/osx_native.mm");
+ build.file("cpp_src/osx_native.mm");
+ }
let mut flags = cxxflags.split_whitespace();
while let Some(f) = flags.next() {
// A universal wx prefix reports `-arch arm64 -arch x86_64`; cargo builds
--- a/cpp_src/widgets_bridge.cpp
+++ b/cpp_src/widgets_bridge.cpp
@@ -597,7 +597,11 @@
} else if (GetAttr().HasBackgroundColour()) {
cellbg = GetAttr().GetBackgroundColour();
} else {
+#ifdef __WXOSX__
+ cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
+#else
cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
+#endif
}
x += DrawShape(dc, shape, x, cell, seg.has_colour ? seg.colour : plain, cellbg);
continue;
@@ -688,6 +692,11 @@
(unsigned char)(c.blue * 255.0 + 0.5));
}
}
+#elif defined(__WXOSX__)
+ // The DC arrives with the control's foreground colour, which is not the
+ // theme-aware label colour native cells use; take the list text colour so
+ // uncoloured segments follow light/dark like the native columns.
+ return wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
#endif
return wxColour();
}
@@ -1463,8 +1472,21 @@
// Set (or clear, when r<0) the alternating-row (zebra) background colour. The
// app passes a theme-aware colour; odd rows get it (see AttrListStore).
+#ifdef __WXOSX__
+extern "C" void wxbr_osx_set_alternating_rows(void* win, int on);
+#endif
void wxbr_dvlc_set_zebra(void* win, int r, int g, int b) {
+#ifdef __WXOSX__
+ // wxOSX draws custom cells without clipping them to the cell, so the
+ // renderer's over-sized background fill (which GTK/MSW clip) painted over
+ // neighbouring cells and hid their text until a row was selected. Use the
+ // table's native alternating rows instead: AppKit paints them under every
+ // cell, in the theme's colours, and no row carries a background attribute.
+ dvlc_store(win)->zebra = wxColour();
+ wxbr_osx_set_alternating_rows(win, r >= 0 ? 1 : 0);
+#else
dvlc_store(win)->zebra = (r < 0) ? wxColour() : wxColour(r, g, b);
+#endif
}
// The model-row index of the first (top) visible row, or -1 if none. Used to
--- a/cpp_src/osx_native.mm
+++ b/cpp_src/osx_native.mm
@@ -0,0 +1,60 @@
+// osx_native.mm -- alkane-toolkit extension: the few wxOSX/Cocoa calls the
+// bridge needs that wx does not wrap. Compiled on macOS only (see build.rs).
+
+#include <wx/wx.h>
+#include <wx/dataview.h>
+
+#import <Cocoa/Cocoa.h>
+
+// The NSTableView (NSOutlineView) behind a wxDataViewCtrl. wx's handle is the
+// enclosing NSScrollView.
+static NSTableView* TableOf(wxWindow* win) {
+ if (!win) return nil;
+ id h = (id)win->GetHandle();
+ if ([h isKindOfClass:[NSTableView class]]) return (NSTableView*)h;
+ if ([h isKindOfClass:[NSScrollView class]]) {
+ id doc = [(NSScrollView*)h documentView];
+ if ([doc isKindOfClass:[NSTableView class]]) return (NSTableView*)doc;
+ }
+ return nil;
+}
+
+extern "C" {
+
+// Native alternating-row backgrounds. AppKit paints them under every cell,
+// custom-drawn ones included, in the theme's own colours (light and dark), so
+// custom renderers never paint row backgrounds on macOS.
+void wxbr_osx_set_alternating_rows(void* win, int on) {
+ NSTableView* tv = TableOf((wxWindow*)win);
+ if (tv) [tv setUsesAlternatingRowBackgroundColors:(on ? YES : NO)];
+}
+
+// Write a PNG of a top-level window's content, rendered by the app itself
+// (no screen-recording permission needed). Used by the self-test with
+// ALKANE_SNAPSHOT=path to check what the table really draws. Returns 1 on
+// success.
+int wxbr_osx_snapshot(void* win, const char* path) {
+ wxWindow* w = (wxWindow*)win;
+ if (!w || !path) return 0;
+ wxTopLevelWindow* tlw = wxDynamicCast(wxGetTopLevelParent(w), wxTopLevelWindow);
+ if (!tlw) return 0;
+ tlw->Show();
+ tlw->Raise();
+ // Let the window lay out and paint before capturing it.
+ for (int i = 0; i < 30; ++i) {
+ wxYield();
+ wxMilliSleep(30);
+ }
+ NSWindow* nsw = (NSWindow*)tlw->GetWXWindow();
+ NSView* view = [nsw contentView];
+ if (!view) return 0;
+ NSRect rect = [view bounds];
+ NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:rect];
+ if (!rep) return 0;
+ [view cacheDisplayInRect:rect toBitmapImageRep:rep];
+ NSData* png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
+ if (!png) return 0;
+ return [png writeToFile:[NSString stringWithUTF8String:path] atomically:YES] ? 1 : 0;
+}
+
+}
--- a/src/widgets.rs
+++ b/src/widgets.rs
@@ -107,6 +107,8 @@
pub fn wxbr_launch_browser(url: *const c_char) -> c_int;
pub fn wxbr_set_appearance_system();
pub fn wxbr_is_dark() -> c_int;
+ #[cfg(target_os = "macos")]
+ pub fn wxbr_osx_snapshot(win: *mut c_void, path: *const c_char) -> c_int;
pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8);
pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
@@ -271,6 +273,22 @@
unsafe { notsafe::wxbr_is_dark() != 0 }
}
+/// Write a PNG of `win`'s top-level window as the app itself renders it (shows
+/// the window and lets it paint first). macOS only, for the self-test's
+/// `ALKANE_SNAPSHOT`; returns false elsewhere or on failure.
+pub fn snapshot_png(win: &Widget, path: &str) -> bool {
+ #[cfg(target_os = "macos")]
+ {
+ let Ok(p) = CString::new(path) else { return false };
+ unsafe { notsafe::wxbr_osx_snapshot(win.0, p.as_ptr()) != 0 }
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ let _ = (win, path);
+ false
+ }
+}
+
/// A theme colour. `which`: 0=window bg, 1=window text, 2=highlight,
/// 3=button face, 4=grey text. Returns (r, g, b).
pub fn system_color(which: i32) -> (u8, u8, u8) {
@@ -0,0 +1,17 @@
Subject: [PATCH 0011] macOS self-test snapshot includes the window background
--- a/cpp_src/osx_native.mm
+++ b/cpp_src/osx_native.mm
@@ -46,7 +46,11 @@
wxMilliSleep(30);
}
NSWindow* nsw = (NSWindow*)tlw->GetWXWindow();
- NSView* view = [nsw contentView];
+ // The content view's superview (the window's frame view) also draws the
+ // window background and title bar. The content view alone leaves every
+ // area the app's panels do not paint transparent, which read as white.
+ NSView* view = [[nsw contentView] superview];
+ if (!view) view = [nsw contentView];
if (!view) return 0;
NSRect rect = [view bounds];
NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:rect];
+3
View File
@@ -27,6 +27,9 @@ later changes go in new patches.
| 0006 | Build: platform from the target, not the host; Windows/GNU via `wx-config` |
| 0007 | `wxbridge.cpp` on wxMSW: no input-swallowing overlay, appearance before the first window, work-area sizing, executable icon, UTF-8 menu fix |
| 0008 | Widget bridge on wxMSW: generic data-view input and tooltips, DIP boundary, anti-aliased shapes, themed selection colours |
| 0009 | macOS: wxOSX (universal static) through `wx-config`, logical sizes on Apple, clang compiler runtime for `@available` |
| 0010 | macOS table: native alternating rows instead of the unclipped custom-cell background fill (it hid neighbouring cells' text), theme-aware text colour, `osx_native.mm`, self-test window snapshot |
| 0011 | macOS self-test snapshot captures the window's frame view, so areas the app does not paint show the real window background instead of transparency |
History: the round 19 health-tooltip fix (GTK tip area converted from
bin-window to widget coordinates) used to be a separate patch on top of an
+66 -7
View File
@@ -42,12 +42,12 @@ fn is_linux() -> bool {
target_os() == "linux"
}
// alkane-toolkit patch: Linux (wxGTK) and Windows/GNU (wxMSW built with
// mingw-w64, natively or cross from Linux) both build against a wx prefix
// described by its wx-config script, so they share one code path. Upstream's
// MSVC/nmake and macOS download-and-build paths are left as they were.
// alkane-toolkit patch: Linux (wxGTK), macOS (wxOSX/Cocoa, universal static
// build) and Windows/GNU (wxMSW built with mingw-w64, natively or cross from
// Linux) all build against a wx prefix described by its wx-config script, so
// they share one code path. Upstream's MSVC/nmake path is left as it was.
fn uses_wx_config() -> bool {
is_linux() || (is_windows() && !is_msvc())
is_linux() || is_osx() || (is_windows() && !is_msvc())
}
fn is_stub() -> bool {
@@ -278,7 +278,20 @@ fn build_with_wx_config() -> Result<(), Box<dyn Error>> {
build.cpp(true)
.file("cpp_src/wxbridge.cpp")
.file("cpp_src/widgets_bridge.cpp");
for f in cxxflags.split_whitespace() {
if is_osx() {
// Cocoa calls wx does not wrap (native alternating rows, self-test
// snapshot); clang compiles .mm as Objective-C++.
println!("cargo:rerun-if-changed=cpp_src/osx_native.mm");
build.file("cpp_src/osx_native.mm");
}
let mut flags = cxxflags.split_whitespace();
while let Some(f) = flags.next() {
// A universal wx prefix reports `-arch arm64 -arch x86_64`; cargo builds
// one target at a time and cc adds that target's own -arch.
if is_osx() && f == "-arch" {
flags.next();
continue;
}
build.flag(f);
}
if is_linux() {
@@ -308,7 +321,53 @@ fn build_with_wx_config() -> Result<(), Box<dyn Error>> {
}
build.compile("wxbridge");
for flag in libs.split_whitespace() {
if is_osx() {
// wxOSX's Objective-C uses @available, which calls
// ___isPlatformVersionAtLeast from clang's compiler runtime. clang
// links that runtime itself, but rustc drives the link without it.
let resource_dir = Command::new("clang")
.arg("-print-resource-dir")
.output()
.ok()
.filter(|o| o.status.success())
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|s| s.trim().to_string())
.ok_or("wx-rs: cannot ask clang for its resource directory")?;
let rt = Path::new(&resource_dir).join("lib").join("darwin");
if !rt.join("libclang_rt.osx.a").exists() {
return Err(format!("wx-rs: no libclang_rt.osx.a in {}", rt.display()).into());
}
println!("cargo:rustc-link-search=native={}", rt.display());
println!("cargo:rustc-link-lib=static=clang_rt.osx");
}
let mut lib_flags = libs.split_whitespace();
while let Some(flag) = lib_flags.next() {
if is_osx() {
match flag {
"-arch" => {
lib_flags.next();
continue;
}
"-framework" => {
if let Some(name) = lib_flags.next() {
println!("cargo:rustc-link-lib=framework={}", name);
}
continue;
}
"-weak_framework" => {
if let Some(name) = lib_flags.next() {
println!("cargo:rustc-link-arg=-Wl,-weak_framework,{}", name);
}
continue;
}
_ if flag.starts_with("-Wl,") => {
println!("cargo:rustc-link-arg={}", flag);
continue;
}
_ => {}
}
}
if flag.ends_with(".a") && Path::new(flag).is_absolute() {
// static wx builds: wx-config emits the archive as a full path
let file = Path::new(flag).file_name().and_then(|f| f.to_str()).unwrap_or(flag);
@@ -0,0 +1,64 @@
// osx_native.mm -- alkane-toolkit extension: the few wxOSX/Cocoa calls the
// bridge needs that wx does not wrap. Compiled on macOS only (see build.rs).
#include <wx/wx.h>
#include <wx/dataview.h>
#import <Cocoa/Cocoa.h>
// The NSTableView (NSOutlineView) behind a wxDataViewCtrl. wx's handle is the
// enclosing NSScrollView.
static NSTableView* TableOf(wxWindow* win) {
if (!win) return nil;
id h = (id)win->GetHandle();
if ([h isKindOfClass:[NSTableView class]]) return (NSTableView*)h;
if ([h isKindOfClass:[NSScrollView class]]) {
id doc = [(NSScrollView*)h documentView];
if ([doc isKindOfClass:[NSTableView class]]) return (NSTableView*)doc;
}
return nil;
}
extern "C" {
// Native alternating-row backgrounds. AppKit paints them under every cell,
// custom-drawn ones included, in the theme's own colours (light and dark), so
// custom renderers never paint row backgrounds on macOS.
void wxbr_osx_set_alternating_rows(void* win, int on) {
NSTableView* tv = TableOf((wxWindow*)win);
if (tv) [tv setUsesAlternatingRowBackgroundColors:(on ? YES : NO)];
}
// Write a PNG of a top-level window's content, rendered by the app itself
// (no screen-recording permission needed). Used by the self-test with
// ALKANE_SNAPSHOT=path to check what the table really draws. Returns 1 on
// success.
int wxbr_osx_snapshot(void* win, const char* path) {
wxWindow* w = (wxWindow*)win;
if (!w || !path) return 0;
wxTopLevelWindow* tlw = wxDynamicCast(wxGetTopLevelParent(w), wxTopLevelWindow);
if (!tlw) return 0;
tlw->Show();
tlw->Raise();
// Let the window lay out and paint before capturing it.
for (int i = 0; i < 30; ++i) {
wxYield();
wxMilliSleep(30);
}
NSWindow* nsw = (NSWindow*)tlw->GetWXWindow();
// The content view's superview (the window's frame view) also draws the
// window background and title bar. The content view alone leaves every
// area the app's panels do not paint transparent, which read as white.
NSView* view = [[nsw contentView] superview];
if (!view) view = [nsw contentView];
if (!view) return 0;
NSRect rect = [view bounds];
NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:rect];
if (!rep) return 0;
[view cacheDisplayInRect:rect toBitmapImageRep:rep];
NSData* png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
if (!png) return 0;
return [png writeToFile:[NSString stringWithUTF8String:path] atomically:YES] ? 1 : 0;
}
}
@@ -597,7 +597,11 @@ class SegmentRenderer : public wxDataViewCustomRenderer {
} else if (GetAttr().HasBackgroundColour()) {
cellbg = GetAttr().GetBackgroundColour();
} else {
#ifdef __WXOSX__
cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
#else
cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
#endif
}
x += DrawShape(dc, shape, x, cell, seg.has_colour ? seg.colour : plain, cellbg);
continue;
@@ -688,6 +692,11 @@ class SegmentRenderer : public wxDataViewCustomRenderer {
(unsigned char)(c.blue * 255.0 + 0.5));
}
}
#elif defined(__WXOSX__)
// The DC arrives with the control's foreground colour, which is not the
// theme-aware label colour native cells use; take the list text colour so
// uncoloured segments follow light/dark like the native columns.
return wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
#endif
return wxColour();
}
@@ -1463,8 +1472,21 @@ void wxbr_dvlc_set_row_color(void* win, unsigned row, int r, int g, int b) {
// Set (or clear, when r<0) the alternating-row (zebra) background colour. The
// app passes a theme-aware colour; odd rows get it (see AttrListStore).
#ifdef __WXOSX__
extern "C" void wxbr_osx_set_alternating_rows(void* win, int on);
#endif
void wxbr_dvlc_set_zebra(void* win, int r, int g, int b) {
#ifdef __WXOSX__
// wxOSX draws custom cells without clipping them to the cell, so the
// renderer's over-sized background fill (which GTK/MSW clip) painted over
// neighbouring cells and hid their text until a row was selected. Use the
// table's native alternating rows instead: AppKit paints them under every
// cell, in the theme's colours, and no row carries a background attribute.
dvlc_store(win)->zebra = wxColour();
wxbr_osx_set_alternating_rows(win, r >= 0 ? 1 : 0);
#else
dvlc_store(win)->zebra = (r < 0) ? wxColour() : wxColour(r, g, b);
#endif
}
// The model-row index of the first (top) visible row, or -1 if none. Used to
+7 -21
View File
@@ -492,21 +492,12 @@ extern "C" {
// The logical window size
Size get_client_size() {
#ifdef __APPLE__
int status_bar_height;
if (wxGetApp().frame->GetStatusBar()) {
status_bar_height = wxGetApp().frame->GetStatusBar()->GetRect().height;
} else {
status_bar_height = 0;
}
float scale_factor = wxGetApp().frame->GetDPIScaleFactor();
wxSize s = wxGetApp().frame->GetClientSize();
struct Size sz = {s.x * scale_factor, (s.y + status_bar_height) * scale_factor};
return sz;
#elif defined(__linux__)
// alkane-toolkit patch: wxGTK3 path (upstream only knew Apple vs Windows)
#if defined(__linux__) || defined(__APPLE__)
// alkane-toolkit patch: wxGTK3 and wxOSX both report logical (DIP) sizes,
// like every other bridge size. Upstream's Apple branch scaled to physical
// pixels through a float -> int narrowing current clang rejects.
wxSize s = wxGetApp().frame->GetClientSize();
return Size {(unsigned int)s.x, (unsigned int)s.y};
return Size {s.x, s.y};
#else
HWND hwnd = (HWND) wxGetApp().frame->GetHandle();
RECT rect;
@@ -559,15 +550,10 @@ extern "C" {
}
Size get_mouse_position(wxMouseEvent &event) {
#ifdef __APPLE__
wxPoint p = event.GetPosition();
float scale_factor = wxGetApp().frame->GetDPIScaleFactor();
struct Size s = {p.x * scale_factor, p.y * scale_factor};
return s;
#else
// alkane-toolkit patch: logical (DIP) coordinates on every platform,
// matching get_client_size (upstream scaled them on Apple).
wxPoint p = event.GetPosition();
return Size {p.x, p.y};
#endif
}
int get_mouse_wheel_rotation(wxMouseEvent &event) {
+18
View File
@@ -107,6 +107,8 @@ mod notsafe {
pub fn wxbr_launch_browser(url: *const c_char) -> c_int;
pub fn wxbr_set_appearance_system();
pub fn wxbr_is_dark() -> c_int;
#[cfg(target_os = "macos")]
pub fn wxbr_osx_snapshot(win: *mut c_void, path: *const c_char) -> c_int;
pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8);
pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int);
@@ -271,6 +273,22 @@ pub fn is_dark() -> bool {
unsafe { notsafe::wxbr_is_dark() != 0 }
}
/// Write a PNG of `win`'s top-level window as the app itself renders it (shows
/// the window and lets it paint first). macOS only, for the self-test's
/// `ALKANE_SNAPSHOT`; returns false elsewhere or on failure.
pub fn snapshot_png(win: &Widget, path: &str) -> bool {
#[cfg(target_os = "macos")]
{
let Ok(p) = CString::new(path) else { return false };
unsafe { notsafe::wxbr_osx_snapshot(win.0, p.as_ptr()) != 0 }
}
#[cfg(not(target_os = "macos"))]
{
let _ = (win, path);
false
}
}
/// A theme colour. `which`: 0=window bg, 1=window text, 2=highlight,
/// 3=button face, 4=grey text. Returns (r, g, b).
pub fn system_color(which: i32) -> (u8, u8, u8) {
+7
View File
@@ -0,0 +1,7 @@
# Report relay for the Alkane Toolkit's Bug report button (see README.md).
FROM python:3.13-alpine
RUN adduser -D -H -u 10001 relay
COPY relay.py /app/relay.py
USER relay
EXPOSE 8080
ENTRYPOINT ["python3", "-u", "/app/relay.py"]
+65
View File
@@ -0,0 +1,65 @@
# Report relay
Files the app's bug reports and feature requests as issues in
`blueflare/alkane-toolkit`. The app's **Bug report** button posts the report
here; the relay holds the only token, so the public app download carries no
secret.
```
app --HTTPS--> gitea.bluefla.re/alkane-report/ (nginx) --> relay --> Gitea API
```
## What it does
- `POST /v1/report` takes the JSON the app sends (`kind`, `text`, version and
OS context, optional `log`) and answers `201 {"number", "url"}` or an error
`{"error": "..."}` the app shows as is. `GET /healthz` answers `200`.
- Checks: type is `bug`, `ui` or `feature`; text 1 to 8,000 characters;
each context value at most 120 printable characters; log at most 16 KiB;
body at most 64 KiB (nginx enforces the same).
- Rate limits, in memory: 5 reports per client IP per hour and 60 in all.
Over the limit: `429` with `Retry-After`. At most 4 reports in flight.
The client IP is `X-Real-IP` from nginx. Office LAN clients reach nginx
through the router's hairpin NAT, so they share one IP and one limit.
- Scrubs the log a second time (the app scrubs first): password, token,
secret and similar values, `Authorization`/`Cookie` values, URL passwords
and PEM private keys become `[redacted]`.
- `@name` in a report cannot notify anyone (a zero-width space follows `@`).
- The issue title is the text's first line (up to 100 characters). The body
has the type, a context table, the text, and the log in a collapsed block.
Labels: the type (`bug`, `ui`, `feature`) and `from-app`.
- Nothing is stored. Each report leaves one log line: the client IP and the
outcome (`filed #N`, `refused <status>`, `rate limited`, `tracker error`),
never the report's text.
## Gitea account
Issues are filed as `alkane-reporter`, a local non-admin user with a random
password that is never used. Its only access is the org team
`alkane-reporters` (units `repo.code: read`, `repo.issues: write`) on
`alkane-toolkit`. Its token `alkane-toolkit-bug-reports` has the scopes
`write:issue` and `read:repository`. This mirrors FLARE's `flare-reporter`.
## Deployment
On the Gitea host, recorded in the `flare-deployment` repository
(`runbooks/alkane-report-relay.md`):
```sh
# in /opt/flare/deploy/alkane-report-relay, holding relay.py, Dockerfile, compose.yml
PROXY_NETWORK=flare_default docker compose -p alkane-report-relay up -d --build
```
The token file is `secrets/gitea_token` next to the compose file (directory
`0700`, never committed). nginx routes `location /alkane-report/` in the
`gitea.bluefla.re` server block to `alkane-report-relay:8080`.
## Tests
```sh
python3 tools/report-relay/test_relay.py
```
They run the real HTTP handler against a fake Gitea: filing, scrubbing,
mention handling, validation, size and rate limits, and tracker errors. CI
runs them in the `verify` workflow's unit tests.
+26
View File
@@ -0,0 +1,26 @@
# Deployed on the Gitea host next to the shared nginx (see README.md).
# The token file is created on the host and never committed.
services:
report-relay:
build: .
image: alkane-report-relay:local
container_name: alkane-report-relay
restart: unless-stopped
read_only: true
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]
mem_limit: 128m
pids_limit: 64
environment:
GITEA_URL: http://gitea:3000
GITEA_REPO: blueflare/alkane-toolkit
GITEA_TOKEN_FILE: /run/secrets/gitea_token
RATE_PER_IP: "5"
RATE_GLOBAL: "60"
volumes:
- ./secrets/gitea_token:/run/secrets/gitea_token:ro
networks: [proxy]
networks:
proxy:
external: true
name: ${PROXY_NETWORK:?set PROXY_NETWORK to the shared nginx network}
+321
View File
@@ -0,0 +1,321 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Report relay: files Alkane Toolkit bug reports as Gitea issues.
The app's Bug report button posts JSON here; the relay checks it, scrubs the
log again, rate-limits, and files one issue in one repository with its own
token. The app holds no secret. Nothing about a report is stored.
POST /v1/report {"kind": "bug"|"ui"|"feature", "text": "...",
"app_version", "platform", "os", "appearance",
"language", "window": "...", "log": "..."}
-> 201 {"number": N, "url": "..."}
-> 4xx/5xx {"error": "reason the app shows"}
GET /healthz -> 200 ok
Configuration (environment):
GITEA_URL API base, e.g. http://gitea:3000 (default https://gitea.bluefla.re)
GITEA_REPO owner/name the issues go to (default blueflare/alkane-toolkit)
GITEA_TOKEN_FILE file holding the token (scopes write:issue, read:repository)
LISTEN host:port (default 0.0.0.0:8080)
RATE_PER_IP reports per client IP per hour (default 5)
RATE_GLOBAL reports per hour in all (default 60)
The client IP is the X-Real-IP header the reverse proxy sets; the relay must
only be reachable through that proxy.
"""
from collections import deque
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import os
import re
import sys
import threading
import time
import urllib.error
import urllib.request
KINDS = {"bug": "Bug", "ui": "UI change", "feature": "Feature request"}
CONTEXT_FIELDS = (
("app_version", "App version"),
("platform", "Platform"),
("os", "OS"),
("appearance", "Appearance"),
("language", "Language"),
("window", "Window"),
)
FROM_APP_LABEL = "from-app"
MAX_BODY = 64 * 1024
MAX_TEXT_CHARS = 8000
MAX_CONTEXT_CHARS = 120
MAX_LOG_BYTES = 16 * 1024
MAX_TITLE_CHARS = 100
MAX_IN_FLIGHT = 4
HOUR = 3600.0
SECRET_NAME = re.compile(
r"(password|passwd|pass|pwd|secret|token|apikey|api_key|psk|credential)"
r"(['\"]?\s*[:=]\s*['\"]?)([^\s'\",&;]+)",
re.IGNORECASE,
)
HEADER_SECRET = re.compile(r"((?:authorization|cookie)\s*:\s*).+", re.IGNORECASE)
URL_USERINFO = re.compile(r"(://[^/\s:@]+:)[^/\s@]+(@)")
PEM_BLOCK = re.compile(
r"-----BEGIN [A-Z ]*PRIVATE KEY-----.*?(-----END [A-Z ]*PRIVATE KEY-----|\Z)",
re.DOTALL,
)
def scrub(text: str) -> str:
"""Remove credentials (the app scrubs too; this is the second pass)."""
text = PEM_BLOCK.sub("[redacted]", text)
text = HEADER_SECRET.sub(r"\1[redacted]", text)
text = URL_USERINFO.sub(r"\1[redacted]\2", text)
return SECRET_NAME.sub(r"\1\2[redacted]", text)
def no_mentions(text: str) -> str:
"""Stop @name in a report from notifying Gitea users."""
return re.sub(r"@(?=[A-Za-z0-9_])", "@\u200b", text)
def fence_for(text: str) -> str:
"""A backtick fence longer than any backtick run in `text`."""
longest = max((len(m) for m in re.findall(r"`+", text)), default=0)
return "`" * max(3, longest + 1)
def cell(value: str) -> str:
return value.replace("|", "\\|").replace("\n", " ")
class BadReport(Exception):
def __init__(self, status: int, message: str):
super().__init__(message)
self.status = status
self.message = message
def validate(raw: bytes) -> dict:
try:
data = json.loads(raw)
except (ValueError, UnicodeDecodeError):
raise BadReport(400, "The report was not valid JSON")
if not isinstance(data, dict):
raise BadReport(400, "The report was not a JSON object")
kind = data.get("kind", "bug")
if kind not in KINDS:
raise BadReport(422, "Unknown report type")
text = data.get("text")
if not isinstance(text, str) or not text.strip():
raise BadReport(422, "The report is empty")
text = text.strip()
if len(text) > MAX_TEXT_CHARS:
raise BadReport(413, f"The text is longer than {MAX_TEXT_CHARS} characters")
context = {}
for key, _ in CONTEXT_FIELDS:
value = data.get(key, "")
if not isinstance(value, str) or len(value) > MAX_CONTEXT_CHARS:
raise BadReport(422, f"Bad value for {key}")
context[key] = "".join(c for c in value if c.isprintable())
log = data.get("log", "")
if not isinstance(log, str) or len(log.encode()) > MAX_LOG_BYTES:
raise BadReport(413, "The log is too large")
return {"kind": kind, "text": text, "context": context, "log": log}
def issue_title(report: dict) -> str:
first = next(line.strip() for line in report["text"].splitlines() if line.strip())
if len(first) > MAX_TITLE_CHARS:
first = first[: MAX_TITLE_CHARS - 3].rstrip() + "..."
return no_mentions(first)
def issue_body(report: dict) -> str:
ctx = report["context"]
lines = [
f"**Type:** {KINDS[report['kind']]}",
"",
"Sent from the Alkane Toolkit app's Bug report button.",
"",
"| | |",
"|---|---|",
]
for key, label in CONTEXT_FIELDS:
if ctx.get(key):
lines.append(f"| {label} | {cell(ctx[key])} |")
lines += ["", "---", "", no_mentions(report["text"])]
log = scrub(report["log"]).strip()
if log:
fence = fence_for(log)
lines += [
"",
"<details><summary>Recent activity log</summary>",
"",
fence,
no_mentions(log),
fence,
"",
"</details>",
]
return "\n".join(lines) + "\n"
class RateLimiter:
"""Sliding one-hour windows, per client IP and in all."""
def __init__(self, per_ip: int, total: int, clock=time.monotonic):
self.per_ip, self.total, self.clock = per_ip, total, clock
self.by_ip: dict[str, deque] = {}
self.all: deque = deque()
self.lock = threading.Lock()
def take(self, ip: str) -> float:
"""0 if the report may go now, else seconds until it may."""
now = self.clock()
with self.lock:
for q in [self.all, *self.by_ip.values()]:
while q and now - q[0] >= HOUR:
q.popleft()
self.by_ip = {k: q for k, q in self.by_ip.items() if q}
mine = self.by_ip.setdefault(ip, deque())
if len(mine) >= self.per_ip:
return HOUR - (now - mine[0])
if len(self.all) >= self.total:
return HOUR - (now - self.all[0])
mine.append(now)
self.all.append(now)
return 0.0
class Gitea:
def __init__(self, url: str, repo: str, token: str, timeout: float = 20):
self.api = url.rstrip("/") + "/api/v1/repos/" + repo
self.token, self.timeout = token, timeout
self.labels: dict[str, int] = {}
self.labels_at = 0.0
def _call(self, method: str, path: str, body: dict | None = None):
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(self.api + path, data=data, method=method)
req.add_header("Authorization", f"token {self.token}")
req.add_header("Accept", "application/json")
if data is not None:
req.add_header("Content-Type", "application/json")
with urllib.request.urlopen(req, timeout=self.timeout) as resp:
return json.load(resp)
def label_ids(self, names: list[str]) -> list[int]:
"""IDs of the repository's labels by name (cached 10 minutes);
unknown names are skipped."""
if time.monotonic() - self.labels_at > 600:
try:
found = self._call("GET", "/labels?limit=50")
self.labels = {l["name"]: l["id"] for l in found}
self.labels_at = time.monotonic()
except (urllib.error.URLError, OSError, ValueError, KeyError):
pass
return [self.labels[n] for n in names if n in self.labels]
def file(self, title: str, body: str, labels: list[str]) -> tuple[int, str]:
issue = self._call("POST", "/issues", {
"title": title,
"body": body,
"labels": self.label_ids(labels),
})
return int(issue["number"]), str(issue["html_url"])
class Relay:
def __init__(self, gitea: Gitea, limiter: RateLimiter, log=print):
self.gitea, self.limiter, self.log = gitea, limiter, log
self.slots = threading.BoundedSemaphore(MAX_IN_FLIGHT)
def handle(self, ip: str, raw: bytes) -> tuple[int, dict, dict]:
"""Return (status, json body, extra headers)."""
try:
report = validate(raw)
except BadReport as bad:
self.log(f"report from {ip}: refused {bad.status} ({bad.message})")
return bad.status, {"error": bad.message}, {}
wait = self.limiter.take(ip)
if wait > 0:
self.log(f"report from {ip}: rate limited")
return 429, {"error": "Too many reports; try again later"}, {
"Retry-After": str(int(wait) + 1)}
if not self.slots.acquire(blocking=False):
return 503, {"error": "The report service is busy; try again shortly"}, {}
try:
number, url = self.gitea.file(
issue_title(report), issue_body(report), [report["kind"], FROM_APP_LABEL])
except (urllib.error.URLError, OSError, ValueError, KeyError) as e:
self.log(f"report from {ip}: tracker error ({type(e).__name__})")
return 502, {"error": "The issue tracker did not answer; check the issue "
"list before sending again"}, {}
finally:
self.slots.release()
self.log(f"report from {ip}: filed #{number}")
return 201, {"number": number, "url": url}, {}
def make_handler(relay: Relay):
class Handler(BaseHTTPRequestHandler):
server_version = "alkane-report-relay"
sys_version = ""
def _send(self, status: int, body: dict, headers: dict | None = None):
data = json.dumps(body).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
for k, v in (headers or {}).items():
self.send_header(k, v)
self.end_headers()
self.wfile.write(data)
def do_GET(self):
if self.path == "/healthz":
self._send(200, {"status": "ok"})
else:
self._send(404, {"error": "Not found"})
def do_POST(self):
if self.path != "/v1/report":
return self._send(404, {"error": "Not found"})
try:
length = int(self.headers.get("Content-Length", ""))
except ValueError:
return self._send(411, {"error": "Length required"})
if length < 0 or length > MAX_BODY:
return self._send(413, {"error": "The report is too large"})
raw = self.rfile.read(length)
ip = self.headers.get("X-Real-IP") or self.client_address[0]
status, body, headers = relay.handle(ip, raw)
self._send(status, body, headers)
def log_message(self, fmt, *args):
pass # one line per report comes from Relay.handle instead
return Handler
def main() -> None:
token_file = os.environ.get("GITEA_TOKEN_FILE", "/run/secrets/gitea_token")
with open(token_file) as fh:
token = fh.read().strip()
if not token:
sys.exit(f"empty token in {token_file}")
gitea = Gitea(os.environ.get("GITEA_URL", "https://gitea.bluefla.re"),
os.environ.get("GITEA_REPO", "blueflare/alkane-toolkit"), token)
limiter = RateLimiter(int(os.environ.get("RATE_PER_IP", "5")),
int(os.environ.get("RATE_GLOBAL", "60")))
relay = Relay(gitea, limiter, log=lambda m: print(m, flush=True))
host, _, port = os.environ.get("LISTEN", "0.0.0.0:8080").rpartition(":")
server = ThreadingHTTPServer((host or "0.0.0.0", int(port)), make_handler(relay))
print(f"report relay on {host}:{port} -> {gitea.api}", flush=True)
server.serve_forever()
if __name__ == "__main__":
main()
+176
View File
@@ -0,0 +1,176 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Tests for the report relay: python3 tools/report-relay/test_relay.py"""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import pathlib
import sys
import threading
import unittest
import urllib.error
import urllib.request
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
import relay # noqa: E402
TOKEN = "test-token-123"
class FakeGitea:
"""Records issue creations; serves the repository's labels."""
def __init__(self, fail=False):
self.created = []
self.fail = fail
outer = self
class H(BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def _json(self, status, body):
data = json.dumps(body).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
assert self.headers["Authorization"] == f"token {TOKEN}"
if self.path.startswith("/api/v1/repos/blueflare/alkane-toolkit/labels"):
return self._json(200, [{"name": "bug", "id": 1}, {"name": "ui", "id": 2},
{"name": "feature", "id": 3},
{"name": "from-app", "id": 9}])
self._json(404, {})
def do_POST(self):
assert self.headers["Authorization"] == f"token {TOKEN}"
if outer.fail:
return self._json(500, {"message": "boom"})
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
outer.created.append((self.path, body))
n = len(outer.created)
self._json(201, {"number": n,
"html_url": f"https://gitea.example/blueflare/alkane-toolkit/issues/{n}"})
self.server = ThreadingHTTPServer(("127.0.0.1", 0), H)
threading.Thread(target=self.server.serve_forever, daemon=True).start()
self.url = f"http://127.0.0.1:{self.server.server_address[1]}"
def close(self):
self.server.shutdown()
self.server.server_close()
class RelayTests(unittest.TestCase):
def setUp(self):
self.gitea = FakeGitea()
self.now = [1000.0]
self.limiter = relay.RateLimiter(2, 3, clock=lambda: self.now[0])
self.logs = []
r = relay.Relay(relay.Gitea(self.gitea.url, "blueflare/alkane-toolkit", TOKEN),
self.limiter, log=self.logs.append)
self.server = ThreadingHTTPServer(("127.0.0.1", 0), relay.make_handler(r))
threading.Thread(target=self.server.serve_forever, daemon=True).start()
self.url = f"http://127.0.0.1:{self.server.server_address[1]}"
def tearDown(self):
self.server.shutdown()
self.server.server_close()
self.gitea.close()
def post(self, body, ip="198.51.100.7", path="/v1/report", raw=None):
data = raw if raw is not None else json.dumps(body).encode()
req = urllib.request.Request(self.url + path, data=data, method="POST",
headers={"Content-Type": "application/json",
"X-Real-IP": ip})
try:
with urllib.request.urlopen(req, timeout=10) as resp:
return resp.status, json.load(resp), resp.headers
except urllib.error.HTTPError as e:
return e.code, json.load(e), e.headers
def report(self, **over):
body = {"kind": "bug", "text": "Table is blank on macOS\nSteps: open the app.",
"app_version": "0.110.0", "platform": "macos-aarch64", "os": "macOS 15.1",
"appearance": "dark", "language": "en", "window": "1280x800", "log": ""}
body.update(over)
return body
def test_files_an_issue_with_title_body_and_labels(self):
status, body, _ = self.post(self.report(kind="feature", log="12:00 scan done"))
self.assertEqual(status, 201)
self.assertEqual(body["number"], 1)
self.assertTrue(body["url"].endswith("/issues/1"))
path, issue = self.gitea.created[0]
self.assertEqual(path, "/api/v1/repos/blueflare/alkane-toolkit/issues")
self.assertEqual(issue["title"], "Table is blank on macOS")
self.assertEqual(issue["labels"], [3, 9])
self.assertIn("**Type:** Feature request", issue["body"])
self.assertIn("| OS | macOS 15.1 |", issue["body"])
self.assertIn("Steps: open the app.", issue["body"])
self.assertIn("<details><summary>Recent activity log</summary>", issue["body"])
self.assertIn("filed #1", self.logs[-1])
self.assertNotIn("blank", " ".join(self.logs), "report text is never logged")
def test_scrubs_the_log_and_neutralises_mentions(self):
log = ("set pool url=stratum+tcp://w:hunter2@pool:3333 password=hunter2\n"
"Authorization: Bearer abc.def\n``` fence ```")
status, _, _ = self.post(self.report(text="@admin please look", log=log))
self.assertEqual(status, 201)
_, issue = self.gitea.created[0]
self.assertNotIn("hunter2", issue["body"])
self.assertNotIn("abc.def", issue["body"])
self.assertNotIn("@admin", issue["body"])
self.assertNotIn("@admin", issue["title"])
self.assertIn("````\n", issue["body"], "fence longer than the log's own")
def test_rejects_bad_reports(self):
cases = [
(self.report(text=" "), 422),
(self.report(kind="hack"), 422),
(self.report(text="x" * 8001), 413),
(self.report(log="y" * (16 * 1024 + 1)), 413),
(self.report(os="z" * 121), 422),
(self.report(os=5), 422),
([1, 2], 400),
]
for body, want in cases:
status, answer, _ = self.post(body)
self.assertEqual(status, want, body if not isinstance(body, dict) else answer)
self.assertIn("error", answer)
status, _, _ = self.post(None, raw=b"{not json")
self.assertEqual(status, 400)
status, _, _ = self.post(None, raw=b"x" * (relay.MAX_BODY + 1))
self.assertEqual(status, 413)
status, _, _ = self.post(self.report(), path="/v1/other")
self.assertEqual(status, 404)
self.assertEqual(self.gitea.created, [])
def test_rate_limits_per_ip_and_in_all(self):
for _ in range(2):
self.assertEqual(self.post(self.report())[0], 201)
status, body, headers = self.post(self.report())
self.assertEqual(status, 429)
self.assertGreater(int(headers["Retry-After"]), 0)
self.assertEqual(self.post(self.report(), ip="203.0.113.9")[0], 201)
self.assertEqual(self.post(self.report(), ip="203.0.113.10")[0], 429, "global cap of 3")
self.now[0] += relay.HOUR
self.assertEqual(self.post(self.report())[0], 201, "window slides")
def test_tracker_failure_is_reported(self):
self.gitea.fail = True
status, body, _ = self.post(self.report())
self.assertEqual(status, 502)
self.assertIn("check the issue list", body["error"])
def test_title_is_cut(self):
title = relay.issue_title({"text": "\n\n" + "w" * 300})
self.assertEqual(len(title), relay.MAX_TITLE_CHARS)
self.assertTrue(title.endswith("..."))
if __name__ == "__main__":
unittest.main()