gthings-cdp 0.5.0

Chrome DevTools Protocol transport — WebSocket connection, tab management, and browser lifecycle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::connection::Connection;
use crate::error::{CdpError, Result};
use std::path::PathBuf;
use std::sync::OnceLock;

/// Info about a running browser discovered by [`detect`].
#[derive(Debug, Clone, serde::Serialize)]
pub struct DetectedBrowser {
    pub ws_url: String,
    pub browser: String,
    pub version: String,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Multi-strategy browser detection.
///
/// Tries each strategy in order and returns the first successful result:
///
/// 1. **`GTHINGS_CDP_WS_URL`** environment variable (fastest bypass).
/// 2. **HTTP GET** `http://127.0.0.1:{port}/json/version` — parses `webSocketDebuggerUrl`.
/// 3. **HTTP GET** `http://127.0.0.1:{port}/json` — finds first `webSocketDebuggerUrl`.
/// 4. **HTTP GET** `http://127.0.0.1:{port}/json/list` — finds first `webSocketDebuggerUrl`.
/// 5. **DevToolsActivePort** scan across 10+ browser profile directories on macOS.
///
/// Returns [`CdpError::BrowserNotFound`] if all strategies fail.
pub async fn detect(port: u16) -> Result<DetectedBrowser> {
    // 1. Environment variable bypass
    if let Ok(ws_url) = std::env::var("GTHINGS_CDP_WS_URL") {
        if !ws_url.is_empty() {
            tracing::info!("detect: using GTHINGS_CDP_WS_URL env var");
            return Ok(DetectedBrowser {
                ws_url,
                browser: "env".into(),
                version: "unknown".into(),
            });
        }
    }

    // 2. HTTP /json/version
    if let Some(browser) = probe_http_version(port).await {
        tracing::info!("detect: found via /json/version");
        return Ok(browser);
    }

    // 3. HTTP /json
    if let Some(ws_url) = probe_http_list(port, "/json").await {
        tracing::info!("detect: found via /json");
        return Ok(DetectedBrowser {
            ws_url,
            browser: "unknown".into(),
            version: "unknown".into(),
        });
    }

    // 4. HTTP /json/list
    if let Some(ws_url) = probe_http_list(port, "/json/list").await {
        tracing::info!("detect: found via /json/list");
        return Ok(DetectedBrowser {
            ws_url,
            browser: "unknown".into(),
            version: "unknown".into(),
        });
    }

    // 5. DevToolsActivePort scan
    if let Some(browser) = probe_devtools_active_port(port).await {
        tracing::info!("detect: found via DevToolsActivePort");
        return Ok(browser);
    }

    Err(CdpError::BrowserNotFound { port })
}

/// Connect to a browser's CDP WebSocket endpoint.
///
/// This is a thin wrapper around [`Connection::connect`].
pub async fn connect(ws_url: &str) -> Result<Connection> {
    Connection::connect(ws_url).await
}

/// Dismiss the macOS "Allow remote debugging connection?" dialog that appears
/// as a **sheet** in Dia and other Chromium-based browsers when a CDP
/// connection is first attempted.
///
/// Uses `osascript`/System Events to detect the dialog sheet and click the
/// "Allow" button. Polls every 500 ms for up to ~10 seconds (20 attempts)
/// since the dialog may take 1–3 seconds to appear. Logs a warning if the
/// dialog is never found — the WebSocket handshake may still proceed.
#[cfg(target_os = "macos")]
pub fn dismiss_allow_debugging_dialog() {
    /// AppleScript that checks each known browser process for a sheet dialog
    /// (attached to window 1) and clicks the "Allow" button if found.
    /// Returns the browser name if dismissed, or empty string otherwise.
    /// Each `exists` check is wrapped in `try` so that missing processes
    /// don't abort the script.
    const SCRIPT: &str = r#"tell application "System Events"
        set browserName to ""
        try
            if exists (sheet 1 of window 1 of process "Dia") then
                tell process "Dia" to click button "Allow" of sheet 1 of window 1
                set browserName to "Dia"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Chromium") then
                tell process "Chromium" to click button "Allow" of sheet 1 of window 1
                set browserName to "Chromium"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Google Chrome") then
                tell process "Google Chrome" to click button "Allow" of sheet 1 of window 1
                set browserName to "Google Chrome"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Google Chrome Canary") then
                tell process "Google Chrome Canary" to click button "Allow" of sheet 1 of window 1
                set browserName to "Google Chrome Canary"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Microsoft Edge") then
                tell process "Microsoft Edge" to click button "Allow" of sheet 1 of window 1
                set browserName to "Microsoft Edge"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Microsoft Edge Canary") then
                tell process "Microsoft Edge Canary" to click button "Allow" of sheet 1 of window 1
                set browserName to "Microsoft Edge Canary"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Brave Browser") then
                tell process "Brave Browser" to click button "Allow" of sheet 1 of window 1
                set browserName to "Brave Browser"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Arc") then
                tell process "Arc" to click button "Allow" of sheet 1 of window 1
                set browserName to "Arc"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Vivaldi") then
                tell process "Vivaldi" to click button "Allow" of sheet 1 of window 1
                set browserName to "Vivaldi"
            end if
        end try
        try
            if browserName is "" and exists (sheet 1 of window 1 of process "Opera") then
                tell process "Opera" to click button "Allow" of sheet 1 of window 1
                set browserName to "Opera"
            end if
        end try
        return browserName
    end tell"#;

    for attempt in 1..=20 {
        let output = std::process::Command::new("osascript")
            .args(["-e", SCRIPT])
            .output();

        match output {
            Ok(out) => {
                let stdout = String::from_utf8_lossy(&out.stdout);
                let browser = stdout.trim();
                if !browser.is_empty() {
                    tracing::warn!("Dismissed remote debugging dialog for {browser} (attempt {attempt})");
                    return;
                }
            }
            Err(e) => {
                tracing::warn!("osascript failed: {e}");
            }
        }

        if attempt < 20 {
            std::thread::sleep(std::time::Duration::from_millis(500));
        }
    }

    tracing::warn!(
        "Remote debugging dialog not found after 20 attempts — continuing"
    );
}

/// Non-macOS: no-op.
#[cfg(not(target_os = "macos"))]
pub fn dismiss_allow_debugging_dialog() {}
// ---------------------------------------------------------------------------
// Internal probe helpers
// ---------------------------------------------------------------------------

/// Shared HTTP client with sensible timeouts.
fn http_client() -> &'static reqwest::Client {
    static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
    CLIENT.get_or_init(|| {
        reqwest::Client::builder()
            .connect_timeout(std::time::Duration::from_secs(3))
            .timeout(std::time::Duration::from_secs(10))
            .build()
            .expect("valid reqwest client config")
    })
}

