eoka 0.4.0

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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! Chrome binary patcher
//!
//! Patches Chrome/Chromium binary to disable automation detection.
//! Uses Aho-Corasick for O(n) multi-pattern matching.

use aho_corasick::{AhoCorasick, Match};
use memmap2::MmapMut;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use crate::error::{Error, Result};

/// Patch pattern with replacement strategy
#[derive(Clone)]
struct PatchPattern {
    pattern: &'static [u8],
    strategy: PatchStrategy,
}

#[derive(Clone, Copy, PartialEq)]
enum PatchStrategy {
    RandomizePrefix,
    Scramble,
    Nullify,
    Skip,
}

/// All patterns to find and patch
static PATCH_PATTERNS: &[PatchPattern] = &[
    PatchPattern {
        pattern: b"$cdc_",
        strategy: PatchStrategy::RandomizePrefix,
    },
    PatchPattern {
        pattern: b"cdc_",
        strategy: PatchStrategy::RandomizePrefix,
    },
    PatchPattern {
        pattern: b"webdriver",
        strategy: PatchStrategy::Scramble,
    },
    PatchPattern {
        pattern: b"--enable-automation",
        strategy: PatchStrategy::Nullify,
    },
    PatchPattern {
        pattern: b"devtoolsw",
        strategy: PatchStrategy::Scramble,
    },
    PatchPattern {
        pattern: b"debuggerPrivate",
        strategy: PatchStrategy::Scramble,
    },
    PatchPattern {
        pattern: b"HeadlessChrome",
        strategy: PatchStrategy::Scramble,
    },
    PatchPattern {
        pattern: b"$wdc_",
        strategy: PatchStrategy::RandomizePrefix,
    },
    PatchPattern {
        pattern: b"$chromeDriver",
        strategy: PatchStrategy::RandomizePrefix,
    },
    // Detection only
    PatchPattern {
        pattern: b"Runtime.enable",
        strategy: PatchStrategy::Skip,
    },
    PatchPattern {
        pattern: b"Page.addScriptToEvaluateOnNewDocument",
        strategy: PatchStrategy::Skip,
    },
];

/// Compiled pattern matcher (lazy initialized)
static PATTERN_MATCHER: OnceLock<AhoCorasick> = OnceLock::new();

fn get_pattern_matcher() -> Result<&'static AhoCorasick> {
    // OnceLock doesn't support fallible init, so we handle it via a separate error check.
    // The patterns are static &[u8] literals — this will only fail on a bug in aho-corasick.
    if let Some(ac) = PATTERN_MATCHER.get() {
        return Ok(ac);
    }
    let patterns: Vec<&[u8]> = PATCH_PATTERNS.iter().map(|p| p.pattern).collect();
    let ac = AhoCorasick::new(&patterns).map_err(|e| {
        Error::patching(
            "init",
            format!("Failed to build Aho-Corasick automaton: {}", e),
        )
    })?;
    Ok(PATTERN_MATCHER.get_or_init(|| ac))
}

/// Matcher for patterns that patching actually rewrites (excludes `Skip`).
static VERIFY_MATCHER: OnceLock<AhoCorasick> = OnceLock::new();

fn get_verify_matcher() -> Result<&'static AhoCorasick> {
    if let Some(ac) = VERIFY_MATCHER.get() {
        return Ok(ac);
    }
    let patterns: Vec<&[u8]> = PATCH_PATTERNS
        .iter()
        .filter(|p| p.strategy != PatchStrategy::Skip)
        .map(|p| p.pattern)
        .collect();
    let ac = AhoCorasick::new(&patterns).map_err(|e| {
        Error::patching(
            "init",
            format!("Failed to build verification automaton: {}", e),
        )
    })?;
    Ok(VERIFY_MATCHER.get_or_init(|| ac))
}

/// Stable temp-subdir name for the patched copy, keyed on the original binary's
/// identity (path + size + mtime) so the cache hits across runs.
fn stable_cache_name(original_path: &Path) -> String {
    use std::hash::{Hash, Hasher};

    let mut hasher = std::collections::hash_map::DefaultHasher::new();

    let abs = fs::canonicalize(original_path).unwrap_or_else(|_| original_path.to_path_buf());
    abs.hash(&mut hasher);

    if let Ok(meta) = fs::metadata(original_path) {
        meta.len().hash(&mut hasher);
        if let Ok(modified) = meta.modified() {
            if let Ok(dur) = modified.duration_since(std::time::UNIX_EPOCH) {
                dur.as_nanos().hash(&mut hasher);
            }
        }
    }

    format!("eoka-chrome-{:016x}", hasher.finish())
}

