Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 70 additions & 52 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,36 +1128,45 @@ fn start_cliproxyapi(app: tauri::AppHandle) -> Result<serde_json::Value, String>
eprintln!("[PORT_CLEANUP] Warning: {}", e);
}

// Generate random password for local mode
let password = generate_random_password();
// Check if a password already exists in the config, otherwise generate one
let password = if let Some(sk) = conf
.get("remote-management")
.and_then(|rm| rm.get("secret-key"))
.and_then(|sk| sk.as_str())
.filter(|s| !s.trim().is_empty())
{
sk.to_string()
} else {
let new_pass = generate_random_password();

// Store the password for keep-alive authentication
*CLI_PROXY_PASSWORD.lock() = Some(password.clone());
// Ensure remote-management section exists and insert new password
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
);
}

// Ensure remote-management section exists
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
let rm = conf
.as_mapping_mut()
.unwrap()
.get_mut(&serde_yaml::Value::from("remote-management"))
.unwrap()
.as_mapping_mut()
.unwrap();
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(new_pass.as_str()),
);
Comment on lines +1143 to 1164
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of .unwrap() on the configuration mapping is unsafe as it will cause the application to panic if the config.yaml file is malformed or empty (e.g., if it parses to Value::Null). Additionally, the logic for ensuring the remote-management section exists can be simplified and made more robust using the entry API. Finally, this entire block of logic is duplicated in restart_cliproxyapi and should be refactored into a shared helper function to improve maintainability.

Suggested change
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
);
}
// Ensure remote-management section exists
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
let rm = conf
.as_mapping_mut()
.unwrap()
.get_mut(&serde_yaml::Value::from("remote-management"))
.unwrap()
.as_mapping_mut()
.unwrap();
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(new_pass.as_str()),
);
let rm = conf
.as_mapping_mut()
.ok_or_else(|| "Invalid config structure".to_string())?
.entry(serde_yaml::Value::from("remote-management"))
.or_insert_with(|| serde_yaml::Value::Mapping(Default::default()))
.as_mapping_mut()
.ok_or_else(|| "remote-management is not a mapping".to_string())?;
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(new_pass.as_str()),
);

}
new_pass
};

// Set the secret-key
let rm = conf
.as_mapping_mut()
.unwrap()
.get_mut(&serde_yaml::Value::from("remote-management"))
.unwrap()
.as_mapping_mut()
.unwrap();
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(password.as_str()),
);
// Store the password for keep-alive authentication
*CLI_PROXY_PASSWORD.lock() = Some(password.clone());

// Write updated config
let updated_content = serde_yaml::to_string(&conf).map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -1258,36 +1267,45 @@ fn restart_cliproxyapi(app: tauri::AppHandle) -> Result<(), String> {
eprintln!("[PORT_CLEANUP] Warning: {}", e);
}

// Generate random password for local mode
let password = generate_random_password();
// Check if a password already exists in the config, otherwise generate one
let password = if let Some(sk) = conf
.get("remote-management")
.and_then(|rm| rm.get("secret-key"))
.and_then(|sk| sk.as_str())
.filter(|s| !s.trim().is_empty())
{
sk.to_string()
} else {
let new_pass = generate_random_password();

// Store the password for keep-alive authentication
*CLI_PROXY_PASSWORD.lock() = Some(password.clone());
// Ensure remote-management section exists and insert new password
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
);
}

// Ensure remote-management section exists
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
let rm = conf
.as_mapping_mut()
.unwrap()
.get_mut(&serde_yaml::Value::from("remote-management"))
.unwrap()
.as_mapping_mut()
.unwrap();
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(new_pass.as_str()),
);
Comment on lines +1282 to 1303
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the implementation in start_cliproxyapi, the use of .unwrap() here is unsafe and can lead to panics if the configuration file structure is unexpected. Using the entry API provides a cleaner and safer way to manage the configuration mapping. This logic is identical to the one in start_cliproxyapi and should be refactored to avoid duplication.

Suggested change
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
);
}
// Ensure remote-management section exists
if !conf
.as_mapping()
.unwrap()
.contains_key(&serde_yaml::Value::from("remote-management"))
{
conf.as_mapping_mut().unwrap().insert(
serde_yaml::Value::from("remote-management"),
serde_yaml::Value::Mapping(Default::default()),
let rm = conf
.as_mapping_mut()
.unwrap()
.get_mut(&serde_yaml::Value::from("remote-management"))
.unwrap()
.as_mapping_mut()
.unwrap();
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(new_pass.as_str()),
);
let rm = conf
.as_mapping_mut()
.ok_or_else(|| "Invalid config structure".to_string())?
.entry(serde_yaml::Value::from("remote-management"))
.or_insert_with(|| serde_yaml::Value::Mapping(Default::default()))
.as_mapping_mut()
.ok_or_else(|| "remote-management is not a mapping".to_string())?;
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(new_pass.as_str()),
);

}
new_pass
};

// Set the secret-key
let rm = conf
.as_mapping_mut()
.unwrap()
.get_mut(&serde_yaml::Value::from("remote-management"))
.unwrap()
.as_mapping_mut()
.unwrap();
rm.insert(
serde_yaml::Value::from("secret-key"),
serde_yaml::Value::from(password.as_str()),
);
// Store the password for keep-alive authentication
*CLI_PROXY_PASSWORD.lock() = Some(password.clone());

// Write updated config
let updated_content = serde_yaml::to_string(&conf).map_err(|e| e.to_string())?;
Expand Down