/// Probe `/json/version` — the richest endpoint (includes `webSocketDebuggerUrl`,
/// `Browser`, and version info).
async fn probe_http_version(port: u16) -> Option<DetectedBrowser> {
    let url = format!("http://127.0.0.1:{port}/json/version");
    let client = http_client();

    let resp = client.get(&url).send().await.ok()?;
    let body: serde_json::Value = resp.json().await.ok()?;

    let ws_url = body.get("webSocketDebuggerUrl")?.as_str()?;
    let full_browser = body
        .get("Browser")
        .and_then(|v| v.as_str())
        .unwrap_or("unknown");
    let version = full_browser.to_string();
    // Extract short browser name from "Chrome/130.0.0.0" style string
    let browser = full_browser
        .split('/')
        .next()
        .unwrap_or("unknown")
        .to_string();

    Some(DetectedBrowser {
        ws_url: ws_url.to_string(),
        browser,
        version,
    })
}

/// Probe `/json` or `/json/list` — returns the `webSocketDebuggerUrl` of
/// the first available page target.
async fn probe_http_list(port: u16, path: &str) -> Option<String> {
    let url = format!("http://127.0.0.1:{port}{path}");
    let client = http_client();

    let resp = client.get(&url).send().await.ok()?;
    let list: Vec<serde_json::Value> = resp.json().await.ok()?;

    for entry in &list {
        if let Some(ws_url) = entry.get("webSocketDebuggerUrl").and_then(|v| v.as_str()) {
            return Some(ws_url.to_string());
        }
    }

    None
}

/// Scan well-known browser profile directories for a `DevToolsActivePort`
/// file whose port matches the requested port, then verify via TCP connect.
async fn probe_devtools_active_port(port: u16) -> Option<DetectedBrowser> {
    let profile_dirs = get_profile_dirs();
    if profile_dirs.is_empty() {
        return None;
    }

    // Synchronous file reads are fine here — negligible for a handful of files.
    let result: Option<DetectedBrowser> = tokio::task::spawn_blocking(move || {
        for dir in &profile_dirs {
            let active_port_path = dir.join("DevToolsActivePort");
            let content = match std::fs::read_to_string(&active_port_path) {
                Ok(c) => c,
                Err(_) => continue,
            };
            let lines: Vec<&str> = content.trim().lines().collect();
            if lines.len() < 2 {
                continue;
            }
            let file_port: u16 = match lines[0].trim().parse().ok() {
                Some(p) => p,
                None => continue,
            };
            if file_port != port {
                continue;
            }
            let ws_path = lines[1].trim();
            let ws_url = format!("ws://127.0.0.1:{port}{ws_path}");

            // Verify port is accepting TCP connections
            let addr: std::net::SocketAddr = format!("127.0.0.1:{port}").parse().ok()?;
            if std::net::TcpStream::connect_timeout(&addr, std::time::Duration::from_millis(500))
                .is_ok()
            {
                let browser_name = infer_browser_name(dir);
                return Some(DetectedBrowser {
                    ws_url,
                    browser: browser_name,
                    version: "unknown".into(),
                });
            }
        }
        None
    })
    .await
    .ok()?;
    result
}