/// Create a directory (and parents) with restrictive permissions (0o700 on unix).
fn create_dir_secure(dir: &Path) -> Result<()> {
    fs::create_dir_all(dir)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(dir)?.permissions();
        perms.set_mode(0o700);
        fs::set_permissions(dir, perms)?;
    }
    Ok(())
}

/// Find Chrome binary on the system
pub fn find_chrome() -> Result<PathBuf> {
    let candidates = if cfg!(target_os = "macos") {
        vec![
            "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
            "/Applications/Chromium.app/Contents/MacOS/Chromium",
            "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
        ]
    } else if cfg!(target_os = "linux") {
        vec![
            "/usr/bin/google-chrome",
            "/usr/bin/google-chrome-stable",
            "/usr/bin/google-chrome-beta",
            "/usr/bin/google-chrome-unstable",
            "/usr/bin/chromium",
            "/usr/bin/chromium-browser",
            "/snap/bin/chromium",
        ]
    } else if cfg!(target_os = "windows") {
        vec![
            r"C:\Program Files\Google\Chrome\Application\chrome.exe",
            r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
        ]
    } else {
        vec![]
    };

    for candidate in candidates {
        let path = Path::new(candidate);
        if path.exists() {
            #[cfg(target_os = "linux")]
            {
                let resolved = resolve_to_elf(path);
                return Ok(resolved);
            }
            #[cfg(not(target_os = "linux"))]
            return Ok(path.to_path_buf());
        }
    }

    Err(Error::ChromeNotFound)
}

/// On Linux, `/usr/bin/chromium-browser` (and similar) are often shell script
/// wrappers, not actual ELF binaries. The patcher needs the real binary so it
/// can rewrite detection markers. This function checks the magic bytes and, if
/// the candidate is not an ELF, looks for the real binary in the standard lib
/// directories where distros place it.
#[cfg(target_os = "linux")]
fn resolve_to_elf(path: &Path) -> PathBuf {
    if is_elf(path) {
        return path.to_path_buf();
    }

    // Follow symlinks first — google-chrome might be a symlink to the real thing
    let resolved = fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    if is_elf(&resolved) {
        tracing::info!("Resolved symlink {:?} to ELF binary {:?}", path, resolved);
        return resolved;
    }

    // Google Chrome ships `google-chrome[-beta|-unstable]` as a shell wrapper
    // that execs `$HERE/chrome` — the real ELF sits next to the wrapper. This
    // covers every channel (stable in /opt/google/chrome, beta in
    // /opt/google/chrome-beta, …) without hardcoding channel paths.
    if let Some(sibling) = resolved.parent().map(|d| d.join("chrome")) {
        if is_elf(&sibling) {
            tracing::info!("Resolved wrapper {:?} to sibling ELF {:?}", path, sibling);
            return sibling;
        }
    }

    // Derive the binary name from the candidate (e.g. "chromium-browser")
    let name = path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("chromium-browser");

    // Standard lib directories where distros keep the actual ELF
    let lib_candidates = [
        format!("/usr/lib/{0}/{0}", name),
        format!("/usr/lib64/{0}/{0}", name),
        "/usr/lib/chromium-browser/chromium-browser".to_string(),
        "/usr/lib/chromium/chromium".to_string(),
        "/opt/google/chrome/chrome".to_string(),
        "/opt/google/chrome/google-chrome".to_string(),
    ];

    for lib_path in &lib_candidates {
        let p = Path::new(lib_path);
        if p.exists() && is_elf(p) {
            tracing::info!("Resolved wrapper script {:?} to ELF binary {:?}", path, p);
            return p.to_path_buf();
        }
    }

    // Fallback: return as-is and let the patcher fail with a clear error
    tracing::warn!(
        "Chrome candidate {:?} is not an ELF binary and no lib binary was found",
        path,
    );
    path.to_path_buf()
}

/// Check if a file starts with the ELF magic bytes (0x7f ELF).
#[cfg(target_os = "linux")]
fn is_elf(path: &Path) -> bool {
    File::open(path)
        .and_then(|mut f| {
            let mut magic = [0u8; 4];
            f.read_exact(&mut magic)?;
            Ok(magic == [0x7f, b'E', b'L', b'F'])
        })
        .unwrap_or(false)
}

