feat(rustdesk): 优化视频编码协商和添加公共服务器支持

- 调整视频编码优先级为 H264 > H265 > VP8 > VP9,优先使用硬件编码
- 对接 RustDesk 客户端质量预设 (Low/Balanced/Best) 到 BitratePreset
- 添加 secrets.toml 编译时读取机制,支持配置公共服务器
- 默认公共服务器: rustdesk.mofeng.run:21116
- 前端 ID 服务器输入框添加问号提示,显示公共服务器信息
- 用户留空时自动使用公共服务器
This commit is contained in:
mofeng-git
2026-01-02 17:22:34 +08:00
parent be4de59f3b
commit 28ecf951df
29 changed files with 776 additions and 316 deletions

105
build.rs
View File

@@ -1,3 +1,6 @@
use std::fs;
use std::path::Path;
fn main() {
// Set BUILD_DATE environment variable for compile-time access
// Use system time to avoid adding chrono as a build dependency
@@ -17,10 +20,14 @@ fn main() {
// Compile protobuf files for RustDesk protocol
compile_protos();
// Generate secrets module from secrets.toml
generate_secrets();
// Rerun if the script itself changes
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=protos/rendezvous.proto");
println!("cargo:rerun-if-changed=protos/message.proto");
println!("cargo:rerun-if-changed=secrets.toml");
}
/// Compile protobuf files using prost-build
@@ -36,6 +43,104 @@ fn compile_protos() {
.expect("Failed to compile protobuf files");
}
/// Generate secrets module from secrets.toml
///
/// This reads the secrets.toml file and generates a Rust module with
/// compile-time constants for sensitive configuration values.
fn generate_secrets() {
let out_dir = std::env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("secrets_generated.rs");
// Default values if secrets.toml doesn't exist
let mut rustdesk_public_server = String::new();
let mut rustdesk_public_key = String::new();
let mut turn_server = String::new();
let mut turn_username = String::new();
let mut turn_password = String::new();
// Try to read secrets.toml
if let Ok(content) = fs::read_to_string("secrets.toml") {
if let Ok(value) = content.parse::<toml::Value>() {
// RustDesk section
if let Some(rustdesk) = value.get("rustdesk") {
if let Some(v) = rustdesk.get("public_server").and_then(|v| v.as_str()) {
rustdesk_public_server = v.to_string();
}
if let Some(v) = rustdesk.get("public_key").and_then(|v| v.as_str()) {
rustdesk_public_key = v.to_string();
}
}
// TURN section (for future use)
if let Some(turn) = value.get("turn") {
if let Some(v) = turn.get("server").and_then(|v| v.as_str()) {
turn_server = v.to_string();
}
if let Some(v) = turn.get("username").and_then(|v| v.as_str()) {
turn_username = v.to_string();
}
if let Some(v) = turn.get("password").and_then(|v| v.as_str()) {
turn_password = v.to_string();
}
}
} else {
println!("cargo:warning=Failed to parse secrets.toml");
}
} else {
println!("cargo:warning=secrets.toml not found, using empty defaults");
}
// Generate the secrets module
let code = format!(
r#"// Auto-generated secrets module
// DO NOT EDIT - This file is generated by build.rs from secrets.toml
/// RustDesk public server configuration
pub mod rustdesk {{
/// Public RustDesk ID server address (used when user leaves field empty)
pub const PUBLIC_SERVER: &str = "{}";
/// Public key for the RustDesk server (for client connection)
pub const PUBLIC_KEY: &str = "{}";
/// Check if public server is configured
pub const fn has_public_server() -> bool {{
!PUBLIC_SERVER.is_empty()
}}
}}
/// TURN server configuration (for WebRTC)
pub mod turn {{
/// TURN server address
pub const SERVER: &str = "{}";
/// TURN username
pub const USERNAME: &str = "{}";
/// TURN password
pub const PASSWORD: &str = "{}";
/// Check if TURN server is configured
pub const fn is_configured() -> bool {{
!SERVER.is_empty()
}}
}}
"#,
escape_string(&rustdesk_public_server),
escape_string(&rustdesk_public_key),
escape_string(&turn_server),
escape_string(&turn_username),
escape_string(&turn_password),
);
fs::write(&dest_path, code).expect("Failed to write secrets_generated.rs");
}
/// Escape special characters in a string for use in Rust string literals
fn escape_string(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
/// Convert days since Unix epoch to year-month-day
fn days_to_ymd(days: i64) -> (i32, u32, u32) {
// Algorithm from http://howardhinnant.github.io/date_algorithms.html