feat: KVM port management — configuration, switching, and navigation shell
Backend (crates/ericrfb-proxy): - Session cookie now persisted in AppState for device API calls - New kvm.rs with three REST endpoints: GET /api/kvm/ports — scrapes kvm.asp, returns port config as JSON PUT /api/kvm/ports — saves port names, hotkeys, visibility, count POST /api/kvm/switch — switches active KVM port via home2.asp - HTML scraping extracts form values from predictable firmware HTML Frontend (crates/ericrfb-frontend): - New shell.ts: sidebar navigation with page routing pattern (Console, Ports — extensible for Virtual Media, Users, etc.) - Console refactored into pages/console.ts with mount/unmount lifecycle - Port switcher dropdown in toolbar (fetches port list, switches on change) - WebSocket auto-reconnects after port switch - New pages/ports.ts: editable port configuration table - Port count, key pause duration, per-port name/hotkey/show-in-console - Save, reload, and per-port switch buttons - Active port highlighted - Dark theme sidebar with active state indicators Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ tokio.workspace = true
|
||||
axum.workspace = true
|
||||
reqwest.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json = "1"
|
||||
toml.workspace = true
|
||||
tower-http.workspace = true
|
||||
tracing.workspace = true
|
||||
|
||||
252
crates/ericrfb-proxy/src/kvm.rs
Normal file
252
crates/ericrfb-proxy/src/kvm.rs
Normal file
@@ -0,0 +1,252 @@
|
||||
use axum::Json;
|
||||
use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::AppState;
|
||||
use crate::login::ErrorResponse;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct PortsResponse {
|
||||
pub port_count: u16,
|
||||
pub key_pause_duration: u16,
|
||||
pub active_port: u16,
|
||||
pub ports: Vec<PortInfo>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct PortInfo {
|
||||
pub index: u16,
|
||||
pub name: String,
|
||||
pub hotkey: String,
|
||||
pub show_in_rc: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SavePortsRequest {
|
||||
pub port_count: u16,
|
||||
pub key_pause_duration: u16,
|
||||
pub ports: Vec<PortInfo>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SwitchRequest {
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct SwitchResponse {
|
||||
pub active_port: u16,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async fn get_cookie(state: &AppState) -> Result<String, (StatusCode, Json<ErrorResponse>)> {
|
||||
state.session_cookie.read().await.clone().ok_or_else(|| {
|
||||
(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
Json(ErrorResponse {
|
||||
error: "not logged in".into(),
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn device_url(state: &AppState, path: &str) -> String {
|
||||
format!(
|
||||
"http://{}:{}{path}",
|
||||
state.config.omniview.host, state.config.omniview.http_port
|
||||
)
|
||||
}
|
||||
|
||||
fn api_err(msg: impl Into<String>) -> (StatusCode, Json<ErrorResponse>) {
|
||||
(
|
||||
StatusCode::BAD_GATEWAY,
|
||||
Json(ErrorResponse { error: msg.into() }),
|
||||
)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HTML scraping helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn extract_input_value(html: &str, name: &str) -> Option<String> {
|
||||
let needle = format!("name=\"{name}\"");
|
||||
let pos = html.find(&needle)?;
|
||||
let after = &html[pos..];
|
||||
let val_needle = "value=\"";
|
||||
let val_pos = after.find(val_needle)? + val_needle.len();
|
||||
let end = after[val_pos..].find('"')? + val_pos;
|
||||
Some(after[val_pos..end].to_string())
|
||||
}
|
||||
|
||||
fn extract_selected_option(html: &str, name: &str) -> Option<String> {
|
||||
let needle = format!("name=\"{name}\"");
|
||||
let pos = html.find(&needle)?;
|
||||
let after = &html[pos..];
|
||||
// Find "selected>" then the text until newline or '<'
|
||||
let sel_pos = after.find("selected>")?;
|
||||
let text_start = sel_pos + "selected>".len();
|
||||
let text_end = after[text_start..]
|
||||
.find(['<', '\n'])
|
||||
.unwrap_or(0)
|
||||
+ text_start;
|
||||
Some(after[text_start..text_end].trim().to_string())
|
||||
}
|
||||
|
||||
fn has_checked(html: &str, name: &str) -> bool {
|
||||
let needle = format!("name=\"{name}\"");
|
||||
if let Some(pos) = html.find(&needle) {
|
||||
// Check if "checked" appears nearby before the next input/tag
|
||||
let region = &html[pos.saturating_sub(80)..html.len().min(pos + 120)];
|
||||
region.contains("checked")
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// GET /api/kvm/ports
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn get_ports(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<PortsResponse>, (StatusCode, Json<ErrorResponse>)> {
|
||||
let cookie = get_cookie(&state).await?;
|
||||
|
||||
let html = state
|
||||
.http_client
|
||||
.get(device_url(&state, "/kvm.asp"))
|
||||
.header("cookie", &cookie)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| api_err(format!("fetch kvm.asp: {e}")))?
|
||||
.text()
|
||||
.await
|
||||
.map_err(|e| api_err(format!("read kvm.asp: {e}")))?;
|
||||
|
||||
let port_count: u16 = extract_selected_option(&html, "ECG_kvm_nr_ports")
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(16);
|
||||
|
||||
let key_pause_duration: u16 = extract_input_value(&html, "ECG_key_pause_duration")
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(100);
|
||||
|
||||
let active_port: u16 = extract_selected_option(&html, "kvm_active_port_0")
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(0);
|
||||
|
||||
let mut ports = Vec::with_capacity(port_count as usize);
|
||||
for i in 0..port_count {
|
||||
let name = extract_input_value(&html, &format!("ECG_kvm_portname_{i}")).unwrap_or_default();
|
||||
let hotkey = extract_input_value(&html, &format!("ECG_kvm_hotkey_{i}")).unwrap_or_default();
|
||||
let show_in_rc = has_checked(&html, &format!("ECG_kvm_show_in_rc_{i}"));
|
||||
ports.push(PortInfo {
|
||||
index: i,
|
||||
name,
|
||||
hotkey,
|
||||
show_in_rc,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Json(PortsResponse {
|
||||
port_count,
|
||||
key_pause_duration,
|
||||
active_port,
|
||||
ports,
|
||||
}))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// PUT /api/kvm/ports
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn save_ports(
|
||||
State(state): State<AppState>,
|
||||
Json(req): Json<SavePortsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
|
||||
let cookie = get_cookie(&state).await?;
|
||||
|
||||
let mut form: Vec<(String, String)> = vec![
|
||||
("ECG_kvm_nr_ports".into(), req.port_count.to_string()),
|
||||
(
|
||||
"ECG_key_pause_duration".into(),
|
||||
req.key_pause_duration.to_string(),
|
||||
),
|
||||
("ECG_kvm_portname_cnt".into(), req.port_count.to_string()),
|
||||
("ECG_kvm_hotkey_cnt".into(), req.port_count.to_string()),
|
||||
("ECG_kvm_show_in_rc_cnt".into(), req.port_count.to_string()),
|
||||
];
|
||||
|
||||
for port in &req.ports {
|
||||
form.push((
|
||||
format!("ECG_kvm_portname_{}", port.index),
|
||||
port.name.clone(),
|
||||
));
|
||||
form.push((
|
||||
format!("ECG_kvm_hotkey_{}", port.index),
|
||||
port.hotkey.clone(),
|
||||
));
|
||||
if port.show_in_rc {
|
||||
form.push((format!("ECG_kvm_show_in_rc_{}", port.index), "yes".into()));
|
||||
}
|
||||
}
|
||||
|
||||
form.push(("action_apply".into(), "Apply".into()));
|
||||
|
||||
let resp = state
|
||||
.http_client
|
||||
.post(device_url(&state, "/kvm.asp"))
|
||||
.header("cookie", &cookie)
|
||||
.form(&form)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| api_err(format!("post kvm.asp: {e}")))?;
|
||||
|
||||
let body = resp
|
||||
.text()
|
||||
.await
|
||||
.map_err(|e| api_err(format!("read response: {e}")))?;
|
||||
|
||||
if body.contains("ERIC_RESPONSE_OK") {
|
||||
Ok(Json(serde_json::json!({"ok": true})))
|
||||
} else {
|
||||
Err(api_err("device rejected configuration update"))
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// POST /api/kvm/switch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn switch_port(
|
||||
State(state): State<AppState>,
|
||||
Json(req): Json<SwitchRequest>,
|
||||
) -> Result<Json<SwitchResponse>, (StatusCode, Json<ErrorResponse>)> {
|
||||
let cookie = get_cookie(&state).await?;
|
||||
|
||||
let form = [
|
||||
("kvm_active_port_0", req.port.to_string()),
|
||||
("action_switch_0", "Switch".into()),
|
||||
];
|
||||
|
||||
state
|
||||
.http_client
|
||||
.post(device_url(&state, "/home2.asp"))
|
||||
.header("cookie", &cookie)
|
||||
.form(&form)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| api_err(format!("post home2.asp: {e}")))?;
|
||||
|
||||
Ok(Json(SwitchResponse {
|
||||
active_port: req.port,
|
||||
}))
|
||||
}
|
||||
@@ -88,6 +88,9 @@ pub async fn handle_login(
|
||||
let board_name =
|
||||
extract_param(&html, "BOARD_NAME").unwrap_or_else(|| "Remote IP Manager".into());
|
||||
|
||||
// Persist session cookie for KVM API calls
|
||||
*state.session_cookie.write().await = Some(cookie);
|
||||
|
||||
tracing::info!(
|
||||
"login successful: board={board_name}, applet_id={}...",
|
||||
&applet_id[..applet_id.len().min(16)]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod config;
|
||||
mod kvm;
|
||||
mod login;
|
||||
mod ws;
|
||||
|
||||
@@ -7,6 +8,7 @@ use std::sync::Arc;
|
||||
use axum::Router;
|
||||
use axum::routing::{get, post};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::RwLock;
|
||||
use tower_http::services::ServeDir;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
@@ -14,6 +16,7 @@ use tracing_subscriber::EnvFilter;
|
||||
pub struct AppState {
|
||||
pub config: Arc<config::ProxyConfig>,
|
||||
pub http_client: reqwest::Client,
|
||||
pub session_cookie: Arc<RwLock<Option<String>>>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -36,11 +39,14 @@ async fn main() -> anyhow::Result<()> {
|
||||
.danger_accept_invalid_certs(true)
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.build()?,
|
||||
session_cookie: Arc::new(RwLock::new(None)),
|
||||
};
|
||||
|
||||
let app = Router::new()
|
||||
.route("/api/login", post(login::handle_login))
|
||||
.route("/api/ws", get(ws::handle_ws))
|
||||
.route("/api/kvm/ports", get(kvm::get_ports).put(kvm::save_ports))
|
||||
.route("/api/kvm/switch", post(kvm::switch_port))
|
||||
.fallback_service(ServeDir::new(&cfg.static_dir))
|
||||
.with_state(state);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user