/// Chrome binary patcher
pub struct ChromePatcher {
    original_path: PathBuf,
    patched_path: PathBuf,
    #[cfg(target_os = "macos")]
    original_bundle: Option<PathBuf>,
    #[cfg(target_os = "macos")]
    patched_bundle: Option<PathBuf>,
}

impl ChromePatcher {
    /// Create a new patcher for the given Chrome binary
    pub fn new(chrome_path: &Path) -> Result<Self> {
        if !chrome_path.exists() {
            return Err(Error::patching(
                "new",
                format!("Chrome binary not found: {:?}", chrome_path),
            ));
        }

        #[cfg(target_os = "macos")]
        {
            // On macOS, handle the full .app bundle
            let mut bundle_path: Option<PathBuf> = None;
            let mut current = chrome_path.to_path_buf();
            while let Some(parent) = current.parent() {
                if current.extension().map(|e| e == "app").unwrap_or(false) {
                    bundle_path = Some(current.clone());
                    break;
                }
                current = parent.to_path_buf();
            }

            if let Some(bundle) = bundle_path {
                let bundle_name = bundle
                    .file_name()
                    .ok_or_else(|| Error::patching("new", "Invalid bundle path"))?;

                let patched_bundle = std::env::temp_dir()
                    .join(stable_cache_name(chrome_path))
                    .join(bundle_name);

                let relative_path = chrome_path
                    .strip_prefix(&bundle)
                    .map_err(|_| Error::patching("new", "Binary not inside bundle"))?;
                let patched_path = patched_bundle.join(relative_path);

                return Ok(Self {
                    original_path: chrome_path.to_path_buf(),
                    patched_path,
                    original_bundle: Some(bundle),
                    patched_bundle: Some(patched_bundle),
                });
            }
        }

        // Non-macOS or non-bundled binary
        let filename = chrome_path
            .file_name()
            .ok_or_else(|| Error::patching("new", "Invalid path"))?;

        let patched_path = std::env::temp_dir()
            .join(stable_cache_name(chrome_path))
            .join(filename);

        Ok(Self {
            original_path: chrome_path.to_path_buf(),
            patched_path,
            #[cfg(target_os = "macos")]
            original_bundle: None,
            #[cfg(target_os = "macos")]
            patched_bundle: None,
        })
    }

    /// Check if a patched binary already exists and is valid
    pub fn is_patched(&self) -> bool {
        if !self.patched_path.exists() {
            return false;
        }

        let orig_modified = fs::metadata(&self.original_path)
            .and_then(|m| m.modified())
            .ok();
        let patched_modified = fs::metadata(&self.patched_path)
            .and_then(|m| m.modified())
            .ok();

        match (orig_modified, patched_modified) {
            (Some(orig), Some(patched)) if patched > orig => self.verify_patched_sample(),
            _ => false,
        }
    }

    /// Quick verification by checking first 64KB for unpatched patterns
    fn verify_patched_sample(&self) -> bool {
        const SAMPLE_SIZE: usize = 64 * 1024;

        let mut file = match File::open(&self.patched_path) {
            Ok(f) => f,
            Err(_) => return false,
        };

        let mut buffer = vec![0u8; SAMPLE_SIZE];
        let bytes_read = match file.read(&mut buffer) {
            Ok(n) => n,
            Err(_) => return false,
        };
        buffer.truncate(bytes_read);

        match get_verify_matcher() {
            Ok(ac) => !ac.is_match(&buffer),
            Err(_) => false,
        }
    }

    /// Copy the app bundle (macOS) using symlinks for speed
    #[cfg(target_os = "macos")]
    fn copy_bundle(&self) -> Result<()> {
        let (orig_bundle, dest_bundle) = match (&self.original_bundle, &self.patched_bundle) {
            (Some(o), Some(d)) => (o, d),
            _ => return Ok(()),
        };

        if dest_bundle.exists() {
            fs::remove_dir_all(dest_bundle)?;
        }

        if let Some(parent) = dest_bundle.parent() {
            create_dir_secure(parent)?;
        }

        tracing::info!(
            "Creating Chrome bundle with symlinks at {:?}...",
            dest_bundle
        );
        self.copy_bundle_with_symlinks(orig_bundle, dest_bundle)?;

        Ok(())
    }

