eoka_protocol/
launch_spec.rs1use std::path::{Path, PathBuf};
2
3#[derive(Debug, Clone, serde::Serialize)]
4pub enum LaunchSpec {
5 Reuse,
6 Launch {
7 headless: bool,
8 from_profile: Option<PathBuf>,
9 clone_state_from: Option<String>,
10 no_stealth: bool,
11 proxy: Option<String>,
12 no_js: bool,
13 js_allow: Vec<String>,
14 js_block: Vec<String>,
15 persist: bool,
16 geo_align: bool,
17 auto_solve_cloudflare: bool,
18 sandbox: bool,
19 },
20 Connect {
21 ws_url: String,
22 },
23}
24
25pub fn resolve_proxy_spec(
26 proxy: Option<&str>,
27 proxy_file: Option<&Path>,
28) -> Result<Option<String>, String> {
29 if let Some(proxy) = proxy {
30 return Ok(Some(proxy.to_owned()));
31 }
32 let Some(path) = proxy_file else {
33 return Ok(None);
34 };
35 let contents = std::fs::read_to_string(path)
36 .map_err(|e| format!("Failed to read --proxy-file '{}': {}", path.display(), e))?;
37 let lines: Vec<&str> = contents
38 .lines()
39 .map(str::trim)
40 .filter(|line| !line.is_empty() && !line.starts_with('#'))
41 .collect();
42 if lines.is_empty() {
43 return Err(format!(
44 "--proxy-file '{}' does not contain a proxy",
45 path.display()
46 ));
47 }
48 use std::time::{SystemTime, UNIX_EPOCH};
49 let idx = SystemTime::now()
50 .duration_since(UNIX_EPOCH)
51 .map(|d| d.as_nanos() as usize % lines.len())
52 .unwrap_or(0);
53 Ok(Some(lines[idx].to_owned()))
54}
55
56impl LaunchSpec {
57 pub fn configuration_fingerprint(&self) -> String {
58 use sha2::{Digest, Sha256};
59 Sha256::digest(serde_json::to_vec(self).unwrap_or_default())
60 .iter()
61 .map(|byte| format!("{byte:02x}"))
62 .collect()
63 }
64
65 pub fn has_creation_settings(&self) -> bool {
66 match self {
67 Self::Reuse => false,
68 Self::Connect { .. } => true,
69 Self::Launch {
70 from_profile,
71 clone_state_from,
72 no_stealth,
73 proxy,
74 no_js,
75 js_allow,
76 js_block,
77 persist,
78 geo_align,
79 auto_solve_cloudflare,
80 sandbox,
81 ..
82 } => {
83 from_profile.is_some()
84 || clone_state_from.is_some()
85 || *no_stealth
86 || proxy.is_some()
87 || *no_js
88 || !js_allow.is_empty()
89 || !js_block.is_empty()
90 || *persist
91 || !geo_align
92 || *auto_solve_cloudflare
93 || !sandbox
94 }
95 }
96 }
97
98 pub fn is_live(&self) -> bool {
99 matches!(self, LaunchSpec::Connect { .. })
100 }
101}
102
103pub fn resolve_cdp_spec(spec: &str) -> Result<String, String> {
104 let trimmed = spec.trim();
105 if trimmed.eq_ignore_ascii_case("auto") {
106 return auto_connect().map(|(_, url)| url);
107 }
108 if trimmed.starts_with("ws://") || trimmed.starts_with("wss://") {
109 return Ok(trimmed.to_string());
110 }
111 let port: u16 = trimmed.parse().map_err(|_| {
112 format!(
113 "Invalid --cdp spec '{}': expected port, ws:// URL, or 'auto'",
114 spec
115 )
116 })?;
117 eoka::cdp::discover_browser_ws("127.0.0.1", port)
118 .map_err(|e| format!("Failed to discover Chrome on 127.0.0.1:{}: {}", port, e))
119}
120
121pub fn auto_connect() -> Result<(u16, String), String> {
122 eoka::cdp::auto_connect(9222, 9229)
123 .ok_or_else(|| "No running Chrome found on ports 9222-9229".to_string())
124}
125
126pub fn profile_dir(session: &str) -> PathBuf {
127 if let Ok(base) = std::env::var(crate::env::PROFILE_DIR) {
128 return PathBuf::from(base).join(session);
129 }
130 let base = std::env::var("XDG_STATE_HOME")
131 .map(PathBuf::from)
132 .unwrap_or_else(|_| {
133 let home = std::env::var("HOME").unwrap_or_default();
134 if home.is_empty() {
135 std::env::temp_dir()
136 } else {
137 PathBuf::from(home).join(".local/state")
138 }
139 });
140 base.join("eoka/profiles").join(session)
141}