Add magic bytes deployment system for agent modes
- Agent config: Added EmbeddedConfig struct and RunMode enum for filename-based mode detection (Viewer, TempSupport, PermanentAgent) - Agent main: Updated to detect run mode from filename or embedded config - Server: Added /api/download/* endpoints for generating configured binaries - /api/download/viewer - Downloads GuruConnect-Viewer.exe - /api/download/support?code=123456 - Downloads GuruConnect-123456.exe - /api/download/agent?company=X&site=Y - Downloads with embedded config - Dashboard: Updated Build tab with Quick Downloads and Permanent Agent Builder - Included base agent binary in static/downloads 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
268
server/src/api/downloads.rs
Normal file
268
server/src/api/downloads.rs
Normal file
@@ -0,0 +1,268 @@
|
||||
//! Download endpoints for generating configured agent binaries
|
||||
//!
|
||||
//! Provides endpoints for:
|
||||
//! - Viewer-only downloads
|
||||
//! - Temp support session downloads (with embedded code)
|
||||
//! - Permanent agent downloads (with embedded config)
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{Path, Query, State},
|
||||
http::{header, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use tracing::{info, warn, error};
|
||||
|
||||
/// Magic marker for embedded configuration (must match agent)
|
||||
const MAGIC_MARKER: &[u8] = b"GURUCONFIG";
|
||||
|
||||
/// Embedded configuration data structure
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EmbeddedConfig {
|
||||
/// Server WebSocket URL
|
||||
pub server_url: String,
|
||||
/// API key for authentication
|
||||
pub api_key: String,
|
||||
/// Company/organization name
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub company: Option<String>,
|
||||
/// Site/location name
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub site: Option<String>,
|
||||
/// Tags for categorization
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
/// Query parameters for agent download
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AgentDownloadParams {
|
||||
/// Company/organization name
|
||||
pub company: Option<String>,
|
||||
/// Site/location name
|
||||
pub site: Option<String>,
|
||||
/// Comma-separated tags
|
||||
pub tags: Option<String>,
|
||||
/// API key (optional, will use default if not provided)
|
||||
pub api_key: Option<String>,
|
||||
}
|
||||
|
||||
/// Query parameters for support session download
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct SupportDownloadParams {
|
||||
/// 6-digit support code
|
||||
pub code: String,
|
||||
}
|
||||
|
||||
/// Get path to base agent binary
|
||||
fn get_base_binary_path() -> PathBuf {
|
||||
// Check for static/downloads/guruconnect.exe relative to working dir
|
||||
let static_path = PathBuf::from("static/downloads/guruconnect.exe");
|
||||
if static_path.exists() {
|
||||
return static_path;
|
||||
}
|
||||
|
||||
// Also check without static prefix (in case running from server dir)
|
||||
let downloads_path = PathBuf::from("downloads/guruconnect.exe");
|
||||
if downloads_path.exists() {
|
||||
return downloads_path;
|
||||
}
|
||||
|
||||
// Fallback to static path
|
||||
static_path
|
||||
}
|
||||
|
||||
/// Download viewer-only binary (no embedded config, "Viewer" in filename)
|
||||
pub async fn download_viewer() -> impl IntoResponse {
|
||||
let binary_path = get_base_binary_path();
|
||||
|
||||
match std::fs::read(&binary_path) {
|
||||
Ok(binary_data) => {
|
||||
info!("Serving viewer download ({} bytes)", binary_data.len());
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "application/octet-stream")
|
||||
.header(
|
||||
header::CONTENT_DISPOSITION,
|
||||
"attachment; filename=\"GuruConnect-Viewer.exe\""
|
||||
)
|
||||
.header(header::CONTENT_LENGTH, binary_data.len())
|
||||
.body(Body::from(binary_data))
|
||||
.unwrap()
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to read base binary from {:?}: {}", binary_path, e);
|
||||
Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("Agent binary not found"))
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Download support session binary (code embedded in filename)
|
||||
pub async fn download_support(
|
||||
Query(params): Query<SupportDownloadParams>,
|
||||
) -> impl IntoResponse {
|
||||
// Validate support code (must be 6 digits)
|
||||
let code = params.code.trim();
|
||||
if code.len() != 6 || !code.chars().all(|c| c.is_ascii_digit()) {
|
||||
return Response::builder()
|
||||
.status(StatusCode::BAD_REQUEST)
|
||||
.body(Body::from("Invalid support code: must be 6 digits"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let binary_path = get_base_binary_path();
|
||||
|
||||
match std::fs::read(&binary_path) {
|
||||
Ok(binary_data) => {
|
||||
info!("Serving support session download for code {} ({} bytes)", code, binary_data.len());
|
||||
|
||||
// Filename includes the support code
|
||||
let filename = format!("GuruConnect-{}.exe", code);
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "application/octet-stream")
|
||||
.header(
|
||||
header::CONTENT_DISPOSITION,
|
||||
format!("attachment; filename=\"{}\"", filename)
|
||||
)
|
||||
.header(header::CONTENT_LENGTH, binary_data.len())
|
||||
.body(Body::from(binary_data))
|
||||
.unwrap()
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to read base binary: {}", e);
|
||||
Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("Agent binary not found"))
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Download permanent agent binary with embedded configuration
|
||||
pub async fn download_agent(
|
||||
Query(params): Query<AgentDownloadParams>,
|
||||
) -> impl IntoResponse {
|
||||
let binary_path = get_base_binary_path();
|
||||
|
||||
// Read base binary
|
||||
let mut binary_data = match std::fs::read(&binary_path) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
error!("Failed to read base binary: {}", e);
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("Agent binary not found"))
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
// Build embedded config
|
||||
let config = EmbeddedConfig {
|
||||
server_url: "wss://connect.azcomputerguru.com/ws/agent".to_string(),
|
||||
api_key: params.api_key.unwrap_or_else(|| "managed-agent".to_string()),
|
||||
company: params.company.clone(),
|
||||
site: params.site.clone(),
|
||||
tags: params.tags
|
||||
.as_ref()
|
||||
.map(|t| t.split(',').map(|s| s.trim().to_string()).collect())
|
||||
.unwrap_or_default(),
|
||||
};
|
||||
|
||||
// Serialize config to JSON
|
||||
let config_json = match serde_json::to_vec(&config) {
|
||||
Ok(json) => json,
|
||||
Err(e) => {
|
||||
error!("Failed to serialize config: {}", e);
|
||||
return Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::from("Failed to generate config"))
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
// Append magic marker + length + config to binary
|
||||
// Structure: [PE binary][GURUCONFIG][length:u32 LE][json config]
|
||||
binary_data.extend_from_slice(MAGIC_MARKER);
|
||||
binary_data.extend_from_slice(&(config_json.len() as u32).to_le_bytes());
|
||||
binary_data.extend_from_slice(&config_json);
|
||||
|
||||
info!(
|
||||
"Serving permanent agent download: company={:?}, site={:?}, tags={:?} ({} bytes)",
|
||||
config.company, config.site, config.tags, binary_data.len()
|
||||
);
|
||||
|
||||
// Generate filename based on company/site
|
||||
let filename = match (¶ms.company, ¶ms.site) {
|
||||
(Some(company), Some(site)) => {
|
||||
format!("GuruConnect-{}-{}-Setup.exe", sanitize_filename(company), sanitize_filename(site))
|
||||
}
|
||||
(Some(company), None) => {
|
||||
format!("GuruConnect-{}-Setup.exe", sanitize_filename(company))
|
||||
}
|
||||
_ => "GuruConnect-Setup.exe".to_string()
|
||||
};
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, "application/octet-stream")
|
||||
.header(
|
||||
header::CONTENT_DISPOSITION,
|
||||
format!("attachment; filename=\"{}\"", filename)
|
||||
)
|
||||
.header(header::CONTENT_LENGTH, binary_data.len())
|
||||
.body(Body::from(binary_data))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Sanitize a string for use in a filename
|
||||
fn sanitize_filename(s: &str) -> String {
|
||||
s.chars()
|
||||
.map(|c| {
|
||||
if c.is_alphanumeric() || c == '-' || c == '_' {
|
||||
c
|
||||
} else if c == ' ' {
|
||||
'-'
|
||||
} else {
|
||||
'_'
|
||||
}
|
||||
})
|
||||
.collect::<String>()
|
||||
.chars()
|
||||
.take(32) // Limit length
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sanitize_filename() {
|
||||
assert_eq!(sanitize_filename("Acme Corp"), "Acme-Corp");
|
||||
assert_eq!(sanitize_filename("My Company!"), "My-Company_");
|
||||
assert_eq!(sanitize_filename("Test/Site"), "Test_Site");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_embedded_config_serialization() {
|
||||
let config = EmbeddedConfig {
|
||||
server_url: "wss://example.com/ws".to_string(),
|
||||
api_key: "test-key".to_string(),
|
||||
company: Some("Test Corp".to_string()),
|
||||
site: None,
|
||||
tags: vec!["windows".to_string()],
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&config).unwrap();
|
||||
assert!(json.contains("Test Corp"));
|
||||
assert!(json.contains("windows"));
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
pub mod auth;
|
||||
pub mod users;
|
||||
pub mod releases;
|
||||
pub mod downloads;
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State, Query},
|
||||
|
||||
@@ -233,6 +233,11 @@ async fn main() -> Result<()> {
|
||||
.route("/api/releases/:version", put(api::releases::update_release))
|
||||
.route("/api/releases/:version", delete(api::releases::delete_release))
|
||||
|
||||
// Agent downloads (no auth - public download links)
|
||||
.route("/api/download/viewer", get(api::downloads::download_viewer))
|
||||
.route("/api/download/support", get(api::downloads::download_support))
|
||||
.route("/api/download/agent", get(api::downloads::download_agent))
|
||||
|
||||
// HTML page routes (clean URLs)
|
||||
.route("/login", get(serve_login))
|
||||
.route("/dashboard", get(serve_dashboard))
|
||||
|
||||
@@ -477,20 +477,50 @@
|
||||
|
||||
<!-- Build Tab -->
|
||||
<div class="tab-panel" id="build-panel">
|
||||
<!-- Quick Downloads -->
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<div>
|
||||
<h2 class="card-title">Installer Builder</h2>
|
||||
<h2 class="card-title">Quick Downloads</h2>
|
||||
<p class="card-description">Download viewer or create temp support sessions</p>
|
||||
</div>
|
||||
</div>
|
||||
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px;">
|
||||
<div style="padding: 20px; background: hsl(var(--muted)); border-radius: 8px;">
|
||||
<h3 style="font-size: 16px; margin-bottom: 8px;">Viewer Only</h3>
|
||||
<p style="font-size: 13px; color: hsl(var(--muted-foreground)); margin-bottom: 16px;">
|
||||
Installs the protocol handler for connecting to remote sessions. No agent functionality.
|
||||
</p>
|
||||
<a href="/api/download/viewer" class="btn btn-primary" style="text-decoration: none;">Download Viewer</a>
|
||||
</div>
|
||||
<div style="padding: 20px; background: hsl(var(--muted)); border-radius: 8px;">
|
||||
<h3 style="font-size: 16px; margin-bottom: 8px;">Temp Support Session</h3>
|
||||
<p style="font-size: 13px; color: hsl(var(--muted-foreground)); margin-bottom: 12px;">
|
||||
Generate a support code first, then create a download link with that code embedded.
|
||||
</p>
|
||||
<div style="display: flex; gap: 8px; align-items: center;">
|
||||
<input type="text" id="supportCodeInput" placeholder="6-digit code" maxlength="6"
|
||||
style="width: 120px; padding: 8px 12px; font-size: 14px; background: hsl(var(--input)); border: 1px solid hsl(var(--border)); border-radius: 6px; color: hsl(var(--foreground));">
|
||||
<button class="btn btn-outline" onclick="downloadSupportAgent()">Generate Link</button>
|
||||
</div>
|
||||
<div id="supportDownloadLink" style="margin-top: 12px; display: none;">
|
||||
<a href="#" id="supportDownloadAnchor" class="btn btn-primary" style="text-decoration: none;">Download Support Agent</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Permanent Agent Builder -->
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<div>
|
||||
<h2 class="card-title">Permanent Agent Builder</h2>
|
||||
<p class="card-description">Create customized agent installers for unattended access</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-grid">
|
||||
<div class="form-group">
|
||||
<label for="buildName">Name</label>
|
||||
<input type="text" id="buildName" placeholder="Machine name (auto if blank)">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="buildCompany">Company</label>
|
||||
<label for="buildCompany">Company *</label>
|
||||
<input type="text" id="buildCompany" placeholder="Client organization">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
@@ -498,29 +528,19 @@
|
||||
<input type="text" id="buildSite" placeholder="Physical location">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="buildDepartment">Department</label>
|
||||
<input type="text" id="buildDepartment" placeholder="Business unit">
|
||||
<label for="buildTags">Tags</label>
|
||||
<input type="text" id="buildTags" placeholder="Comma-separated (e.g., workstation, finance)">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="buildDeviceType">Device Type</label>
|
||||
<select id="buildDeviceType">
|
||||
<option value="workstation">Workstation</option>
|
||||
<option value="laptop">Laptop</option>
|
||||
<option value="server">Server</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="buildTag">Tag</label>
|
||||
<input type="text" id="buildTag" placeholder="Custom label">
|
||||
<label for="buildApiKey">API Key</label>
|
||||
<input type="text" id="buildApiKey" placeholder="Optional (uses default if blank)">
|
||||
</div>
|
||||
</div>
|
||||
<div style="margin-top: 24px; display: flex; gap: 12px;">
|
||||
<button class="btn btn-primary" disabled>Build EXE (64-bit)</button>
|
||||
<button class="btn btn-outline" disabled>Build EXE (32-bit)</button>
|
||||
<button class="btn btn-outline" disabled>Build MSI</button>
|
||||
<div style="margin-top: 24px; display: flex; gap: 12px; flex-wrap: wrap;">
|
||||
<button class="btn btn-primary" onclick="buildPermanentAgent()">Download Configured Agent</button>
|
||||
</div>
|
||||
<p style="margin-top: 16px; font-size: 13px; color: hsl(var(--muted-foreground));">
|
||||
Agent builds will be available once the agent is compiled.
|
||||
The downloaded agent will have company/site/tags embedded. It will auto-register when run.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1371,6 +1391,46 @@
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
// ========== Download Functions ==========
|
||||
|
||||
function downloadSupportAgent() {
|
||||
const code = document.getElementById("supportCodeInput").value.trim();
|
||||
if (!code || code.length !== 6 || !/^\d{6}$/.test(code)) {
|
||||
alert("Please enter a valid 6-digit support code.");
|
||||
return;
|
||||
}
|
||||
|
||||
const downloadUrl = "/api/download/support?code=" + code;
|
||||
const anchor = document.getElementById("supportDownloadAnchor");
|
||||
anchor.href = downloadUrl;
|
||||
anchor.textContent = "Download GuruConnect-" + code + ".exe";
|
||||
document.getElementById("supportDownloadLink").style.display = "block";
|
||||
|
||||
// Automatically start download
|
||||
window.location.href = downloadUrl;
|
||||
}
|
||||
|
||||
function buildPermanentAgent() {
|
||||
const company = document.getElementById("buildCompany").value.trim();
|
||||
const site = document.getElementById("buildSite").value.trim();
|
||||
const tags = document.getElementById("buildTags").value.trim();
|
||||
const apiKey = document.getElementById("buildApiKey").value.trim();
|
||||
|
||||
if (!company) {
|
||||
alert("Company name is required for permanent agent builds.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Build URL with query parameters
|
||||
let url = "/api/download/agent?company=" + encodeURIComponent(company);
|
||||
if (site) url += "&site=" + encodeURIComponent(site);
|
||||
if (tags) url += "&tags=" + encodeURIComponent(tags);
|
||||
if (apiKey) url += "&api_key=" + encodeURIComponent(apiKey);
|
||||
|
||||
// Start download
|
||||
window.location.href = url;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
BIN
server/static/downloads/guruconnect.exe
Normal file
BIN
server/static/downloads/guruconnect.exe
Normal file
Binary file not shown.
Reference in New Issue
Block a user