    #[cfg(target_os = "macos")]
    fn copy_bundle_with_symlinks(&self, src: &Path, dst: &Path) -> Result<()> {
        use std::os::unix::fs::symlink;

        fs::create_dir_all(dst)?;

        for entry in fs::read_dir(src)? {
            let entry = entry?;
            let src_path = entry.path();
            let file_name = entry.file_name();
            let dst_path = dst.join(&file_name);

            let file_type = entry.file_type()?;

            if file_type.is_dir() {
                if self.original_path.starts_with(&src_path) {
                    self.copy_bundle_with_symlinks(&src_path, &dst_path)?;
                } else {
                    symlink(&src_path, &dst_path)?;
                }
            } else if file_type.is_file() {
                if src_path == self.original_path {
                    fs::copy(&src_path, &dst_path)?;
                    tracing::debug!("Copied binary for patching: {:?}", file_name);
                } else {
                    symlink(&src_path, &dst_path)?;
                }
            } else if file_type.is_symlink() {
                let target = fs::read_link(&src_path)?;
                symlink(&target, &dst_path)?;
            }
        }

        Ok(())
    }

    /// Symlink all sibling files from the original binary's directory into the
    /// patched directory. On Linux, Chromium uses `$ORIGIN` RPATH so it resolves
    /// shared libraries (libffmpeg.so, etc.) and data files (icudtl.dat, v8
    /// snapshots, locale paks) relative to the binary. Without these symlinks
    /// the patched copy in /tmp cannot find its resources and crashes.
    #[cfg(not(target_os = "macos"))]
    fn symlink_companion_files(&self) {
        #[cfg(unix)]
        {
            use std::os::unix::fs::symlink;

            let orig_dir = match self.original_path.parent() {
                Some(d) => d,
                None => return,
            };
            let patch_dir = match self.patched_path.parent() {
                Some(d) => d,
                None => return,
            };

            // Nothing to do if source and destination are the same directory
            if orig_dir == patch_dir {
                return;
            }

            let orig_filename = self.original_path.file_name();

            let entries = match fs::read_dir(orig_dir) {
                Ok(e) => e,
                Err(err) => {
                    tracing::warn!("Could not read Chrome directory {:?}: {}", orig_dir, err);
                    return;
                }
            };

            for entry in entries.flatten() {
                // Skip the binary itself — we're patching that
                if Some(entry.file_name().as_os_str()) == orig_filename {
                    continue;
                }

                let dest = patch_dir.join(entry.file_name());
                if !dest.exists() {
                    if let Err(err) = symlink(entry.path(), &dest) {
                        tracing::trace!(
                            "Could not symlink {:?} -> {:?}: {}",
                            entry.path(),
                            dest,
                            err
                        );
                    }
                }
            }

            tracing::debug!(
                "Symlinked companion files from {:?} into {:?}",
                orig_dir,
                patch_dir
            );
        }
    }

    /// Get path to patched binary (patches if needed)
    pub fn get_patched_path(&self) -> Result<PathBuf> {
        if !self.is_patched() {
            self.patch()?;
        }
        Ok(self.patched_path.clone())
    }

