eoka 0.7.2

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use super::*;
use futures_util::{SinkExt, StreamExt};
use serde_json::{json, Value};
use tokio::net::TcpStream;
use tokio_tungstenite::{tungstenite::Message, WebSocketStream};

async fn command(peer: &mut WebSocketStream<TcpStream>, method: &str) -> Value {
    let message = tokio::time::timeout(Duration::from_secs(2), peer.next())
        .await
        .unwrap()
        .unwrap()
        .unwrap();
    let command: Value = serde_json::from_str(message.to_text().unwrap()).unwrap();
    assert_eq!(command["method"], method);
    command
}

async fn reply(peer: &mut WebSocketStream<TcpStream>, command: &Value, result: Value) {
    peer.send(Message::Text(
        json!({"id":command["id"],"result":result})
            .to_string()
            .into(),
    ))
    .await
    .unwrap();
}

async fn reject(peer: &mut WebSocketStream<TcpStream>, command: &Value, code: i64, message: &str) {
    peer.send(Message::Text(
        json!({"id":command["id"],"error":{"code":code,"message":message}})
            .to_string()
            .into(),
    ))
    .await
    .unwrap();
}

type Fixture = (
    Arc<crate::cdp::Connection>,
    Page,
    WebSocketStream<TcpStream>,
);

async fn fixture() -> Fixture {
    shared_fixtures(1).await.pop().unwrap()
}

async fn shared_fixtures(count: usize) -> Vec<Fixture> {
    let listener = Arc::new(tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap());
    let url = format!("ws://{}", listener.local_addr().unwrap());
    let mut fixtures = Vec::new();
    for index in 0..count {
        let listener = Arc::clone(&listener);
        let accepted = tokio::spawn(async move {
            let (socket, _) = listener.accept().await.unwrap();
            tokio_tungstenite::accept_async(socket).await.unwrap()
        });
        let connection = Arc::new(crate::cdp::Connection::new(
            Transport::connect_with_options(&url, 1, false, false)
                .await
                .unwrap(),
        ));
        let mut peer = accepted.await.unwrap();
        let connecting = Arc::clone(&connection);
        let attached =
            tokio::spawn(async move { connecting.attach_to_target("recorded-target").await });
        let request = command(&mut peer, "Target.attachToTarget").await;
        let session_id = if index == 0 {
            "recording-session"
        } else {
            "other-session"
        };
        reply(&mut peer, &request, json!({"sessionId":session_id})).await;
        let page = Page::new(
            attached.await.unwrap().unwrap(),
            Arc::new(crate::StealthConfig::default()),
        );
        fixtures.push((connection, page, peer));
    }
    fixtures
}

async fn start(page: &Page, peer: &mut WebSocketStream<TcpStream>) -> NativeRecording {
    let page = page.clone();
    let task = tokio::spawn(async move {
        page.start_recording(NativeRecordingOptions {
            fps: Some(60.0),
            max_width: Some(1280),
            max_height: Some(720),
        })
        .await
    });
    let ready = command(peer, "Runtime.evaluate").await;
    reply(
        peer,
        &ready,
        json!({"result":{"type":"object","value":{"ready":true,"top":true}}}),
    )
    .await;
    let request = command(peer, "Page.startScreenRecording").await;
    assert_eq!(
        request["params"],
        json!({"audio":false,"maxWidth":1280,"maxHeight":720,"frameRate":60})
    );
    reply(peer, &request, json!({"stream":"start-stream"})).await;
    task.await.unwrap().unwrap()
}

