fix hosted session binding and harden panel config resolution

This commit is contained in:
晴天
2026-04-10 18:52:03 +08:00
parent 6beaa7f46a
commit 3105e56fd6
4 changed files with 211 additions and 20 deletions

View File

@@ -32,6 +32,78 @@ fn default_openclaw_dir() -> PathBuf {
dirs::home_dir().unwrap_or_default().join(".openclaw")
}
fn panel_path_key(path: &std::path::Path) -> String {
#[cfg(target_os = "windows")]
{
return path
.to_string_lossy()
.replace('/', "\\")
.to_lowercase();
}
#[cfg(not(target_os = "windows"))]
{
path.to_string_lossy().to_string()
}
}
fn push_unique_panel_config_path(paths: &mut Vec<PathBuf>, path: PathBuf) {
let key = panel_path_key(&path);
if paths.iter().any(|existing| panel_path_key(existing) == key) {
return;
}
paths.push(path);
}
fn panel_config_candidate_paths() -> Vec<PathBuf> {
let mut paths = Vec::new();
push_unique_panel_config_path(&mut paths, default_openclaw_dir().join("clawpanel.json"));
#[cfg(target_os = "windows")]
{
if let Ok(profile) = std::env::var("USERPROFILE") {
let trimmed = profile.trim();
if !trimmed.is_empty() {
push_unique_panel_config_path(
&mut paths,
PathBuf::from(trimmed).join(".openclaw").join("clawpanel.json"),
);
}
}
if let (Ok(home_drive), Ok(home_path)) = (
std::env::var("HOMEDRIVE"),
std::env::var("HOMEPATH"),
) {
let combined = format!("{}{}", home_drive.trim(), home_path.trim());
let trimmed = combined.trim();
if !trimmed.is_empty() {
push_unique_panel_config_path(
&mut paths,
PathBuf::from(trimmed).join(".openclaw").join("clawpanel.json"),
);
}
}
if let Ok(appdata) = std::env::var("APPDATA") {
let appdata_path = PathBuf::from(appdata.trim());
if let Some(profile_dir) = appdata_path.parent().and_then(|p| p.parent()) {
push_unique_panel_config_path(
&mut paths,
profile_dir.join(".openclaw").join("clawpanel.json"),
);
}
}
}
paths
}
fn read_panel_config_from(path: &std::path::Path) -> Option<serde_json::Value> {
std::fs::read_to_string(path)
.ok()
.and_then(|content| serde_json::from_str(&content).ok())
}
fn normalize_custom_openclaw_dir(raw: &str) -> Option<PathBuf> {
let trimmed = raw.trim();
if trimmed.is_empty() {
@@ -121,8 +193,21 @@ fn read_gateway_port_from_config() -> u16 {
}
fn panel_config_path() -> PathBuf {
// ClawPanel 自身配置始终在默认目录,不随 openclawDir 变化
default_openclaw_dir().join("clawpanel.json")
let candidates = panel_config_candidate_paths();
for path in &candidates {
if read_panel_config_from(path).is_some() {
return path.clone();
}
}
for path in &candidates {
if path.exists() {
return path.clone();
}
}
candidates
.into_iter()
.next()
.unwrap_or_else(|| default_openclaw_dir().join("clawpanel.json"))
}
#[cfg(target_os = "windows")]
@@ -151,9 +236,12 @@ pub(crate) fn windows_npm_global_prefix() -> Option<String> {
}
pub fn read_panel_config_value() -> Option<serde_json::Value> {
std::fs::read_to_string(panel_config_path())
.ok()
.and_then(|content| serde_json::from_str(&content).ok())
for path in panel_config_candidate_paths() {
if let Some(value) = read_panel_config_from(&path) {
return Some(value);
}
}
None
}
pub fn configured_proxy_url() -> Option<String> {