    /// Perform the patching
    pub fn patch(&self) -> Result<()> {
        tracing::info!("Patching Chrome binary: {:?}", self.original_path);

        #[cfg(target_os = "macos")]
        {
            self.copy_bundle()?;
        }

        #[cfg(not(target_os = "macos"))]
        {
            if let Some(parent) = self.patched_path.parent() {
                create_dir_secure(parent)?;
            }
            // Symlink companion files (.so, .pak, .dat, .bin, locales, etc.)
            // so the patched binary can find its resources. Chromium uses
            // $ORIGIN RPATH, meaning it resolves libraries relative to the
            // binary — without these symlinks the patched copy crashes.
            self.symlink_companion_files();
        }

        #[cfg(target_os = "macos")]
        let read_path = if self.patched_bundle.is_some() {
            &self.patched_path
        } else {
            &self.original_path
        };
        #[cfg(not(target_os = "macos"))]
        let read_path = &self.original_path;

        let file_size = fs::metadata(read_path)?.len() as usize;

        let patch_count = if file_size > 10 * 1024 * 1024 {
            tracing::debug!(
                "Using memory-mapped patching for {}MB file",
                file_size / 1024 / 1024
            );
            self.patch_with_mmap(read_path)?
        } else {
            tracing::debug!("Using in-memory patching for {}KB file", file_size / 1024);
            self.patch_in_memory(read_path)?
        };

        // Make executable on Unix
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&self.patched_path)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&self.patched_path, perms)?;
        }

        // Re-sign on macOS
        #[cfg(target_os = "macos")]
        {
            self.codesign()?;
        }

        tracing::info!(
            "Patched {} occurrences, saved to {:?}",
            patch_count,
            self.patched_path
        );

        Ok(())
    }

    fn patch_with_mmap(&self, read_path: &Path) -> Result<usize> {
        #[cfg(target_os = "macos")]
        let should_patch_in_place = self.patched_bundle.is_some();
        #[cfg(not(target_os = "macos"))]
        let should_patch_in_place = false;

        let file = if should_patch_in_place {
            OpenOptions::new()
                .read(true)
                .write(true)
                .open(&self.patched_path)?
        } else {
            let _ = fs::remove_file(&self.patched_path);
            let mut dst = OpenOptions::new()
                .read(true)
                .write(true)
                .create_new(true)
                .open(&self.patched_path)?;
            let mut src = File::open(read_path)?;
            std::io::copy(&mut src, &mut dst)?;
            dst
        };

        // SAFETY: `file` is a regular file we created exclusively and hold open
        // read-write; no other process maps or writes it concurrently.
        let mut mmap = unsafe { MmapMut::map_mut(&file)? };
        let patch_count = self.apply_patches(&mut mmap)?;

        mmap.flush()?;
        Ok(patch_count)
    }

    fn patch_in_memory(&self, read_path: &Path) -> Result<usize> {
        let mut file = File::open(read_path)?;
        let mut data = Vec::new();
        file.read_to_end(&mut data)?;

        let original_len = data.len();
        let patch_count = self.apply_patches(&mut data)?;

        if data.len() != original_len {
            return Err(Error::patching(
                "patch_in_memory",
                format!(
                    "Binary size changed during patching: {} -> {}",
                    original_len,
                    data.len()
                ),
            ));
        }

        #[cfg(not(target_os = "macos"))]
        if let Some(parent) = self.patched_path.parent() {
            create_dir_secure(parent)?;
        }

        let _ = fs::remove_file(&self.patched_path);
        let mut out_file = OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&self.patched_path)?;
        out_file.write_all(&data)?;

        Ok(patch_count)
    }

    fn apply_patches(&self, data: &mut [u8]) -> Result<usize> {
        let matches: Vec<Match> = get_pattern_matcher()?.find_iter(&*data).collect();
        let mut patch_count = 0;
        let mut patched_ranges: Vec<(usize, usize)> = Vec::new();

        for m in matches {
            let pattern_idx = m.pattern().as_usize();
            let pattern = &PATCH_PATTERNS[pattern_idx];
            let start = m.start();
            let end = m.end();

            if patched_ranges
                .iter()
                .any(|(ps, pe)| start < *pe && end > *ps)
            {
                continue;
            }

            match pattern.strategy {
                PatchStrategy::RandomizePrefix => {
                    let replacement = random_string(pattern.pattern.len());
                    data[start..end].copy_from_slice(replacement.as_bytes());
                    patch_count += 1;
                    patched_ranges.push((start, end));
                }
                PatchStrategy::Scramble => {
                    // Guard against underflow on `len - 1`.
                    if pattern.pattern.len() >= 2 {
                        for i in (0..pattern.pattern.len() - 1).step_by(2) {
                            data.swap(start + i, start + i + 1);
                        }
                        patch_count += 1;
                        patched_ranges.push((start, end));
                    }
                }
                PatchStrategy::Nullify => {
                    for byte in &mut data[start..end] {
                        *byte = b' ';
                    }
                    patch_count += 1;
                    patched_ranges.push((start, end));
                }
                PatchStrategy::Skip => {
                    tracing::trace!(
                        "Found (not patching): {:?} at offset {}",
                        String::from_utf8_lossy(pattern.pattern),
                        start
                    );
                }
            }
        }

        if patch_count > 0 {
            tracing::debug!("Applied {} patches using Aho-Corasick", patch_count);
        }

        Ok(patch_count)
    }

    #[cfg(target_os = "macos")]
    fn codesign(&self) -> Result<()> {
        use std::process::Command;

        let sign_path = self.patched_bundle.as_ref().unwrap_or(&self.patched_path);

        tracing::info!("Re-signing {:?} with ad-hoc signature...", sign_path);

        let sign_str = sign_path
            .to_str()
            .ok_or_else(|| Error::patching("codesign", "Invalid UTF-8 in sign path"))?;

        let _ = Command::new("codesign")
            .args(["--remove-signature", sign_str])
            .output();

        let output = Command::new("codesign")
            .args(["-s", "-", "-f", "--deep", "--no-strict", sign_str])
            .output()?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            tracing::warn!("codesign warning: {}", stderr);
        } else {
            tracing::info!("Successfully re-signed patched binary");
        }

        Ok(())
    }
}

