diff --git a/src/api/admin/pages/deploy.rs b/src/api/admin/pages/deploy.rs index 13cfeb9..0195b61 100644 --- a/src/api/admin/pages/deploy.rs +++ b/src/api/admin/pages/deploy.rs @@ -9,14 +9,52 @@ use super::shared::{html_escape, require_admin}; use crate::api::error::ApiError; use crate::api::middleware::AuthedUser; use axum::extract::Form; +use axum::http::HeaderMap; use axum::response::Html; use serde::Deserialize; use serde_json::json; -pub async fn index(admin: AuthedUser) -> Result, ApiError> { +pub async fn index(admin: AuthedUser, headers: HeaderMap) -> Result, ApiError> { require_admin(&admin)?; let pubkey = read_pubkey(); - Ok(Html(render_form(&pubkey, "", "", "", "", None))) + // Best-effort prefill: the Host the admin's browser is currently + // talking to is almost always the same machine running hbbs, so it's + // the right default for the rendezvous-host field. Reverse proxies + // forward the original Host through unless explicitly stripped, so + // this works behind nginx/Caddy/Traefik too. Operator can edit if + // hbbr lives on a different host. + let host_default = headers + .get(axum::http::header::HOST) + .and_then(|v| v.to_str().ok()) + .map(host_only) + .unwrap_or("") + .to_string(); + let (api_default, relay_default) = if host_default.is_empty() { + (String::new(), String::new()) + } else { + (format!("https://{}", host_default), host_default.clone()) + }; + Ok(Html(render_form( + &pubkey, + &host_default, + &api_default, + &relay_default, + "", + None, + ))) +} + +/// Strip an optional `:port` (and IPv6 brackets) from a Host-header value. +/// "rustdesk.example.com:21114" -> "rustdesk.example.com" +/// "[::1]:21114" -> "::1" +/// "10.196.83.110" -> "10.196.83.110" +fn host_only(s: &str) -> &str { + if let Some(rest) = s.strip_prefix('[') { + if let Some(end) = rest.find(']') { + return &rest[..end]; + } + } + s.rsplit_once(':').map(|(h, _)| h).unwrap_or(s) } #[derive(Debug, Deserialize)] @@ -117,7 +155,14 @@ fn render_form(

The hostname or IP clients reach hbbs at (TCP/UDP 21116).

@@ -125,15 +170,17 @@ fn render_form(
-

Full URL of this admin/login API. Leave blank to disable login on the client.

+

Full URL of this admin/login API. Defaults to https://<host>; edit if your API runs on a different scheme/port. Leave blank to disable login on the client.

Only set if hbbr runs on a separate host; otherwise leave blank.