/// Return all possible macOS browser profile directories.
fn get_profile_dirs() -> Vec<PathBuf> {
    let home = std::env::var("HOME").ok().map(PathBuf::from);
    let home = match home {
        Some(h) => h,
        None => return Vec::new(),
    };

    let app_support = home.join("Library/Application Support");
    let dirs = vec![
        app_support.join("Dia/User Data"),
        app_support.join("Google/Chrome"),
        app_support.join("Google/Chrome Canary"),
        app_support.join("Chromium"),
        app_support.join("Microsoft Edge"),
        app_support.join("Microsoft Edge Canary"),
        app_support.join("BraveSoftware/Brave-Browser"),
        app_support.join("Arc/User Data"),
        app_support.join("Vivaldi"),
        app_support.join("com.operasoftware.Opera"),
    ];
    dirs.into_iter().filter(|p| p.exists()).collect()
}

/// Infer a human-readable browser name from a profile directory path.
fn infer_browser_name(path: &std::path::Path) -> String {
    let s = path.to_string_lossy();
    let s = s.as_ref();
    if s.contains("Dia") {
        "Dia".into()
    } else if s.contains("Chrome Canary") {
        "Google Chrome Canary".into()
    } else if s.contains("Chrome") {
        "Google Chrome".into()
    } else if s.contains("Chromium") {
        "Chromium".into()
    } else if s.contains("Edge Canary") {
        "Microsoft Edge Canary".into()
    } else if s.contains("Edge") {
        "Microsoft Edge".into()
    } else if s.contains("Brave") {
        "Brave".into()
    } else if s.contains("Arc") {
        "Arc".into()
    } else if s.contains("Vivaldi") {
        "Vivaldi".into()
    } else if s.contains("Opera") {
        "Opera".into()
    } else {
        "unknown".into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detect_no_browser() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(async { detect(29_999).await });
        assert!(result.is_err(), "detect on unused port should return error");
        match result {
            Err(CdpError::BrowserNotFound { port }) => assert_eq!(port, 29_999),
            _ => panic!("expected BrowserNotFound"),
        }
    }

    #[test]
    fn test_infer_browser_name() {
        fn p(s: &str) -> &std::path::Path {
            std::path::Path::new(s)
        }
        assert_eq!(infer_browser_name(p("/Dia/User Data")), "Dia");
        assert_eq!(infer_browser_name(p("/Google/Chrome")), "Google Chrome");
        assert_eq!(
            infer_browser_name(p("/Google/Chrome Canary")),
            "Google Chrome Canary"
        );
        assert_eq!(infer_browser_name(p("/Chromium")), "Chromium");
        assert_eq!(infer_browser_name(p("/Microsoft Edge")), "Microsoft Edge");
        assert_eq!(
            infer_browser_name(p("/BraveSoftware/Brave-Browser")),
            "Brave"
        );
        assert_eq!(infer_browser_name(p("/Arc/User Data")), "Arc");
        assert_eq!(infer_browser_name(p("/Vivaldi")), "Vivaldi");
        assert_eq!(infer_browser_name(p("/com.operasoftware.Opera")), "Opera");
        assert_eq!(infer_browser_name(p("/Unknown/Path")), "unknown");
    }

    #[test]
    fn test_detected_browser_serialize() {
        let db = DetectedBrowser {
            ws_url: "ws://127.0.0.1:9222/devtools/browser/abc".into(),
            browser: "Chrome".into(),
            version: "130.0.0.0".into(),
        };
        let json_str = serde_json::to_string(&db).unwrap();
        // Round-trip through Value to assert specific field values
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(
            parsed["ws_url"].as_str(),
            Some("ws://127.0.0.1:9222/devtools/browser/abc")
        );
        assert_eq!(parsed["browser"].as_str(), Some("Chrome"));
        assert_eq!(parsed["version"].as_str(), Some("130.0.0.0"));
    }
}