fn random_string(len: usize) -> String {
    (0..len)
        .map(|_| {
            let idx = fastrand::u8(0..36);
            if idx < 10 {
                (b'0' + idx) as char
            } else {
                (b'a' + idx - 10) as char
            }
        })
        .collect()
}

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

    #[test]
    fn test_find_chrome() {
        if let Ok(path) = find_chrome() {
            println!("Found Chrome at: {:?}", path);
            assert!(path.exists());
        }
    }

    /// Verify that find_chrome returns an ELF binary, not a shell wrapper.
    #[cfg(target_os = "linux")]
    #[test]
    fn test_find_chrome_returns_elf() {
        if let Ok(path) = find_chrome() {
            assert!(
                is_elf(&path),
                "find_chrome() returned {:?} which is not an ELF binary",
                path,
            );
        }
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_is_elf() {
        // /usr/bin/ls should be an ELF binary on any Linux
        let ls = Path::new("/usr/bin/ls");
        if ls.exists() {
            assert!(is_elf(ls), "/usr/bin/ls should be ELF");
        }

        // A non-existent path is not ELF
        assert!(!is_elf(Path::new("/nonexistent")));
    }

    #[test]
    fn test_random_string() {
        let s = random_string(10);
        assert_eq!(s.len(), 10);
        assert!(s.chars().all(|c| c.is_alphanumeric()));
    }

    #[test]
    fn test_aho_corasick_patterns() {
        let test_data = b"$cdc_test webdriver HeadlessChrome";
        let ac = get_pattern_matcher().expect("pattern matcher should build");
        let matches: Vec<_> = ac.find_iter(test_data).collect();
        assert!(matches.len() >= 3);
    }

    #[test]
    fn test_verify_matcher_matches_rewritten_patterns() {
        let ac = get_verify_matcher().expect("verify matcher should build");
        // Patterns that patching actually rewrites must be flagged by the
        // verification matcher (an unpatched sample should fail verification).
        assert!(ac.is_match(b"webdriver"), "should match webdriver");
        assert!(ac.is_match(b"$cdc_"), "should match $cdc_");
    }

    #[test]
    fn test_verify_matcher_excludes_skip_patterns() {
        let ac = get_verify_matcher().expect("verify matcher should build");
        // Skip-strategy patterns are detection-only and never rewritten, so
        // they must NOT be part of verification — otherwise every real Chrome
        // binary would look "unpatched" forever.
        assert!(
            !ac.is_match(b"Runtime.enable"),
            "Skip pattern Runtime.enable must be excluded from verification"
        );
        assert!(
            !ac.is_match(b"Page.addScriptToEvaluateOnNewDocument"),
            "Skip pattern must be excluded from verification"
        );
    }

    #[test]
    fn test_apply_patches_preserves_length() {
        // Build a patcher against a real (throwaway) file so ChromePatcher::new
        // succeeds, then patch an in-memory buffer directly.
        let dir = std::env::temp_dir().join(format!("eoka-patcher-test-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        let fake = dir.join("fake-chrome");
        fs::write(&fake, b"placeholder").unwrap();

        let patcher = ChromePatcher::new(&fake).expect("patcher should construct");

        let mut buf = b"prefix webdriver suffix".to_vec();
        let original_len = buf.len();
        let count = patcher.apply_patches(&mut buf).expect("apply_patches");

        // Scramble strategy swaps bytes in place: length is unchanged.
        assert_eq!(buf.len(), original_len);
        assert!(count >= 1, "webdriver should have been patched");
        // The literal "webdriver" must no longer be present verbatim.
        assert!(
            !buf.windows(b"webdriver".len()).any(|w| w == b"webdriver"),
            "webdriver should have been scrambled"
        );

        let _ = fs::remove_file(&fake);
        let _ = fs::remove_dir_all(&dir);
    }
}