The Rust rewrite of save_state only captured cookies and localStorage for the current page's origin, silently dropping cross-domain data (e.g. SSO/CAS auth cookies). This was a regression from the JS version. Cookies: replace Network.getCookies with Network.getAllCookies to return cookies from all domains the browser has visited. localStorage: track visited origins in BrowserManager during navigation, then collect their localStorage via a temporary CDP target with Fetch interception (serves blank HTML to avoid real network requests). Co-authored-by: hyunjinee <leehj0110@kakao.com>
101 lines
2.6 KiB
Rust
101 lines
2.6 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use serde_json::{json, Value};
|
|
|
|
use super::cdp::client::CdpClient;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Cookie {
|
|
pub name: String,
|
|
pub value: String,
|
|
pub domain: String,
|
|
pub path: String,
|
|
#[serde(default)]
|
|
pub expires: f64,
|
|
#[serde(default)]
|
|
pub size: i64,
|
|
#[serde(default)]
|
|
pub http_only: bool,
|
|
#[serde(default)]
|
|
pub secure: bool,
|
|
#[serde(default)]
|
|
pub session: bool,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub same_site: Option<String>,
|
|
}
|
|
|
|
pub async fn get_all_cookies(client: &CdpClient, session_id: &str) -> Result<Vec<Cookie>, String> {
|
|
let result = client
|
|
.send_command_no_params("Network.getAllCookies", Some(session_id))
|
|
.await?;
|
|
|
|
let cookies: Vec<Cookie> = result
|
|
.get("cookies")
|
|
.and_then(|v| serde_json::from_value(v.clone()).ok())
|
|
.unwrap_or_default();
|
|
|
|
Ok(cookies)
|
|
}
|
|
|
|
pub async fn get_cookies(
|
|
client: &CdpClient,
|
|
session_id: &str,
|
|
urls: Option<Vec<String>>,
|
|
) -> Result<Vec<Cookie>, String> {
|
|
let params = match urls {
|
|
Some(ref u) if !u.is_empty() => json!({ "urls": u }),
|
|
_ => json!({}),
|
|
};
|
|
|
|
let result = client
|
|
.send_command("Network.getCookies", Some(params), Some(session_id))
|
|
.await?;
|
|
|
|
let cookies: Vec<Cookie> = result
|
|
.get("cookies")
|
|
.and_then(|v| serde_json::from_value(v.clone()).ok())
|
|
.unwrap_or_default();
|
|
|
|
Ok(cookies)
|
|
}
|
|
|
|
pub async fn set_cookies(
|
|
client: &CdpClient,
|
|
session_id: &str,
|
|
cookies: Vec<Value>,
|
|
current_url: Option<&str>,
|
|
) -> Result<(), String> {
|
|
let cookies: Vec<Value> = cookies
|
|
.into_iter()
|
|
.map(|mut c| {
|
|
// Auto-fill url if no domain/path/url provided
|
|
if c.get("url").is_none() && c.get("domain").is_none() && current_url.is_some() {
|
|
c.as_object_mut().map(|m| {
|
|
m.insert(
|
|
"url".to_string(),
|
|
Value::String(current_url.unwrap().to_string()),
|
|
)
|
|
});
|
|
}
|
|
c
|
|
})
|
|
.collect();
|
|
|
|
client
|
|
.send_command(
|
|
"Network.setCookies",
|
|
Some(json!({ "cookies": cookies })),
|
|
Some(session_id),
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn clear_cookies(client: &CdpClient, session_id: &str) -> Result<(), String> {
|
|
client
|
|
.send_command_no_params("Network.clearBrowserCookies", Some(session_id))
|
|
.await?;
|
|
Ok(())
|
|
}
|