#[test]
fn native_integer_options_and_chunk_boundaries() {
    for fps in [0.0, -1.0, 0.5, 30.5, 121.0, f64::INFINITY, f64::NAN] {
        assert!(matches!(
            NativeRecordingOptions {
                fps: Some(fps),
                ..Default::default()
            }
            .params(),
            Err(RecordingError::InvalidOptions(_))
        ));
    }
    for dimension in [0, i32::MAX as u32 + 1] {
        assert!(NativeRecordingOptions {
            max_width: Some(dimension),
            ..Default::default()
        }
        .params()
        .is_err());
        assert!(NativeRecordingOptions {
            max_height: Some(dimension),
            ..Default::default()
        }
        .params()
        .is_err());
    }
    for size in [0, CHUNK_BYTES - 1, CHUNK_BYTES] {
        let data = vec![0xa5; size];
        let chunk = decode_chunk(IoReadResult {
            data: base64::engine::general_purpose::STANDARD.encode(&data),
            base64_encoded: true,
            eof: true,
        })
        .unwrap();
        assert_eq!(chunk, RecordingChunk { data, eof: true });
    }
    for encoded in [false, true] {
        let data = "x".repeat(CHUNK_BYTES + 1);
        let data = if encoded {
            base64::engine::general_purpose::STANDARD.encode(data)
        } else {
            data
        };
        assert!(decode_chunk(IoReadResult {
            data,
            base64_encoded: encoded,
            eof: false
        })
        .is_err());
    }
    assert!(decode_chunk(IoReadResult {
        data: "not-base64".into(),
        base64_encoded: true,
        eof: false
    })
    .is_err());
}

