diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed3a7af2..67485f9a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,3 +44,15 @@ jobs: run: dotnet build dotnet/CloakBrowser.sln -c Release - name: Run tests run: dotnet test dotnet/CloakBrowser.sln -c Release --no-build + + rust: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + - name: Install Rust toolchain + run: | + rustup toolchain install stable --profile minimal + rustup default stable + - name: Test + run: cargo test -p cloakbrowser --lib + working-directory: rust diff --git a/.gitignore b/.gitignore index 86985413..528806c2 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,10 @@ CLAUDE.md js/node_modules/ js/dist/ +# Rust +rust/target/ +**/Cargo.lock.bak + # Distribution *.tar.gz *.whl diff --git a/README.md b/README.md index d8d6d774..da4b0b88 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,20 @@ dotnet add package CloakBrowser > Community-maintained .NET client built on Microsoft.Playwright. See [`dotnet/README.md`](dotnet/README.md) for the full API. +**Rust:** + +```toml +# Cargo.toml +cloakbrowser = { path = "rust/cloakbrowser" } # monorepo path +tokio = { version = "1", features = ["rt-multi-thread", "macros"] } +``` + +```bash +cd rust && cargo run -p cloakbrowser-cli -- install +``` + +> Community-maintained Rust client built on [`playwright-rs`](https://crates.io/crates/playwright-rs). See [`rust/README.md`](rust/README.md) for the full API. + --- On first run, the stealth Chromium binary is automatically downloaded (~200MB, cached locally). @@ -1389,6 +1403,7 @@ Issues and PRs welcome. If something isn't working, [open an issue](https://gith ## Contributors - [@evelaa123](https://github.com/evelaa123) — humanize behavior, persistent contexts, Windows fix, .NET client +- [@enrell](https://github.com/enrell) — Rust client - [@yahooguntu](https://github.com/yahooguntu) — persistent contexts - [@kitiho](https://github.com/kitiho) — null viewport fix - [@eofreternal](https://github.com/eofreternal) — humanConfig type fix, humanized method option types, iframe pointer-events fix diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 00000000..7c274838 --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1,3 @@ +/target/ +**/*.rs.bk +.cargo-ok diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 00000000..ba292e97 --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,2841 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] +name = "anyhow" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3" + +[[package]] +name = "arbitrary" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" +dependencies = [ + "derive_arbitrary", +] + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + +[[package]] +name = "bitflags" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "bzip2" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49ecfb22d906f800d4fe833b6282cf4dc1c298f5057ca0b5445e5c209735ca47" +dependencies = [ + "bzip2-sys", +] + +[[package]] +name = "bzip2-sys" +version = "0.1.13+1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225bff33b2141874fe80d71e07d6eec4f85c5c216453dd96388240f96e1acc14" +dependencies = [ + "cc", + "pkg-config", +] + +[[package]] +name = "cc" +version = "1.2.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17dd265a7d0f31ef544e1b20e03add05d3b45b491b633b10d67145d2acc1a38" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "chacha20" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d524456ba66e72eb8b115ff89e01e497f8e6d11d78b70b1aa13c0fbd97540a81" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.0", + "rand_core 0.10.1", +] + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "num-traits", + "windows-link", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "cloakbrowser" +version = "0.4.10" +dependencies = [ + "anyhow", + "base64", + "chrono", + "dirs", + "ed25519-dalek", + "flate2", + "hex", + "maxminddb", + "playwright-rs", + "rand 0.8.7", + "regex", + "reqwest", + "serde", + "serde_json", + "sha2", + "tar", + "tempfile", + "thiserror", + "tokio", + "tracing", + "url", + "urlencoding", + "zip 2.4.2", +] + +[[package]] +name = "cloakbrowser-cli" +version = "0.4.10" +dependencies = [ + "anyhow", + "clap", + "cloakbrowser", + "serde_json", + "tokio", +] + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "constant_time_eq" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6" + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "cpufeatures" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" +dependencies = [ + "libc", +] + +[[package]] +name = "crc" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853" + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "curve25519-dalek-derive", + "digest", + "fiat-crypto", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "data-encoding" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4ae5f15dda3c708c0ade84bfee31ccab44a3da4f88015ed22f63732abe300c8" + +[[package]] +name = "deflate64" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac6b926516df9c60bfa16e107b21086399f8285a44ca9711344b9e553c5146e2" + +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid", + "zeroize", +] + +[[package]] +name = "deranged" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" + +[[package]] +name = "derive_arbitrary" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e567bd82dcff979e4b03460c307b3cdc9e96fde3d73bed1496d2bc75d9dd62a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.61.2", +] + +[[package]] +name = "displaydoc" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" +dependencies = [ + "curve25519-dalek", + "ed25519", + "serde", + "sha2", + "subtle", + "zeroize", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "fastrand" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" + +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "filetime" +version = "0.2.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759" +dependencies = [ + "cfg-if", + "libc", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "flate2" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" +dependencies = [ + "crc32fast", + "miniz_oxide", + "zlib-rs", +] + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi 5.3.0", + "wasip2", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi 6.0.0", + "rand_core 0.10.1", + "wasm-bindgen", +] + +[[package]] +name = "glob" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "http" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6970f50e31d6fc17d3fa27329444bfa74e196cf62e95052a3f6fee181dba6425" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hyper" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55281c53a1894c864990125767da440a4e630446785086f52523b20033b74498" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ca68d021ef39cf6463ab54c1d0f5daf03377b70561305bb89a8f83aab66e0f" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2984d1cd16c883d7935b9e07e44071dca8d917fd52ecc02c04d5fa0b5a3f191c" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c56e5ee99d6e3d33bd91c5d85458b6005a22140021cc324cea84dd0e72cff3b4" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3be0ae77ea334f4da67c12f149704f19f81d1adf7c51cf482943e84a2bad38" + +[[package]] +name = "icu_properties" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bee3b67d0ea5c2cca5003417989af8996f8604e34fb9ddf96208a033901e70de" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14" + +[[package]] +name = "icu_provider" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + +[[package]] +name = "ipnet" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2" + +[[package]] +name = "ipnetwork" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf370abdafd54d13e54a620e8c3e1145f28e46cc9d704bc6d94414559df41763" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom 0.4.3", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "libredox" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c943259e342f1e06ff2da7a83eabdfe7f92ce10262688dbf1895ff0b3e6e4652" +dependencies = [ + "libc", +] + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad" + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "lzma-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "297e814c836ae64db86b36cf2a557ba54368d03f6afcd7d947c266692f71115e" +dependencies = [ + "byteorder", + "crc", +] + +[[package]] +name = "lzma-sys" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fda04ab3764e6cde78b9974eec4f779acaba7c4e84b36eca3cf77c581b85d27" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + +[[package]] +name = "maxminddb" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "144de2546bf4846c6c84b7f76be035f7ebbc1e7d40cfb05810ba45c129508321" +dependencies = [ + "ipnetwork", + "log", + "memchr", + "serde", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02bd0af71c67b473010cbbc60715ee815645a4dc942899111f494b4b737d6fda" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "native-tls" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "465500e14ea162429d264d44189adc38b199b62b1c21eea9f69e4b73cb03bbf2" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "openssl" +version = "0.10.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77823a27f0babb03091cb9ed9ef80af3b39dbc82f97e8fa530374b7dafd87a45" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "openssl-probe" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" + +[[package]] +name = "openssl-sys" +version = "0.9.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b47e7e6bb2c38cd930d25a23b40fa52e068c10e85f3e03a7f5ba5aaca5713695" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "pbkdf2" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +dependencies = [ + "digest", + "hmac", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "pkg-config" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" + +[[package]] +name = "playwright-rs" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3317d37f816d9fbd550ba155a146068575a0c97e5af969f5d0895199344fa0b5" +dependencies = [ + "async-trait", + "base64", + "bytes", + "dirs", + "flate2", + "futures-util", + "glob", + "libc", + "parking_lot", + "playwright-rs-macros", + "regex", + "serde", + "serde_json", + "tar", + "thiserror", + "tokio", + "tokio-tungstenite", + "tracing", + "ureq", + "url", + "zip 8.6.0", +] + +[[package]] +name = "playwright-rs-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dacee5a7346e793f1a4ca72db4b1c0e698c1e21626cdef409e6ec295629c4cd" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "potential_utf" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564" +dependencies = [ + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quinn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c1a41e437b6bbd489372cd4971de128e85c855f56c57f283d20ff016cf7c0a8" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4bfc015262b9df63c8845072ce59068853ff5872180c2ce2f13038b970e560" +dependencies = [ + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand 0.10.2", + "rand_pcg", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35a133f956daabe89a61a685c2649f13d82d5aa4bd5d12d1277e1072a21c0694" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.61.2", +] + +[[package]] +name = "quote" +version = "1.0.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22f6172bdec972074665ed81ed53b71da00bfc44b65a753cfde883ec4c702a1a" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + +[[package]] +name = "rand" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core 0.10.1", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_users" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac" +dependencies = [ + "getrandom 0.2.17", + "libredox", + "thiserror", +] + +[[package]] +name = "regex" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0e75113e14dc5acb068cd0786884f214f1312650a3d36d269f5c4f3cdee8a2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f388202e4b80542a0921078cc23b6333bcf1409c1e3f86404cae4766a6131db" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tokio-util", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", + "webpki-roots", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b92b125634d9b795e7beca796cc790df15a7fb38323bf3196fda83292d06b1f" +dependencies = [ + "log", + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "764899a24af3980067ee14bc143654f297b22eaebfe3c7b6b211920a5a59b046" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "schannel" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "rand_core 0.6.4", +] + +[[package]] +name = "simd-adler32" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" + +[[package]] +name = "socket2" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52d1cfed4120b4d927bf7c0f86d2087a4a7d6027c906d9f9d525a80573b9be51" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tar" +version = "0.4.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840" +dependencies = [ + "filetime", + "libc", + "xattr", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "time" +version = "0.3.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18dfaaeddcb932337b5e7866ee7d0ce9b76d2fd092997146f187ec09b4558a50" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "tinystr" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb4ebadaa0af04fab11ae01eb5f9fdb5f9c5b875506e210e71c07873528baa7f" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c" +dependencies = [ + "futures-util", + "log", + "native-tls", + "tokio", + "tokio-native-tls", + "tungstenite", +] + +[[package]] +name = "tokio-util" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", + "url", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "tungstenite" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c01152af293afb9c7c2a57e4b559c5620b421f6d133261c60dd2d0cdb38e6b8" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "native-tls", + "rand 0.9.5", + "sha1", + "thiserror", +] + +[[package]] +name = "typed-path" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e" + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "ureq" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dea7109cdcd5864d4eeb1b58a1648dc9bf520360d7af16ec26d0a9354bafcfc0" +dependencies = [ + "base64", + "flate2", + "log", + "percent-encoding", + "rustls", + "rustls-pki-types", + "ureq-proto", + "utf8-zero", + "webpki-roots", +] + +[[package]] +name = "ureq-proto" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e994ba84b0bd1b1b0cf92878b7ef898a5c1760108fe7b6010327e274917a808c" +dependencies = [ + "base64", + "http", + "httparse", + "log", +] + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + +[[package]] +name = "utf8-zero" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62df1340f32221cb9c54d6a27b030e3dba64361d4a95bed55f9aacb44da291d" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "web-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf85cb06032201fa7c6f829d7db5a7e5aa45bcc0655327713065f6f0576731bf" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "writeable" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4" + +[[package]] +name = "xattr" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156" +dependencies = [ + "libc", + "rustix", +] + +[[package]] +name = "xz2" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388c44dc09d76f1536602ead6d325eb532f5c122f17782bd57fb47baeeb767e2" +dependencies = [ + "lzma-sys", +] + +[[package]] +name = "yoke" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7cbbc0a705a0fd05cc3676525980d2bf5a9bc4adac6d6475209a7887cf59d19" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e817b7b52d0c7358d3246da9d69935ebb18116b2b102b4230dac079b4862f5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerotrie" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f9152d31db0792fa83f70fb2f83148effb5c1f5b8c7686c3459e361d9bc20bf" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zip" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fabe6324e908f85a1c52063ce7aa26b68dcb7eb6dbc83a2d148403c9bc3eba50" +dependencies = [ + "aes", + "arbitrary", + "bzip2", + "constant_time_eq", + "crc32fast", + "crossbeam-utils", + "deflate64", + "displaydoc", + "flate2", + "getrandom 0.3.4", + "hmac", + "indexmap", + "lzma-rs", + "memchr", + "pbkdf2", + "sha1", + "thiserror", + "time", + "xz2", + "zeroize", + "zopfli", + "zstd", +] + +[[package]] +name = "zip" +version = "8.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d04a6b5381502aa6087c94c669499eb1602eb9c5e8198e534de571f7154809b" +dependencies = [ + "crc32fast", + "flate2", + "indexmap", + "memchr", + "typed-path", + "zopfli", +] + +[[package]] +name = "zlib-rs" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b142a20ec14a91d5bc708c1dc21b080c550113d8aa77afa29635673a65dd02c5" + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" + +[[package]] +name = "zopfli" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249" +dependencies = [ + "bumpalo", + "crc32fast", + "log", + "simd-adler32", +] + +[[package]] +name = "zstd" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a" +dependencies = [ + "zstd-safe", +] + +[[package]] +name = "zstd-safe" +version = "7.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d" +dependencies = [ + "zstd-sys", +] + +[[package]] +name = "zstd-sys" +version = "2.0.16+zstd.1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748" +dependencies = [ + "cc", + "pkg-config", +] diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 00000000..b6b51fb5 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,44 @@ +[workspace] +resolver = "2" +members = [ + "cloakbrowser", + "cloakbrowser-cli", +] +default-members = ["cloakbrowser", "cloakbrowser-cli"] + +[workspace.package] +version = "0.4.10" +edition = "2021" +license = "MIT" +repository = "https://github.com/CloakHQ/CloakBrowser" +homepage = "https://github.com/CloakHQ/CloakBrowser" +authors = ["CloakHQ"] +keywords = ["stealth", "browser", "chromium", "playwright", "scraping"] +categories = ["web-programming", "development-tools"] + +[workspace.dependencies] +cloakbrowser = { path = "cloakbrowser", version = "0.4.10" } +anyhow = "1" +base64 = "0.22" +chrono = { version = "0.4", default-features = false, features = ["clock", "std"] } +clap = { version = "4", features = ["derive"] } +dirs = "6" +ed25519-dalek = { version = "2", features = ["pkcs8"] } +flate2 = "1" +hex = "0.4" +maxminddb = "0.25" +playwright-rs = "0.14" +rand = "0.8" +regex = "1" +reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls", "stream"] } +serde = { version = "1", features = ["derive"] } +serde_json = "1" +sha2 = "0.10" +tar = "0.4" +tempfile = "3" +thiserror = "2" +tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "sync", "time"] } +tracing = "0.1" +url = "2" +urlencoding = "2" +zip = "2" diff --git a/rust/README.md b/rust/README.md new file mode 100644 index 00000000..3b5c07d2 --- /dev/null +++ b/rust/README.md @@ -0,0 +1,310 @@ +# CloakBrowser for Rust + +A **Rust** port of the [CloakBrowser](https://github.com/CloakHQ/CloakBrowser) wrapper — +stealth Chromium that passes bot-detection tests, built on top of +[`playwright-rs`](https://crates.io/crates/playwright-rs). + +CloakBrowser is a thin wrapper around a closed-source, source-level patched +Chromium binary. This port reproduces the wrapper functionality with identical +behavior to the Python, JavaScript, and .NET clients — same launch flags, same +proxy / GeoIP / WebRTC logic, same download verification (Ed25519 + SHA-256). + +> **Status:** Community-style client (like `dotnet/`). Launch, download/Pro +> routing, proxy/GeoIP/WebRTC, persistent contexts, CLI, and explicit +> **HumanPage** humanize are implemented. Transparent Playwright monkey-patching +> is not available in Rust (same sealed-API constraint as .NET). + +--- + +## Table of contents + +- [Feature matrix](#feature-matrix) +- [Requirements](#requirements) +- [Installation](#installation) +- [Project layout](#project-layout) +- [Quick start](#quick-start) +- [Launch API](#launch-api) +- [CLI](#cli) +- [Environment variables](#environment-variables) +- [Building & testing](#building--testing) +- [Mapping to the Python source](#mapping-to-the-python-source) +- [License](#license) + +--- + +## Feature matrix + +| Capability | Status | Source | +| --- | :---: | --- | +| Automatic binary **download / cache / auto-update** (SHA-256 + Ed25519) | ✅ | `download.rs`, `config.rs` | +| **Stealth launch args** (random fingerprint seed, platform spoofing) | ✅ | `launch.rs`, `config.rs` | +| **Proxy** — HTTP/HTTPS + SOCKS5, inline URL-encoded credentials | ✅ | `proxy.rs` | +| **GeoIP** timezone/locale from proxy exit IP (MaxMind GeoLite2) | ✅ | `geoip.rs` (`geoip` feature) | +| **WebRTC** IP spoofing (`--fingerprint-webrtc-ip=auto`) | ✅ | `launch.rs` | +| **Pro license** routing + env injection | ✅ | `license.rs` | +| **Persistent contexts** | ✅ | `launch_persistent_context` + Widevine hint | +| **Humanize** — Bezier mouse, human typing, scrolling | ✅ | `human/` (`HumanPage`) | +| **CLI** (`install` / `info` / `update` / `clear-cache`) | ✅ | `cloakbrowser-cli` | + +--- + +## Requirements + +| Dependency | Version | Why | +| --- | --- | --- | +| Rust | **1.75+** (edition 2021) | toolchain | +| `playwright-rs` | **0.14** | browser automation | +| `tokio` | **1.x** | async runtime | + +On first Playwright use, the driver/browsers may need a one-time install +(`playwright-rs` handles its own driver). CloakBrowser separately downloads its +patched Chromium into `~/.cloakbrowser/`. + +--- + +## Installation + +### As a library (path / git) + +```toml +[dependencies] +cloakbrowser = { path = "rust/cloakbrowser" } # monorepo +# or +# cloakbrowser = { git = "https://github.com/CloakHQ/CloakBrowser", path = "rust/cloakbrowser" } +tokio = { version = "1", features = ["rt-multi-thread", "macros"] } +``` + +Disable GeoIP if you do not need it: + +```toml +cloakbrowser = { path = "rust/cloakbrowser", default-features = false } +``` + +### CLI + +```bash +cd rust +cargo install --path cloakbrowser-cli +cloakbrowser install +cloakbrowser info --quick +``` + +The patched Chromium binary downloads automatically on first launch, cached under +`~/.cloakbrowser` (override with `CLOAKBROWSER_CACHE_DIR`). + +--- + +## Project layout + +``` +rust/ +├── Cargo.toml # workspace +├── README.md +├── cloakbrowser/ # the library +│ ├── Cargo.toml +│ └── src/ +│ ├── lib.rs +│ ├── config.rs # <- cloakbrowser/config.py +│ ├── download.rs # <- cloakbrowser/download.py +│ ├── license.rs # <- cloakbrowser/license.py +│ ├── geoip.rs # <- cloakbrowser/geoip.py +│ ├── proxy.rs # <- browser.py proxy helpers +│ ├── launch.rs # <- browser.py launch / build_args +│ ├── human/ # <- cloakbrowser/human/* +│ │ ├── config.rs / mouse.rs / keyboard.rs / scroll.rs / page.rs +│ ├── widevine.rs # <- cloakbrowser/widevine.py +│ ├── options.rs # launch + persistent context options +│ ├── diagnostics.rs # CLI info/doctor +│ ├── log.rs / error.rs / version.rs +├── cloakbrowser-cli/ # <- cloakbrowser/__main__.py +│ └── src/main.rs +└── examples/ + ├── basic.rs + ├── humanize.rs + └── persistent_context.rs +``` + +--- + +## Quick start + +```rust +use cloakbrowser::{launch, LaunchOptions}; + +#[tokio::main] +async fn main() -> cloakbrowser::Result<()> { + let browser = launch(LaunchOptions { + headless: true, + ..Default::default() + }) + .await?; + + let page = browser.new_page().await?; + let _ = page.goto("https://bot.incolumitas.com/", None).await; + println!("{}", page.title().await.unwrap_or_default()); + browser.close().await?; + Ok(()) +} +``` + +With proxy + GeoIP: + +```rust +let browser = launch(LaunchOptions { + headless: false, + proxy: Some("http://user:pass@residential-proxy:port".into()), + geoip: true, + ..Default::default() +}) +.await?; +``` + +### Humanize + +Rust cannot monkey-patch Playwright's `Page` (same constraint as .NET). Use +[`HumanPage`] for humanized interactions: + +```rust +use cloakbrowser::{launch, LaunchOptions, HumanPreset}; + +let browser = launch(LaunchOptions { + humanize: true, + human_preset: HumanPreset::Default, // or Careful + ..Default::default() +}) +.await?; + +let mut page = browser.new_human_page().await?; +page.goto("https://example.com").await?; +page.fill("#email", "user@example.com").await?; +page.click("#submit").await?; +// raw Playwright page still available: +let _ = page.page().title().await; +``` + +| Method | Behavior | +| --- | --- | +| `click` / `hover` | Bezier mouse path, aim delay, hold | +| `type_text` / `fill` | Per-key timing, mistypes, CDP shift symbols | +| `scroll_to` | Accel → cruise → decel wheel bursts | +| `move_to` | Explicit Bezier move | + +--- + +## Launch API + +`launch(LaunchOptions)` returns a [`CloakBrowser`] that owns the Playwright +driver and browser process. Call `close().await` when done. + +| Field | Default | Notes | +| --- | --- | --- | +| `headless` | `true` | | +| `proxy` | `None` | URL string or `ProxySettings` | +| `args` | `None` | Extra Chromium flags | +| `stealth_args` | `true` | Fingerprint seed + platform | +| `timezone` / `locale` | `None` | Or filled by `geoip` | +| `geoip` | `false` | Requires `geoip` feature | +| `humanize` | `false` | Enables config for `new_human_page()` | +| `human_preset` | `Default` | `Default` or `Careful` | +| `license_key` | `None` | Also `CLOAKBROWSER_LICENSE_KEY` | +| `browser_version` | `None` | Also `CLOAKBROWSER_VERSION` | + +### Persistent context + +```rust +use cloakbrowser::{launch_persistent_context, LaunchContextOptions, ViewportOption}; + +let ctx = launch_persistent_context( + "./my-profile", + LaunchContextOptions { + headless: false, + humanize: true, + viewport: ViewportOption::Auto, // no_viewport when headed + user_agent: None, + ..Default::default() + }, +) +.await?; + +let page = ctx.new_page().await?; +// ... cookies/localStorage persist in ./my-profile +ctx.close().await?; +``` + +| Field | Notes | +| --- | --- | +| `viewport` | `Auto` / `Size { w, h }` / `NoViewport` | +| `user_agent` | Optional custom UA | +| `color_scheme` | `light` / `dark` / `no-preference` | +| `storage_state_path` | Playwright storage-state JSON path | + +Timezone/locale still go through **binary fingerprint flags** (not CDP). Widevine +CDM hint seeding runs on Linux when a sideloaded CDM is present. + +--- + +## CLI + +```bash +cloakbrowser install # download free (or Pro) binary +cloakbrowser info --quick # diagnostics without launch +cloakbrowser info --json +cloakbrowser update +cloakbrowser clear-cache +``` + +--- + +## Environment variables + +| Variable | Purpose | +| --- | --- | +| `CLOAKBROWSER_CACHE_DIR` | Binary cache root (default `~/.cloakbrowser`) | +| `CLOAKBROWSER_BINARY_PATH` | Skip download; use a local Chromium | +| `CLOAKBROWSER_LICENSE_KEY` | Pro license | +| `CLOAKBROWSER_VERSION` | Pin a Chromium version | +| `CLOAKBROWSER_DOWNLOAD_URL` | Custom download mirror | +| `CLOAKBROWSER_SKIP_CHECKSUM` | Only for custom mirrors | +| `CLOAKBROWSER_AUTO_UPDATE` | Set `false` to freeze auto-update | +| `CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS` | GeoIP resolution timeout | + +--- + +## Building & testing + +```bash +cd rust +cargo test -p cloakbrowser +cargo build -p cloakbrowser-cli +cargo run -p cloakbrowser-cli -- info --quick +``` + +Unit tests cover config, proxy resolution, checksum parsing, zip-slip guards, +and launch-arg composition without network or a browser. + +--- + +## Mapping to the Python source + +| Rust | Python | +| --- | --- | +| `config.rs` | `cloakbrowser/config.py` | +| `download.rs` | `cloakbrowser/download.py` | +| `license.rs` | `cloakbrowser/license.py` | +| `geoip.rs` | `cloakbrowser/geoip.py` | +| `proxy.rs` | `_resolve_proxy_config` et al. in `browser.py` | +| `launch.rs` | `launch` / `build_args` in `browser.py` | +| `human/*` | `cloakbrowser/human/*` | +| `widevine.rs` | `cloakbrowser/widevine.py` | +| `launch_persistent_context` | `launch_persistent_context` in `browser.py` | +| `cloakbrowser-cli` | `cloakbrowser/__main__.py` | + +Parity targets match the .NET client in `dotnet/` (same version pins, same +signing keys, same stealth flags). + +--- + +## License + +MIT for this wrapper (see repo root `LICENSE`). The patched Chromium binary is +subject to `BINARY-LICENSE.md`. diff --git a/rust/cloakbrowser-cli/Cargo.toml b/rust/cloakbrowser-cli/Cargo.toml new file mode 100644 index 00000000..c12a63fe --- /dev/null +++ b/rust/cloakbrowser-cli/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "cloakbrowser-cli" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +authors.workspace = true +description = "CLI for cloakbrowser — download and manage the stealth Chromium binary" +publish = false + +[[bin]] +name = "cloakbrowser" +path = "src/main.rs" + +[dependencies] +anyhow.workspace = true +clap.workspace = true +cloakbrowser = { workspace = true, features = ["geoip"] } +serde_json.workspace = true +tokio.workspace = true diff --git a/rust/cloakbrowser-cli/src/main.rs b/rust/cloakbrowser-cli/src/main.rs new file mode 100644 index 00000000..62f30161 --- /dev/null +++ b/rust/cloakbrowser-cli/src/main.rs @@ -0,0 +1,218 @@ +//! CLI for cloakbrowser — download and manage the stealth Chromium binary. +//! Direct port of Python `cloakbrowser/__main__.py` / .NET `CloakBrowser.Cli`. +//! +//! Usage: +//! cloakbrowser install # Download binary (with progress) +//! cloakbrowser info # Environment + binary diagnostics (--quick, --json) +//! cloakbrowser doctor # Alias for info +//! cloakbrowser update # Check for and download newer binary +//! cloakbrowser clear-cache # Remove cached binaries + +use clap::{Parser, Subcommand}; +use cloakbrowser::log::{self, Level}; + +const UPGRADE_HINT: &str = + "→ Try the latest Pro binary (Chromium 148) free for 7 days: https://cloakbrowser.dev"; + +#[derive(Parser)] +#[command( + name = "cloakbrowser", + about = "CloakBrowser — stealth Chromium binary manager", + version = cloakbrowser::VERSION +)] +struct Cli { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand)] +enum Commands { + /// Download the stealth Chromium binary + Install, + /// Environment + binary diagnostics + Info { + /// Skip launch probe + #[arg(long)] + quick: bool, + /// Alias for --quick + #[arg(long = "no-launch")] + no_launch: bool, + /// Emit JSON + #[arg(long)] + json: bool, + }, + /// Alias for info + Doctor { + #[arg(long)] + quick: bool, + #[arg(long = "no-launch")] + no_launch: bool, + #[arg(long)] + json: bool, + }, + /// Check for and download a newer binary + Update, + /// Remove cached binaries + #[command(name = "clear-cache")] + ClearCache, +} + +#[tokio::main] +async fn main() { + log::set_min_level(Level::Info); + + let cli = Cli::parse(); + let code = match run(cli).await { + Ok(()) => 0, + Err(e) => { + eprintln!("Error: {e}"); + 1 + } + }; + std::process::exit(code); +} + +async fn run(cli: Cli) -> anyhow::Result<()> { + match cli.command { + None => { + print_help(); + anyhow::bail!("no command"); + } + Some(Commands::Install) => { + let path = cloakbrowser::ensure_binary(None, None).await?; + println!("{}", path.display()); + eprintln!("{UPGRADE_HINT}"); + } + Some(Commands::Info { + quick, + no_launch, + json, + }) + | Some(Commands::Doctor { + quick, + no_launch, + json, + }) => { + cmd_info(quick || no_launch, json)?; + } + Some(Commands::Update) => { + match cloakbrowser::check_for_update().await? { + Some(v) => { + println!("Updated to Chromium {v}"); + let info = cloakbrowser::binary_info(None)?; + println!("{}", info.binary_path); + } + None => { + let info = cloakbrowser::binary_info(None)?; + println!("Already up to date (Chromium {})", info.version); + } + } + } + Some(Commands::ClearCache) => { + cloakbrowser::clear_cache()?; + println!("Cache cleared."); + } + } + Ok(()) +} + +fn cmd_info(quick: bool, as_json: bool) -> anyhow::Result<()> { + let diag = cloakbrowser::diagnostics::collect(quick); + if as_json { + println!("{}", serde_json::to_string_pretty(&diag)?); + return Ok(()); + } + + let env = diag.get("environment").and_then(|v| v.as_object()); + println!("CloakBrowser diagnostics"); + if let Some(env) = env { + println!( + "Rust: {}", + env.get("rust").and_then(|v| v.as_str()).unwrap_or("?") + ); + println!( + "OS: {} {}", + env.get("os").and_then(|v| v.as_str()).unwrap_or("?"), + env.get("arch").and_then(|v| v.as_str()).unwrap_or("?") + ); + println!( + "Platform: {}", + env.get("platform_tag") + .and_then(|v| v.as_str()) + .unwrap_or("unknown") + ); + } + + if let Some(binary) = diag.get("binary").and_then(|v| v.as_object()) { + if let Some(err) = binary.get("error") { + println!("Binary: unavailable ({err})"); + } else { + let tier = binary.get("tier").and_then(|v| v.as_str()).unwrap_or("?"); + if tier == "override" { + println!("Version: set via CLOAKBROWSER_BINARY_PATH (see Launch line)"); + } else if let Some(ver) = binary.get("version").and_then(|v| v.as_str()) { + println!("Version: {ver} ({tier})"); + } + if let Some(path) = binary.get("path").and_then(|v| v.as_str()) { + println!("Binary: {path}"); + } + if let Some(installed) = binary.get("installed") { + println!("Installed: {installed}"); + } + if let Some(cd) = binary.get("cache_dir").and_then(|v| v.as_str()) { + if !cd.is_empty() { + println!("Cache: {cd}"); + } + } + if let Some(ov) = binary.get("override").and_then(|v| v.as_str()) { + println!("Override: {ov} (CLOAKBROWSER_BINARY_PATH)"); + } + } + } + + if let Some(launch) = diag.get("launch").and_then(|v| v.as_object()) { + if launch.get("tested").and_then(|v| v.as_bool()) != Some(true) { + println!( + "Launch: {}", + launch + .get("reason") + .and_then(|v| v.as_str()) + .unwrap_or("skipped") + ); + } + } + + if let Some(lic) = diag.get("license").and_then(|v| v.as_object()) { + let present = lic.get("present").and_then(|v| v.as_bool()).unwrap_or(false); + println!( + "License: {}", + if present { + "present" + } else { + "none (free tier)" + } + ); + } + + eprintln!("\n{UPGRADE_HINT}"); + Ok(()) +} + +fn print_help() { + eprintln!( + "\ +CloakBrowser — stealth Chromium binary manager + +Usage: + cloakbrowser install Download binary + cloakbrowser info Environment + binary diagnostics + cloakbrowser doctor Alias for info + cloakbrowser update Check for and download newer binary + cloakbrowser clear-cache Remove cached binaries + +Flags for info/doctor: + --quick / --no-launch Skip launch probe + --json Emit JSON +" + ); +} diff --git a/rust/cloakbrowser/Cargo.toml b/rust/cloakbrowser/Cargo.toml new file mode 100644 index 00000000..13fd7b80 --- /dev/null +++ b/rust/cloakbrowser/Cargo.toml @@ -0,0 +1,57 @@ +[package] +name = "cloakbrowser" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +authors.workspace = true +keywords.workspace = true +categories.workspace = true +description = "Stealth Chromium that passes every bot detection test. Drop-in Playwright (Rust) replacement with source-level fingerprint patches." +readme = "../README.md" + +[features] +default = ["geoip"] +# MaxMind GeoLite2 timezone/locale resolution from proxy exit IP. +geoip = ["dep:maxminddb"] + +[dependencies] +anyhow.workspace = true +base64.workspace = true +chrono.workspace = true +dirs.workspace = true +ed25519-dalek.workspace = true +flate2.workspace = true +hex.workspace = true +maxminddb = { workspace = true, optional = true } +playwright-rs.workspace = true +rand.workspace = true +regex.workspace = true +reqwest.workspace = true +serde.workspace = true +serde_json.workspace = true +sha2.workspace = true +tar.workspace = true +thiserror.workspace = true +tokio.workspace = true +tracing.workspace = true +url.workspace = true +urlencoding.workspace = true +zip.workspace = true + +[dev-dependencies] +tempfile.workspace = true +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } + +[[example]] +name = "basic" +path = "../examples/basic.rs" + +[[example]] +name = "humanize" +path = "../examples/humanize.rs" + +[[example]] +name = "persistent_context" +path = "../examples/persistent_context.rs" diff --git a/rust/cloakbrowser/src/config.rs b/rust/cloakbrowser/src/config.rs new file mode 100644 index 00000000..db757895 --- /dev/null +++ b/rust/cloakbrowser/src/config.rs @@ -0,0 +1,467 @@ +//! Stealth configuration and platform detection for CloakBrowser. +//! Direct port of Python `cloakbrowser/config.py` / .NET `Config.cs`. + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::sync::OnceLock; + +use regex::Regex; + +use crate::error::{Error, Result}; +use crate::license; + +// --------------------------------------------------------------------------- +// Chromium version shipped with this release. +// Different platforms may ship different versions during transition periods. +// CHROMIUM_VERSION is the latest across all platforms (for display/reference). +// Use get_chromium_version() for the current platform's actual version. +// --------------------------------------------------------------------------- +pub const CHROMIUM_VERSION: &str = "146.0.7680.177.5"; + +/// Ed25519 public keys for verifying downloaded binaries (base64 of 32-byte raw keys). +pub const BINARY_SIGNING_PUBKEYS: &[&str] = &["MKFKwIhUcKWq5xTuNA0Ovg99njcDEcEJvmWYYhApvaU="]; + +/// Playwright default args to suppress — these leak automation signals. +pub const IGNORE_DEFAULT_ARGS: &[&str] = &["--enable-automation", "--enable-unsafe-swiftshader"]; + +/// Default viewport — HEADLESS only (headed uses no_viewport). +pub const DEFAULT_VIEWPORT_WIDTH: i32 = 1920; +pub const DEFAULT_VIEWPORT_HEIGHT: i32 = 947; + +/// First Chromium build that reports coherent headless dimensions without viewport emulation. +pub const HEADLESS_NO_VIEWPORT_MIN_VERSION: Option<&str> = Some("148.0.7778.215.4"); + +/// GitHub Releases API. +pub const GITHUB_API_URL: &str = "https://api.github.com/repos/CloakHQ/cloakbrowser/releases"; + +/// GitHub Releases download base. +pub const GITHUB_DOWNLOAD_BASE_URL: &str = + "https://github.com/CloakHQ/cloakbrowser/releases/download"; + +fn platform_chromium_versions() -> &'static HashMap<&'static str, &'static str> { + static MAP: OnceLock> = OnceLock::new(); + MAP.get_or_init(|| { + HashMap::from([ + ("linux-x64", "146.0.7680.177.5"), + ("linux-arm64", "146.0.7680.177.3"), + ("darwin-arm64", "145.0.7632.109.2"), + ("darwin-x64", "145.0.7632.109.2"), + ("windows-x64", "146.0.7680.177.5"), + ]) + }) +} + +fn http_proxy_inline_auth_min_version() -> &'static HashMap<&'static str, &'static str> { + static MAP: OnceLock> = OnceLock::new(); + MAP.get_or_init(|| { + HashMap::from([ + ("linux-x64", "146.0.7680.177.5"), + ("windows-x64", "146.0.7680.177.5"), + ("linux-arm64", "148.0.7778.215.3"), + ("darwin-arm64", "148.0.7778.215.3"), + ("darwin-x64", "148.0.7778.215.3"), + ]) + }) +} + +/// Build stealth args with a random fingerprint seed per launch. +/// +/// On macOS, skips platform/GPU spoofing — runs as a native Mac browser. +pub fn get_default_stealth_args() -> Vec { + let seed = rand::random::() % 90000 + 10000; + let mut base = vec![ + "--no-sandbox".to_string(), + format!("--fingerprint={seed}"), + ]; + + if cfg!(target_os = "macos") { + base.push("--fingerprint-platform=macos".to_string()); + } else { + // Linux/Windows: Windows fingerprint profile. + base.push("--fingerprint-platform=windows".to_string()); + } + base +} + +/// Platforms with pre-built binaries available for download. +pub fn available_platforms() -> Vec<&'static str> { + let mut keys: Vec<_> = platform_chromium_versions().keys().copied().collect(); + keys.sort_unstable(); + keys +} + +/// Return the Chromium version for the current platform. +pub fn get_chromium_version() -> String { + let tag = match get_platform_tag() { + Ok(t) => t, + Err(_) => return CHROMIUM_VERSION.to_string(), + }; + platform_chromium_versions() + .get(tag.as_str()) + .copied() + .unwrap_or(CHROMIUM_VERSION) + .to_string() +} + +fn version_pin_re() -> &'static Regex { + static RE: OnceLock = OnceLock::new(); + RE.get_or_init(|| Regex::new(r"^[0-9]+(?:\.[0-9]+){3,4}$").unwrap()) +} + +/// Return an explicit Chromium version pin from arg/env, or None. +/// +/// The explicit argument wins over `CLOAKBROWSER_VERSION`. Only numeric dotted +/// versions are accepted because the value is interpolated into cache paths and +/// download URLs. +pub fn normalize_requested_version(version: Option<&str>) -> Result> { + let raw = match version { + Some(v) => Some(v.to_string()), + None => std::env::var("CLOAKBROWSER_VERSION").ok(), + }; + let Some(raw) = raw else { + return Ok(None); + }; + let normalized = raw.trim(); + if normalized.is_empty() { + return Ok(None); + } + if !version_pin_re().is_match(normalized) { + return Err(Error::InvalidVersion( + "Invalid browser version pin. Use a full numeric Chromium version, \ + e.g. '148.0.7778.215.2'." + .into(), + )); + } + Ok(Some(normalized.to_string())) +} + +/// Return the platform tag for binary download (e.g. `linux-x64`, `darwin-arm64`). +pub fn get_platform_tag() -> Result { + let arch = std::env::consts::ARCH; + let os = std::env::consts::OS; + + let tag = match (os, arch) { + ("linux", "x86_64") => "linux-x64", + ("linux", "aarch64") => "linux-arm64", + ("macos", "aarch64") => "darwin-arm64", + ("macos", "x86_64") => "darwin-x64", + ("windows", "x86_64") => "windows-x64", + _ => { + return Err(Error::UnsupportedPlatform(format!( + "{os} {arch}. Supported: linux-x64, linux-arm64, darwin-arm64, darwin-x64, windows-x64" + ))); + } + }; + Ok(tag.to_string()) +} + +/// Return the cache directory for downloaded binaries. +/// Override with `CLOAKBROWSER_CACHE_DIR`. Default: `~/.cloakbrowser/`. +pub fn get_cache_dir() -> PathBuf { + if let Ok(custom) = std::env::var("CLOAKBROWSER_CACHE_DIR") { + if !custom.is_empty() { + return PathBuf::from(custom); + } + } + dirs::home_dir() + .unwrap_or_else(|| PathBuf::from(".")) + .join(".cloakbrowser") +} + +/// Return the directory for a Chromium version binary. +pub fn get_binary_dir(version: Option<&str>, pro: bool) -> PathBuf { + let v = version + .map(|s| s.to_string()) + .unwrap_or_else(get_chromium_version); + let suffix = if pro { "-pro" } else { "" }; + get_cache_dir().join(format!("chromium-{v}{suffix}")) +} + +/// Return the expected path to the chrome executable. +pub fn get_binary_path(version: Option<&str>, pro: bool) -> PathBuf { + let binary_dir = get_binary_dir(version, pro); + if cfg!(target_os = "macos") { + binary_dir + .join("Chromium.app") + .join("Contents") + .join("MacOS") + .join("Chromium") + } else if cfg!(target_os = "windows") { + binary_dir.join("chrome.exe") + } else { + binary_dir.join("chrome") + } +} + +/// Raise a clear error if no pre-built binary exists for this platform. +/// Skipped when `CLOAKBROWSER_BINARY_PATH` is set. +pub fn check_platform_available() -> Result<()> { + if get_local_binary_override().is_some() { + return Ok(()); + } + let tag = get_platform_tag()?; + if !platform_chromium_versions().contains_key(tag.as_str()) { + let available = available_platforms().join(", "); + return Err(Error::msg(format!( + "\nCloakBrowser — Pre-built binaries are currently only available for: {available}.\n\n\ + To use CloakBrowser now, set CLOAKBROWSER_BINARY_PATH to a local Chromium binary." + ))); + } + Ok(()) +} + +/// True when a binary exists and is executable. +pub fn is_executable_file(path: &Path) -> bool { + if !path.is_file() { + return false; + } + if cfg!(target_os = "windows") { + return true; + } + use std::os::unix::fs::PermissionsExt; + path.metadata() + .map(|m| m.permissions().mode() & 0o111 != 0) + .unwrap_or(false) +} + +/// Return the best available version: auto-updated if available, else platform default. +/// +/// When `pro=true`, returns `None` when no cached Pro binary matches — a valid +/// Pro license must NEVER fall back to the free binary. +pub fn get_effective_version(pro: bool) -> Option { + let base = get_chromium_version(); + let cache = get_cache_dir(); + let tag = get_platform_tag().ok()?; + + if pro { + let marker = cache.join(format!("latest_pro_version_{tag}")); + if marker.exists() { + if let Ok(version) = std::fs::read_to_string(&marker) { + let version = version.trim().to_string(); + if !version.is_empty() { + let binary = get_binary_path(Some(&version), true); + if is_executable_file(&binary) { + return Some(version); + } + } + } + } + return None; + } + + for name in [format!("latest_version_{tag}"), "latest_version".into()] { + let marker = cache.join(&name); + if marker.exists() { + if let Ok(version) = std::fs::read_to_string(&marker) { + let version = version.trim().to_string(); + if !version.is_empty() && version_newer(&version, &base) { + let binary = get_binary_path(Some(&version), false); + if binary.exists() { + return Some(version); + } + } + } + } + } + Some(base) +} + +/// Parse `"145.0.7718.0"` into component ints for comparison. +pub fn version_tuple(v: &str) -> Result> { + v.split('.') + .map(|x| { + x.parse::() + .map_err(|_| Error::InvalidVersion(format!("bad version component in {v}"))) + }) + .collect() +} + +/// Return true if version `a` is strictly newer than version `b`. +pub fn version_newer(a: &str, b: &str) -> bool { + let Ok(ta) = version_tuple(a) else { + return false; + }; + let Ok(tb) = version_tuple(b) else { + return false; + }; + let len = ta.len().max(tb.len()); + for i in 0..len { + let va = ta.get(i).copied().unwrap_or(0); + let vb = tb.get(i).copied().unwrap_or(0); + if va != vb { + return va > vb; + } + } + false +} + +/// Download base URL (override with `CLOAKBROWSER_DOWNLOAD_URL`). +pub fn download_base_url() -> String { + std::env::var("CLOAKBROWSER_DOWNLOAD_URL").unwrap_or_else(|_| "https://cloakbrowser.dev".into()) +} + +/// Archive extension for the current platform. +pub fn get_archive_ext() -> &'static str { + if cfg!(target_os = "windows") { + ".zip" + } else { + ".tar.gz" + } +} + +/// Archive filename for a platform tag. +pub fn get_archive_name(tag: Option<&str>) -> Result { + let t = match tag { + Some(t) => t.to_string(), + None => get_platform_tag()?, + }; + Ok(format!("cloakbrowser-{t}{}", get_archive_ext())) +} + +/// Full download URL for the current platform's binary archive. +pub fn get_download_url(version: Option<&str>) -> Result { + let v = version + .map(|s| s.to_string()) + .unwrap_or_else(get_chromium_version); + Ok(format!( + "{}/chromium-v{v}/{}", + download_base_url(), + get_archive_name(None)? + )) +} + +/// GitHub Releases fallback URL for the binary archive. +pub fn get_fallback_download_url(version: Option<&str>) -> Result { + let v = version + .map(|s| s.to_string()) + .unwrap_or_else(get_chromium_version); + Ok(format!( + "{GITHUB_DOWNLOAD_BASE_URL}/chromium-v{v}/{}", + get_archive_name(None)? + )) +} + +/// Pro binary download URL for an explicit version. +pub fn get_pro_download_url(version: &str) -> String { + format!("{}/api/download/{version}", download_base_url()) +} + +/// Base URL for the Pro signed manifest of a version. +pub fn get_pro_manifest_base_url(version: &str) -> String { + format!( + "{}/releases/pro/chromium-v{version}", + download_base_url() + ) +} + +/// "Latest Pro" display download URL. +pub fn get_pro_latest_download_url() -> String { + format!("{}/api/download/latest", download_base_url()) +} + +/// Local binary override via `CLOAKBROWSER_BINARY_PATH`. +pub fn get_local_binary_override() -> Option { + std::env::var("CLOAKBROWSER_BINARY_PATH") + .ok() + .filter(|s| !s.is_empty()) +} + +/// Whether headless can launch with `no_viewport` on the resolved binary. +pub fn binary_supports_headless_no_viewport( + license_key: Option<&str>, + browser_version: Option<&str>, +) -> bool { + let Some(floor) = HEADLESS_NO_VIEWPORT_MIN_VERSION else { + return false; + }; + let declared = normalize_requested_version(browser_version).ok().flatten(); + let version = if let Some(d) = declared { + d + } else if get_local_binary_override().is_some() { + return false; + } else { + let pro = license::resolve_license_key(license_key).is_some(); + match get_effective_version(pro) { + Some(v) => v, + None => return false, + } + }; + !version_newer(floor, &version) +} + +/// Whether the resolved binary accepts inline HTTP proxy credentials. +pub fn binary_supports_http_proxy_inline_auth( + license_key: Option<&str>, + browser_version: Option<&str>, +) -> bool { + let Ok(tag) = get_platform_tag() else { + return false; + }; + let Some(&floor) = http_proxy_inline_auth_min_version().get(tag.as_str()) else { + return false; + }; + let declared = normalize_requested_version(browser_version).ok().flatten(); + let version = if let Some(d) = declared { + Some(d) + } else if get_local_binary_override().is_some() { + return false; + } else { + let pro = license::resolve_license_key(license_key).is_some(); + get_effective_version(pro) + }; + match version { + // Pro with no cached marker => resolve latest Pro from server, which + // always ships the patch. + None => true, + Some(v) => !version_newer(floor, &v), + } +} + +/// Whether the wrapper may auto-add `--start-maximized`. +pub fn binary_supports_maximized_window( + license_key: Option<&str>, + browser_version: Option<&str>, +) -> bool { + binary_supports_headless_no_viewport(license_key, browser_version) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn version_newer_basic() { + assert!(version_newer("146.0.7680.177.5", "145.0.0.0")); + assert!(!version_newer("145.0.0.0", "146.0.7680.177.5")); + assert!(!version_newer("146.0.7680.177.5", "146.0.7680.177.5")); + } + + #[test] + fn normalize_version_valid() { + assert_eq!( + normalize_requested_version(Some("148.0.7778.215.2")) + .unwrap() + .as_deref(), + Some("148.0.7778.215.2") + ); + } + + #[test] + fn normalize_version_invalid() { + assert!(normalize_requested_version(Some("latest")).is_err()); + assert!(normalize_requested_version(Some("148")).is_err()); + } + + #[test] + fn stealth_args_have_fingerprint() { + let args = get_default_stealth_args(); + assert!(args.iter().any(|a| a.starts_with("--fingerprint="))); + assert!(args.iter().any(|a| a == "--no-sandbox")); + } + + #[test] + fn archive_name_contains_platform() { + let name = get_archive_name(Some("linux-x64")).unwrap(); + assert!(name.starts_with("cloakbrowser-linux-x64")); + } +} diff --git a/rust/cloakbrowser/src/diagnostics.rs b/rust/cloakbrowser/src/diagnostics.rs new file mode 100644 index 00000000..e7ddec7d --- /dev/null +++ b/rust/cloakbrowser/src/diagnostics.rs @@ -0,0 +1,82 @@ +//! Environment + binary diagnostics for the CLI (`info` / `doctor`). +//! Simplified port of .NET `Diagnostics.cs` / Python CLI info. + +use std::collections::HashMap; + +use crate::config; +use crate::download; +use crate::license; +use crate::version::VERSION; + +/// Collect a diagnostics snapshot (no browser launch by default). +pub fn collect(quick: bool) -> HashMap { + let mut root = HashMap::new(); + + let mut env = serde_json::Map::new(); + env.insert("rust".into(), VERSION.into()); + env.insert("os".into(), std::env::consts::OS.into()); + env.insert("arch".into(), std::env::consts::ARCH.into()); + env.insert( + "platform_tag".into(), + config::get_platform_tag() + .map(|t| t.into()) + .unwrap_or(serde_json::Value::Null), + ); + root.insert("environment".into(), serde_json::Value::Object(env)); + + let mut binary = serde_json::Map::new(); + match download::binary_info(None) { + Ok(info) => { + binary.insert("version".into(), info.version.into()); + binary.insert("tier".into(), info.tier.into()); + binary.insert("path".into(), info.binary_path.into()); + binary.insert("installed".into(), info.installed.into()); + binary.insert("cache_dir".into(), info.cache_dir.into()); + binary.insert("bundled_version".into(), info.bundled_version.into()); + binary.insert("platform".into(), info.platform.into()); + binary.insert("download_url".into(), info.download_url.into()); + if let Some(ov) = config::get_local_binary_override() { + binary.insert("override".into(), ov.into()); + binary.insert("tier".into(), "override".into()); + } + if config::normalize_requested_version(None) + .ok() + .flatten() + .is_some() + { + binary.insert("pinned".into(), true.into()); + } + } + Err(e) => { + binary.insert("error".into(), e.to_string().into()); + } + } + root.insert("binary".into(), serde_json::Value::Object(binary)); + + let mut launch = serde_json::Map::new(); + if quick { + launch.insert("tested".into(), false.into()); + launch.insert("reason".into(), "skipped (--quick)".into()); + } else { + launch.insert("tested".into(), false.into()); + launch.insert( + "reason".into(), + "launch probe not run (use without --quick after install)".into(), + ); + } + root.insert("launch".into(), serde_json::Value::Object(launch)); + + let mut license_map = serde_json::Map::new(); + match license::resolve_license_key(None) { + Some(_) => { + license_map.insert("present".into(), true.into()); + license_map.insert("source".into(), "resolved".into()); + } + None => { + license_map.insert("present".into(), false.into()); + } + } + root.insert("license".into(), serde_json::Value::Object(license_map)); + + root +} diff --git a/rust/cloakbrowser/src/download.rs b/rust/cloakbrowser/src/download.rs new file mode 100644 index 00000000..b628c9da --- /dev/null +++ b/rust/cloakbrowser/src/download.rs @@ -0,0 +1,1196 @@ +//! Binary download and cache management for CloakBrowser. +//! Direct port of Python `cloakbrowser/download.py` / .NET `Download.cs`. + +use std::collections::HashMap; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use base64::Engine; +use ed25519_dalek::{Signature, Verifier, VerifyingKey}; +use flate2::read::GzDecoder; +use sha2::{Digest, Sha256}; +use tar::Archive; + +use crate::config; +use crate::error::{Error, Result}; +use crate::license; +use crate::log; +use crate::version::VERSION; + +const UPDATE_CHECK_INTERVAL: u64 = 3600; +const WELCOME_FREE_INTERVAL: i64 = 3 * 24 * 3600; +const PRO_MAJOR: &str = "148"; + +static WRAPPER_UPDATE_CHECKED: AtomicBool = AtomicBool::new(false); + +/// Info about the current binary installation. +#[derive(Debug, Clone, serde::Serialize)] +pub struct BinaryInfo { + pub version: String, + pub tier: String, + pub bundled_version: String, + pub platform: String, + pub binary_path: String, + pub installed: bool, + pub cache_dir: String, + pub download_url: String, +} + +fn now_unix() -> i64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.as_secs() as i64) + .unwrap_or(0) +} + +fn http_client() -> Result { + Ok(reqwest::Client::builder() + .timeout(std::time::Duration::from_secs(600)) + .user_agent(format!("cloakbrowser-rust/{VERSION}")) + .build()?) +} + +/// Whether the welcome banner should be shown. +pub fn welcome_due(marker: &Path, pro: bool) -> bool { + if !marker.exists() { + return true; + } + if pro { + return false; + } + match std::fs::read_to_string(marker) { + Ok(text) => match text.trim().parse::() { + Ok(last) => now_unix() - last >= WELCOME_FREE_INTERVAL, + Err(_) => true, + }, + Err(_) => true, + } +} + +fn show_welcome(pro: bool) { + let marker = config::get_cache_dir().join(".welcome_shown"); + if !welcome_due(&marker, pro) { + return; + } + + let mut msg = String::from("\n CloakBrowser - stealth Chromium for automation\n"); + msg.push_str(" https://github.com/CloakHQ/CloakBrowser\n\n"); + if pro { + msg.push_str(&format!( + " CloakBrowser Pro active (v{PRO_MAJOR}) - latest binary, newest patches.\n" + )); + msg.push_str(" Pro support -> support@cloakbrowser.dev\n"); + } else { + let chromium_ver = config::get_chromium_version(); + let free_major = chromium_ver.split('.').next().unwrap_or("146"); + msg.push_str(&format!( + " Running free tier (v{free_major}). Pro = latest binary (v{PRO_MAJOR}) + newest anti-bot patches.\n" + )); + msg.push_str(" Try Pro free for 7 days -> https://cloakbrowser.dev\n"); + } + msg.push_str(" Star us if CloakBrowser helps your project!\n\n"); + eprint!("{msg}"); + + let _ = std::fs::create_dir_all(config::get_cache_dir()); + let _ = std::fs::write(&marker, now_unix().to_string()); +} + +/// Ensure the stealth Chromium binary is available. Download if needed. +/// Returns the path to the chrome executable. +pub async fn ensure_binary( + license_key: Option<&str>, + browser_version: Option<&str>, +) -> Result { + // Local override first. + if let Some(local) = config::get_local_binary_override() { + let path = PathBuf::from(&local); + if !path.exists() { + return Err(Error::BinaryNotFound(format!( + "CLOAKBROWSER_BINARY_PATH set to '{local}' but file does not exist" + ))); + } + log::info(format!("Using local binary override: {local}")); + return Ok(path); + } + + let requested_version = config::normalize_requested_version(browser_version)?; + + // Pro license key check (custom CLOAKBROWSER_DOWNLOAD_URL overrides Pro path). + let mut key = license::resolve_license_key(license_key); + if std::env::var("CLOAKBROWSER_DOWNLOAD_URL") + .map(|s| !s.is_empty()) + .unwrap_or(false) + { + key = None; + } + + if let Some(ref k) = key { + if let Some(info) = license::validate_license(k).await { + if info.valid { + return ensure_pro_binary(k, requested_version.as_deref()) + .await + .map_err(|e| match e { + Error::BinaryVerification(_) => e, + other => Error::ProUnavailable(format!( + "{other}. Your license is valid but the Pro binary could not be \ + downloaded right now. Retry in a moment. To use the free binary \ + instead, unset CLOAKBROWSER_LICENSE_KEY." + )), + }); + } else { + log::warning(format!( + "License validation failed (plan={}), using free tier", + info.plan + )); + } + } else { + log::warning("License validation unavailable, using free tier"); + } + } + + config::check_platform_available()?; + + if let Some(ref pinned) = requested_version { + let pinned_path = config::get_binary_path(Some(pinned), false); + if config::is_executable_file(&pinned_path) { + log::debug(format!( + "Pinned binary found in cache: {} (version {pinned})", + pinned_path.display() + )); + show_welcome(false); + return Ok(pinned_path); + } + + log::info(format!( + "Stealth Chromium {pinned} not found. Downloading for {}...", + config::get_platform_tag()? + )); + download_and_extract(Some(pinned)).await?; + + if !config::is_executable_file(&pinned_path) { + return Err(Error::msg(format!( + "Pinned download completed but binary not found at expected path: {}. \ + This may indicate a packaging issue. Please report at \ + https://github.com/CloakHQ/cloakbrowser/issues", + pinned_path.display() + ))); + } + show_welcome(false); + return Ok(pinned_path); + } + + // Free tier never returns null (bundled base is the floor). + let effective = config::get_effective_version(false) + .unwrap_or_else(config::get_chromium_version); + let binary_path = config::get_binary_path(Some(&effective), false); + + if config::is_executable_file(&binary_path) { + log::debug(format!( + "Binary found in cache: {} (version {effective})", + binary_path.display() + )); + show_welcome(false); + maybe_trigger_update_check(); + return Ok(binary_path); + } + + let platform_version = config::get_chromium_version(); + if effective != platform_version { + let fallback = config::get_binary_path(None, false); + if config::is_executable_file(&fallback) { + log::debug(format!("Binary found in cache: {}", fallback.display())); + maybe_trigger_update_check(); + return Ok(fallback); + } + } + + log::info(format!( + "Stealth Chromium {platform_version} not found. Downloading for {}...", + config::get_platform_tag()? + )); + download_and_extract(None).await?; + + let binary_path = config::get_binary_path(None, false); + if !binary_path.exists() { + return Err(Error::msg(format!( + "Download completed but binary not found at expected path: {}. \ + This may indicate a packaging issue. Please report at \ + https://github.com/CloakHQ/cloakbrowser/issues", + binary_path.display() + ))); + } + + maybe_trigger_update_check(); + Ok(binary_path) +} + +async fn ensure_pro_binary( + license_key: &str, + requested_version: Option<&str>, +) -> Result { + if let Some(pinned) = requested_version { + let pinned_path = config::get_binary_path(Some(pinned), true); + if config::is_executable_file(&pinned_path) { + log::debug(format!( + "Pinned Pro binary found in cache: {} (version {pinned})", + pinned_path.display() + )); + show_welcome(true); + return Ok(pinned_path); + } + + log::info(format!( + "Downloading Pro Chromium {pinned} for {}...", + config::get_platform_tag()? + )); + download_pro_binary(pinned, license_key).await?; + + let pinned_path = config::get_binary_path(Some(pinned), true); + if !config::is_executable_file(&pinned_path) { + return Err(Error::msg(format!( + "Pro download completed but binary not found at: {}", + pinned_path.display() + ))); + } + show_welcome(true); + return Ok(pinned_path); + } + + let effective = config::get_effective_version(true); + let frozen = std::env::var("CLOAKBROWSER_AUTO_UPDATE") + .map(|v| v.eq_ignore_ascii_case("false")) + .unwrap_or(false); + + if frozen && pro_binary_ready(effective.as_deref()) { + show_welcome(true); + return Ok(config::get_binary_path(effective.as_deref(), true)); + } + + let latest = license::get_pro_latest_version().await; + + let version = if let Some(ref latest_v) = latest { + if !pro_binary_ready(effective.as_deref()) + || effective + .as_ref() + .map(|e| config::version_newer(latest_v, e)) + .unwrap_or(true) + { + Some(latest_v.clone()) + } else { + effective.clone() + } + } else { + effective.clone() + }; + + let Some(version) = version else { + return Err(Error::msg( + "Could not determine latest Pro version from server", + )); + }; + + let ready_path = config::get_binary_path(Some(&version), true); + if config::is_executable_file(&ready_path) { + if Some(&version) != effective.as_ref() { + write_pro_version_marker(&version); + } + log::debug(format!( + "Pro binary found in cache: {} (version {version})", + ready_path.display() + )); + show_welcome(true); + return Ok(ready_path); + } + + match download_pro_binary(&version, license_key).await { + Ok(()) => {} + Err(e @ Error::BinaryVerification(_)) => return Err(e), + Err(e) => { + if pro_binary_ready(effective.as_deref()) { + log::warning(format!( + "Pro update to {version} failed; launching cached Pro binary {}", + effective.as_deref().unwrap_or("?") + )); + show_welcome(true); + return Ok(config::get_binary_path(effective.as_deref(), true)); + } + return Err(e); + } + } + + let downloaded = config::get_binary_path(Some(&version), true); + if !downloaded.exists() { + return Err(Error::msg(format!( + "Pro download completed but binary not found at: {}", + downloaded.display() + ))); + } + + write_pro_version_marker(&version); + show_welcome(true); + Ok(downloaded) +} + +fn pro_binary_ready(version: Option<&str>) -> bool { + match version { + Some(v) if !v.is_empty() => { + config::is_executable_file(&config::get_binary_path(Some(v), true)) + } + _ => false, + } +} + +fn write_pro_version_marker(version: &str) { + let Ok(tag) = config::get_platform_tag() else { + return; + }; + let marker = config::get_cache_dir().join(format!("latest_pro_version_{tag}")); + let _ = std::fs::create_dir_all(config::get_cache_dir()); + let tmp = marker.with_extension("tmp"); + if std::fs::write(&tmp, version).is_ok() { + let _ = std::fs::rename(&tmp, &marker); + } +} + +fn write_version_marker(version: &str) { + let Ok(tag) = config::get_platform_tag() else { + return; + }; + let marker = config::get_cache_dir().join(format!("latest_version_{tag}")); + let _ = std::fs::create_dir_all(config::get_cache_dir()); + let tmp = marker.with_extension("tmp"); + if std::fs::write(&tmp, version).is_ok() { + let _ = std::fs::rename(&tmp, &marker); + } +} + +async fn download_and_extract(version: Option<&str>) -> Result<()> { + let primary_url = config::get_download_url(version)?; + let fallback_url = config::get_fallback_download_url(version)?; + let binary_dir = config::get_binary_dir(version, false); + let binary_path = config::get_binary_path(version, false); + + if let Some(parent) = binary_dir.parent() { + std::fs::create_dir_all(parent)?; + } + + let tmp_path = std::env::temp_dir().join(format!( + "cloakbrowser-{}{}", + uuid_like(), + config::get_archive_ext() + )); + + let result = async { + match download_file(&primary_url, &tmp_path, None).await { + Ok(()) => {} + Err(primary_err) => { + if std::env::var("CLOAKBROWSER_DOWNLOAD_URL").is_ok() { + return Err(primary_err); + } + log::warning(format!( + "Primary download failed ({primary_err}), trying GitHub Releases..." + )); + download_file(&fallback_url, &tmp_path, None).await?; + } + } + + verify_download_checksum(&tmp_path, version).await?; + extract_archive(&tmp_path, &binary_dir, Some(&binary_path))?; + show_welcome(false); + Ok(()) + } + .await; + + let _ = std::fs::remove_file(&tmp_path); + result +} + +async fn download_pro_binary(version: &str, license_key: &str) -> Result<()> { + let download_url = config::get_pro_download_url(version); + let binary_dir = config::get_binary_dir(Some(version), true); + let binary_path = config::get_binary_path(Some(version), true); + let platform_tag = config::get_platform_tag()?; + + if let Some(parent) = binary_dir.parent() { + std::fs::create_dir_all(parent)?; + } + + let tmp_path = std::env::temp_dir().join(format!( + "cloakbrowser-{}{}", + uuid_like(), + config::get_archive_ext() + )); + + let mut headers = HashMap::new(); + headers.insert("Authorization".into(), format!("Bearer {license_key}")); + headers.insert("X-Platform".into(), platform_tag); + + let result = async { + download_file(&download_url, &tmp_path, Some(&headers)).await?; + verify_pro_download(&tmp_path, version).await?; + extract_archive(&tmp_path, &binary_dir, Some(&binary_path))?; + Ok(()) + } + .await; + + let _ = std::fs::remove_file(&tmp_path); + result +} + +fn uuid_like() -> String { + format!("{:x}", rand::random::()) +} + +async fn download_file( + url: &str, + dest: &Path, + headers: Option<&HashMap>, +) -> Result<()> { + log::info(format!("Downloading from {url}")); + let client = http_client()?; + let mut req = client.get(url); + if let Some(h) = headers { + for (k, v) in h { + req = req.header(k.as_str(), v.as_str()); + } + } + let resp = req.send().await?; + if !resp.status().is_success() { + return Err(Error::msg(format!( + "Download failed: HTTP {} {}", + resp.status().as_u16(), + resp.status().canonical_reason().unwrap_or("") + ))); + } + + let total = resp.content_length().unwrap_or(0); + let bytes = resp.bytes().await?; + let mut file = std::fs::File::create(dest)?; + file.write_all(&bytes)?; + + if total > 0 { + log::info(format!( + "Download progress: 100% ({}/{} MB)", + bytes.len() as u64 / (1024 * 1024), + total / (1024 * 1024) + )); + } + + log::info(format!( + "Download complete: {} MB", + dest.metadata().map(|m| m.len() / (1024 * 1024)).unwrap_or(0) + )); + Ok(()) +} + +// --------------------------------------------------------------------------- +// Verification +// --------------------------------------------------------------------------- + +/// Verify free-tier download integrity and authenticity. +pub async fn verify_download_checksum(file_path: &Path, version: Option<&str>) -> Result<()> { + let tarball_name = config::get_archive_name(None)?; + + if std::env::var("CLOAKBROWSER_DOWNLOAD_URL").is_ok() { + // Self-hosted: plain same-origin checksum; skippable. + if std::env::var("CLOAKBROWSER_SKIP_CHECKSUM") + .map(|v| v.eq_ignore_ascii_case("true")) + .unwrap_or(false) + { + log::warning( + "CLOAKBROWSER_SKIP_CHECKSUM set - skipping verification for custom download URL", + ); + return Ok(()); + } + let Some(checksums) = fetch_checksums(version).await else { + log::warning( + "SHA256SUMS not available from custom URL - skipping checksum verification", + ); + return Ok(()); + }; + let Some(expected) = checksums.get(&tarball_name) else { + log::warning(format!( + "SHA256SUMS found but no entry for {tarball_name} - skipping verification" + )); + return Ok(()); + }; + return verify_checksum(file_path, expected); + } + + // Official path: non-bypassable Ed25519 signature. + let Some((manifest_bytes, sig_bytes)) = fetch_signed_manifest(version).await else { + return Err(Error::msg( + "Could not fetch a signed SHA256SUMS (SHA256SUMS + SHA256SUMS.sig) \ + for this release - refusing to use an unverified binary. \ + Retry, or report at https://github.com/CloakHQ/cloakbrowser/issues", + )); + }; + + verify_signature(&manifest_bytes, &sig_bytes)?; + let manifest_text = String::from_utf8_lossy(&manifest_bytes); + + let requested = version + .map(|s| s.to_string()) + .unwrap_or_else(config::get_chromium_version); + let declared = parse_manifest_version(&manifest_text); + if declared.as_deref() != Some(requested.as_str()) { + return Err(Error::msg(format!( + "Version mismatch in signed SHA256SUMS: requested {requested}, \ + manifest declares {}. Refusing (possible downgrade).", + declared.as_deref().unwrap_or("none") + ))); + } + + let checksums = parse_checksums(&manifest_text); + let Some(expected) = checksums.get(&tarball_name) else { + return Err(Error::msg(format!( + "Signature-verified SHA256SUMS has no entry for {tarball_name} - \ + cannot confirm binary integrity." + ))); + }; + verify_checksum(file_path, expected) +} + +/// Verify a Pro archive with the same non-bypassable Ed25519 signature check. +pub async fn verify_pro_download(file_path: &Path, version: &str) -> Result<()> { + let base_url = config::get_pro_manifest_base_url(version); + let client = http_client()?; + + let (manifest_bytes, sig_bytes) = match async { + let manifest = client + .get(format!("{base_url}/SHA256SUMS")) + .send() + .await? + .error_for_status()? + .bytes() + .await?; + let sig = client + .get(format!("{base_url}/SHA256SUMS.sig")) + .send() + .await? + .error_for_status()? + .bytes() + .await?; + Ok::<_, Error>((manifest.to_vec(), sig.to_vec())) + } + .await + { + Ok(pair) => pair, + Err(e) => { + return Err(Error::msg(format!( + "Could not fetch the signed SHA256SUMS for Pro {version} ({e})" + ))); + } + }; + + if let Err(e) = verify_signature(&manifest_bytes, &sig_bytes) { + return Err(Error::BinaryVerification(e.to_string())); + } + + let manifest_text = String::from_utf8_lossy(&manifest_bytes); + let declared = parse_manifest_version(&manifest_text); + if declared.as_deref() != Some(version) { + return Err(Error::BinaryVerification(format!( + "Version mismatch in signed Pro SHA256SUMS: requested {version}, \ + manifest declares {}. Refusing (possible downgrade).", + declared.as_deref().unwrap_or("none") + ))); + } + + let tarball_name = config::get_archive_name(None)?; + let checksums = parse_checksums(&manifest_text); + let Some(expected) = checksums.get(&tarball_name) else { + return Err(Error::BinaryVerification(format!( + "Signature-verified Pro SHA256SUMS has no entry for {tarball_name} - \ + cannot confirm binary integrity." + ))); + }; + verify_checksum(file_path, expected).map_err(|e| Error::BinaryVerification(e.to_string())) +} + +async fn fetch_signed_manifest(version: Option<&str>) -> Option<(Vec, Vec)> { + let v = version + .map(|s| s.to_string()) + .unwrap_or_else(config::get_chromium_version); + let bases = [ + format!("{}/chromium-v{v}", config::download_base_url()), + format!("{}/chromium-v{v}", config::GITHUB_DOWNLOAD_BASE_URL), + ]; + let client = http_client().ok()?; + for b in bases { + let Ok(manifest_resp) = client.get(format!("{b}/SHA256SUMS")).send().await else { + continue; + }; + if !manifest_resp.status().is_success() { + continue; + } + let Ok(manifest_bytes) = manifest_resp.bytes().await else { + continue; + }; + let Ok(sig_resp) = client.get(format!("{b}/SHA256SUMS.sig")).send().await else { + continue; + }; + if !sig_resp.status().is_success() { + continue; + } + let Ok(sig_bytes) = sig_resp.bytes().await else { + continue; + }; + return Some((manifest_bytes.to_vec(), sig_bytes.to_vec())); + } + None +} + +async fn fetch_checksums(version: Option<&str>) -> Option> { + let v = version + .map(|s| s.to_string()) + .unwrap_or_else(config::get_chromium_version); + let has_custom = std::env::var("CLOAKBROWSER_DOWNLOAD_URL").is_ok(); + let mut urls = vec![format!( + "{}/chromium-v{v}/SHA256SUMS", + config::download_base_url() + )]; + if !has_custom { + urls.push(format!( + "{}/chromium-v{v}/SHA256SUMS", + config::GITHUB_DOWNLOAD_BASE_URL + )); + } + let client = http_client().ok()?; + for url in urls { + let Ok(resp) = client.get(&url).send().await else { + continue; + }; + if !resp.status().is_success() { + continue; + } + let Ok(text) = resp.text().await else { + continue; + }; + return Some(parse_checksums(&text)); + } + None +} + +/// Read the `version=` line from a signed manifest. +pub fn parse_manifest_version(text: &str) -> Option { + for line in text.replace("\r\n", "\n").split('\n') { + let line = line.trim(); + if let Some(rest) = line.strip_prefix("version=") { + return Some(rest.trim().to_string()); + } + } + None +} + +/// Parse SHA256SUMS format: `<64-hex sha256> filename` per line. +pub fn parse_checksums(text: &str) -> HashMap { + let mut result = HashMap::new(); + for raw_line in text.trim().replace("\r\n", "\n").split('\n') { + let parts: Vec<&str> = raw_line.split_whitespace().collect(); + if parts.len() < 2 { + continue; + } + let hash_val = parts[0].to_lowercase(); + if hash_val.len() != 64 || !hash_val.chars().all(|c| c.is_ascii_hexdigit()) { + continue; + } + let name = parts[1].trim_start_matches('*'); + result.insert(name.to_string(), hash_val); + } + result +} + +/// Verify a detached Ed25519 signature over the raw manifest bytes. +/// `sig_b64` is base64 of the 64-byte raw signature (as stored in SHA256SUMS.sig). +pub fn verify_signature(manifest_bytes: &[u8], sig_b64: &[u8]) -> Result<()> { + let sig_text = String::from_utf8_lossy(sig_b64).trim().to_string(); + let signature = base64::engine::general_purpose::STANDARD + .decode(sig_text.as_bytes()) + .map_err(|e| Error::msg(format!("Malformed SHA256SUMS.sig (not valid base64): {e}")))?; + + let sig = Signature::from_slice(&signature).map_err(|_| { + Error::msg( + "SHA256SUMS signature verification failed - malformed signature. \ + Report at https://github.com/CloakHQ/cloakbrowser/issues", + ) + })?; + + for pubkey_b64 in config::BINARY_SIGNING_PUBKEYS { + let Ok(pub_bytes) = base64::engine::general_purpose::STANDARD.decode(pubkey_b64.as_bytes()) + else { + continue; + }; + if pub_bytes.len() != 32 { + continue; + } + let Ok(key_bytes): std::result::Result<[u8; 32], _> = pub_bytes.try_into() else { + continue; + }; + let Ok(vk) = VerifyingKey::from_bytes(&key_bytes) else { + continue; + }; + if vk.verify(manifest_bytes, &sig).is_ok() { + log::info("SHA256SUMS signature verified: Ed25519 OK"); + return Ok(()); + } + } + + Err(Error::msg( + "SHA256SUMS signature verification failed - no pinned key validated the \ + manifest. The binary's authenticity could not be confirmed. \ + Report at https://github.com/CloakHQ/cloakbrowser/issues", + )) +} + +fn verify_checksum(file_path: &Path, expected_hash: &str) -> Result<()> { + let mut file = std::fs::File::open(file_path)?; + let mut hasher = Sha256::new(); + let mut buf = [0u8; 8192]; + loop { + let n = file.read(&mut buf)?; + if n == 0 { + break; + } + hasher.update(&buf[..n]); + } + let actual = hex::encode(hasher.finalize()); + if actual != expected_hash.to_lowercase() { + return Err(Error::msg(format!( + "Checksum verification failed!\n Expected: {expected_hash}\n Got: {actual}\n \ + File may be corrupted or tampered with. Please retry or report at \ + https://github.com/CloakHQ/cloakbrowser/issues" + ))); + } + log::info("Checksum verified: SHA-256 OK"); + Ok(()) +} + +// --------------------------------------------------------------------------- +// Extraction +// --------------------------------------------------------------------------- + +fn extract_archive(archive_path: &Path, dest_dir: &Path, binary_path: Option<&Path>) -> Result<()> { + log::info(format!("Extracting to {}", dest_dir.display())); + + if dest_dir.exists() { + std::fs::remove_dir_all(dest_dir)?; + } + std::fs::create_dir_all(dest_dir)?; + + if archive_path + .extension() + .and_then(|e| e.to_str()) + .map(|e| e.eq_ignore_ascii_case("zip")) + .unwrap_or(false) + || archive_path + .to_string_lossy() + .ends_with(".zip") + { + extract_zip(archive_path, dest_dir)?; + } else { + extract_tar(archive_path, dest_dir)?; + } + + flatten_single_subdir(dest_dir)?; + + let bp = binary_path + .map(|p| p.to_path_buf()) + .unwrap_or_else(|| config::get_binary_path(None, false)); + if bp.exists() { + make_executable(&bp)?; + } + + #[cfg(target_os = "macos")] + remove_quarantine(dest_dir); + + if bp.exists() { + log::info(format!("Binary ready: {}", bp.display())); + } + Ok(()) +} + +/// Resolve an archive entry to an absolute path under `destination_dir`, +/// guarding against path-traversal / zip-slip. +pub fn resolve_safe_entry_path(destination_dir: &Path, entry_name: &str) -> Result { + let dest_full = std::fs::canonicalize(destination_dir).unwrap_or_else(|_| { + if destination_dir.is_absolute() { + destination_dir.to_path_buf() + } else { + std::env::current_dir() + .unwrap_or_else(|_| PathBuf::from(".")) + .join(destination_dir) + } + }); + // Normalize without requiring the path to exist yet for members. + let dest_full = path_clean(&dest_full); + let dest_prefix = if dest_full.as_os_str().to_string_lossy().ends_with('/') { + dest_full.clone() + } else { + PathBuf::from(format!( + "{}{}", + dest_full.display(), + std::path::MAIN_SEPARATOR + )) + }; + + let member = path_clean(&dest_full.join(entry_name)); + let member_str = member.to_string_lossy(); + let dest_str = dest_full.to_string_lossy(); + let prefix_str = dest_prefix.to_string_lossy(); + + if member_str != dest_str && !member_str.starts_with(prefix_str.as_ref()) { + return Err(Error::msg(format!( + "Archive contains path traversal: {entry_name}" + ))); + } + Ok(member) +} + +fn path_clean(path: &Path) -> PathBuf { + let mut out = PathBuf::new(); + for comp in path.components() { + match comp { + std::path::Component::ParentDir => { + out.pop(); + } + std::path::Component::CurDir => {} + other => out.push(other.as_os_str()), + } + } + out +} + +fn extract_tar(archive_path: &Path, dest_dir: &Path) -> Result<()> { + let file = std::fs::File::open(archive_path)?; + let gz = GzDecoder::new(file); + let mut archive = Archive::new(gz); + + for entry in archive.entries()? { + let mut entry = entry?; + let path = entry.path()?.into_owned(); + let name = path.to_string_lossy().to_string(); + + // Symlinks — allow relative only (macOS .app bundles need them). + if entry.header().entry_type().is_symlink() { + if let Ok(Some(link)) = entry.link_name() { + let link = link.to_string_lossy(); + if link.starts_with('/') || link.split('/').any(|p| p == "..") { + log::warning(format!("Skipping suspicious symlink: {name} -> {link}")); + continue; + } + } + let link_path = dest_dir.join(&path); + if let Some(parent) = link_path.parent() { + std::fs::create_dir_all(parent)?; + } + entry.unpack(&link_path)?; + continue; + } + + let member_path = resolve_safe_entry_path(dest_dir, &name)?; + if entry.header().entry_type().is_dir() { + std::fs::create_dir_all(&member_path)?; + continue; + } + if let Some(parent) = member_path.parent() { + std::fs::create_dir_all(parent)?; + } + entry.unpack(&member_path)?; + } + Ok(()) +} + +fn extract_zip(archive_path: &Path, dest_dir: &Path) -> Result<()> { + let file = std::fs::File::open(archive_path)?; + let mut archive = zip::ZipArchive::new(file) + .map_err(|e| Error::msg(format!("Failed to open zip archive: {e}")))?; + + // Validate all entries first. + for i in 0..archive.len() { + let entry = archive + .by_index(i) + .map_err(|e| Error::msg(format!("zip entry error: {e}")))?; + let name = entry.name().to_string(); + resolve_safe_entry_path(dest_dir, &name)?; + } + + for i in 0..archive.len() { + let mut entry = archive + .by_index(i) + .map_err(|e| Error::msg(format!("zip entry error: {e}")))?; + let name = entry.name().to_string(); + let outpath = resolve_safe_entry_path(dest_dir, &name)?; + if entry.is_dir() { + std::fs::create_dir_all(&outpath)?; + } else { + if let Some(parent) = outpath.parent() { + std::fs::create_dir_all(parent)?; + } + let mut outfile = std::fs::File::create(&outpath)?; + std::io::copy(&mut entry, &mut outfile)?; + } + } + Ok(()) +} + +fn flatten_single_subdir(dest_dir: &Path) -> Result<()> { + let entries: Vec<_> = std::fs::read_dir(dest_dir)? + .filter_map(|e| e.ok()) + .collect(); + if entries.len() != 1 { + return Ok(()); + } + let entry = &entries[0]; + let path = entry.path(); + if !path.is_dir() { + return Ok(()); + } + let name = path + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("") + .to_string(); + if name.ends_with(".app") { + log::debug(format!("Keeping .app bundle intact: {name}")); + return Ok(()); + } + log::debug(format!("Flattening single subdirectory: {name}")); + for item in std::fs::read_dir(&path)? { + let item = item?; + let target = dest_dir.join(item.file_name()); + std::fs::rename(item.path(), target)?; + } + std::fs::remove_dir_all(&path)?; + Ok(()) +} + +fn make_executable(path: &Path) -> Result<()> { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mut perms = std::fs::metadata(path)?.permissions(); + perms.set_mode(perms.mode() | 0o111); + std::fs::set_permissions(path, perms)?; + } + let _ = path; + Ok(()) +} + +#[cfg(target_os = "macos")] +fn remove_quarantine(path: &Path) { + let _ = std::process::Command::new("xattr") + .args(["-cr"]) + .arg(path) + .output(); + log::debug(format!( + "Removed quarantine attributes from {}", + path.display() + )); +} + +/// Remove all cached binaries. Forces re-download on next launch. +pub fn clear_cache() -> Result<()> { + let cache_dir = config::get_cache_dir(); + if cache_dir.exists() { + std::fs::remove_dir_all(&cache_dir)?; + log::info(format!("Cache cleared: {}", cache_dir.display())); + } + Ok(()) +} + +/// Return info about the current binary installation. +pub fn binary_info(browser_version: Option<&str>) -> Result { + let requested = config::normalize_requested_version(browser_version)?; + let pro_version = requested + .clone() + .or_else(|| config::get_effective_version(true)); + let pro = pro_binary_ready(pro_version.as_deref()); + + let (effective, binary_path, cache_pro) = if pro { + let v = pro_version.clone().unwrap_or_default(); + ( + v.clone(), + config::get_binary_path(Some(&v), true), + true, + ) + } else { + let v = requested + .clone() + .or_else(|| config::get_effective_version(false)) + .unwrap_or_else(config::get_chromium_version); + (v.clone(), config::get_binary_path(Some(&v), false), false) + }; + + Ok(BinaryInfo { + version: effective.clone(), + tier: if pro { "pro" } else { "free" }.into(), + bundled_version: config::CHROMIUM_VERSION.into(), + platform: config::get_platform_tag()?, + binary_path: binary_path.display().to_string(), + installed: binary_path.exists(), + cache_dir: config::get_binary_dir(Some(&effective), cache_pro) + .display() + .to_string(), + download_url: if pro { + config::get_pro_latest_download_url() + } else { + config::get_download_url(Some(&effective))? + }, + }) +} + +/// Manually check for a newer Chromium version. Returns new version or null. +pub async fn check_for_update() -> Result> { + let latest = get_latest_chromium_version().await?; + let Some(latest) = latest else { + return Ok(None); + }; + if !config::version_newer(&latest, &config::get_chromium_version()) { + return Ok(None); + } + + let binary_dir = config::get_binary_dir(Some(&latest), false); + if binary_dir.exists() { + write_version_marker(&latest); + return Ok(Some(latest)); + } + + log::info(format!("Downloading update: Chromium {latest}...")); + download_and_extract(Some(&latest)).await?; + write_version_marker(&latest); + Ok(Some(latest)) +} + +async fn get_latest_chromium_version() -> Result> { + let client = http_client()?; + // Prefer cloakbrowser.dev version endpoint when available. + let url = format!( + "{}/api/download/version?tier=free", + config::download_base_url() + ); + if let Ok(resp) = client + .get(&url) + .header("X-Platform", config::get_platform_tag()?) + .send() + .await + { + if resp.status().is_success() { + if let Ok(data) = resp.json::().await { + if let Some(v) = data.get("version").and_then(|v| v.as_str()) { + return Ok(Some(v.to_string())); + } + } + } + } + + // GitHub releases fallback: find newest chromium-v* tag. + let resp = client + .get(config::GITHUB_API_URL) + .header("Accept", "application/vnd.github+json") + .send() + .await?; + if !resp.status().is_success() { + return Ok(None); + } + let releases: Vec = resp.json().await?; + let mut best: Option = None; + for rel in releases { + if let Some(tag) = rel.get("tag_name").and_then(|t| t.as_str()) { + if let Some(v) = tag.strip_prefix("chromium-v") { + if best + .as_ref() + .map(|b| config::version_newer(v, b)) + .unwrap_or(true) + { + best = Some(v.to_string()); + } + } + } + } + Ok(best) +} + +fn maybe_trigger_update_check() { + if WRAPPER_UPDATE_CHECKED.swap(true, Ordering::SeqCst) { + return; + } + if std::env::var("CLOAKBROWSER_AUTO_UPDATE") + .map(|v| v.eq_ignore_ascii_case("false")) + .unwrap_or(false) + { + return; + } + // Fire-and-forget background check. + tokio::spawn(async { + // Rate-limit via marker mtime. + let Ok(tag) = config::get_platform_tag() else { + return; + }; + let marker = config::get_cache_dir().join(format!(".last_update_check_{tag}")); + if marker.exists() { + if let Ok(meta) = marker.metadata() { + if let Ok(modified) = meta.modified() { + if let Ok(age) = SystemTime::now().duration_since(modified) { + if age.as_secs() < UPDATE_CHECK_INTERVAL { + return; + } + } + } + } + } + let _ = std::fs::create_dir_all(config::get_cache_dir()); + let _ = std::fs::write(&marker, now_unix().to_string()); + + if let Ok(Some(latest)) = check_for_update().await { + log::info(format!("Auto-updated stealth Chromium to {latest}")); + } + }); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_checksums_basic() { + let text = "\ +abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789 cloakbrowser-linux-x64.tar.gz +version=146.0.7680.177.5 +# comment +"; + let map = parse_checksums(text); + assert_eq!( + map.get("cloakbrowser-linux-x64.tar.gz").map(String::as_str), + Some("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789") + ); + } + + #[test] + fn parse_manifest_version_line() { + let text = "version=146.0.7680.177.5\nabc file.tar.gz\n"; + assert_eq!( + parse_manifest_version(text).as_deref(), + Some("146.0.7680.177.5") + ); + } + + #[test] + fn zip_slip_blocked() { + let dest = std::env::temp_dir().join("cloak-zipslip-test"); + let _ = std::fs::create_dir_all(&dest); + let err = resolve_safe_entry_path(&dest, "../evil").unwrap_err(); + assert!(err.to_string().contains("path traversal")); + let _ = std::fs::remove_dir_all(&dest); + } + + #[test] + fn welcome_due_missing_marker() { + let p = std::env::temp_dir().join("cloak-welcome-missing"); + let _ = std::fs::remove_file(&p); + assert!(welcome_due(&p, false)); + assert!(welcome_due(&p, true)); + } +} diff --git a/rust/cloakbrowser/src/error.rs b/rust/cloakbrowser/src/error.rs new file mode 100644 index 00000000..1d40506d --- /dev/null +++ b/rust/cloakbrowser/src/error.rs @@ -0,0 +1,55 @@ +//! Error types for CloakBrowser. + +use thiserror::Error; + +/// Library-wide error type. +#[derive(Debug, Error)] +pub enum Error { + #[error("{0}")] + Message(String), + + #[error("unsupported platform: {0}")] + UnsupportedPlatform(String), + + #[error("invalid browser version pin: {0}")] + InvalidVersion(String), + + #[error("binary not found: {0}")] + BinaryNotFound(String), + + /// A downloaded binary could not be authenticated (bad/missing signature, + /// version mismatch, or checksum failure). + /// + /// Distinct from transient download/network errors: a verification failure is + /// a tampering signal and MUST surface, never silently fall back to another + /// binary. + #[error("binary verification failed: {0}")] + BinaryVerification(String), + + #[error("pro binary unavailable: {0}")] + ProUnavailable(String), + + #[error("io error: {0}")] + Io(#[from] std::io::Error), + + #[error("http error: {0}")] + Http(#[from] reqwest::Error), + + #[error("json error: {0}")] + Json(#[from] serde_json::Error), + + #[error("playwright error: {0}")] + Playwright(String), + + #[error(transparent)] + Other(#[from] anyhow::Error), +} + +impl Error { + pub fn msg(s: impl Into) -> Self { + Self::Message(s.into()) + } +} + +/// Result alias for this crate. +pub type Result = std::result::Result; diff --git a/rust/cloakbrowser/src/geoip.rs b/rust/cloakbrowser/src/geoip.rs new file mode 100644 index 00000000..19f4aa9f --- /dev/null +++ b/rust/cloakbrowser/src/geoip.rs @@ -0,0 +1,395 @@ +//! GeoIP-based timezone and locale detection from a proxy IP. +//! Direct port of Python `cloakbrowser/geoip.py` / .NET `GeoIp.cs`. +//! +//! Enabled with the `geoip` feature (default). Downloads GeoLite2-City.mmdb +//! (~70 MB) on first use into `~/.cloakbrowser/geoip/`. + +use std::collections::HashMap; +use std::net::IpAddr; +use std::path::{Path, PathBuf}; +use std::sync::OnceLock; +use std::time::SystemTime; + +use crate::config; +use crate::error::{Error, Result}; +use crate::log; +use crate::proxy::{self, Proxy}; +use crate::version::VERSION; + +const GEOIP_DB_URL: &str = + "https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb"; +const GEOIP_DB_FILENAME: &str = "GeoLite2-City.mmdb"; +const GEOIP_UPDATE_INTERVAL: u64 = 30 * 86_400; // 30 days +const DEFAULT_GEOIP_TIMEOUT_SECONDS: f64 = 5.0; +const GEOIP_TIMEOUT_ENV: &str = "CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS"; + +const IP_ECHO_URLS: &[&str] = &[ + "https://api.ipify.org", + "https://checkip.amazonaws.com", + "https://ifconfig.me/ip", +]; + +fn country_locale_map() -> &'static HashMap<&'static str, &'static str> { + static MAP: OnceLock> = OnceLock::new(); + MAP.get_or_init(|| { + HashMap::from([ + ("US", "en-US"), + ("GB", "en-GB"), + ("AU", "en-AU"), + ("CA", "en-CA"), + ("NZ", "en-NZ"), + ("IE", "en-IE"), + ("ZA", "en-ZA"), + ("SG", "en-SG"), + ("DE", "de-DE"), + ("AT", "de-AT"), + ("CH", "de-CH"), + ("FR", "fr-FR"), + ("BE", "fr-BE"), + ("ES", "es-ES"), + ("MX", "es-MX"), + ("AR", "es-AR"), + ("CO", "es-CO"), + ("CL", "es-CL"), + ("BR", "pt-BR"), + ("PT", "pt-PT"), + ("IT", "it-IT"), + ("NL", "nl-NL"), + ("JP", "ja-JP"), + ("KR", "ko-KR"), + ("CN", "zh-CN"), + ("TW", "zh-TW"), + ("HK", "zh-HK"), + ("RU", "ru-RU"), + ("UA", "uk-UA"), + ("PL", "pl-PL"), + ("CZ", "cs-CZ"), + ("RO", "ro-RO"), + ("IL", "he-IL"), + ("TR", "tr-TR"), + ("SA", "ar-SA"), + ("AE", "ar-AE"), + ("EG", "ar-EG"), + ("IN", "hi-IN"), + ("ID", "id-ID"), + ("PH", "en-PH"), + ("TH", "th-TH"), + ("VN", "vi-VN"), + ("MY", "ms-MY"), + ("SE", "sv-SE"), + ("NO", "nb-NO"), + ("DK", "da-DK"), + ("FI", "fi-FI"), + ("GR", "el-GR"), + ("HU", "hu-HU"), + ("BG", "bg-BG"), + ("SI", "sl-SI"), + ("SK", "sk-SK"), + ("HR", "hr-HR"), + ("RS", "sr-RS"), + ("LT", "lt-LT"), + ("LV", "lv-LV"), + ("EE", "et-EE"), + ("IS", "is-IS"), + ("LU", "fr-LU"), + ("MT", "en-MT"), + ("CY", "el-CY"), + ("MD", "ro-MD"), + ("BY", "ru-BY"), + ("GE", "ka-GE"), + ("AL", "sq-AL"), + ("MK", "mk-MK"), + ("BA", "bs-BA"), + ("PE", "es-PE"), + ("VE", "es-VE"), + ("EC", "es-EC"), + ("UY", "es-UY"), + ("CR", "es-CR"), + ("DO", "es-DO"), + ("GT", "es-GT"), + ("BO", "es-BO"), + ("PY", "es-PY"), + ("PK", "en-PK"), + ("BD", "bn-BD"), + ("LK", "si-LK"), + ("KZ", "ru-KZ"), + ("IR", "fa-IR"), + ("IQ", "ar-IQ"), + ("JO", "ar-JO"), + ("LB", "ar-LB"), + ("KW", "ar-KW"), + ("QA", "ar-QA"), + ("OM", "ar-OM"), + ("BH", "ar-BH"), + ("NG", "en-NG"), + ("KE", "en-KE"), + ("MA", "fr-MA"), + ("DZ", "ar-DZ"), + ("TN", "ar-TN"), + ("GH", "en-GH"), + ("AM", "hy-AM"), + ("AZ", "az-AZ"), + ("UZ", "uz-UZ"), + ("KG", "ky-KG"), + ("TJ", "tg-TJ"), + ("TM", "tk-TM"), + ("ME", "sr-ME"), + ("XK", "sq-XK"), + ("LI", "de-LI"), + ("MC", "fr-MC"), + ("AD", "ca-AD"), + ("MM", "my-MM"), + ("KH", "km-KH"), + ("LA", "lo-LA"), + ("MN", "mn-MN"), + ("BN", "ms-BN"), + ("MO", "zh-MO"), + ("YE", "ar-YE"), + ("SY", "ar-SY"), + ("PS", "ar-PS"), + ("LY", "ar-LY"), + ("ET", "am-ET"), + ("TZ", "sw-TZ"), + ("UG", "en-UG"), + ("SN", "fr-SN"), + ("CI", "fr-CI"), + ("CM", "fr-CM"), + ("AO", "pt-AO"), + ("MZ", "pt-MZ"), + ("ZM", "en-ZM"), + ("ZW", "en-ZW"), + ("HN", "es-HN"), + ("NI", "es-NI"), + ("SV", "es-SV"), + ("PA", "es-PA"), + ("JM", "en-JM"), + ("TT", "en-TT"), + ("PR", "es-PR"), + ]) + }) +} + +fn geoip_db_path() -> PathBuf { + config::get_cache_dir().join("geoip").join(GEOIP_DB_FILENAME) +} + +fn get_geoip_timeout_seconds() -> f64 { + std::env::var(GEOIP_TIMEOUT_ENV) + .ok() + .and_then(|s| s.parse().ok()) + .filter(|&v| v > 0.0) + .unwrap_or(DEFAULT_GEOIP_TIMEOUT_SECONDS) +} + +fn http_client(timeout_secs: f64) -> Result { + Ok(reqwest::Client::builder() + .timeout(std::time::Duration::from_secs_f64(timeout_secs.max(1.0))) + .user_agent(format!("cloakbrowser-rust/{VERSION}")) + .build()?) +} + +/// Ensure GeoLite2-City.mmdb is present. Downloads on first use. +pub async fn ensure_geoip_db() -> Result { + let path = geoip_db_path(); + if path.exists() { + if let Ok(meta) = path.metadata() { + if let Ok(modified) = meta.modified() { + if let Ok(age) = SystemTime::now().duration_since(modified) { + if age.as_secs() < GEOIP_UPDATE_INTERVAL { + return Ok(path); + } + } + } + } + // Stale: try refresh in place; keep old on failure. + if download_geoip_db(&path).await.is_ok() { + return Ok(path); + } + return Ok(path); + } + + download_geoip_db(&path).await?; + Ok(path) +} + +async fn download_geoip_db(path: &Path) -> Result<()> { + log::info(format!("Downloading GeoLite2-City database to {}...", path.display())); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let client = http_client(600.0)?; + let resp = client.get(GEOIP_DB_URL).send().await?.error_for_status()?; + let bytes = resp.bytes().await?; + let tmp = path.with_extension("tmp"); + std::fs::write(&tmp, &bytes)?; + std::fs::rename(&tmp, path)?; + log::info(format!( + "GeoIP database ready ({} MB)", + bytes.len() / (1024 * 1024) + )); + Ok(()) +} + +/// Resolve the exit IP through the proxy (or machine public IP when proxy is None). +pub async fn resolve_proxy_exit_ip(proxy_url: Option<&str>) -> Option { + let timeout = get_geoip_timeout_seconds(); + let client = build_proxy_client(proxy_url, timeout).ok()?; + + for url in IP_ECHO_URLS { + if let Ok(resp) = client.get(*url).send().await { + if resp.status().is_success() { + if let Ok(text) = resp.text().await { + let ip = text.trim(); + if ip.parse::().is_ok() { + return Some(ip.to_string()); + } + } + } + } + } + None +} + +fn build_proxy_client(proxy_url: Option<&str>, timeout: f64) -> Result { + let mut builder = reqwest::Client::builder() + .timeout(std::time::Duration::from_secs_f64(timeout.max(1.0))) + .user_agent(format!("cloakbrowser-rust/{VERSION}")); + + if let Some(url) = proxy_url { + let proxy = reqwest::Proxy::all(url) + .map_err(|e| Error::msg(format!("Invalid proxy for geoip: {e}")))?; + builder = builder.proxy(proxy); + } + + Ok(builder.build()?) +} + +/// Resolve timezone, locale, and exit IP from a proxy. +/// Never panics; returns None fields on failure. +pub async fn resolve_proxy_geo_with_ip( + proxy_url: Option<&str>, +) -> (Option, Option, Option) { + #[cfg(not(feature = "geoip"))] + { + log::warning( + "geoip requested but cloakbrowser was built without the `geoip` feature. \ + Enable it with: cloakbrowser = { version = \"...\", features = [\"geoip\"] }", + ); + let exit_ip = resolve_proxy_exit_ip(proxy_url).await; + return (None, None, exit_ip); + } + + #[cfg(feature = "geoip")] + { + let exit_ip = resolve_proxy_exit_ip(proxy_url).await; + + let db_path = match ensure_geoip_db().await { + Ok(p) => p, + Err(e) => { + log::warning(format!("GeoIP database unavailable: {e}")); + return (None, None, exit_ip); + } + }; + + let Some(ip_str) = exit_ip + .clone() + .or_else(|| proxy_url.and_then(host_from_proxy_url)) + else { + return (None, None, exit_ip); + }; + + let ip: IpAddr = match ip_str.parse() { + Ok(ip) => ip, + Err(_) => match resolve_hostname_to_ip(&ip_str) { + Some(ip) => ip, + None => return (None, None, exit_ip), + }, + }; + + match lookup_geo(&db_path, ip) { + Ok((tz, locale)) => (tz, locale, exit_ip), + Err(e) => { + log::debug(format!("GeoIP lookup failed for {ip}: {e}")); + (None, None, exit_ip) + } + } + } +} + +#[cfg(feature = "geoip")] +fn lookup_geo(db_path: &Path, ip: IpAddr) -> Result<(Option, Option)> { + let reader = maxminddb::Reader::open_readfile(db_path) + .map_err(|e| Error::msg(format!("Failed to open GeoIP DB: {e}")))?; + + let city: maxminddb::geoip2::City = reader + .lookup(ip) + .map_err(|e| Error::msg(format!("GeoIP lookup error: {e}")))?; + + let timezone = city + .location + .as_ref() + .and_then(|l| l.time_zone) + .map(|s| s.to_string()); + + let country = city + .country + .as_ref() + .and_then(|c| c.iso_code) + .map(|s| s.to_string()); + + let locale = country.and_then(|c| { + country_locale_map() + .get(c.as_str()) + .map(|s| s.to_string()) + }); + + Ok((timezone, locale)) +} + +fn host_from_proxy_url(url: &str) -> Option { + let parsed = url::Url::parse(&proxy::ensure_proxy_scheme(url)).ok()?; + parsed.host_str().map(|s| s.to_string()) +} + +fn resolve_hostname_to_ip(host: &str) -> Option { + use std::net::ToSocketAddrs; + (host, 0u16) + .to_socket_addrs() + .ok()? + .next() + .map(|a| a.ip()) +} + +/// Auto-fill timezone/locale from the egress IP when geoip is enabled. +/// Returns `(timezone, locale, exit_ip)`. +pub async fn maybe_resolve_geoip( + geoip: bool, + proxy: Option<&Proxy>, + timezone: Option, + locale: Option, +) -> (Option, Option, Option) { + if !geoip { + return (timezone, locale, None); + } + + let proxy_url = proxy.and_then(|p| proxy::extract_proxy_url(Some(p))); + + // When both tz/locale are explicit, only resolve exit IP for WebRTC — + // and only when a proxy is present. + if timezone.is_some() && locale.is_some() { + let exit_ip = if proxy_url.is_some() { + resolve_proxy_exit_ip(proxy_url.as_deref()).await + } else { + None + }; + return (timezone, locale, exit_ip); + } + + let (geo_tz, geo_locale, exit_ip) = + resolve_proxy_geo_with_ip(proxy_url.as_deref()).await; + ( + timezone.or(geo_tz), + locale.or(geo_locale), + exit_ip, + ) +} diff --git a/rust/cloakbrowser/src/human/config.rs b/rust/cloakbrowser/src/human/config.rs new file mode 100644 index 00000000..d0342f74 --- /dev/null +++ b/rust/cloakbrowser/src/human/config.rs @@ -0,0 +1,269 @@ +//! Humanize configuration and presets. +//! Direct port of Python `cloakbrowser/human/config.py` / .NET `HumanConfig.cs`. + +use serde::{Deserialize, Serialize}; + +/// Inclusive numeric range `(min, max)`. +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +pub struct Range { + pub min: f64, + pub max: f64, +} + +impl Range { + pub const fn new(min: f64, max: f64) -> Self { + Self { min, max } + } +} + +impl From<(f64, f64)> for Range { + fn from(t: (f64, f64)) -> Self { + Self { + min: t.0, + max: t.1, + } + } +} + +/// Humanize behavior preset names. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum HumanPreset { + #[default] + Default, + Careful, +} + +impl HumanPreset { + /// Parse `'default'` / `'careful'` (case-insensitive). + pub fn parse(s: &str) -> Result { + match s.trim().to_ascii_lowercase().as_str() { + "default" => Ok(Self::Default), + "careful" => Ok(Self::Careful), + other => Err(format!( + "Unknown humanize preset '{other}'. Valid presets: careful, default" + )), + } + } +} + +/// All tunable parameters for human-like behavior. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct HumanConfig { + // Keyboard + pub typing_delay: f64, + pub typing_delay_spread: f64, + pub typing_pause_chance: f64, + pub typing_pause_range: Range, + pub shift_down_delay: Range, + pub shift_up_delay: Range, + pub key_hold: Range, + pub mistype_chance: f64, + pub mistype_delay_notice: Range, + pub mistype_delay_correct: Range, + pub field_switch_delay: Range, + + // Mouse — movement + pub mouse_steps_divisor: f64, + pub mouse_min_steps: i32, + pub mouse_max_steps: i32, + pub mouse_wobble_max: f64, + pub mouse_overshoot_chance: f64, + pub mouse_overshoot_px: Range, + pub mouse_burst_size: Range, + pub mouse_burst_pause: Range, + + // Mouse — clicks + pub click_aim_delay_input: Range, + pub click_aim_delay_button: Range, + pub click_hold_input: Range, + pub click_hold_button: Range, + pub click_input_x_range: Range, + + // Mouse — idle + pub idle_drift_px: f64, + pub idle_pause_range: Range, + + // Scroll + pub scroll_delta_base: Range, + pub scroll_delta_variance: f64, + pub scroll_pause_fast: Range, + pub scroll_pause_slow: Range, + pub scroll_accel_steps: Range, + pub scroll_decel_steps: Range, + pub scroll_overshoot_chance: f64, + pub scroll_overshoot_px: Range, + pub scroll_settle_delay: Range, + pub scroll_target_zone: Range, + pub scroll_pre_move_delay: Range, + + // Initial cursor (address-bar area) + pub initial_cursor_x: Range, + pub initial_cursor_y: Range, + + // Idle between actions (opt-in) + pub idle_between_actions: bool, + pub idle_between_duration: Range, +} + +impl Default for HumanConfig { + fn default() -> Self { + Self { + typing_delay: 70.0, + typing_delay_spread: 40.0, + typing_pause_chance: 0.1, + typing_pause_range: Range::new(400.0, 1000.0), + shift_down_delay: Range::new(30.0, 70.0), + shift_up_delay: Range::new(20.0, 50.0), + key_hold: Range::new(15.0, 35.0), + mistype_chance: 0.02, + mistype_delay_notice: Range::new(100.0, 300.0), + mistype_delay_correct: Range::new(50.0, 150.0), + field_switch_delay: Range::new(800.0, 1500.0), + + mouse_steps_divisor: 8.0, + mouse_min_steps: 25, + mouse_max_steps: 80, + mouse_wobble_max: 1.5, + mouse_overshoot_chance: 0.15, + mouse_overshoot_px: Range::new(3.0, 6.0), + mouse_burst_size: Range::new(3.0, 5.0), + mouse_burst_pause: Range::new(8.0, 18.0), + + click_aim_delay_input: Range::new(60.0, 140.0), + click_aim_delay_button: Range::new(80.0, 200.0), + click_hold_input: Range::new(40.0, 100.0), + click_hold_button: Range::new(60.0, 150.0), + click_input_x_range: Range::new(0.05, 0.30), + + idle_drift_px: 3.0, + idle_pause_range: Range::new(300.0, 1000.0), + + scroll_delta_base: Range::new(80.0, 130.0), + scroll_delta_variance: 0.2, + scroll_pause_fast: Range::new(30.0, 80.0), + scroll_pause_slow: Range::new(80.0, 200.0), + scroll_accel_steps: Range::new(2.0, 3.0), + scroll_decel_steps: Range::new(2.0, 3.0), + scroll_overshoot_chance: 0.1, + scroll_overshoot_px: Range::new(50.0, 150.0), + scroll_settle_delay: Range::new(300.0, 600.0), + scroll_target_zone: Range::new(0.20, 0.80), + scroll_pre_move_delay: Range::new(100.0, 300.0), + + initial_cursor_x: Range::new(400.0, 700.0), + initial_cursor_y: Range::new(45.0, 60.0), + + idle_between_actions: false, + idle_between_duration: Range::new(0.3, 0.8), + } + } +} + +impl HumanConfig { + /// Careful preset — slower and more deliberate. + pub fn careful() -> Self { + Self { + typing_delay: 100.0, + typing_delay_spread: 50.0, + typing_pause_chance: 0.15, + typing_pause_range: Range::new(500.0, 1200.0), + shift_down_delay: Range::new(40.0, 90.0), + shift_up_delay: Range::new(30.0, 70.0), + key_hold: Range::new(20.0, 45.0), + field_switch_delay: Range::new(1000.0, 2000.0), + mouse_overshoot_chance: 0.10, + mouse_burst_pause: Range::new(12.0, 25.0), + click_aim_delay_input: Range::new(80.0, 180.0), + click_aim_delay_button: Range::new(120.0, 280.0), + click_hold_input: Range::new(60.0, 140.0), + click_hold_button: Range::new(80.0, 200.0), + scroll_pause_fast: Range::new(100.0, 200.0), + scroll_pause_slow: Range::new(250.0, 600.0), + scroll_settle_delay: Range::new(400.0, 800.0), + scroll_pre_move_delay: Range::new(150.0, 400.0), + idle_between_actions: true, + idle_between_duration: Range::new(0.4, 1.0), + ..Self::default() + } + } + + /// Resolve a preset into a full config. + pub fn resolve(preset: HumanPreset) -> Self { + match preset { + HumanPreset::Default => Self::default(), + HumanPreset::Careful => Self::careful(), + } + } +} + +// --------------------------------------------------------------------------- +// Random / timing helpers (mirror Python human/config.py) +// --------------------------------------------------------------------------- + +/// Random float in `[lo, hi]`. +pub fn rand(lo: f64, hi: f64) -> f64 { + let (lo, hi) = if hi < lo { (hi, lo) } else { (lo, hi) }; + lo + rand::random::() * (hi - lo) +} + +/// Random integer in `[lo, hi]` inclusive. +pub fn rand_int(lo: i32, hi: i32) -> i32 { + let (lo, hi) = if hi < lo { (hi, lo) } else { (lo, hi) }; + if lo == hi { + return lo; + } + let span = (hi - lo + 1) as u32; + lo + (rand::random::() % span) as i32 +} + +/// Random float from a [`Range`]. +pub fn rand_range(r: Range) -> f64 { + rand(r.min, r.max) +} + +/// Random integer from a [`Range`], inclusive. +pub fn rand_int_range(r: Range) -> i32 { + rand_int(r.min as i32, r.max as i32) +} + +/// True with the given probability in `[0, 1]`. +pub fn chance(probability: f64) -> bool { + rand::random::() < probability +} + +/// Async sleep for `ms` milliseconds (no-op if <= 0). +pub async fn sleep_ms(ms: f64) { + if ms > 0.0 { + tokio::time::sleep(std::time::Duration::from_millis(ms.round() as u64)).await; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn careful_is_slower() { + let d = HumanConfig::default(); + let c = HumanConfig::careful(); + assert!(c.typing_delay > d.typing_delay); + assert!(c.idle_between_actions); + } + + #[test] + fn parse_preset() { + assert_eq!(HumanPreset::parse("DEFAULT").unwrap(), HumanPreset::Default); + assert_eq!(HumanPreset::parse("careful").unwrap(), HumanPreset::Careful); + assert!(HumanPreset::parse("turbo").is_err()); + } + + #[test] + fn rand_in_bounds() { + for _ in 0..50 { + let v = rand(1.0, 2.0); + assert!((1.0..=2.0).contains(&v)); + let i = rand_int(3, 5); + assert!((3..=5).contains(&i)); + } + } +} diff --git a/rust/cloakbrowser/src/human/keyboard.rs b/rust/cloakbrowser/src/human/keyboard.rs new file mode 100644 index 00000000..c4349f85 --- /dev/null +++ b/rust/cloakbrowser/src/human/keyboard.rs @@ -0,0 +1,353 @@ +//! Human-like keyboard input. +//! Direct port of Python `cloakbrowser/human/keyboard.py` / .NET `HumanKeyboard.cs`. + +use std::collections::{HashMap, HashSet}; +use std::sync::OnceLock; + +use playwright_rs::protocol::{CDPSession, Keyboard, Page}; +use serde_json::json; + +use super::config::{self, HumanConfig}; +use crate::error::{Error, Result}; +use crate::log; + +fn shift_symbols() -> &'static HashSet { + static S: OnceLock> = OnceLock::new(); + S.get_or_init(|| "@#!$%^&*()_+{}|:\"<>?~".chars().collect()) +} + +fn nearby_keys() -> &'static HashMap { + static M: OnceLock> = OnceLock::new(); + M.get_or_init(|| { + HashMap::from([ + ('a', "sqwz"), + ('b', "vghn"), + ('c', "xdfv"), + ('d', "sfecx"), + ('e', "wrsdf"), + ('f', "dgrtcv"), + ('g', "fhtyb"), + ('h', "gjybn"), + ('i', "ujko"), + ('j', "hkunm"), + ('k', "jloi"), + ('l', "kop"), + ('m', "njk"), + ('n', "bhjm"), + ('o', "iklp"), + ('p', "ol"), + ('q', "wa"), + ('r', "edft"), + ('s', "awedxz"), + ('t', "rfgy"), + ('u', "yhji"), + ('v', "cfgb"), + ('w', "qase"), + ('x', "zsdc"), + ('y', "tghu"), + ('z', "asx"), + ('1', "2q"), + ('2', "13qw"), + ('3', "24we"), + ('4', "35er"), + ('5', "46rt"), + ('6', "57ty"), + ('7', "68yu"), + ('8', "79ui"), + ('9', "80io"), + ('0', "9p"), + ]) + }) +} + +fn shift_symbol_codes() -> &'static HashMap { + static M: OnceLock> = OnceLock::new(); + M.get_or_init(|| { + HashMap::from([ + ('!', "Digit1"), + ('@', "Digit2"), + ('#', "Digit3"), + ('$', "Digit4"), + ('%', "Digit5"), + ('^', "Digit6"), + ('&', "Digit7"), + ('*', "Digit8"), + ('(', "Digit9"), + (')', "Digit0"), + ('_', "Minus"), + ('+', "Equal"), + ('{', "BracketLeft"), + ('}', "BracketRight"), + ('|', "Backslash"), + (':', "Semicolon"), + ('"', "Quote"), + ('<', "Comma"), + ('>', "Period"), + ('?', "Slash"), + ('~', "Backquote"), + ]) + }) +} + +fn shift_symbol_keycodes() -> &'static HashMap { + static M: OnceLock> = OnceLock::new(); + M.get_or_init(|| { + HashMap::from([ + ('!', 49), + ('@', 50), + ('#', 51), + ('$', 52), + ('%', 53), + ('^', 54), + ('&', 55), + ('*', 56), + ('(', 57), + (')', 48), + ('_', 189), + ('+', 187), + ('{', 219), + ('}', 221), + ('|', 220), + (':', 186), + ('"', 222), + ('<', 188), + ('>', 190), + ('?', 191), + ('~', 192), + ]) + }) +} + +fn is_ascii(c: char) -> bool { + c as u32 <= 0x7F +} + +fn get_nearby_key(ch: char) -> char { + let lower = ch.to_ascii_lowercase(); + if let Some(neighbors) = nearby_keys().get(&lower) { + if !neighbors.is_empty() { + let idx = (rand::random::() as usize) % neighbors.len(); + let wrong = neighbors.chars().nth(idx).unwrap_or(ch); + return if ch.is_ascii_uppercase() { + wrong.to_ascii_uppercase() + } else { + wrong + }; + } + } + ch +} + +/// Type `text` with human-like per-character timing. +/// +/// When `cdp` is provided, shift symbols use CDP `Input.dispatchKeyEvent` +/// (`isTrusted=true`). Otherwise falls back to `insert_text` + evaluate. +pub async fn human_type( + page: &Page, + keyboard: &Keyboard, + text: &str, + cfg: &HumanConfig, + cdp: Option<&CDPSession>, +) -> Result<()> { + let chars: Vec = text.chars().collect(); + for (i, &ch) in chars.iter().enumerate() { + // Non-ASCII — insertText + if !is_ascii(ch) { + config::sleep_ms(config::rand_range(cfg.key_hold)).await; + keyboard + .insert_text(&ch.to_string()) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + if i + 1 < chars.len() { + inter_char_delay(cfg).await; + } + continue; + } + + // Mistype chance — ASCII alphanumeric only + if config::chance(cfg.mistype_chance) && ch.is_ascii_alphanumeric() { + let wrong = get_nearby_key(ch); + type_normal_char(keyboard, wrong, cfg).await?; + config::sleep_ms(config::rand_range(cfg.mistype_delay_notice)).await; + keyboard + .down("Backspace") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.key_hold)).await; + keyboard + .up("Backspace") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.mistype_delay_correct)).await; + } + + if ch.is_ascii_uppercase() && ch.is_ascii_alphabetic() { + type_shifted_char(keyboard, ch, cfg).await?; + } else if shift_symbols().contains(&ch) { + type_shift_symbol(page, keyboard, ch, cfg, cdp).await?; + } else { + type_normal_char(keyboard, ch, cfg).await?; + } + + if i + 1 < chars.len() { + inter_char_delay(cfg).await; + } + } + Ok(()) +} + +async fn type_normal_char(keyboard: &Keyboard, ch: char, cfg: &HumanConfig) -> Result<()> { + let key = ch.to_string(); + keyboard + .down(&key) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.key_hold)).await; + keyboard + .up(&key) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + Ok(()) +} + +async fn type_shifted_char(keyboard: &Keyboard, ch: char, cfg: &HumanConfig) -> Result<()> { + keyboard + .down("Shift") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.shift_down_delay)).await; + let key = ch.to_string(); + keyboard + .down(&key) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.key_hold)).await; + keyboard + .up(&key) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.shift_up_delay)).await; + keyboard + .up("Shift") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + Ok(()) +} + +async fn type_shift_symbol( + page: &Page, + keyboard: &Keyboard, + ch: char, + cfg: &HumanConfig, + cdp: Option<&CDPSession>, +) -> Result<()> { + if let Some(cdp) = cdp { + let code = shift_symbol_codes().get(&ch).copied().unwrap_or(""); + let key_code = shift_symbol_keycodes().get(&ch).copied().unwrap_or(0); + + keyboard + .down("Shift") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.shift_down_delay)).await; + + let ch_s = ch.to_string(); + cdp.send( + "Input.dispatchKeyEvent", + Some(json!({ + "type": "keyDown", + "modifiers": 8, + "key": ch_s, + "code": code, + "windowsVirtualKeyCode": key_code, + "text": ch_s, + "unmodifiedText": ch_s, + })), + ) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + + config::sleep_ms(config::rand_range(cfg.key_hold)).await; + + cdp.send( + "Input.dispatchKeyEvent", + Some(json!({ + "type": "keyUp", + "modifiers": 8, + "key": ch_s, + "code": code, + "windowsVirtualKeyCode": key_code, + })), + ) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + + config::sleep_ms(config::rand_range(cfg.shift_up_delay)).await; + keyboard + .up("Shift") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + } else { + // Fallback — detectable but functional. + log::debug("shift-symbol typed without CDP (fallback path)"); + keyboard + .down("Shift") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand_range(cfg.shift_down_delay)).await; + keyboard + .insert_text(&ch.to_string()) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + let _ = page + .evaluate_expression( + &format!( + r#"(() => {{ + const el = document.activeElement; + if (el) {{ + el.dispatchEvent(new KeyboardEvent('keydown', {{ key: {}, bubbles: true }})); + el.dispatchEvent(new KeyboardEvent('keyup', {{ key: {}, bubbles: true }})); + }} + }})()"#, + serde_json::to_string(&ch.to_string()).unwrap_or_else(|_| "\"\"".into()), + serde_json::to_string(&ch.to_string()).unwrap_or_else(|_| "\"\"".into()), + ), + ) + .await; + config::sleep_ms(config::rand_range(cfg.shift_up_delay)).await; + keyboard + .up("Shift") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + } + Ok(()) +} + +async fn inter_char_delay(cfg: &HumanConfig) { + if config::chance(cfg.typing_pause_chance) { + config::sleep_ms(config::rand_range(cfg.typing_pause_range)).await; + } else { + let delay = + cfg.typing_delay + (rand::random::() - 0.5) * 2.0 * cfg.typing_delay_spread; + config::sleep_ms(delay.max(10.0)).await; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn nearby_produces_neighbor() { + for _ in 0..20 { + let n = get_nearby_key('a'); + assert!(nearby_keys()[&'a'].contains(n) || n == 'a'); + } + } + + #[test] + fn shift_set_contains_at() { + assert!(shift_symbols().contains(&'@')); + assert!(!shift_symbols().contains(&'a')); + } +} diff --git a/rust/cloakbrowser/src/human/mod.rs b/rust/cloakbrowser/src/human/mod.rs new file mode 100644 index 00000000..bdf27a4e --- /dev/null +++ b/rust/cloakbrowser/src/human/mod.rs @@ -0,0 +1,38 @@ +//! Human-like behavioral layer (mouse Bezier, typing, scroll). +//! +//! Direct port of Python `cloakbrowser/human/*` and .NET `CloakBrowser.Human/`. +//! +//! Rust cannot monkey-patch Playwright's sealed `Page` API the way Python/JS do. +//! Use [`HumanPage`] for humanized click/fill/type/hover — same approach as .NET's +//! explicit engine. When `humanize=true` at launch, prefer +//! [`crate::CloakBrowser::new_human_page`]. +//! +//! # Example +//! +//! ```no_run +//! use cloakbrowser::{launch, LaunchOptions, human::{HumanPage, HumanConfig}}; +//! +//! #[tokio::main] +//! async fn main() -> cloakbrowser::Result<()> { +//! let browser = launch(LaunchOptions { +//! humanize: true, +//! ..Default::default() +//! }).await?; +//! +//! let mut page = browser.new_human_page().await?; +//! page.goto("https://example.com").await?; +//! page.click("a").await?; +//! browser.close().await?; +//! Ok(()) +//! } +//! ``` + +pub mod config; +pub mod keyboard; +pub mod mouse; +pub mod page; +pub mod scroll; + +pub use config::{HumanConfig, HumanPreset, Range}; +pub use mouse::{BoundingBox, Point}; +pub use page::{HumanActionOptions, HumanPage}; diff --git a/rust/cloakbrowser/src/human/mouse.rs b/rust/cloakbrowser/src/human/mouse.rs new file mode 100644 index 00000000..1bc79d0f --- /dev/null +++ b/rust/cloakbrowser/src/human/mouse.rs @@ -0,0 +1,253 @@ +//! Human-like mouse movement and clicking. +//! Direct port of Python `cloakbrowser/human/mouse.py` / .NET `HumanMouse.cs`. + +use super::config::{self, HumanConfig}; +use crate::error::{Error, Result}; +use playwright_rs::protocol::Mouse; + +/// A 2D point used by the mouse-movement curve math. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Point { + pub x: f64, + pub y: f64, +} + +impl Point { + pub fn new(x: f64, y: f64) -> Self { + Self { x, y } + } +} + +/// Element bounding box in CSS pixels. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct BoundingBox { + pub x: f64, + pub y: f64, + pub width: f64, + pub height: f64, +} + +/// Cubic ease-in-out, matching the Python implementation. +pub fn ease_in_out(t: f64) -> f64 { + if t < 0.5 { + 4.0 * t * t * t + } else { + 1.0 - (-2.0 * t + 2.0).powi(3) / 2.0 + } +} + +/// Cubic Bezier interpolation between four control points. +pub fn bezier(p0: Point, p1: Point, p2: Point, p3: Point, t: f64) -> Point { + let u = 1.0 - t; + let uu = u * u; + let uuu = uu * u; + let tt = t * t; + let ttt = tt * t; + Point { + x: uuu * p0.x + 3.0 * uu * t * p1.x + 3.0 * u * tt * p2.x + ttt * p3.x, + y: uuu * p0.y + 3.0 * uu * t * p1.y + 3.0 * u * tt * p2.y + ttt * p3.y, + } +} + +fn random_control_points(start: Point, end: Point) -> (Point, Point) { + let dx = end.x - start.x; + let dy = end.y - start.y; + let mut dist = (dx * dx + dy * dy).sqrt(); + if dist == 0.0 { + dist = 1.0; + } + let px = -dy / dist; + let py = dx / dist; + let bias1 = config::rand(-0.3, 0.3) * dist; + let bias2 = config::rand(-0.3, 0.3) * dist; + ( + Point::new( + start.x + dx * 0.25 + px * bias1, + start.y + dy * 0.25 + py * bias1, + ), + Point::new( + start.x + dx * 0.75 + px * bias2, + start.y + dy * 0.75 + py * bias2, + ), + ) +} + +async fn move_xy(mouse: &Mouse, x: f64, y: f64) -> Result<()> { + mouse + .move_to(x.round() as i32, y.round() as i32, None) + .await + .map_err(|e| Error::Playwright(e.to_string())) +} + +/// Move the cursor along a human-like Bezier curve with wobble, burst pauses, +/// and an optional overshoot. +pub async fn human_move( + mouse: &Mouse, + start_x: f64, + start_y: f64, + end_x: f64, + end_y: f64, + cfg: &HumanConfig, +) -> Result<()> { + let dist = ((end_x - start_x).powi(2) + (end_y - start_y).powi(2)).sqrt(); + if dist < 1.0 { + return Ok(()); + } + + let steps = (dist / cfg.mouse_steps_divisor) + .round() + .clamp(cfg.mouse_min_steps as f64, cfg.mouse_max_steps as f64) as i32; + let start = Point::new(start_x, start_y); + let end = Point::new(end_x, end_y); + let (cp1, cp2) = random_control_points(start, end); + + let mut burst_counter = 0; + let burst_size = config::rand_int_range(cfg.mouse_burst_size); + + for i in 0..=steps { + let progress = i as f64 / steps as f64; + let eased_t = ease_in_out(progress); + let pt = bezier(start, cp1, cp2, end, eased_t); + + let wobble_amp = (std::f64::consts::PI * progress).sin() * cfg.mouse_wobble_max; + let wx = pt.x + (rand::random::() - 0.5) * 2.0 * wobble_amp; + let wy = pt.y + (rand::random::() - 0.5) * 2.0 * wobble_amp; + + move_xy(mouse, wx, wy).await?; + + burst_counter += 1; + if burst_counter >= burst_size && i < steps { + config::sleep_ms(config::rand_range(cfg.mouse_burst_pause)).await; + burst_counter = 0; + } + } + + if config::chance(cfg.mouse_overshoot_chance) { + let overshoot_dist = config::rand_range(cfg.mouse_overshoot_px); + let angle = (end_y - start_y).atan2(end_x - start_x); + move_xy( + mouse, + end_x + angle.cos() * overshoot_dist, + end_y + angle.sin() * overshoot_dist, + ) + .await?; + config::sleep_ms(config::rand(30.0, 70.0)).await; + move_xy( + mouse, + end_x + (rand::random::() - 0.5) * 4.0, + end_y + (rand::random::() - 0.5) * 4.0, + ) + .await?; + } + + Ok(()) +} + +/// Compute a randomized click point inside a bounding box. +pub fn click_target(box_: BoundingBox, is_input: bool, cfg: &HumanConfig) -> Point { + let (x_frac, y_frac) = if is_input { + ( + config::rand_range(cfg.click_input_x_range), + config::rand(0.30, 0.70), + ) + } else { + (config::rand(0.35, 0.65), config::rand(0.35, 0.65)) + }; + Point::new( + (box_.x + box_.width * x_frac).round(), + (box_.y + box_.height * y_frac).round(), + ) +} + +/// Perform a human-like press: aim delay, mouse down, hold, mouse up. +pub async fn human_click(mouse: &Mouse, is_input: bool, cfg: &HumanConfig) -> Result<()> { + let aim_delay = if is_input { + config::rand_range(cfg.click_aim_delay_input) + } else { + config::rand_range(cfg.click_aim_delay_button) + }; + config::sleep_ms(aim_delay).await; + + let hold_time = if is_input { + config::rand_range(cfg.click_hold_input) + } else { + config::rand_range(cfg.click_hold_button) + }; + + mouse + .down(None) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(hold_time).await; + mouse + .up(None) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + Ok(()) +} + +/// Drift the cursor with tiny random movements for ~`seconds` seconds. +pub async fn human_idle( + mouse: &Mouse, + seconds: f64, + mut cx: f64, + mut cy: f64, + cfg: &HumanConfig, +) -> Result<(f64, f64)> { + let end = std::time::Instant::now() + std::time::Duration::from_secs_f64(seconds); + while std::time::Instant::now() < end { + cx += (rand::random::() - 0.5) * 2.0 * cfg.idle_drift_px; + cy += (rand::random::() - 0.5) * 2.0 * cfg.idle_drift_px; + move_xy(mouse, cx, cy).await?; + config::sleep_ms(config::rand_range(cfg.idle_pause_range)).await; + } + Ok((cx, cy)) +} + +/// Wheel helper used by scroll (and direct wheel humanization). +pub async fn wheel(mouse: &Mouse, delta_x: i32, delta_y: i32) -> Result<()> { + mouse + .wheel(delta_x, delta_y) + .await + .map_err(|e| Error::Playwright(e.to_string())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ease_endpoints() { + assert!((ease_in_out(0.0) - 0.0).abs() < 1e-9); + assert!((ease_in_out(1.0) - 1.0).abs() < 1e-9); + assert!(ease_in_out(0.5) > 0.4 && ease_in_out(0.5) < 0.6); + } + + #[test] + fn bezier_endpoints() { + let p0 = Point::new(0.0, 0.0); + let p1 = Point::new(10.0, 20.0); + let p2 = Point::new(30.0, 40.0); + let p3 = Point::new(100.0, 50.0); + let a = bezier(p0, p1, p2, p3, 0.0); + let b = bezier(p0, p1, p2, p3, 1.0); + assert!((a.x - 0.0).abs() < 1e-9 && (a.y - 0.0).abs() < 1e-9); + assert!((b.x - 100.0).abs() < 1e-9 && (b.y - 50.0).abs() < 1e-9); + } + + #[test] + fn click_target_inside_box() { + let cfg = HumanConfig::default(); + let box_ = BoundingBox { + x: 100.0, + y: 200.0, + width: 80.0, + height: 40.0, + }; + for _ in 0..20 { + let p = click_target(box_, false, &cfg); + assert!(p.x >= box_.x && p.x <= box_.x + box_.width); + assert!(p.y >= box_.y && p.y <= box_.y + box_.height); + } + } +} diff --git a/rust/cloakbrowser/src/human/page.rs b/rust/cloakbrowser/src/human/page.rs new file mode 100644 index 00000000..4cd22149 --- /dev/null +++ b/rust/cloakbrowser/src/human/page.rs @@ -0,0 +1,470 @@ +//! HumanPage — explicit humanized action API over a Playwright Page. +//! +//! Rust (like .NET) cannot monkey-patch Playwright's `Page`. Use this wrapper +//! for humanized click/fill/type/hover/scroll; access the raw page via +//! [`HumanPage::page`] for everything else. +//! +//! Behavioral port of .NET `HumanPage` / Python `patch_page` flows. + +use playwright_rs::protocol::{CDPSession, Page}; + +use super::config::{self, HumanConfig}; +use super::keyboard; +use super::mouse::{self, BoundingBox}; +use super::scroll; +use crate::error::{Error, Result}; +use crate::log; + +/// Options accepted by humanized action methods. +#[derive(Debug, Clone)] +pub struct HumanActionOptions { + /// Overall timeout in milliseconds (default 30000). + pub timeout: f64, + /// Skip motion guarantees / soft checks when true. + pub force: bool, +} + +impl Default for HumanActionOptions { + fn default() -> Self { + Self { + timeout: 30000.0, + force: false, + } + } +} + +impl HumanActionOptions { + pub fn new() -> Self { + Self::default() + } + + pub fn timeout(mut self, ms: f64) -> Self { + self.timeout = ms; + self + } + + pub fn force(mut self, v: bool) -> Self { + self.force = v; + self + } +} + +/// Cursor state tracked across humanized actions. +#[derive(Debug, Clone)] +struct CursorState { + x: f64, + y: f64, + initialized: bool, +} + +/// A human-like wrapper around a Playwright [`Page`]. +pub struct HumanPage { + page: Page, + cfg: HumanConfig, + cursor: CursorState, + cdp: Option, +} + +impl HumanPage { + /// Create a humanized page (initializes CDP stealth path + cursor when possible). + pub async fn create(page: Page, cfg: HumanConfig) -> Result { + let cdp = match page.context() { + Ok(ctx) => match ctx.new_cdp_session(&page).await { + Ok(s) => Some(s), + Err(e) => { + log::debug(format!( + "Could not create CDP session - stealth keyboard disabled: {e}" + )); + None + } + }, + Err(e) => { + log::debug(format!("Could not get page context for CDP: {e}")); + None + } + }; + + let mut hp = Self { + page, + cfg, + cursor: CursorState { + x: 0.0, + y: 0.0, + initialized: false, + }, + cdp, + }; + hp.init_cursor().await?; + Ok(hp) + } + + /// Create with the default humanize config. + pub async fn create_default(page: Page) -> Result { + Self::create(page, HumanConfig::default()).await + } + + /// The underlying real Playwright page. + pub fn page(&self) -> &Page { + &self.page + } + + /// Mutable access to the underlying page. + pub fn page_mut(&mut self) -> &mut Page { + &mut self.page + } + + /// The resolved behavior configuration. + pub fn config(&self) -> &HumanConfig { + &self.cfg + } + + /// Current virtual cursor position `(x, y)`. + pub fn cursor(&self) -> (f64, f64) { + (self.cursor.x, self.cursor.y) + } + + async fn init_cursor(&mut self) -> Result<()> { + self.cursor.x = config::rand_range(self.cfg.initial_cursor_x); + self.cursor.y = config::rand_range(self.cfg.initial_cursor_y); + let mouse = self.page.mouse(); + // Best-effort — viewport may not be ready. + if mouse + .move_to( + self.cursor.x.round() as i32, + self.cursor.y.round() as i32, + None, + ) + .await + .is_ok() + { + self.cursor.initialized = true; + } + Ok(()) + } + + async fn ensure_cursor(&mut self) -> Result<()> { + if !self.cursor.initialized { + self.init_cursor().await?; + } + Ok(()) + } + + async fn maybe_idle(&mut self) -> Result<()> { + if self.cfg.idle_between_actions { + let secs = config::rand( + self.cfg.idle_between_duration.min, + self.cfg.idle_between_duration.max, + ); + let mouse = self.page.mouse(); + let (x, y) = mouse::human_idle( + &mouse, + secs, + self.cursor.x, + self.cursor.y, + &self.cfg, + ) + .await?; + self.cursor.x = x; + self.cursor.y = y; + } + Ok(()) + } + + async fn get_box(&self, selector: &str) -> Result> { + scroll::get_element_box(&self.page, selector).await + } + + async fn wait_for_box(&self, selector: &str, timeout_ms: f64) -> Result { + let deadline = std::time::Instant::now() + + std::time::Duration::from_millis(timeout_ms.max(1.0) as u64); + loop { + if let Some(b) = self.get_box(selector).await? { + return Ok(b); + } + if std::time::Instant::now() >= deadline { + return Err(Error::msg(format!( + "Timeout {timeout_ms}ms waiting for element: {selector}" + ))); + } + config::sleep_ms(50.0).await; + } + } + + async fn is_input_element(&self, selector: &str) -> bool { + let sel_json = serde_json::to_string(selector).unwrap_or_else(|_| "\"\"".into()); + let expr = format!( + r#"(() => {{ + const el = document.querySelector({sel_json}); + if (!el) return 'false'; + const tag = el.tagName.toLowerCase(); + return (tag === 'input' || tag === 'textarea' + || el.getAttribute('contenteditable') === 'true') ? 'true' : 'false'; + }})()"# + ); + match self.page.evaluate_value(&expr).await { + Ok(v) => v.contains("true"), + Err(_) => false, + } + } + + /// Navigate and keep the humanize session (CDP may need re-bind after nav). + pub async fn goto(&mut self, url: &str) -> Result<()> { + self.page + .goto(url, None) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + // Re-init CDP after navigation (session can go stale). + self.cdp = match self.page.context() { + Ok(ctx) => ctx.new_cdp_session(&self.page).await.ok(), + Err(_) => None, + }; + self.cursor.initialized = false; + self.ensure_cursor().await?; + Ok(()) + } + + /// Human-like click on `selector`. + pub async fn click(&mut self, selector: &str) -> Result<()> { + self.click_with(selector, HumanActionOptions::default()).await + } + + /// Human-like click with options. + pub async fn click_with( + &mut self, + selector: &str, + options: HumanActionOptions, + ) -> Result<()> { + self.ensure_cursor().await?; + self.maybe_idle().await?; + + if !options.force { + let _ = self.wait_for_box(selector, options.timeout).await?; + } + + let mouse = self.page.mouse(); + let sel = selector.to_string(); + let page = self.page.clone(); + let scroll = scroll::human_scroll_into_view( + &self.page, + &mouse, + || { + let page = page.clone(); + let sel = sel.clone(); + async move { scroll::get_element_box(&page, &sel).await } + }, + self.cursor.x, + self.cursor.y, + &self.cfg, + ) + .await?; + self.cursor.x = scroll.cursor_x; + self.cursor.y = scroll.cursor_y; + let mut box_ = scroll.box_; + + if !options.force && scroll.did_scroll { + config::sleep_ms(config::rand(50.0, 120.0)).await; + if let Some(b) = self.get_box(selector).await? { + box_ = b; + } + } + + let is_input = self.is_input_element(selector).await; + let target = mouse::click_target(box_, is_input, &self.cfg); + let mouse = self.page.mouse(); + mouse::human_move( + &mouse, + self.cursor.x, + self.cursor.y, + target.x, + target.y, + &self.cfg, + ) + .await?; + self.cursor.x = target.x; + self.cursor.y = target.y; + mouse::human_click(&mouse, is_input, &self.cfg).await?; + Ok(()) + } + + /// Human-like hover over `selector`. + pub async fn hover(&mut self, selector: &str) -> Result<()> { + self.hover_with(selector, HumanActionOptions::default()).await + } + + /// Human-like hover with options. + pub async fn hover_with( + &mut self, + selector: &str, + options: HumanActionOptions, + ) -> Result<()> { + self.ensure_cursor().await?; + self.maybe_idle().await?; + + if !options.force { + let _ = self.wait_for_box(selector, options.timeout).await?; + } + + let mouse = self.page.mouse(); + let sel = selector.to_string(); + let page = self.page.clone(); + let scroll = scroll::human_scroll_into_view( + &self.page, + &mouse, + || { + let page = page.clone(); + let sel = sel.clone(); + async move { scroll::get_element_box(&page, &sel).await } + }, + self.cursor.x, + self.cursor.y, + &self.cfg, + ) + .await?; + self.cursor.x = scroll.cursor_x; + self.cursor.y = scroll.cursor_y; + let mut box_ = scroll.box_; + + if !options.force && scroll.did_scroll { + if let Some(b) = self.get_box(selector).await? { + box_ = b; + } + } + + let target = mouse::click_target(box_, false, &self.cfg); + let mouse = self.page.mouse(); + mouse::human_move( + &mouse, + self.cursor.x, + self.cursor.y, + target.x, + target.y, + &self.cfg, + ) + .await?; + self.cursor.x = target.x; + self.cursor.y = target.y; + Ok(()) + } + + /// Human-like typing into `selector` (appends to existing value). + pub async fn type_text(&mut self, selector: &str, text: &str) -> Result<()> { + self.type_text_with(selector, text, HumanActionOptions::default()) + .await + } + + /// Human-like type with options. + pub async fn type_text_with( + &mut self, + selector: &str, + text: &str, + options: HumanActionOptions, + ) -> Result<()> { + if !options.force { + let _ = self.wait_for_box(selector, options.timeout).await?; + } + config::sleep_ms(config::rand_range(self.cfg.field_switch_delay)).await; + self.click_with( + selector, + HumanActionOptions { + timeout: options.timeout, + force: options.force, + }, + ) + .await?; + config::sleep_ms(config::rand(100.0, 250.0)).await; + + let keyboard = self.page.keyboard(); + keyboard::human_type( + &self.page, + &keyboard, + text, + &self.cfg, + self.cdp.as_ref(), + ) + .await?; + Ok(()) + } + + /// Human-like fill of `selector` (select-all, delete, then type). + pub async fn fill(&mut self, selector: &str, value: &str) -> Result<()> { + self.fill_with(selector, value, HumanActionOptions::default()) + .await + } + + /// Human-like fill with options. + pub async fn fill_with( + &mut self, + selector: &str, + value: &str, + options: HumanActionOptions, + ) -> Result<()> { + if !options.force { + let _ = self.wait_for_box(selector, options.timeout).await?; + } + config::sleep_ms(config::rand_range(self.cfg.field_switch_delay)).await; + self.click_with( + selector, + HumanActionOptions { + timeout: options.timeout, + force: options.force, + }, + ) + .await?; + config::sleep_ms(config::rand(100.0, 250.0)).await; + + let keyboard = self.page.keyboard(); + let select_all = if cfg!(target_os = "macos") { + "Meta+a" + } else { + "Control+a" + }; + keyboard + .press(select_all, None) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand(30.0, 80.0)).await; + keyboard + .press("Backspace", None) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + config::sleep_ms(config::rand(50.0, 150.0)).await; + + keyboard::human_type( + &self.page, + &keyboard, + value, + &self.cfg, + self.cdp.as_ref(), + ) + .await?; + Ok(()) + } + + /// Human-like scroll until `selector` is in the target viewport zone. + pub async fn scroll_to(&mut self, selector: &str) -> Result<()> { + self.ensure_cursor().await?; + let mouse = self.page.mouse(); + let result = scroll::scroll_to_element( + &self.page, + &mouse, + selector, + self.cursor.x, + self.cursor.y, + &self.cfg, + ) + .await?; + self.cursor.x = result.cursor_x; + self.cursor.y = result.cursor_y; + Ok(()) + } + + /// Move the humanized cursor to absolute coordinates (Bezier path). + pub async fn move_to(&mut self, x: f64, y: f64) -> Result<()> { + self.ensure_cursor().await?; + let mouse = self.page.mouse(); + mouse::human_move(&mouse, self.cursor.x, self.cursor.y, x, y, &self.cfg).await?; + self.cursor.x = x; + self.cursor.y = y; + Ok(()) + } +} diff --git a/rust/cloakbrowser/src/human/scroll.rs b/rust/cloakbrowser/src/human/scroll.rs new file mode 100644 index 00000000..80adf940 --- /dev/null +++ b/rust/cloakbrowser/src/human/scroll.rs @@ -0,0 +1,237 @@ +//! Human-like scrolling via mouse wheel events. +//! Direct port of Python `cloakbrowser/human/scroll.py` / .NET `HumanScroll.cs`. + +use playwright_rs::protocol::{Mouse, Page}; +use serde::Deserialize; + +use super::config::{self, HumanConfig}; +use super::mouse::{self, BoundingBox}; +use crate::error::{Error, Result}; + +/// Result of a humanized scroll-into-view operation. +#[derive(Debug, Clone)] +pub struct ScrollResult { + pub box_: BoundingBox, + pub cursor_x: f64, + pub cursor_y: f64, + pub did_scroll: bool, +} + +fn is_in_viewport(bounds: BoundingBox, viewport_height: f64, cfg: &HumanConfig) -> bool { + let top_edge = bounds.y; + let bottom_edge = bounds.y + bounds.height; + let zone_top = viewport_height * cfg.scroll_target_zone.min; + let zone_bottom = viewport_height * cfg.scroll_target_zone.max; + top_edge >= zone_top && bottom_edge <= zone_bottom +} + +/// Send one logical scroll as a burst of small wheel events. +async fn smooth_wheel(mouse: &Mouse, delta: i32, _cfg: &HumanConfig) -> Result<()> { + let abs_d = delta.unsigned_abs() as f64; + let sign = if delta > 0 { 1 } else { -1 }; + let mut sent = 0.0; + while sent < abs_d { + let step_size = config::rand(20.0, 40.0); + let chunk = step_size.min(abs_d - sent); + mouse::wheel(mouse, 0, (chunk.round() as i32) * sign).await?; + sent += chunk; + config::sleep_ms(config::rand(8.0, 20.0)).await; + } + Ok(()) +} + +#[derive(Deserialize)] +struct WindowSize { + width: f64, + height: f64, +} + +async fn resolve_viewport(page: &Page) -> Result<(f64, f64)> { + if let Some(vp) = page.viewport_size() { + return Ok((vp.width as f64, vp.height as f64)); + } + // Headed / no_viewport: live window dimensions. + let raw = page + .evaluate_value("() => JSON.stringify({ width: window.innerWidth, height: window.innerHeight })") + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + let size: WindowSize = serde_json::from_str(&raw) + .map_err(|e| Error::msg(format!("Failed to parse viewport: {e}")))?; + if size.height == 0.0 { + return Err(Error::msg("Viewport size not available")); + } + Ok((size.width, size.height)) +} + +/// Humanized scroll-into-view using a box provider (selector or handle based). +pub async fn human_scroll_into_view( + page: &Page, + mouse: &Mouse, + mut get_box: F, + mut cursor_x: f64, + mut cursor_y: f64, + cfg: &HumanConfig, +) -> Result +where + F: FnMut() -> Fut, + Fut: std::future::Future>>, +{ + let (viewport_width, viewport_height) = resolve_viewport(page).await?; + + let mut box_ = get_box() + .await? + .ok_or_else(|| Error::msg("Element not found while scrolling into view"))?; + + if is_in_viewport(box_, viewport_height, cfg) { + return Ok(ScrollResult { + box_, + cursor_x, + cursor_y, + did_scroll: false, + }); + } + + // Move cursor into scroll area. + let scroll_area_x = (viewport_width * config::rand(0.3, 0.7)).round(); + let scroll_area_y = (viewport_height * config::rand(0.3, 0.7)).round(); + mouse::human_move(mouse, cursor_x, cursor_y, scroll_area_x, scroll_area_y, cfg).await?; + cursor_x = scroll_area_x; + cursor_y = scroll_area_y; + config::sleep_ms(config::rand_range(cfg.scroll_pre_move_delay)).await; + + let target_y = + viewport_height * config::rand(cfg.scroll_target_zone.min, cfg.scroll_target_zone.max); + let element_center = box_.y + box_.height / 2.0; + let distance_to_scroll = element_center - target_y; + + let direction = if distance_to_scroll > 0.0 { 1 } else { -1 }; + let abs_distance = distance_to_scroll.abs(); + let avg_delta = (cfg.scroll_delta_base.min + cfg.scroll_delta_base.max) / 2.0; + let total_clicks = ((abs_distance / avg_delta).ceil() as i32).max(3); + let accel_steps = config::rand_int_range(cfg.scroll_accel_steps); + let decel_steps = config::rand_int_range(cfg.scroll_decel_steps); + + let mut scrolled = 0.0; + for i in 0..total_clicks { + let (delta, pause) = if i < accel_steps { + (config::rand(80.0, 100.0), config::rand_range(cfg.scroll_pause_slow)) + } else if i >= total_clicks - decel_steps { + (config::rand(60.0, 90.0), config::rand_range(cfg.scroll_pause_slow)) + } else { + ( + config::rand_range(cfg.scroll_delta_base), + config::rand_range(cfg.scroll_pause_fast), + ) + }; + + let delta = delta * (1.0 + (rand::random::() - 0.5) * 2.0 * cfg.scroll_delta_variance); + let delta_int = (delta.round() as i32) * direction; + + smooth_wheel(mouse, delta_int, cfg).await?; + scrolled += delta_int.unsigned_abs() as f64; + config::sleep_ms(pause).await; + + if i % 3 == 2 || i == total_clicks - 1 { + if let Some(b) = get_box().await? { + box_ = b; + if is_in_viewport(box_, viewport_height, cfg) { + break; + } + } + } + if scrolled >= abs_distance * 1.1 { + break; + } + } + + // Optional overshoot + correction. + if config::chance(cfg.scroll_overshoot_chance) { + let overshoot_px = + (config::rand_range(cfg.scroll_overshoot_px).round() as i32) * direction; + smooth_wheel(mouse, overshoot_px, cfg).await?; + config::sleep_ms(config::rand_range(cfg.scroll_settle_delay)).await; + let corrections = config::rand_int_range(config::Range::new(1.0, 2.0)); + for _ in 0..corrections { + let corr_delta = (config::rand(40.0, 80.0).round() as i32) * -direction; + smooth_wheel(mouse, corr_delta, cfg).await?; + config::sleep_ms(config::rand(100.0, 250.0)).await; + } + } + + config::sleep_ms(config::rand_range(cfg.scroll_settle_delay)).await; + + box_ = get_box() + .await? + .ok_or_else(|| Error::msg("Element lost after scrolling into view"))?; + + Ok(ScrollResult { + box_, + cursor_x, + cursor_y, + did_scroll: true, + }) +} + +/// Selector-based humanized scroll. +pub async fn scroll_to_element( + page: &Page, + mouse: &Mouse, + selector: &str, + cursor_x: f64, + cursor_y: f64, + cfg: &HumanConfig, +) -> Result { + let sel = selector.to_string(); + human_scroll_into_view( + page, + mouse, + || { + let page = page.clone(); + let sel = sel.clone(); + async move { get_element_box(&page, &sel).await } + }, + cursor_x, + cursor_y, + cfg, + ) + .await +} + +/// Locate `selector` and return its bounding box. +pub async fn get_element_box(page: &Page, selector: &str) -> Result> { + let locator = page.locator(selector).await; + match locator.bounding_box().await { + Ok(Some(b)) => Ok(Some(BoundingBox { + x: b.x, + y: b.y, + width: b.width, + height: b.height, + })), + Ok(None) => Ok(None), + Err(_) => Ok(None), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn viewport_zone_check() { + let cfg = HumanConfig::default(); + let box_in = BoundingBox { + x: 0.0, + y: 300.0, + width: 100.0, + height: 40.0, + }; + assert!(is_in_viewport(box_in, 1000.0, &cfg)); + let box_out = BoundingBox { + x: 0.0, + y: 10.0, + width: 100.0, + height: 40.0, + }; + assert!(!is_in_viewport(box_out, 1000.0, &cfg)); + } +} diff --git a/rust/cloakbrowser/src/launch.rs b/rust/cloakbrowser/src/launch.rs new file mode 100644 index 00000000..d94c0b4c --- /dev/null +++ b/rust/cloakbrowser/src/launch.rs @@ -0,0 +1,865 @@ +//! Core browser launch functions for CloakBrowser. +//! Thin wrappers around Playwright that use the patched stealth Chromium binary. +//! Direct port of Python `cloakbrowser/browser.py` / .NET `CloakLauncher.cs`. + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; + +use playwright_rs::api::launch_options::{IgnoreDefaultArgs, LaunchOptions as PwLaunchOptions}; +use playwright_rs::protocol::browser_context::{BrowserContextOptions, Viewport}; +use playwright_rs::protocol::proxy::ProxySettings as PwProxy; +use playwright_rs::{Browser, BrowserContext, Playwright}; + +use crate::config; +use crate::download; +use crate::error::{Error, Result}; +use crate::geoip; +use crate::human::{HumanConfig, HumanPage}; +use crate::license; +use crate::log; +use crate::options::{LaunchContextOptions, LaunchOptions, ViewportOption}; +use crate::proxy::{self, Proxy}; +use crate::widevine; + +/// Handle that owns both the Playwright driver and the launched Browser. +/// +/// Dropping this does **not** auto-close the browser (Playwright has no async Drop). +/// Call [`CloakBrowser::close`] explicitly. +pub struct CloakBrowser { + playwright: Playwright, + browser: Browser, + pub headless: bool, + pub headless_no_viewport: bool, + /// Humanize config when `humanize=true` at launch; `None` otherwise. + human_config: Option, +} + +impl CloakBrowser { + /// Borrow the underlying Playwright `Browser`. + pub fn browser(&self) -> &Browser { + &self.browser + } + + /// Mutable borrow of the underlying Playwright `Browser`. + pub fn browser_mut(&mut self) -> &mut Browser { + &mut self.browser + } + + /// Whether headless can use `no_viewport` on this binary. + pub fn headless_no_viewport(&self) -> bool { + self.headless_no_viewport + } + + /// Whether humanize was requested at launch. + pub fn humanize_enabled(&self) -> bool { + self.human_config.is_some() + } + + /// Resolved humanize config (if `humanize=true` at launch). + pub fn human_config(&self) -> Option<&HumanConfig> { + self.human_config.as_ref() + } + + /// Create a new raw Playwright page. + pub async fn new_page(&self) -> Result { + self.browser + .new_page() + .await + .map_err(|e| Error::Playwright(e.to_string())) + } + + /// Create a new [`HumanPage`] with the launch humanize config (or default config + /// if humanize was not enabled at launch). + /// + /// Prefer this over raw Playwright click/fill when you want human-like motion. + pub async fn new_human_page(&self) -> Result { + let page = self.new_page().await?; + let cfg = self + .human_config + .clone() + .unwrap_or_default(); + HumanPage::create(page, cfg).await + } + + /// Wrap an existing Playwright page with humanize (uses launch config or default). + pub async fn wrap_page(&self, page: playwright_rs::Page) -> Result { + let cfg = self.human_config.clone().unwrap_or_default(); + HumanPage::create(page, cfg).await + } + + /// Close the browser and stop the Playwright driver. + pub async fn close(self) -> Result<()> { + if let Err(e) = self.browser.close().await { + log::warning(format!("browser.close failed: {e}")); + } + // Playwright driver cleanup happens when `playwright` drops. + let _ = self.playwright; + Ok(()) + } +} + +/// A launched stealth browser context (persistent profile or standalone context). +/// +/// Owns the Playwright driver. For persistent contexts there is no separate +/// `Browser` handle — closing the context closes Chromium. +pub struct CloakContext { + playwright: Playwright, + context: BrowserContext, + human_config: Option, + /// Profile directory when launched via `launch_persistent_context`. + user_data_dir: Option, +} + +impl CloakContext { + /// The underlying Playwright `BrowserContext`. + pub fn context(&self) -> &BrowserContext { + &self.context + } + + /// Mutable access to the context. + pub fn context_mut(&mut self) -> &mut BrowserContext { + &mut self.context + } + + /// Profile directory (persistent launches only). + pub fn user_data_dir(&self) -> Option<&Path> { + self.user_data_dir.as_deref() + } + + /// Whether humanize was requested at launch. + pub fn humanize_enabled(&self) -> bool { + self.human_config.is_some() + } + + /// Create a new raw Playwright page on this context. + pub async fn new_page(&self) -> Result { + self.context + .new_page() + .await + .map_err(|e| Error::Playwright(e.to_string())) + } + + /// Create a humanized page (uses launch humanize config or default). + pub async fn new_human_page(&self) -> Result { + let page = self.new_page().await?; + let cfg = self.human_config.clone().unwrap_or_default(); + HumanPage::create(page, cfg).await + } + + /// Wrap an existing page with humanize. + pub async fn wrap_page(&self, page: playwright_rs::Page) -> Result { + let cfg = self.human_config.clone().unwrap_or_default(); + HumanPage::create(page, cfg).await + } + + /// Close the context (and browser process for persistent launches) and stop Playwright. + pub async fn close(self) -> Result<()> { + if let Err(e) = self.context.close().await { + log::warning(format!("context.close failed: {e}")); + } + let _ = self.playwright; + Ok(()) + } +} + +/// Launch a stealth Chromium browser. +/// +/// Returns a [`CloakBrowser`] wrapping a standard Playwright `Browser`. +/// +/// # Example +/// +/// ```no_run +/// use cloakbrowser::{launch, LaunchOptions}; +/// +/// #[tokio::main] +/// async fn main() -> cloakbrowser::Result<()> { +/// let browser = launch(LaunchOptions::default()).await?; +/// let page = browser.new_page().await?; +/// page.goto("https://example.com", None).await.ok(); +/// browser.close().await?; +/// Ok(()) +/// } +/// ``` +pub async fn launch(options: LaunchOptions) -> Result { + let human_config = if options.humanize { + Some(HumanConfig::resolve(options.human_preset)) + } else { + None + }; + + let binary_path = download::ensure_binary( + options.license_key.as_deref(), + options.browser_version.as_deref(), + ) + .await?; + + let (timezone, locale, exit_ip) = geoip::maybe_resolve_geoip( + options.geoip, + options.proxy.as_ref(), + options.timezone.clone(), + options.locale.clone(), + ) + .await; + + let proxy_resolution = proxy::resolve( + options.proxy.as_ref(), + options.browser_version.as_deref(), + options.license_key.as_deref(), + )?; + + let mut args = resolve_webrtc_args(options.args.clone(), options.proxy.as_ref()).await; + args = append_webrtc_exit_ip(args, exit_ip.as_deref()); + + let mut combined: Vec = args.unwrap_or_default(); + combined.extend(proxy_resolution.extra_args); + + let start_maximized = config::binary_supports_maximized_window( + options.license_key.as_deref(), + options.browser_version.as_deref(), + ) && !options.suppress_maximize; + + let chrome_args = build_args( + options.stealth_args, + Some(combined), + timezone.as_deref(), + locale.as_deref(), + options.headless, + options.extension_paths.as_deref(), + start_maximized, + ); + + maybe_warn_windows_fonts(&chrome_args); + + log::debug(format!( + "Launching stealth Chromium (headless={}, args={})", + options.headless, + chrome_args.len() + )); + + let launch_env = + license::build_launch_env(options.license_key.as_deref(), options.env.as_ref()); + + let mut pw_opts = PwLaunchOptions::default() + .executable_path(binary_path.display().to_string()) + .headless(options.headless) + .args(chrome_args) + .ignore_default_args(IgnoreDefaultArgs::Array( + config::IGNORE_DEFAULT_ARGS + .iter() + .map(|s| s.to_string()) + .collect(), + )); + + if let Some(env) = launch_env { + pw_opts = pw_opts.env(env); + } + + if let Some(ps) = proxy_resolution.playwright_proxy { + let mut pw_proxy = PwProxy::new(ps.server); + if let Some(b) = ps.bypass { + pw_proxy = pw_proxy.bypass(b); + } + if let Some(u) = ps.username { + pw_proxy = pw_proxy.username(u); + } + if let Some(p) = ps.password { + pw_proxy = pw_proxy.password(p); + } + pw_opts = pw_opts.proxy(pw_proxy); + } + + let playwright = Playwright::launch() + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + + let browser = playwright + .chromium() + .launch_with_options(pw_opts) + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + + let headless_no_viewport = config::binary_supports_headless_no_viewport( + options.license_key.as_deref(), + options.browser_version.as_deref(), + ); + + Ok(CloakBrowser { + playwright, + browser, + headless: options.headless, + headless_no_viewport, + human_config, + }) +} + +/// Convenience: launch with default options. +pub async fn launch_default() -> Result { + launch(LaunchOptions::default()).await +} + +/// Launch stealth Chromium with a **persistent profile** and return a +/// [`CloakContext`]. +/// +/// Persists cookies, localStorage, cache, and other browser state in +/// `user_data_dir`. Reuse the same path across sessions to restore state. +/// Also avoids incognito-mode detection on some fingerprint scanners. +/// +/// Timezone/locale go through binary fingerprint flags (not CDP emulation). +/// +/// # Example +/// +/// ```no_run +/// use cloakbrowser::{launch_persistent_context, LaunchContextOptions}; +/// +/// #[tokio::main] +/// async fn main() -> cloakbrowser::Result<()> { +/// let ctx = launch_persistent_context( +/// "./my-profile", +/// LaunchContextOptions { +/// headless: false, +/// humanize: true, +/// ..Default::default() +/// }, +/// ) +/// .await?; +/// +/// let mut page = ctx.new_human_page().await?; +/// page.goto("https://example.com").await?; +/// ctx.close().await?; +/// Ok(()) +/// } +/// ``` +pub async fn launch_persistent_context( + user_data_dir: impl AsRef, + options: LaunchContextOptions, +) -> Result { + let user_data_dir = user_data_dir.as_ref().to_path_buf(); + std::fs::create_dir_all(&user_data_dir)?; + + let human_config = if options.humanize { + Some(HumanConfig::resolve(options.human_preset)) + } else { + None + }; + + let binary_path = download::ensure_binary( + options.license_key.as_deref(), + options.browser_version.as_deref(), + ) + .await?; + + let (timezone, locale, exit_ip) = geoip::maybe_resolve_geoip( + options.geoip, + options.proxy.as_ref(), + options.timezone.clone(), + options.locale.clone(), + ) + .await; + + let proxy_resolution = proxy::resolve( + options.proxy.as_ref(), + options.browser_version.as_deref(), + options.license_key.as_deref(), + )?; + + let mut args = resolve_webrtc_args(options.args.clone(), options.proxy.as_ref()).await; + args = append_webrtc_exit_ip(args, exit_ip.as_deref()); + + let mut combined: Vec = args.unwrap_or_default(); + combined.extend(proxy_resolution.extra_args); + + // Don't auto-maximize when the caller chose a viewport geometry. + let suppress_maximize = matches!( + options.viewport, + ViewportOption::Size { .. } | ViewportOption::NoViewport + ); + let start_maximized = config::binary_supports_maximized_window( + options.license_key.as_deref(), + options.browser_version.as_deref(), + ) && !suppress_maximize; + + let chrome_args = build_args( + options.stealth_args, + Some(combined), + timezone.as_deref(), + locale.as_deref(), + options.headless, + options.extension_paths.as_deref(), + start_maximized, + ); + + maybe_warn_windows_fonts(&chrome_args); + + log::debug(format!( + "Launching persistent stealth Chromium (headless={}, user_data_dir={})", + options.headless, + user_data_dir.display() + )); + + // Seed Widevine CDM hint (Linux-only; no-op elsewhere). + widevine::seed_widevine_hint(&user_data_dir, &binary_path); + + // License: ensure key is visible to the child when it must be injected. + // BrowserContextOptions has no env map; inject into process env when needed. + let _license_guard = LicenseEnvGuard::apply(options.license_key.as_deref()); + + // BrowserContextOptions is #[non_exhaustive] — start from Default and set fields. + let mut ctx_opts = BrowserContextOptions::default(); + ctx_opts.executable_path = Some(binary_path.display().to_string()); + ctx_opts.headless = Some(options.headless); + ctx_opts.args = Some(chrome_args); + ctx_opts.ignore_default_args = Some(IgnoreDefaultArgs::Array( + config::IGNORE_DEFAULT_ARGS + .iter() + .map(|s| s.to_string()) + .collect(), + )); + ctx_opts.user_agent = options.user_agent.clone(); + ctx_opts.color_scheme = options.color_scheme.clone(); + ctx_opts.storage_state_path = options.storage_state_path.clone(); + // locale / timezone via binary flags only — not CDP (detectable). + ctx_opts.locale = None; + ctx_opts.timezone_id = None; + + apply_context_viewport( + &mut ctx_opts, + &options.viewport, + options.headless, + options.license_key.as_deref(), + options.browser_version.as_deref(), + ); + + if let Some(ps) = proxy_resolution.playwright_proxy { + let mut pw_proxy = PwProxy::new(ps.server); + if let Some(b) = ps.bypass { + pw_proxy = pw_proxy.bypass(b); + } + if let Some(u) = ps.username { + pw_proxy = pw_proxy.username(u); + } + if let Some(p) = ps.password { + pw_proxy = pw_proxy.password(p); + } + ctx_opts.proxy = Some(pw_proxy); + } + + let playwright = Playwright::launch() + .await + .map_err(|e| Error::Playwright(e.to_string()))?; + + let context = match playwright + .chromium() + .launch_persistent_context_with_options(user_data_dir.display().to_string(), ctx_opts) + .await + { + Ok(ctx) => ctx, + Err(e) => { + return Err(Error::Playwright(e.to_string())); + } + }; + + Ok(CloakContext { + playwright, + context, + human_config, + user_data_dir: Some(user_data_dir), + }) +} + +/// Resolve viewport / no_viewport for a context launch. +/// +/// Port of Python `_resolve_context_viewport` / .NET `ResolveContextViewport`. +pub fn resolve_context_viewport( + viewport: &ViewportOption, + headless: bool, + license_key: Option<&str>, + browser_version: Option<&str>, +) -> (Option, Option) { + match viewport { + ViewportOption::NoViewport => (None, Some(true)), + ViewportOption::Size { width, height } => ( + Some(Viewport { + width: *width, + height: *height, + }), + None, + ), + ViewportOption::Auto => { + let headless_no_viewport = + headless && config::binary_supports_headless_no_viewport(license_key, browser_version); + if headless && !headless_no_viewport { + ( + Some(Viewport { + width: config::DEFAULT_VIEWPORT_WIDTH as u32, + height: config::DEFAULT_VIEWPORT_HEIGHT as u32, + }), + None, + ) + } else { + (None, Some(true)) + } + } + } +} + +fn apply_context_viewport( + opts: &mut BrowserContextOptions, + viewport: &ViewportOption, + headless: bool, + license_key: Option<&str>, + browser_version: Option<&str>, +) { + let (vp, no_vp) = resolve_context_viewport(viewport, headless, license_key, browser_version); + opts.viewport = vp; + opts.no_viewport = no_vp; +} + +/// Temporarily inject `CLOAKBROWSER_LICENSE_KEY` into the process environment +/// when Playwright can't receive a custom env map (persistent context path). +struct LicenseEnvGuard { + injected: bool, + previous: Option, +} + +impl LicenseEnvGuard { + fn apply(license_key: Option<&str>) -> Self { + let (key, source) = license::resolve_license_key_with_source(license_key); + // Only inject when the binary would not otherwise see the key. + use license::LicenseKeySource; + let needs_inject = matches!( + source, + LicenseKeySource::Param | LicenseKeySource::CustomFile + ); + if !needs_inject { + return Self { + injected: false, + previous: None, + }; + } + let Some(key) = key else { + return Self { + injected: false, + previous: None, + }; + }; + let previous = std::env::var("CLOAKBROWSER_LICENSE_KEY").ok(); + // SAFETY: single-threaded at launch; restores on drop. + unsafe { + std::env::set_var("CLOAKBROWSER_LICENSE_KEY", &key); + } + Self { + injected: true, + previous, + } + } +} + +impl Drop for LicenseEnvGuard { + fn drop(&mut self) { + if !self.injected { + return; + } + unsafe { + match &self.previous { + Some(v) => std::env::set_var("CLOAKBROWSER_LICENSE_KEY", v), + None => std::env::remove_var("CLOAKBROWSER_LICENSE_KEY"), + } + } + } +} + +/// Combine stealth args with user-provided args and locale flags. +/// +/// Deduplicates by flag key (everything before `=`). +/// Priority: stealth defaults < user args < dedicated params (timezone/locale). +pub fn build_args( + stealth_args: bool, + extra_args: Option>, + timezone: Option<&str>, + locale: Option<&str>, + headless: bool, + extension_paths: Option<&[String]>, + start_maximized: bool, +) -> Vec { + let mut seen: HashMap = HashMap::new(); + + if stealth_args { + for arg in config::get_default_stealth_args() { + let key = arg.split('=').next().unwrap_or(&arg).to_string(); + seen.insert(key, arg); + } + } + + // GPU blocklist bypass: + // - Headed mode (all platforms) + // - Windows (all modes) + if !headless || cfg!(target_os = "windows") { + seen.insert( + "--ignore-gpu-blocklist".into(), + "--ignore-gpu-blocklist".into(), + ); + } + + if let Some(extra) = extra_args { + for arg in extra { + let key = arg.split('=').next().unwrap_or(&arg).to_string(); + if let Some(old) = seen.get(&key) { + log::debug(format!("Arg override: {old} -> {arg}")); + } + seen.insert(key, arg); + } + } + + if let Some(tz) = timezone { + let key = "--fingerprint-timezone"; + let flag = format!("{key}={tz}"); + if let Some(old) = seen.get(key) { + log::debug(format!("Arg override: {old} -> {flag}")); + } + seen.insert(key.into(), flag); + } + + if let Some(loc) = locale { + for key in ["--lang", "--fingerprint-locale"] { + let flag = format!("{key}={loc}"); + if let Some(old) = seen.get(key) { + log::debug(format!("Arg override: {old} -> {flag}")); + } + seen.insert(key.into(), flag); + } + } + + if let Some(paths) = extension_paths { + if !paths.is_empty() { + let abs: Vec = paths + .iter() + .map(|p| { + PathBuf::from(p) + .canonicalize() + .unwrap_or_else(|_| PathBuf::from(p)) + .display() + .to_string() + }) + .collect(); + let ext_val = abs.join(","); + seen.insert( + "--load-extension".into(), + format!("--load-extension={ext_val}"), + ); + seen.insert( + "--disable-extensions-except".into(), + format!("--disable-extensions-except={ext_val}"), + ); + } + } + + if start_maximized + && !seen.contains_key("--start-maximized") + && !seen.contains_key("--window-size") + && !seen.contains_key("--window-position") + { + seen.insert("--start-maximized".into(), "--start-maximized".into()); + } + + seen.into_values().collect() +} + +async fn resolve_webrtc_args( + args: Option>, + proxy: Option<&Proxy>, +) -> Option> { + let mut args = args?; + let idx = args.iter().position(|a| a == "--fingerprint-webrtc-ip=auto"); + let Some(idx) = idx else { + return Some(args); + }; + + let proxy_url = proxy::extract_proxy_url(proxy); + let Some(proxy_url) = proxy_url else { + log::warning("--fingerprint-webrtc-ip=auto requires a proxy; removing flag"); + args.remove(idx); + return Some(args); + }; + + match geoip::resolve_proxy_exit_ip(Some(&proxy_url)).await { + Some(exit_ip) => { + args[idx] = format!("--fingerprint-webrtc-ip={exit_ip}"); + } + None => { + log::warning( + "Could not resolve proxy exit IP for WebRTC spoofing; removing --fingerprint-webrtc-ip=auto", + ); + args.remove(idx); + } + } + Some(args) +} + +fn append_webrtc_exit_ip( + args: Option>, + exit_ip: Option<&str>, +) -> Option> { + let Some(exit_ip) = exit_ip else { + return args; + }; + let mut args = args.unwrap_or_default(); + if !args.iter().any(|a| a.starts_with("--fingerprint-webrtc-ip")) { + args.push(format!("--fingerprint-webrtc-ip={exit_ip}")); + } + Some(args) +} + +// --------------------------------------------------------------------------- +// Windows-font mismatch warning (Linux only) +// --------------------------------------------------------------------------- + +const WINDOWS_FONT_TELLS: &[&str] = &[ + "Segoe UI", + "Segoe UI Light", + "Calibri", + "Marlett", + "MS UI Gothic", + "Franklin Gothic", + "Consolas", + "Courier New", +]; + +static FONT_WARNING_CHECKED: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + +fn maybe_warn_windows_fonts(chrome_args: &[String]) { + if !cfg!(target_os = "linux") { + return; + } + if FONT_WARNING_CHECKED.swap(true, std::sync::atomic::Ordering::SeqCst) { + return; + } + // Only warn when spoofing Windows. + let spoofing_windows = chrome_args + .iter() + .any(|a| a == "--fingerprint-platform=windows"); + if !spoofing_windows { + return; + } + + let n = count_fonts_present(WINDOWS_FONT_TELLS); + match n { + Some(n) if n < WINDOWS_FONT_TELLS.len() => { + log::warning(format!( + "Windows platform spoof is active but only {n}/{} Windows fonts are installed. \ + Font fingerprinting anti-bot systems may flag the mismatch. \ + Install fonts (e.g. ttf-mscorefonts-installer) or set --fingerprint-platform=linux.", + WINDOWS_FONT_TELLS.len() + )); + } + _ => {} + } +} + +fn count_fonts_present(tells: &[&str]) -> Option { + let output = std::process::Command::new("fc-list") + .output() + .ok()?; + if !output.status.success() { + return None; + } + let listing = String::from_utf8_lossy(&output.stdout).to_lowercase(); + Some( + tells + .iter() + .filter(|f| listing.contains(&f.to_lowercase())) + .count(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn build_args_stealth_and_timezone() { + let args = build_args( + true, + Some(vec!["--foo=bar".into()]), + Some("America/New_York"), + Some("en-US"), + true, + None, + false, + ); + assert!(args.iter().any(|a| a.starts_with("--fingerprint="))); + assert!(args.iter().any(|a| a == "--fingerprint-timezone=America/New_York")); + assert!(args.iter().any(|a| a == "--lang=en-US")); + assert!(args.iter().any(|a| a == "--fingerprint-locale=en-US")); + assert!(args.iter().any(|a| a == "--foo=bar")); + } + + #[test] + fn build_args_user_overrides_stealth() { + let args = build_args( + true, + Some(vec!["--fingerprint=99999".into()]), + None, + None, + true, + None, + false, + ); + assert!(args.iter().any(|a| a == "--fingerprint=99999")); + assert_eq!( + args.iter() + .filter(|a| a.starts_with("--fingerprint=")) + .count(), + 1 + ); + } + + #[test] + fn append_webrtc_once() { + let args = append_webrtc_exit_ip(None, Some("1.2.3.4")); + assert_eq!( + args.unwrap(), + vec!["--fingerprint-webrtc-ip=1.2.3.4".to_string()] + ); + + let args = append_webrtc_exit_ip( + Some(vec!["--fingerprint-webrtc-ip=9.9.9.9".into()]), + Some("1.2.3.4"), + ); + assert_eq!( + args.unwrap(), + vec!["--fingerprint-webrtc-ip=9.9.9.9".to_string()] + ); + } + + #[test] + fn context_viewport_headed_is_no_viewport() { + let (vp, no) = resolve_context_viewport(&ViewportOption::Auto, false, None, None); + assert!(vp.is_none()); + assert_eq!(no, Some(true)); + } + + #[test] + fn context_viewport_explicit_size() { + let (vp, no) = resolve_context_viewport( + &ViewportOption::Size { + width: 800, + height: 600, + }, + true, + None, + None, + ); + assert!(vp.is_some()); + assert_eq!(vp.unwrap().width, 800); + assert!(no.is_none()); + } + + #[test] + fn context_viewport_no_viewport_flag() { + let (vp, no) = + resolve_context_viewport(&ViewportOption::NoViewport, true, None, None); + assert!(vp.is_none()); + assert_eq!(no, Some(true)); + } +} diff --git a/rust/cloakbrowser/src/lib.rs b/rust/cloakbrowser/src/lib.rs new file mode 100644 index 00000000..b0298b9f --- /dev/null +++ b/rust/cloakbrowser/src/lib.rs @@ -0,0 +1,76 @@ +//! # CloakBrowser for Rust +//! +//! Stealth Chromium that passes bot-detection tests. Thin wrapper around a +//! closed-source, source-level patched Chromium binary, driven via +//! [`playwright-rs`](https://crates.io/crates/playwright-rs). +//! +//! Direct port of the Python / JavaScript / .NET wrappers in this monorepo. +//! +//! ## Quick start +//! +//! ```no_run +//! use cloakbrowser::{launch, LaunchOptions}; +//! +//! #[tokio::main] +//! async fn main() -> cloakbrowser::Result<()> { +//! let browser = launch(LaunchOptions { +//! headless: true, +//! ..Default::default() +//! }).await?; +//! +//! let page = browser.new_page().await?; +//! let _ = page.goto("https://example.com", None).await; +//! println!("{}", page.title().await.unwrap_or_default()); +//! browser.close().await?; +//! Ok(()) +//! } +//! ``` +//! +//! ## Modules +//! +//! | Module | Python source | +//! |--------|---------------| +//! | [`config`] | `cloakbrowser/config.py` | +//! | [`download`] | `cloakbrowser/download.py` | +//! | [`license`] | `cloakbrowser/license.py` | +//! | [`proxy`] | proxy helpers in `browser.py` | +//! | [`geoip`] | `cloakbrowser/geoip.py` | +//! | [`launch`] | `cloakbrowser/browser.py` | +//! | [`human`] | `cloakbrowser/human/*` | + +// Prefer working parity with Python/.NET over exhaustive rustdoc on every field. +#![allow(missing_docs)] + +pub mod config; +pub mod diagnostics; +pub mod download; +pub mod error; +pub mod geoip; +pub mod human; +pub mod launch; +pub mod license; +pub mod log; +pub mod options; +pub mod proxy; +pub mod version; +pub mod widevine; + +pub use config::{ + binary_supports_headless_no_viewport, binary_supports_http_proxy_inline_auth, + binary_supports_maximized_window, get_binary_path, get_cache_dir, get_chromium_version, + get_default_stealth_args, get_platform_tag, CHROMIUM_VERSION, IGNORE_DEFAULT_ARGS, +}; +pub use download::{binary_info, check_for_update, clear_cache, ensure_binary, BinaryInfo}; +pub use error::{Error, Result}; +pub use human::{HumanActionOptions, HumanConfig, HumanPage, HumanPreset}; +pub use launch::{ + build_args, launch, launch_default, launch_persistent_context, resolve_context_viewport, + CloakBrowser, CloakContext, +}; +pub use license::{resolve_license_key, validate_license, LicenseInfo}; +pub use options::{LaunchContextOptions, LaunchOptions, ViewportOption}; +pub use proxy::{Proxy, ProxySettings}; +pub use version::VERSION; + +// Re-export playwright types users need for day-to-day scripting. +pub use playwright_rs::{Browser, BrowserContext, Page, Playwright}; diff --git a/rust/cloakbrowser/src/license.rs b/rust/cloakbrowser/src/license.rs new file mode 100644 index 00000000..13b8e58a --- /dev/null +++ b/rust/cloakbrowser/src/license.rs @@ -0,0 +1,361 @@ +//! License validation and caching for CloakBrowser Pro. +//! Direct port of Python `cloakbrowser/license.py` / .NET `License.cs`. + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +use crate::config; +use crate::error::{Error, Result}; +use crate::log; +use crate::version::VERSION; + +pub const VALIDATE_URL: &str = "https://cloakbrowser.dev/api/license/validate"; +pub const PRO_VERSION_URL: &str = "https://cloakbrowser.dev/api/download/version"; + +const LICENSE_CACHE_TTL: f64 = 86400.0; // 24 hours +const PRO_VERSION_CHECK_INTERVAL: f64 = 3600.0; // 1 hour + +/// Result of a CloakBrowser Pro license validation. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct LicenseInfo { + pub valid: bool, + pub plan: String, + pub expires: Option, +} + +/// Source of a resolved license key — determines env injection into the child process. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LicenseKeySource { + /// Explicit `license_key` param. + Param, + /// `CLOAKBROWSER_LICENSE_KEY` env var. + Env, + /// Default `~/.cloakbrowser/license.key` (binary reads it directly). + DefaultFile, + /// Custom cache dir `license.key` (binary can't see it). + CustomFile, + /// No key resolved. + None, +} + +fn now_unix() -> f64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.as_secs_f64()) + .unwrap_or(0.0) +} + +fn sha256_hex(s: &str) -> String { + let mut hasher = Sha256::new(); + hasher.update(s.as_bytes()); + hex::encode(hasher.finalize()) +} + +fn http_client() -> Result { + Ok(reqwest::Client::builder() + .timeout(std::time::Duration::from_secs(10)) + .user_agent(format!("cloakbrowser-rust/{VERSION}")) + .build()?) +} + +/// Resolve license key with source tracking for env-injection decisions. +pub fn resolve_license_key_with_source( + license_key: Option<&str>, +) -> (Option, LicenseKeySource) { + // 1. Explicit param + if let Some(key) = license_key { + let trimmed = key.trim(); + if !trimmed.is_empty() { + return (Some(trimmed.to_string()), LicenseKeySource::Param); + } + } + + // 2. Environment variable + if let Ok(env_key) = std::env::var("CLOAKBROWSER_LICENSE_KEY") { + let env_key = env_key.trim().to_string(); + if !env_key.is_empty() { + return (Some(env_key), LicenseKeySource::Env); + } + } + + // 3. File in the wrapper cache dir + let cache_dir = config::get_cache_dir(); + let key_file = cache_dir.join("license.key"); + if let Ok(content) = std::fs::read_to_string(&key_file) { + let content = content.trim().to_string(); + if !content.is_empty() { + let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); + let default_cache = home.join(".cloakbrowser"); + let source = match ( + std::fs::canonicalize(&cache_dir), + std::fs::canonicalize(&default_cache), + ) { + (Ok(a), Ok(b)) if a == b => LicenseKeySource::DefaultFile, + _ if cache_dir == default_cache => LicenseKeySource::DefaultFile, + _ => LicenseKeySource::CustomFile, + }; + return (Some(content), source); + } + } + + (None, LicenseKeySource::None) +} + +/// Resolve the license key: explicit param > env var > file > None. +pub fn resolve_license_key(license_key: Option<&str>) -> Option { + resolve_license_key_with_source(license_key).0 +} + +/// Build a child-process env dict with any needed license key injection. +/// +/// Returns `None` when no injection is needed and no custom `user_env` was +/// given — Playwright treats `env=None` as "inherit parent env". +pub fn build_launch_env( + license_key: Option<&str>, + user_env: Option<&HashMap>, +) -> Option> { + let (key, source) = resolve_license_key_with_source(license_key); + + // Default file: binary reads it directly — no env injection needed, + // UNLESS the caller passes a custom env. + if source == LicenseKeySource::DefaultFile && user_env.is_none() { + return None; + } + + // No key at all: pass through user env or None. + if source == LicenseKeySource::None || key.is_none() { + return user_env.cloned(); + } + + // Env source, no custom user env: child inherits parent env. + if source == LicenseKeySource::Env && user_env.is_none() { + return None; + } + + let mut merged = if let Some(user) = user_env { + user.clone() + } else { + std::env::vars().collect() + }; + + if let Some(k) = key { + merged.insert("CLOAKBROWSER_LICENSE_KEY".into(), k); + } + + Some(merged) +} + +/// Validate a license key with the CloakBrowser server. +/// +/// Checks a local file cache first (24h TTL). Falls back to a stale cache if the +/// server is unreachable. Returns `None` on total failure. +pub async fn validate_license(license_key: &str) -> Option { + let cache_path = config::get_cache_dir().join(".license_cache"); + let key_sha = sha256_hex(license_key); + + if let Some(cached) = read_cache(&cache_path, &key_sha, false) { + return Some(cached); + } + + match validate_license_http(license_key).await { + Ok(info) => { + if info.valid { + write_cache(&cache_path, &key_sha, &info); + } + Some(info) + } + Err(e) => { + log::warning(format!("License validation request failed: {e}")); + if let Some(stale) = read_cache(&cache_path, &key_sha, true) { + log::warning("Using cached license validation (server unreachable)"); + return Some(stale); + } + None + } + } +} + +async fn validate_license_http(license_key: &str) -> Result { + let client = http_client()?; + let body = serde_json::json!({ "license_key": license_key }); + let resp = client.post(VALIDATE_URL).json(&body).send().await?; + let resp = resp.error_for_status()?; + let data: serde_json::Value = resp.json().await?; + + Ok(LicenseInfo { + valid: data + .get("valid") + .and_then(|v| v.as_bool()) + .unwrap_or(false), + plan: data + .get("plan") + .and_then(|v| v.as_str()) + .unwrap_or("solo") + .to_string(), + expires: data + .get("expires") + .and_then(|v| v.as_str()) + .map(|s| s.to_string()), + }) +} + +/// Get the latest Pro binary version from the server. +/// Rate-limited to 1 call per hour via a marker file. +pub async fn get_pro_latest_version() -> Option { + let tag = config::get_platform_tag().ok()?; + let marker = config::get_cache_dir().join(format!(".last_pro_version_check_{tag}")); + + if marker.exists() { + if let Ok(meta) = marker.metadata() { + if let Ok(modified) = meta.modified() { + if let Ok(age) = SystemTime::now().duration_since(modified) { + if age.as_secs_f64() < PRO_VERSION_CHECK_INTERVAL { + if let Ok(content) = std::fs::read_to_string(&marker) { + let content = content.trim(); + return if content.is_empty() { + None + } else { + Some(content.to_string()) + }; + } + } + } + } + } + } + + match fetch_pro_version().await { + Ok(version) => { + let _ = std::fs::create_dir_all(marker.parent().unwrap_or(Path::new("."))); + let tmp = marker.with_extension("tmp"); + if std::fs::write(&tmp, &version).is_ok() { + let _ = std::fs::rename(&tmp, &marker); + } + Some(version) + } + Err(e) => { + log::debug(format!("Pro version check failed: {e}")); + None + } + } +} + +async fn fetch_pro_version() -> Result { + let client = http_client()?; + let tag = config::get_platform_tag()?; + let resp = client + .get(PRO_VERSION_URL) + .header("X-Platform", tag) + .send() + .await? + .error_for_status()?; + let data: serde_json::Value = resp.json().await?; + data.get("version") + .and_then(|v| v.as_str()) + .map(|s| s.to_string()) + .ok_or_else(|| Error::msg("Pro version response missing 'version' field")) +} + +#[derive(Deserialize, Serialize)] +struct CacheData { + key_sha256: String, + valid: bool, + plan: String, + expires: Option, + validated_at: f64, +} + +fn read_cache(cache_path: &Path, key_sha: &str, ignore_ttl: bool) -> Option { + let text = std::fs::read_to_string(cache_path).ok()?; + let data: CacheData = serde_json::from_str(&text).ok()?; + + if data.key_sha256 != key_sha { + return None; + } + + if !ignore_ttl && now_unix() - data.validated_at > LICENSE_CACHE_TTL { + return None; + } + + // Expired license reported as invalid. + if let Some(ref expires) = data.expires { + if let Ok(exp) = chrono::DateTime::parse_from_rfc3339(expires) { + if exp < chrono::Utc::now() { + return Some(LicenseInfo { + valid: false, + plan: data.plan, + expires: data.expires, + }); + } + } + // Also try ISO without timezone (Python isoformat). + if let Ok(exp) = chrono::NaiveDateTime::parse_from_str(expires, "%Y-%m-%dT%H:%M:%S") { + let exp = exp.and_utc(); + if exp < chrono::Utc::now() { + return Some(LicenseInfo { + valid: false, + plan: data.plan, + expires: data.expires, + }); + } + } + } + + Some(LicenseInfo { + valid: data.valid, + plan: data.plan, + expires: data.expires, + }) +} + +fn write_cache(cache_path: &Path, key_sha: &str, info: &LicenseInfo) { + let _ = std::fs::create_dir_all(cache_path.parent().unwrap_or(Path::new("."))); + let payload = CacheData { + key_sha256: key_sha.to_string(), + valid: info.valid, + plan: info.plan.clone(), + expires: info.expires.clone(), + validated_at: now_unix(), + }; + let tmp = cache_path.with_extension("tmp"); + if let Ok(json) = serde_json::to_string(&payload) { + if std::fs::write(&tmp, json).is_ok() { + let _ = std::fs::rename(&tmp, cache_path); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn resolve_param_wins() { + // Ensure env doesn't interfere if already set — param still wins. + let (key, source) = resolve_license_key_with_source(Some(" my-key ")); + assert_eq!(key.as_deref(), Some("my-key")); + assert_eq!(source, LicenseKeySource::Param); + } + + #[test] + fn resolve_empty_param_falls_through() { + let (key, source) = resolve_license_key_with_source(Some(" ")); + // May be env/file/none depending on environment — just not Param. + assert_ne!(source, LicenseKeySource::Param); + let _ = key; + } + + #[test] + fn build_launch_env_param_injects() { + let env = build_launch_env(Some("test-key-xyz"), None); + assert!(env.is_some()); + assert_eq!( + env.unwrap().get("CLOAKBROWSER_LICENSE_KEY").map(String::as_str), + Some("test-key-xyz") + ); + } +} diff --git a/rust/cloakbrowser/src/log.rs b/rust/cloakbrowser/src/log.rs new file mode 100644 index 00000000..ff5a3282 --- /dev/null +++ b/rust/cloakbrowser/src/log.rs @@ -0,0 +1,73 @@ +//! Logging facade used across the library. +//! +//! By default only Info/Warning/Error go to stderr. Set `MinLevel` via +//! [`set_min_level`] or enable `tracing` subscribers for structured logs. + +use std::sync::atomic::{AtomicU8, Ordering}; + +/// Log severity levels for CloakBrowser. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[repr(u8)] +pub enum Level { + Debug = 0, + Info = 1, + Warning = 2, + Error = 3, + None = 4, +} + +static MIN_LEVEL: AtomicU8 = AtomicU8::new(Level::Info as u8); + +/// Minimum level that will be emitted. Defaults to Info. +pub fn set_min_level(level: Level) { + MIN_LEVEL.store(level as u8, Ordering::Relaxed); +} + +/// Current minimum log level. +pub fn min_level() -> Level { + match MIN_LEVEL.load(Ordering::Relaxed) { + 0 => Level::Debug, + 1 => Level::Info, + 2 => Level::Warning, + 3 => Level::Error, + _ => Level::None, + } +} + +fn emit(level: Level, message: &str) { + if level < min_level() { + return; + } + let tag = match level { + Level::Debug => "debug", + Level::Info => "info", + Level::Warning => "warning", + Level::Error => "error", + Level::None => return, + }; + eprintln!("[cloakbrowser:{tag}] {message}"); + + match level { + Level::Debug => tracing::debug!("{message}"), + Level::Info => tracing::info!("{message}"), + Level::Warning => tracing::warn!("{message}"), + Level::Error => tracing::error!("{message}"), + Level::None => {} + } +} + +pub fn debug(message: impl AsRef) { + emit(Level::Debug, message.as_ref()); +} + +pub fn info(message: impl AsRef) { + emit(Level::Info, message.as_ref()); +} + +pub fn warning(message: impl AsRef) { + emit(Level::Warning, message.as_ref()); +} + +pub fn error(message: impl AsRef) { + emit(Level::Error, message.as_ref()); +} diff --git a/rust/cloakbrowser/src/options.rs b/rust/cloakbrowser/src/options.rs new file mode 100644 index 00000000..61292571 --- /dev/null +++ b/rust/cloakbrowser/src/options.rs @@ -0,0 +1,300 @@ +//! Launch option records (mirrors Python keyword args / .NET LaunchOptions). + +use crate::human::HumanPreset; +use crate::proxy::Proxy; + +/// Options for [`crate::launch::launch`]. +#[derive(Debug, Clone)] +pub struct LaunchOptions { + /// Run in headless mode (default true). + pub headless: bool, + /// Proxy URL string or structured settings. + pub proxy: Option, + /// Additional Chromium CLI arguments. + pub args: Option>, + /// Include the default stealth fingerprint args (default true). + pub stealth_args: bool, + /// IANA timezone, e.g. `America/New_York`. + pub timezone: Option, + /// BCP 47 locale, e.g. `en-US`. + pub locale: Option, + /// Auto-detect timezone/locale (and WebRTC exit IP) from the proxy IP. + pub geoip: bool, + /// Enable human-like behavior layer. Use [`crate::CloakBrowser::new_human_page`]. + pub humanize: bool, + /// Humanize preset when `humanize` is true (default / careful). + pub human_preset: HumanPreset, + /// Chrome extension paths to load. + pub extension_paths: Option>, + /// CloakBrowser Pro license key. + pub license_key: Option, + /// Exact Chromium version pin. + pub browser_version: Option, + /// Suppress auto `--start-maximized` (set by context launchers). + pub suppress_maximize: bool, + /// Extra env vars for the browser process (Playwright `env`). + pub env: Option>, +} + +impl Default for LaunchOptions { + fn default() -> Self { + Self { + headless: true, + proxy: None, + args: None, + stealth_args: true, + timezone: None, + locale: None, + geoip: false, + humanize: false, + human_preset: HumanPreset::Default, + extension_paths: None, + license_key: None, + browser_version: None, + suppress_maximize: false, + env: None, + } + } +} + +impl LaunchOptions { + pub fn new() -> Self { + Self::default() + } + + pub fn headless(mut self, v: bool) -> Self { + self.headless = v; + self + } + + pub fn proxy(mut self, proxy: impl Into) -> Self { + self.proxy = Some(proxy.into()); + self + } + + pub fn args(mut self, args: Vec) -> Self { + self.args = Some(args); + self + } + + pub fn stealth_args(mut self, v: bool) -> Self { + self.stealth_args = v; + self + } + + pub fn timezone(mut self, tz: impl Into) -> Self { + self.timezone = Some(tz.into()); + self + } + + pub fn locale(mut self, locale: impl Into) -> Self { + self.locale = Some(locale.into()); + self + } + + pub fn geoip(mut self, v: bool) -> Self { + self.geoip = v; + self + } + + pub fn humanize(mut self, v: bool) -> Self { + self.humanize = v; + self + } + + pub fn human_preset(mut self, preset: HumanPreset) -> Self { + self.human_preset = preset; + self + } + + pub fn extension_paths(mut self, paths: Vec) -> Self { + self.extension_paths = Some(paths); + self + } + + pub fn license_key(mut self, key: impl Into) -> Self { + self.license_key = Some(key.into()); + self + } + + pub fn browser_version(mut self, version: impl Into) -> Self { + self.browser_version = Some(version.into()); + self + } +} + +/// Viewport resolution for context launchers. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum ViewportOption { + /// Use CloakBrowser defaults (fixed 1920×947 headless, no_viewport headed / + /// modern headless). + #[default] + Auto, + /// Explicit emulated size. + Size { width: u32, height: u32 }, + /// Disable viewport emulation (track the real OS window). + NoViewport, +} + +/// Options for context-producing launchers (`launch_persistent_context`, etc.). +/// +/// Mirrors .NET `LaunchContextOptions` / Python kwargs on `launch_persistent_context`. +#[derive(Debug, Clone)] +pub struct LaunchContextOptions { + /// Run in headless mode (default true). + pub headless: bool, + /// Proxy URL string or structured settings. + pub proxy: Option, + /// Additional Chromium CLI arguments. + pub args: Option>, + /// Include the default stealth fingerprint args (default true). + pub stealth_args: bool, + /// IANA timezone — sets binary `--fingerprint-timezone` (not CDP). + pub timezone: Option, + /// BCP 47 locale — sets binary `--lang` / `--fingerprint-locale`. + pub locale: Option, + /// Auto-detect timezone/locale from proxy IP. + pub geoip: bool, + /// Enable humanize layer for pages from this context. + pub humanize: bool, + /// Humanize preset. + pub human_preset: HumanPreset, + /// Chrome extension paths. + pub extension_paths: Option>, + /// Pro license key. + pub license_key: Option, + /// Chromium version pin. + pub browser_version: Option, + /// Custom user agent string. + pub user_agent: Option, + /// Viewport handling (default: Auto). + pub viewport: ViewportOption, + /// Color scheme: `light`, `dark`, or `no-preference`. + pub color_scheme: Option, + /// Path to a Playwright storage-state JSON. + pub storage_state_path: Option, +} + +impl Default for LaunchContextOptions { + fn default() -> Self { + Self { + headless: true, + proxy: None, + args: None, + stealth_args: true, + timezone: None, + locale: None, + geoip: false, + humanize: false, + human_preset: HumanPreset::Default, + extension_paths: None, + license_key: None, + browser_version: None, + user_agent: None, + viewport: ViewportOption::Auto, + color_scheme: None, + storage_state_path: None, + } + } +} + +impl LaunchContextOptions { + pub fn new() -> Self { + Self::default() + } + + pub fn headless(mut self, v: bool) -> Self { + self.headless = v; + self + } + + pub fn proxy(mut self, proxy: impl Into) -> Self { + self.proxy = Some(proxy.into()); + self + } + + pub fn humanize(mut self, v: bool) -> Self { + self.humanize = v; + self + } + + pub fn human_preset(mut self, preset: HumanPreset) -> Self { + self.human_preset = preset; + self + } + + pub fn geoip(mut self, v: bool) -> Self { + self.geoip = v; + self + } + + pub fn user_agent(mut self, ua: impl Into) -> Self { + self.user_agent = Some(ua.into()); + self + } + + pub fn viewport_size(mut self, width: u32, height: u32) -> Self { + self.viewport = ViewportOption::Size { width, height }; + self + } + + pub fn no_viewport(mut self) -> Self { + self.viewport = ViewportOption::NoViewport; + self + } + + pub fn color_scheme(mut self, scheme: impl Into) -> Self { + self.color_scheme = Some(scheme.into()); + self + } + + pub fn timezone(mut self, tz: impl Into) -> Self { + self.timezone = Some(tz.into()); + self + } + + pub fn locale(mut self, locale: impl Into) -> Self { + self.locale = Some(locale.into()); + self + } + + pub fn license_key(mut self, key: impl Into) -> Self { + self.license_key = Some(key.into()); + self + } + + pub fn browser_version(mut self, version: impl Into) -> Self { + self.browser_version = Some(version.into()); + self + } + + pub fn storage_state_path(mut self, path: impl Into) -> Self { + self.storage_state_path = Some(path.into()); + self + } + + /// Convert shared launch fields into a [`LaunchOptions`] (for shared helpers). + pub fn as_launch_options(&self) -> LaunchOptions { + // start_maximized is suppressed when the caller chose a viewport geometry. + let suppress_maximize = matches!( + self.viewport, + ViewportOption::Size { .. } | ViewportOption::NoViewport + ); + LaunchOptions { + headless: self.headless, + proxy: self.proxy.clone(), + args: self.args.clone(), + stealth_args: self.stealth_args, + timezone: self.timezone.clone(), + locale: self.locale.clone(), + geoip: self.geoip, + humanize: self.humanize, + human_preset: self.human_preset, + extension_paths: self.extension_paths.clone(), + license_key: self.license_key.clone(), + browser_version: self.browser_version.clone(), + suppress_maximize, + env: None, + } + } +} diff --git a/rust/cloakbrowser/src/proxy.rs b/rust/cloakbrowser/src/proxy.rs new file mode 100644 index 00000000..8b70bbe3 --- /dev/null +++ b/rust/cloakbrowser/src/proxy.rs @@ -0,0 +1,552 @@ +//! Proxy resolution: maps a proxy URL or settings into Playwright proxy +//! options and/or Chrome `--proxy-server` args. +//! Direct port of the proxy helpers in Python `cloakbrowser/browser.py`. + +use crate::config; +use crate::error::{Error, Result}; +use crate::log; + +/// Playwright-compatible proxy configuration. +#[derive(Debug, Clone, Default)] +pub struct ProxySettings { + pub server: String, + pub bypass: Option, + pub username: Option, + pub password: Option, +} + +impl ProxySettings { + pub fn new(server: impl Into) -> Self { + Self { + server: server.into(), + ..Default::default() + } + } +} + +/// A proxy input accepted by launch helpers. +#[derive(Debug, Clone)] +pub enum Proxy { + Url(String), + Settings(ProxySettings), +} + +impl From<&str> for Proxy { + fn from(s: &str) -> Self { + Proxy::Url(s.to_string()) + } +} + +impl From for Proxy { + fn from(s: String) -> Self { + Proxy::Url(s) + } +} + +impl From for Proxy { + fn from(s: ProxySettings) -> Self { + Proxy::Settings(s) + } +} + +/// Result of resolving a proxy: Playwright proxy (or None) plus extra Chrome args. +#[derive(Debug, Clone, Default)] +pub struct ProxyResolution { + pub playwright_proxy: Option, + pub extra_args: Vec, +} + +// -- URL parsing ------------------------------------------------------------- + +#[derive(Debug, Default, Clone)] +struct ParsedUrl { + scheme: String, + username: Option, // None = absent, Some("") = present-but-empty + password: Option, + host: String, + port: Option, + path: String, + query: String, + fragment: String, +} + +/// Prepend `http://` to schemeless proxy URLs. +pub fn ensure_proxy_scheme(proxy_url: &str) -> String { + if proxy_url.contains("://") { + proxy_url.to_string() + } else { + format!("http://{proxy_url}") + } +} + +fn parse_url(url: &str) -> Result { + let mut p = ParsedUrl::default(); + let mut rest = url.to_string(); + + if let Some(idx) = rest.find("://") { + p.scheme = rest[..idx].to_lowercase(); + rest = rest[idx + 3..].to_string(); + } + + if let Some(hash_idx) = rest.find('#') { + p.fragment = rest[hash_idx + 1..].to_string(); + rest = rest[..hash_idx].to_string(); + } + + if let Some(q_idx) = rest.find('?') { + p.query = rest[q_idx + 1..].to_string(); + rest = rest[..q_idx].to_string(); + } + + let (netloc, path) = if let Some(slash_idx) = rest.find('/') { + (rest[..slash_idx].to_string(), rest[slash_idx..].to_string()) + } else { + (rest, String::new()) + }; + p.path = path; + + let (hostport, userinfo) = if let Some(at_idx) = netloc.rfind('@') { + ( + netloc[at_idx + 1..].to_string(), + Some(netloc[..at_idx].to_string()), + ) + } else { + (netloc, None) + }; + + if let Some(userinfo) = userinfo { + if let Some(colon) = userinfo.find(':') { + p.username = Some(userinfo[..colon].to_string()); + p.password = Some(userinfo[colon + 1..].to_string()); + } else { + p.username = Some(userinfo); + p.password = None; + } + } + + if hostport.starts_with('[') { + let close = hostport + .find(']') + .ok_or_else(|| Error::msg("Invalid IPv6 proxy host"))?; + p.host = hostport[1..close].to_string(); + let after = &hostport[close + 1..]; + if let Some(port_str) = after.strip_prefix(':') { + p.port = Some(parse_port(port_str)?); + } + } else if let Some(colon) = hostport.rfind(':') { + p.host = hostport[..colon].to_string(); + p.port = Some(parse_port(&hostport[colon + 1..])?); + } else { + p.host = hostport; + } + + // Python urlparse().hostname lowercases the host. + p.host = p.host.to_lowercase(); + Ok(p) +} + +fn parse_port(s: &str) -> Result { + if s.is_empty() { + return Err(Error::msg("Invalid port: empty")); + } + s.parse::() + .map_err(|_| Error::msg(format!("Invalid port: {s}"))) +} + +fn quote(s: &str) -> String { + urlencoding::encode(s).into_owned() +} + +fn unquote(s: &str) -> String { + urlencoding::decode(s) + .map(|c| c.into_owned()) + .unwrap_or_else(|_| s.to_string()) +} + +#[allow(clippy::too_many_arguments)] // mirrors Python urlparse assemble pieces +fn assemble_proxy_url( + scheme: &str, + host: &str, + port: Option, + enc_user: &str, + enc_pass: Option<&str>, + path: &str, + query: &str, + fragment: &str, +) -> String { + let host = if host.contains(':') { + format!("[{host}]") + } else { + host.to_string() + }; + + let userinfo = if let Some(pass) = enc_pass { + format!("{enc_user}:{pass}@") + } else if !enc_user.is_empty() { + format!("{enc_user}@") + } else { + String::new() + }; + + let mut netloc = format!("{userinfo}{host}"); + if let Some(port) = port { + netloc.push_str(&format!(":{port}")); + } + + let mut sb = String::new(); + if !scheme.is_empty() { + sb.push_str(scheme); + sb.push_str("://"); + } + sb.push_str(&netloc); + sb.push_str(path); + if !query.is_empty() { + sb.push('?'); + sb.push_str(query); + } + if !fragment.is_empty() { + sb.push('#'); + sb.push_str(fragment); + } + sb +} + +// -- SOCKS ------------------------------------------------------------------- + +pub fn is_socks_proxy_url(url: &str) -> bool { + let lower = url.to_ascii_lowercase(); + lower.starts_with("socks5://") || lower.starts_with("socks5h://") +} + +fn is_socks_proxy(proxy: &Proxy) -> bool { + match proxy { + Proxy::Url(s) => is_socks_proxy_url(s), + Proxy::Settings(ps) => is_socks_proxy_url(&ps.server), + } +} + +fn reconstruct_socks_url(proxy: &ProxySettings) -> Result { + let username = proxy.username.as_deref().unwrap_or(""); + if username.is_empty() { + return Ok(proxy.server.clone()); + } + let parsed = parse_url(&proxy.server)?; + let enc_user = quote(username); + let enc_pass = proxy + .password + .as_ref() + .filter(|p| !p.is_empty()) + .map(|p| quote(p)); + Ok(assemble_proxy_url( + &parsed.scheme, + &parsed.host, + parsed.port, + &enc_user, + enc_pass.as_deref(), + &parsed.path, + "", + "", + )) +} + +fn normalize_socks_string_url(url: &str) -> String { + let parsed = match parse_url(url) { + Ok(p) => p, + Err(e) => { + log::warning(format!( + "Malformed SOCKS5 proxy URL, passing through unchanged: {e}" + )); + return url.to_string(); + } + }; + if parsed.username.is_none() && parsed.password.is_none() { + return url.to_string(); + } + let raw_user = parsed.username.clone().unwrap_or_default(); + let enc_user = if raw_user.is_empty() { + String::new() + } else { + quote(&unquote(&raw_user)) + }; + let enc_pass = parsed.password.as_ref().map(|p| { + if p.is_empty() { + String::new() + } else { + quote(&unquote(p)) + } + }); + let normalized = assemble_proxy_url( + &parsed.scheme, + &parsed.host, + parsed.port, + &enc_user, + enc_pass.as_deref(), + &parsed.path, + &parsed.query, + &parsed.fragment, + ); + if enc_user != raw_user || enc_pass.as_deref() != parsed.password.as_deref() { + log::info( + "Auto URL-encoded SOCKS5 proxy credentials (special characters detected). \ + Pre-encode the URL to suppress this notice.", + ); + } + normalized +} + +// -- HTTP -------------------------------------------------------------------- + +fn has_credentials(proxy: &Proxy) -> bool { + match proxy { + Proxy::Settings(ps) => ps + .username + .as_ref() + .map(|u| !u.is_empty()) + .unwrap_or(false), + Proxy::Url(s) => s.contains('@'), + } +} + +fn reconstruct_http_url(proxy: &ProxySettings) -> Result { + let username = proxy.username.as_deref().unwrap_or(""); + if username.is_empty() { + return Ok(proxy.server.clone()); + } + let parsed = parse_url(&ensure_proxy_scheme(&proxy.server))?; + let enc_user = quote(username); + let enc_pass = proxy + .password + .as_ref() + .filter(|p| !p.is_empty()) + .map(|p| quote(p)); + Ok(assemble_proxy_url( + &parsed.scheme, + &parsed.host, + parsed.port, + &enc_user, + enc_pass.as_deref(), + &parsed.path, + "", + "", + )) +} + +fn normalize_http_string_url(url: &str) -> String { + let normalized = ensure_proxy_scheme(url); + let parsed = match parse_url(&normalized) { + Ok(p) => p, + Err(e) => { + log::warning(format!( + "Malformed HTTP proxy URL, passing through unchanged: {e}" + )); + return normalized; + } + }; + if parsed.username.is_none() && parsed.password.is_none() { + return normalized; + } + let raw_user = parsed.username.clone().unwrap_or_default(); + let enc_user = if raw_user.is_empty() { + String::new() + } else { + quote(&unquote(&raw_user)) + }; + let enc_pass = parsed.password.as_ref().map(|p| { + if p.is_empty() { + String::new() + } else { + quote(&unquote(p)) + } + }); + let result = assemble_proxy_url( + &parsed.scheme, + &parsed.host, + parsed.port, + &enc_user, + enc_pass.as_deref(), + &parsed.path, + &parsed.query, + &parsed.fragment, + ); + if enc_user != raw_user || enc_pass.as_deref() != parsed.password.as_deref() { + log::info( + "Auto URL-encoded HTTP proxy credentials (special characters detected). \ + Pre-encode the URL to suppress this notice.", + ); + } + result +} + +fn parse_proxy_url(proxy: &str) -> Result { + let mut normalized = proxy.to_string(); + if proxy.contains('@') && !proxy.contains("://") { + normalized = format!("http://{proxy}"); + } + let parsed = parse_url(&normalized)?; + if parsed.username.as_ref().map(|u| u.is_empty()).unwrap_or(true) { + return Ok(ProxySettings::new(proxy)); + } + + let mut netloc = parsed.host.clone(); + if let Some(port) = parsed.port { + netloc.push_str(&format!(":{port}")); + } + let mut server = String::new(); + if !parsed.scheme.is_empty() { + server.push_str(&parsed.scheme); + server.push_str("://"); + } + server.push_str(&netloc); + server.push_str(&parsed.path); + + Ok(ProxySettings { + server, + username: parsed.username.map(|u| unquote(&u)), + password: parsed + .password + .filter(|p| !p.is_empty()) + .map(|p| unquote(&p)), + bypass: None, + }) +} + +/// Extract a normalized proxy URL string (for geoip / webrtc). +pub fn extract_proxy_url(proxy: Option<&Proxy>) -> Option { + match proxy { + None => None, + Some(Proxy::Settings(ps)) => { + if ps.server.is_empty() { + return None; + } + if is_socks_proxy_url(&ps.server) { + reconstruct_socks_url(ps).ok() + } else { + Some(ensure_proxy_scheme(&ps.server)) + } + } + Some(Proxy::Url(s)) => Some(ensure_proxy_scheme(s)), + } +} + +/// Resolve a proxy into Playwright options + extra Chrome args. +pub fn resolve( + proxy: Option<&Proxy>, + browser_version: Option<&str>, + license_key: Option<&str>, +) -> Result { + let Some(proxy) = proxy else { + return Ok(ProxyResolution::default()); + }; + + // SOCKS5: bypass Playwright, pass directly to Chrome via --proxy-server. + if is_socks_proxy(proxy) { + return match proxy { + Proxy::Settings(psd) => { + let url = reconstruct_socks_url(psd)?; + let mut extra = vec![format!("--proxy-server={url}")]; + if let Some(ref bypass) = psd.bypass { + if !bypass.is_empty() { + extra.push(format!("--proxy-bypass-list={bypass}")); + } + } + Ok(ProxyResolution { + playwright_proxy: None, + extra_args: extra, + }) + } + Proxy::Url(s) => Ok(ProxyResolution { + playwright_proxy: None, + extra_args: vec![format!("--proxy-server={}", normalize_socks_string_url(s))], + }), + }; + } + + // HTTP/HTTPS with credentials on binaries that support inline auth. + if has_credentials(proxy) + && config::binary_supports_http_proxy_inline_auth(license_key, browser_version) + { + return match proxy { + Proxy::Settings(psd) => { + let url = reconstruct_http_url(psd)?; + let mut extra = vec![format!("--proxy-server={url}")]; + if let Some(ref bypass) = psd.bypass { + if !bypass.is_empty() { + extra.push(format!("--proxy-bypass-list={bypass}")); + } + } + Ok(ProxyResolution { + playwright_proxy: None, + extra_args: extra, + }) + } + Proxy::Url(s) => Ok(ProxyResolution { + playwright_proxy: None, + extra_args: vec![format!("--proxy-server={}", normalize_http_string_url(s))], + }), + }; + } + + // HTTP/HTTPS without credentials (or older binary): Playwright proxy. + match proxy { + Proxy::Settings(dict) => Ok(ProxyResolution { + playwright_proxy: Some(dict.clone()), + extra_args: vec![], + }), + Proxy::Url(s) => Ok(ProxyResolution { + playwright_proxy: Some(parse_proxy_url(s)?), + extra_args: vec![], + }), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ensure_scheme() { + assert_eq!( + ensure_proxy_scheme("1.2.3.4:8080"), + "http://1.2.3.4:8080" + ); + assert_eq!( + ensure_proxy_scheme("socks5://h:1"), + "socks5://h:1" + ); + } + + #[test] + fn socks_detection() { + assert!(is_socks_proxy_url("socks5://host:1080")); + assert!(is_socks_proxy_url("socks5h://host:1080")); + assert!(!is_socks_proxy_url("http://host:8080")); + } + + #[test] + fn resolve_socks_string() { + let p = Proxy::from("socks5://user:pass@host:1080"); + let r = resolve(Some(&p), None, None).unwrap(); + assert!(r.playwright_proxy.is_none()); + assert!(r.extra_args[0].starts_with("--proxy-server=socks5://")); + } + + #[test] + fn resolve_http_no_creds() { + let p = Proxy::from("http://proxy:8080"); + let r = resolve(Some(&p), Some("146.0.7680.177.5"), None).unwrap(); + assert!(r.playwright_proxy.is_some()); + assert!(r.extra_args.is_empty()); + assert_eq!(r.playwright_proxy.unwrap().server, "http://proxy:8080"); + } + + #[test] + fn parse_url_with_auth() { + let p = parse_url("http://user:p%40ss@host:8080/path").unwrap(); + assert_eq!(p.scheme, "http"); + assert_eq!(p.username.as_deref(), Some("user")); + assert_eq!(p.password.as_deref(), Some("p%40ss")); + assert_eq!(p.host, "host"); + assert_eq!(p.port, Some(8080)); + assert_eq!(p.path, "/path"); + } +} diff --git a/rust/cloakbrowser/src/version.rs b/rust/cloakbrowser/src/version.rs new file mode 100644 index 00000000..e4503aaa --- /dev/null +++ b/rust/cloakbrowser/src/version.rs @@ -0,0 +1,4 @@ +//! Wrapper version (mirrors Python `cloakbrowser/_version.py`). + +/// The CloakBrowser Rust wrapper version. +pub const VERSION: &str = "0.4.10"; diff --git a/rust/cloakbrowser/src/widevine.rs b/rust/cloakbrowser/src/widevine.rs new file mode 100644 index 00000000..b15c94f1 --- /dev/null +++ b/rust/cloakbrowser/src/widevine.rs @@ -0,0 +1,135 @@ +//! Widevine CDM hint-file seeding for persistent contexts. +//! Direct port of Python `cloakbrowser/widevine.py` / .NET `Widevine.cs`. +//! +//! Linux only. Never bundles the CDM — only writes a hint when a user-provided +//! CDM is already present next to the binary or in the cache dir. + +use std::path::{Path, PathBuf}; + +use crate::config; +use crate::log; + +const HINT_FILENAME: &str = "latest-component-updated-widevine-cdm"; + +fn seeding_disabled() -> bool { + match std::env::var("CLOAKBROWSER_WIDEVINE") { + Ok(v) => matches!( + v.trim().to_ascii_lowercase().as_str(), + "0" | "false" | "off" | "no" + ), + Err(_) => false, + } +} + +/// Locate a sideloaded Widevine CDM directory, or `None` if absent. +/// +/// Resolution order: +/// 1. `CLOAKBROWSER_WIDEVINE_CDM` (exclusive override) +/// 2. `/WidevineCdm` +/// 3. `~/.cloakbrowser/WidevineCdm` +/// +/// A directory counts only if it contains `manifest.json`. +pub fn resolve_widevine_cdm_dir(binary_path: &Path) -> Option { + if let Ok(custom) = std::env::var("CLOAKBROWSER_WIDEVINE_CDM") { + if custom.trim().is_empty() { + return None; + } + let cdm_dir = PathBuf::from(custom); + return if cdm_dir.join("manifest.json").is_file() { + Some(cdm_dir.canonicalize().unwrap_or(cdm_dir)) + } else { + None + }; + } + + let next_to_binary = binary_path + .parent() + .unwrap_or_else(|| Path::new(".")) + .join("WidevineCdm"); + let cache_cdm = config::get_cache_dir().join("WidevineCdm"); + + for cdm_dir in [next_to_binary, cache_cdm] { + if cdm_dir.join("manifest.json").is_file() { + return Some(cdm_dir.canonicalize().unwrap_or(cdm_dir)); + } + } + None +} + +/// Write the Widevine CDM hint file into a persistent profile before launch. +/// +/// No-op on non-Linux, when disabled via `CLOAKBROWSER_WIDEVINE`, when +/// `user_data_dir` is empty, or when no CDM is present. Never panics. +pub fn seed_widevine_hint(user_data_dir: &Path, binary_path: &Path) { + if !cfg!(target_os = "linux") { + return; + } + if seeding_disabled() { + log::debug("Widevine hint seeding disabled via CLOAKBROWSER_WIDEVINE"); + return; + } + if user_data_dir.as_os_str().is_empty() { + return; + } + + let result = (|| -> std::io::Result<()> { + let Some(cdm_dir) = resolve_widevine_cdm_dir(binary_path) else { + if std::env::var_os("CLOAKBROWSER_WIDEVINE_CDM").is_some() { + log::warning( + "CLOAKBROWSER_WIDEVINE_CDM is set but has no manifest.json; \ + skipping Widevine hint seeding", + ); + } else { + log::debug("No sideloaded Widevine CDM found; skipping hint seeding"); + } + return Ok(()); + }; + + let hint_dir = user_data_dir.join("WidevineCdm"); + std::fs::create_dir_all(&hint_dir)?; + let hint_file = hint_dir.join(HINT_FILENAME); + let content = serde_json::json!({ "Path": cdm_dir.to_string_lossy() }).to_string(); + // Compact separators: serde_json default is compact for objects without spaces + // in default to_string() — actually default is compact. Good. + + if hint_file.is_file() { + if let Ok(existing) = std::fs::read_to_string(&hint_file) { + if existing == content { + return Ok(()); + } + } else { + log::warning("Existing Widevine hint unreadable; rewriting"); + } + } + + std::fs::write(&hint_file, content.as_bytes())?; + log::info(format!( + "Seeded Widevine CDM hint -> {}", + cdm_dir.display() + )); + Ok(()) + })(); + + if let Err(e) = result { + log::warning(format!("Failed to seed Widevine CDM hint file: {e}")); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn resolve_missing_returns_none() { + let fake = std::env::temp_dir().join("cloak-no-chrome-xyz/chrome"); + // Clear custom env for this test scope by ensuring it's not pointing at a real CDM. + let result = resolve_widevine_cdm_dir(&fake); + // May still find cache CDM on a real install — just ensure function doesn't panic. + let _ = result; + } + + #[test] + fn seed_empty_user_data_is_noop() { + seed_widevine_hint(Path::new(""), Path::new("/tmp/chrome")); + } +} diff --git a/rust/examples/basic.rs b/rust/examples/basic.rs new file mode 100644 index 00000000..ff0dfa33 --- /dev/null +++ b/rust/examples/basic.rs @@ -0,0 +1,24 @@ +//! Basic CloakBrowser launch example. +//! +//! ```bash +//! cd rust && cargo run --example basic -p cloakbrowser +//! ``` + +use cloakbrowser::{launch, LaunchOptions}; + +#[tokio::main] +async fn main() -> cloakbrowser::Result<()> { + let browser = launch(LaunchOptions { + headless: true, + ..Default::default() + }) + .await?; + + let page = browser.new_page().await?; + let _ = page.goto("https://example.com", None).await; + let title = page.title().await.unwrap_or_default(); + println!("Title: {title}"); + + browser.close().await?; + Ok(()) +} diff --git a/rust/examples/humanize.rs b/rust/examples/humanize.rs new file mode 100644 index 00000000..761b617d --- /dev/null +++ b/rust/examples/humanize.rs @@ -0,0 +1,35 @@ +//! Humanize demo — Bezier mouse + human typing. +//! +//! ```bash +//! cd rust && cargo run --example humanize -p cloakbrowser +//! ``` + +use cloakbrowser::{launch, LaunchOptions, HumanPreset}; + +#[tokio::main] +async fn main() -> cloakbrowser::Result<()> { + let browser = launch(LaunchOptions { + headless: true, + humanize: true, + human_preset: HumanPreset::Default, + ..Default::default() + }) + .await?; + + let mut page = browser.new_human_page().await?; + page.goto("data:text/html,\ +

