Prefill deploy parameters by defaults.

This commit is contained in:
2026-05-02 18:44:59 +02:00
parent 4730c46f46
commit 8b0219a877
+52 -5
View File
@@ -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<Html<String>, ApiError> {
pub async fn index(admin: AuthedUser, headers: HeaderMap) -> Result<Html<String>, 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(
<label class="block text-xs font-medium text-slate-400 mb-1" for="host">Rendezvous host (required)</label>
<input id="host" name="host" type="text" required value="{host}"
placeholder="rustdesk.example.com or 203.0.113.10"
oninput="document.getElementById('api').placeholder='http://'+this.value+':21114'"
oninput="
const h = this.value.trim();
const api = document.getElementById('api');
const relay = document.getElementById('relay');
if (api.dataset.derived !== '0') api.value = h ? 'https://' + h : '';
if (relay.dataset.derived !== '0') relay.value = h;
api.placeholder = h ? 'https://' + h : 'https://rustdesk.example.com';
"
class="w-full bg-slate-800 border border-slate-700 rounded px-3 py-2 text-sm focus:outline-none focus:border-sky-500" />
<p class="text-xs text-slate-500 mt-1">The hostname or IP clients reach hbbs at (TCP/UDP 21116).</p>
</div>
@@ -125,15 +170,17 @@ fn render_form(
<div>
<label class="block text-xs font-medium text-slate-400 mb-1" for="api">API URL (optional)</label>
<input id="api" name="api" type="text" value="{api}"
placeholder="http://host:21114"
placeholder="https://rustdesk.example.com"
oninput="this.dataset.derived = '0';"
class="w-full bg-slate-800 border border-slate-700 rounded px-3 py-2 text-sm focus:outline-none focus:border-sky-500" />
<p class="text-xs text-slate-500 mt-1">Full URL of this admin/login API. Leave blank to disable login on the client.</p>
<p class="text-xs text-slate-500 mt-1">Full URL of this admin/login API. Defaults to <code>https://&lt;host&gt;</code>; edit if your API runs on a different scheme/port. Leave blank to disable login on the client.</p>
</div>
<div>
<label class="block text-xs font-medium text-slate-400 mb-1" for="relay">Relay host (optional)</label>
<input id="relay" name="relay" type="text" value="{relay}"
placeholder="rustdesk.example.com"
oninput="this.dataset.derived = '0';"
class="w-full bg-slate-800 border border-slate-700 rounded px-3 py-2 text-sm focus:outline-none focus:border-sky-500" />
<p class="text-xs text-slate-500 mt-1">Only set if hbbr runs on a separate host; otherwise leave blank.</p>
</div>