f8ead215d8
build-windows / build-hello-agent-x64 (push) Successful in 5m41s
A single-binary, Flutter-free remote-support agent that speaks the stock
RustDesk wire protocol. Designed for one-line MDM deployment against a
self-hosted rustdesk-server: a supporter using the unmodified rustdesk.exe
client connects, the controlled-side user gets a native Win32 approval
prompt, click Yes / No.
CLI surface
hello-agent.exe --install # register + start service
hello-agent.exe --uninstall # stop, delete, clean up
hello-agent.exe --config <BLOB> # admin-UI deploy string
hello-agent.exe --install --config <BLOB> # MDM one-liner
--config accepts both forms emitted by the rustdesk-server admin UI: the
reversed-base64 deploy string and the host=,key=,api=,relay= filename
form. Decoded via the upstream custom_server module, persisted via
hbb_common::config::Config::set_option.
Architecture
--service runs as a Session 0 LocalSystem service. It polls
WTSGetActiveConsoleSessionId and (re)spawns hello-agent.exe --server
into the active console session via librustdesk::platform::run_as_user,
handling the Session 0 → user-session token impersonation.
--server is the worker. It boots three concurrent components:
1. cm_popup: an IPC listener on the rustdesk `_cm` named pipe
2. librustdesk::start_server(true, false): the upstream protocol
stack — rendezvous mediator, NAT punch, IPC server, screen
capture, login validation, hbbs_http heartbeat / sysinfo sync
3. (implicit) ApproveMode::Click is pinned in config, so every
incoming connection routes through cm_popup
The popup mechanism reuses an existing upstream contract without any
patches to the protocol code: when a peer connects with no password,
Connection::start in the upstream code calls try_start_cm_ipc, which
ipc::connect-s the `_cm` pipe before falling back to spawning a Flutter
CM child. Since cm_popup is up first, step 1 succeeds; we read the
Data::Login{authorized:false} frame, show MessageBoxTimeoutW (Yes/No,
60s, top-most, system-modal), and reply Data::Authorize or Data::Close.
Source tree
src/main.rs CLI dispatcher + run_server() composition
src/cli.rs hand-rolled argv parser + unit tests
src/service.rs windows-service install/uninstall/dispatcher
src/config_import.rs --config blob decoding + persistence
src/cm_popup.rs _cm IPC listener + Win32 approval dialog
Vendoring
The upstream RustDesk crate is vendored under vendor/rustdesk/ — full
workspace including libs/{hbb_common, scrap, enigo, clipboard,
virtual_display, remote_printer}. This makes the build self-contained
(no submodules, no sibling-repo checkout in CI) and gives us freedom to
fork in a different direction later. Excluded from the vendor: .git,
target/, flutter/, appimage/, flatpak/, fastlane/, docs/, examples/,
ci/, build.py, Dockerfile, upstream README/CLAUDE/AGENTS/GEMINI.
One local divergence vs. upstream: vendor/rustdesk/src/lib.rs flips
`mod custom_server` → `pub mod custom_server` so config_import.rs can
call get_custom_server_from_string without going through the
ui_interface shim. Documented in README.md → "Re-syncing the vendored
copy".
CI
.gitea/workflows/build-windows.yml builds on a self-hosted Windows
runner with Rust 1.75, LLVM 15.0.6 (libclang for bindgen via libvpx-sys),
and a vcpkg cache. The vendored vcpkg.json drives x64-windows-static
deps. The workflow stages the resulting hello-agent.exe into
SignOutput\, reports authenticode signing status (warns on unsigned),
and uploads as artifact. ~15 min full build, faster on incremental.
Out of scope for this commit: Linux/macOS builds, code signing, MSI
packaging, coexistence with stock rustdesk on the same box (currently
shares the RustDesk APP_NAME and config dir).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
181 lines
6.2 KiB
Rust
181 lines
6.2 KiB
Rust
use hbb_common::{bail, ResultType};
|
|
use tiny_skia::{FillRule, Paint, PathBuilder, PixmapMut, Point, Rect, Transform};
|
|
use ttf_parser::Face;
|
|
// A helper struct to bridge `ttf-parser` and `tiny-skia`.
|
|
struct PathBuilderWrapper<'a> {
|
|
path_builder: &'a mut PathBuilder,
|
|
transform: Transform,
|
|
}
|
|
|
|
impl ttf_parser::OutlineBuilder for PathBuilderWrapper<'_> {
|
|
fn move_to(&mut self, x: f32, y: f32) {
|
|
let mut pt = Point::from_xy(x, y);
|
|
self.transform.map_point(&mut pt);
|
|
self.path_builder.move_to(pt.x, pt.y);
|
|
}
|
|
|
|
fn line_to(&mut self, x: f32, y: f32) {
|
|
let mut pt = Point::from_xy(x, y);
|
|
self.transform.map_point(&mut pt);
|
|
self.path_builder.line_to(pt.x, pt.y);
|
|
}
|
|
|
|
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
|
|
let mut pt1 = Point::from_xy(x1, y1);
|
|
self.transform.map_point(&mut pt1);
|
|
let mut pt = Point::from_xy(x, y);
|
|
self.transform.map_point(&mut pt);
|
|
self.path_builder.quad_to(pt1.x, pt1.y, pt.x, pt.y);
|
|
}
|
|
|
|
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
|
|
let mut pt1 = Point::from_xy(x1, y1);
|
|
self.transform.map_point(&mut pt1);
|
|
let mut pt2 = Point::from_xy(x2, y2);
|
|
self.transform.map_point(&mut pt2);
|
|
let mut pt = Point::from_xy(x, y);
|
|
self.transform.map_point(&mut pt);
|
|
self.path_builder
|
|
.cubic_to(pt1.x, pt1.y, pt2.x, pt2.y, pt.x, pt.y);
|
|
}
|
|
|
|
fn close(&mut self) {
|
|
self.path_builder.close();
|
|
}
|
|
}
|
|
|
|
// Draws a string of text with the white background rectangle onto the pixmap.
|
|
pub(super) fn draw_text(
|
|
pixmap: &mut PixmapMut,
|
|
face: &Face,
|
|
text: &str,
|
|
x: f32,
|
|
y: f32,
|
|
paint: &Paint,
|
|
font_size: f32,
|
|
) {
|
|
let units_per_em = face.units_per_em() as f32;
|
|
let scale = font_size / units_per_em;
|
|
|
|
// --- 1. Calculate text dimensions for the background ---
|
|
let mut total_width = 0.0;
|
|
for ch in text.chars() {
|
|
let glyph_id = face.glyph_index(ch).unwrap_or_default();
|
|
if let Some(h_advance) = face.glyph_hor_advance(glyph_id) {
|
|
total_width += h_advance as f32 * scale;
|
|
}
|
|
}
|
|
|
|
// Use font metrics for a consistent background height.
|
|
let font_height = (face.ascender() - face.descender()) as f32 * scale;
|
|
let ascent = face.ascender() as f32 * scale;
|
|
// Add some padding around the text
|
|
let padding = 3.0;
|
|
|
|
let mut bg_filled = false;
|
|
// --- 2. Draw the white background rectangle ---
|
|
if let Some(bg_rect) = Rect::from_xywh(
|
|
x - padding,
|
|
y - ascent - padding,
|
|
total_width + 2.0 * padding,
|
|
font_height + 2.0 * padding,
|
|
) {
|
|
// Corner radius
|
|
let radius = 5.0;
|
|
let path = {
|
|
let mut pb = PathBuilder::new();
|
|
let r_x = bg_rect.x();
|
|
let r_y = bg_rect.y();
|
|
let r_w = bg_rect.width();
|
|
let r_h = bg_rect.height();
|
|
pb.move_to(r_x + radius, r_y);
|
|
pb.line_to(r_x + r_w - radius, r_y);
|
|
pb.quad_to(r_x + r_w, r_y, r_x + r_w, r_y + radius);
|
|
pb.line_to(r_x + r_w, r_y + r_h - radius);
|
|
pb.quad_to(r_x + r_w, r_y + r_h, r_x + r_w - radius, r_y + r_h);
|
|
pb.line_to(r_x + radius, r_y + r_h);
|
|
pb.quad_to(r_x, r_y + r_h, r_x, r_y + r_h - radius);
|
|
pb.line_to(r_x, r_y + radius);
|
|
pb.quad_to(r_x, r_y, r_x + radius, r_y);
|
|
pb.close();
|
|
pb.finish()
|
|
};
|
|
|
|
if let Some(path) = path {
|
|
let mut bg_paint = Paint::default();
|
|
bg_paint.set_color_rgba8(255, 255, 255, 255);
|
|
bg_paint.anti_alias = true;
|
|
pixmap.fill_path(
|
|
&path,
|
|
&bg_paint,
|
|
FillRule::Winding,
|
|
Transform::identity(),
|
|
None,
|
|
);
|
|
bg_filled = true;
|
|
}
|
|
}
|
|
|
|
// --- 3. Draw the text ---
|
|
let transform = Transform::from_translate(x, y).pre_scale(scale, -scale);
|
|
let mut path_builder = PathBuilder::new();
|
|
let mut current_x = 0.0;
|
|
|
|
for ch in text.chars() {
|
|
let glyph_id = face.glyph_index(ch).unwrap_or_default();
|
|
|
|
let mut builder = PathBuilderWrapper {
|
|
path_builder: &mut path_builder,
|
|
transform: transform.post_translate(current_x, 0.0),
|
|
};
|
|
|
|
face.outline_glyph(glyph_id, &mut builder);
|
|
|
|
if let Some(h_advance) = face.glyph_hor_advance(glyph_id) {
|
|
current_x += h_advance as f32 * scale;
|
|
}
|
|
}
|
|
|
|
if let Some(path) = path_builder.finish() {
|
|
if bg_filled {
|
|
let mut text_paint = Paint::default();
|
|
text_paint.set_color_rgba8(0, 0, 0, 255);
|
|
text_paint.anti_alias = true;
|
|
pixmap.fill_path(
|
|
&path,
|
|
&text_paint,
|
|
FillRule::Winding,
|
|
Transform::identity(),
|
|
None,
|
|
);
|
|
} else {
|
|
pixmap.fill_path(&path, paint, FillRule::Winding, Transform::identity(), None);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn create_font_face() -> ResultType<Face<'static>> {
|
|
let mut font_db = fontdb::Database::new();
|
|
font_db.load_system_fonts();
|
|
let query = fontdb::Query {
|
|
families: &[fontdb::Family::Monospace, fontdb::Family::SansSerif],
|
|
..fontdb::Query::default()
|
|
};
|
|
let Some(font_id) = font_db.query(&query) else {
|
|
bail!("No monospace or sans-serif font found!");
|
|
};
|
|
let Some((font_source, face_index)) = font_db.face_source(font_id) else {
|
|
bail!("No face found for font!");
|
|
};
|
|
// Load the font data into a static slice to satisfy `ttf-parser`'s lifetime requirements.
|
|
// We use `Box::leak` to leak the memory, which is acceptable here since the font data
|
|
// is needed for the entire lifetime of the application.
|
|
let font_data: &'static [u8] = Box::leak(match font_source {
|
|
fontdb::Source::File(path) => std::fs::read(path)?.into_boxed_slice(),
|
|
fontdb::Source::Binary(data) => data.as_ref().as_ref().to_vec().into_boxed_slice(),
|
|
fontdb::Source::SharedFile(path, _) => std::fs::read(path)?.into_boxed_slice(),
|
|
});
|
|
let face = Face::parse(font_data, face_index)?;
|
|
Ok(face)
|
|
}
|