#[tokio::test]
async fn stop_once_read_bounded_eof_and_close_both_native_handles() {
    let (connection, page, mut peer) = fixture().await;
    let recording = Arc::new(start(&page, &mut peer).await);
    assert!(matches!(
        page.start_recording(Default::default()).await,
        Err(RecordingError::AlreadyActive)
    ));
    assert!(matches!(
        recording.read_chunk().await,
        Err(RecordingError::InvalidState(_))
    ));
    let stopping = Arc::clone(&recording);
    let task = tokio::spawn(async move { stopping.stop().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"stop-stream"})).await;
    task.await.unwrap().unwrap();
    recording.stop().await.unwrap();
    let reading = Arc::clone(&recording);
    let task = tokio::spawn(async move { reading.read_chunk().await });
    let request = command(&mut peer, "IO.read").await;
    assert_eq!(
        request["params"],
        json!({"handle":"stop-stream","size":65536})
    );
    reply(
        &mut peer,
        &request,
        json!({"data":"AAEC/w==","base64Encoded":true,"eof":true}),
    )
    .await;
    assert_eq!(
        task.await.unwrap().unwrap(),
        RecordingChunk {
            data: vec![0, 1, 2, 255],
            eof: true
        }
    );
    assert_eq!(
        recording.read_chunk().await.unwrap(),
        RecordingChunk {
            data: Vec::new(),
            eof: true
        }
    );
    let closing = Arc::clone(&recording);
    let task = tokio::spawn(async move { closing.close().await });
    for stream in ["start-stream", "stop-stream"] {
        let request = command(&mut peer, "IO.close").await;
        assert_eq!(request["params"]["handle"], stream);
        reply(&mut peer, &request, json!({})).await;
    }
    task.await.unwrap().unwrap();
    recording.close().await.unwrap();
    let replacement = Arc::new(start(&page, &mut peer).await);
    // Old handles must not accidentally close the target's replacement recording.
    recording.close().await.unwrap();
    drop(recording);
    let cancelling = Arc::clone(&replacement);
    let task = tokio::spawn(async move { cancelling.cancel().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"start-stream"})).await;
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    task.await.unwrap().unwrap();
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn cancelled_start_retains_target_until_owned_stop_and_close() {
    let (connection, page, mut peer) = fixture().await;
    let starting = page.clone();
    let task = tokio::spawn(async move { starting.start_recording(Default::default()).await });
    let ready = command(&mut peer, "Runtime.evaluate").await;
    reply(
        &mut peer,
        &ready,
        json!({"result":{"value":{"ready":true,"top":true}}}),
    )
    .await;
    let request = command(&mut peer, "Page.startScreenRecording").await;
    task.abort();
    let _ = task.await;
    assert!(matches!(
        page.start_recording(Default::default()).await,
        Err(RecordingError::AlreadyActive)
    ));
    reply(&mut peer, &request, json!({"stream":"abandoned-stream"})).await;
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"abandoned-stream"})).await;
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    // Await bounded teardown to avoid racing its target release acknowledgement.
    connection.cancel_recordings().await.unwrap();
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn unsupported_and_not_ready_fail_without_fallback_or_stop() {
    let (connection, page, mut peer) = fixture().await;
    for ready in [false, true] {
        let starting = page.clone();
        let task = tokio::spawn(async move { starting.start_recording(Default::default()).await });
        let request = command(&mut peer, "Runtime.evaluate").await;
        reply(
            &mut peer,
            &request,
            json!({"result":{"value":{"ready":ready,"top":true}}}),
        )
        .await;
        if ready {
            let request = command(&mut peer, "Page.startScreenRecording").await;
            reject(
                &mut peer,
                &request,
                -32601,
                "Page.startScreenRecording wasn't found",
            )
            .await;
            assert!(matches!(
                task.await.unwrap(),
                Err(RecordingError::Unsupported(_))
            ));
        } else {
            assert!(matches!(
                task.await.unwrap(),
                Err(RecordingError::Precondition(_))
            ));
        }
    }
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn unacknowledged_stop_is_never_repeated_and_retains_reservation() {
    let (connection, page, mut peer) = fixture().await;
    let recording = Arc::new(start(&page, &mut peer).await);
    let stopping = Arc::clone(&recording);
    let task = tokio::spawn(async move { stopping.stop().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reject(&mut peer, &request, -32000, "native finalization failed").await;
    assert!(matches!(
        task.await.unwrap(),
        Err(RecordingError::CleanupUncertain(_))
    ));
    assert!(matches!(
        recording.stop().await,
        Err(RecordingError::CleanupUncertain(_))
    ));
    let closing = Arc::clone(&recording);
    let task = tokio::spawn(async move { closing.cancel().await });
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    assert!(matches!(
        task.await.unwrap(),
        Err(RecordingError::CleanupUncertain(_))
    ));
    assert!(matches!(
        page.start_recording(Default::default()).await,
        Err(RecordingError::AlreadyActive)
    ));
    assert!(connection.disconnect().await.is_err());
    assert!(connection.transport().is_closed());
}

#[tokio::test]
async fn borrowed_disconnect_stops_owned_recording_without_browser_close() {
    let (connection, page, mut peer) = fixture().await;
    let recording = start(&page, &mut peer).await;
    let closing = Arc::clone(&connection);
    let task = tokio::spawn(async move { closing.disconnect().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"start-stream"})).await;
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    task.await.unwrap().unwrap();
    assert!(connection.transport().is_closed());
    assert!(matches!(
        recording.stop().await,
        Err(RecordingError::Core(_))
    ));
}

#[tokio::test]
async fn failed_stream_close_retains_target_and_retry_closes_without_double_stop() {
    let (connection, page, mut peer) = fixture().await;
    let recording = Arc::new(start(&page, &mut peer).await);
    let closing = Arc::clone(&recording);
    let task = tokio::spawn(async move { closing.cancel().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"start-stream"})).await;
    let request = command(&mut peer, "IO.close").await;
    reject(&mut peer, &request, -32000, "stream cleanup failed").await;
    assert!(matches!(
        task.await.unwrap(),
        Err(RecordingError::CleanupUncertain(_))
    ));
    assert!(matches!(
        page.start_recording(Default::default()).await,
        Err(RecordingError::AlreadyActive)
    ));
    let closing = Arc::clone(&recording);
    let task = tokio::spawn(async move { closing.close().await });
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    task.await.unwrap().unwrap();
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn partial_stream_cleanup_retries_only_the_unacknowledged_handle() {
    for failed_stream in ["start-stream", "stop-stream"] {
        let (connection, page, mut peer) = fixture().await;
        let recording = Arc::new(start(&page, &mut peer).await);
        let closing = Arc::clone(&recording);
        let task = tokio::spawn(async move { closing.cancel().await });
        let request = command(&mut peer, "Page.stopScreenRecording").await;
        reply(&mut peer, &request, json!({"stream":"stop-stream"})).await;
        for stream in ["start-stream", "stop-stream"] {
            let request = command(&mut peer, "IO.close").await;
            assert_eq!(request["params"]["handle"], stream);
            if stream == failed_stream {
                reject(&mut peer, &request, -32000, "stream cleanup failed").await;
            } else {
                reply(&mut peer, &request, json!({})).await;
            }
        }
        assert!(matches!(
            task.await.unwrap(),
            Err(RecordingError::CleanupUncertain(_))
        ));
        assert!(matches!(
            page.start_recording(Default::default()).await,
            Err(RecordingError::AlreadyActive)
        ));
        let closing = Arc::clone(&recording);
        let task = tokio::spawn(async move { closing.close().await });
        let request = command(&mut peer, "IO.close").await;
        assert_eq!(request["params"]["handle"], failed_stream);
        reply(&mut peer, &request, json!({})).await;
        task.await.unwrap().unwrap();
        assert!(recording.state.finished.load(Ordering::Acquire));
        recording.close().await.unwrap();
        connection.disconnect().await.unwrap();
    }
}

#[tokio::test]
async fn read_error_closes_storage_and_never_reissues_stop() {
    let (connection, page, mut peer) = fixture().await;
    let recording = Arc::new(start(&page, &mut peer).await);
    let stopping = Arc::clone(&recording);
    let task = tokio::spawn(async move { stopping.stop().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"start-stream"})).await;
    task.await.unwrap().unwrap();
    let reading = Arc::clone(&recording);
    let task = tokio::spawn(async move { reading.read_chunk().await });
    let request = command(&mut peer, "IO.read").await;
    reject(&mut peer, &request, -32000, "backing storage read failed").await;
    assert!(matches!(task.await.unwrap(), Err(RecordingError::Core(_))));
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    recording.close().await.unwrap();
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn target_loss_health_signal_does_not_wait_for_native_command_completion() {
    let (connection, page, mut peer) = fixture().await;
    let recording = Arc::new(start(&page, &mut peer).await);
    let stopping = Arc::clone(&recording);
    let task = tokio::spawn(async move { stopping.stop().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    peer.send(Message::Text(
        json!({
            "method":"Inspector.targetCrashed", "sessionId":"recording-session", "params":{}
        })
        .to_string()
        .into(),
    ))
    .await
    .unwrap();
    assert!(matches!(
        tokio::time::timeout(Duration::from_millis(500), recording.wait_for_failure())
            .await
            .unwrap(),
        RecordingError::Precondition(_)
    ));
    reply(&mut peer, &request, json!({"stream":"start-stream"})).await;
    assert!(matches!(
        task.await.unwrap(),
        Err(RecordingError::Precondition(_))
    ));
    let closing = Arc::clone(&recording);
    let task = tokio::spawn(async move { closing.close().await });
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    task.await.unwrap().unwrap();
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn explicit_target_close_preserves_cleanup_error_but_still_closes_only_the_target() {
    let (connection, page, mut peer) = fixture().await;
    let recording = start(&page, &mut peer).await;
    let closing = Arc::clone(&connection);
    let task = tokio::spawn(async move { closing.close_target("recorded-target").await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reject(&mut peer, &request, -32000, "native finalization failed").await;
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    let request = command(&mut peer, "Target.closeTarget").await;
    assert_eq!(request["params"]["targetId"], "recorded-target");
    reply(&mut peer, &request, json!({"success":true})).await;
    let error = task.await.unwrap().unwrap_err();
    assert!(error.to_string().contains("native finalization failed"));
    assert!(!connection.transport().is_closed());
    drop(recording);
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn cancelled_enqueued_read_cleans_up_instead_of_silently_losing_final_bytes() {
    let (connection, page, mut peer) = fixture().await;
    let recording = Arc::new(start(&page, &mut peer).await);
    let stopping = Arc::clone(&recording);
    let task = tokio::spawn(async move { stopping.stop().await });
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"start-stream"})).await;
    task.await.unwrap().unwrap();

    let mut read = Box::pin(recording.read_chunk());
    assert!(
        std::future::poll_fn(|cx| {
            std::task::Poll::Ready(Future::poll(read.as_mut(), cx).is_pending())
        })
        .await
    );
    let request = command(&mut peer, "IO.read").await;
    reply(
        &mut peer,
        &request,
        json!({"data":"final bytes","eof":true}),
    )
    .await;
    // Wait until the worker has enqueued its result, without polling the caller's receiver.
    tokio::time::timeout(Duration::from_secs(2), async {
        loop {
            if recording.state.lifecycle.lock().await.phase == Phase::Eof {
                break;
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    assert!(matches!(
        recording.read_chunk().await,
        Err(RecordingError::InvalidState(message)) if message.contains("awaiting delivery")
    ));
    assert!(!recording.state.cleanup_requested.load(Ordering::Acquire));
    drop(read);
    assert!(recording.state.cleanup_requested.load(Ordering::Acquire));
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    recording.close().await.unwrap();
    assert!(matches!(
        recording.read_chunk().await,
        Err(RecordingError::InvalidState(_))
    ));
    connection.disconnect().await.unwrap();
}

#[tokio::test]
async fn another_connection_shares_admission_but_cannot_cancel_the_recording_on_disconnect() {
    let mut fixtures = shared_fixtures(2).await;
    let (other, other_page, mut other_peer) = fixtures.pop().unwrap();
    let (owner, owner_page, mut owner_peer) = fixtures.pop().unwrap();
    let recording = Arc::new(start(&owner_page, &mut owner_peer).await);
    assert!(matches!(
        other_page.start_recording(Default::default()).await,
        Err(RecordingError::AlreadyActive)
    ));

    let session = other_page.session.clone();
    let detach = tokio::spawn(async move { session.detach().await });
    let request = command(&mut other_peer, "Target.detachFromTarget").await;
    assert_eq!(request["params"]["sessionId"], "other-session");
    reply(&mut other_peer, &request, json!({})).await;
    detach.await.unwrap().unwrap();
    other.disconnect().await.unwrap();
    assert!(!owner.transport().is_closed());
    assert!(!recording.state.cleanup_requested.load(Ordering::Acquire));

    let closing = Arc::clone(&recording);
    let task = tokio::spawn(async move { closing.cancel().await });
    let request = command(&mut owner_peer, "Page.stopScreenRecording").await;
    assert_eq!(request["sessionId"], "recording-session");
    reply(&mut owner_peer, &request, json!({"stream":"start-stream"})).await;
    let request = command(&mut owner_peer, "IO.close").await;
    reply(&mut owner_peer, &request, json!({})).await;
    task.await.unwrap().unwrap();
    owner.disconnect().await.unwrap();
}

#[tokio::test]
async fn cancelled_disconnect_waiter_still_admits_cleanup_and_closes_transport() {
    let (connection, page, mut peer) = fixture().await;
    let recording = start(&page, &mut peer).await;
    let mut disconnect = Box::pin(connection.disconnect());
    // Poll only once, then cancel before the owned teardown/JoinSet tasks can run.
    assert!(
        std::future::poll_fn(|cx| {
            std::task::Poll::Ready(Future::poll(disconnect.as_mut(), cx).is_pending())
        })
        .await
    );
    drop(disconnect);
    let request = command(&mut peer, "Page.stopScreenRecording").await;
    reply(&mut peer, &request, json!({"stream":"start-stream"})).await;
    let request = command(&mut peer, "IO.close").await;
    reply(&mut peer, &request, json!({})).await;
    tokio::time::timeout(Duration::from_secs(2), async {
        while !connection.transport().is_closed() {
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    assert!(matches!(
        recording.stop().await,
        Err(RecordingError::Core(_))
    ));
}