Hello

\ + \ + \ + ") + .await?; + + page.fill("#email", "user@example.com").await?; + page.click("#go").await?; + + println!("Cursor at: {:?}", page.cursor()); + println!("Done (humanized fill + click)."); + + browser.close().await?; + Ok(()) +} diff --git a/rust/examples/persistent_context.rs b/rust/examples/persistent_context.rs new file mode 100644 index 00000000..8d81192f --- /dev/null +++ b/rust/examples/persistent_context.rs @@ -0,0 +1,38 @@ +//! Persistent profile example — cookies/state survive across runs. +//! +//! ```bash +//! cd rust && cargo run --example persistent_context -p cloakbrowser +//! ``` + +use cloakbrowser::{launch_persistent_context, LaunchContextOptions}; + +#[tokio::main] +async fn main() -> cloakbrowser::Result<()> { + let profile = std::env::temp_dir().join("cloakbrowser-rust-profile"); + println!("Profile dir: {}", profile.display()); + + let ctx = launch_persistent_context( + &profile, + LaunchContextOptions { + headless: true, + ..Default::default() + }, + ) + .await?; + + let page = ctx.new_page().await?; + let _ = page + .goto("https://example.com", None) + .await; + println!( + "Title: {}", + page.title().await.unwrap_or_default() + ); + println!( + "Profile will be reused next run at: {}", + profile.display() + ); + + ctx.close().await?; + Ok(()) +}