Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.
Python ai-agentsanti-detectantidetect-browserbot-detectionbrowser-automationcaptcha-bypasschromiumcloudflarecloudflare-bypassfingerprintheadless-browserplaywrightpuppeteerpythonrecaptchaseleniumstealth-browserundetectedweb-scrapingwebscraping
fix(cache): keep the saved license key when clearing the cache (#671)
Mert Kayaf322120397d2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c5a967b..acec5d5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@ Changes are tagged: **[wrapper]** for Python/JS wrapper, **[binary]** for Chromi## [Unreleased]+- **[wrapper]** `clear-cache` (`clear_cache()`, JS `clearCache()`, .NET `Download.ClearCache()`) removes only the cached browser builds. It deleted the whole `~/.cloakbrowser` folder, including the key saved by `login`, so the next launch failed with exit 77 until you logged in again. Python, JavaScript, and .NET- **[wrapper]** `cloakserve`: `geoip=true` now works without a proxy and sets the timezone and locale from the machine's public IP (#658)- **[wrapper]** `cloakserve`: the GeoIP lookup runs off the event loop, so other connected clients no longer stall while a `geoip=true` launch resolves its timezone and locale- **[wrapper]** `cloakserve --headless=false` now runs headed. The flag was also forwarded to Chrome, which treats any `--headless` switch as headless, so no window opened on the display (#651)diff --git a/cloakbrowser/download.py b/cloakbrowser/download.pyindex dd0776e..1d4f8c7 100644--- a/cloakbrowser/download.py+++ b/cloakbrowser/download.py@@ -961,13 +961,17 @@ def _remove_quarantine(path: Path) -> None:def clear_cache() -> None:- """Remove all cached binaries. Forces re-download on next launch."""+ """Remove all cached binaries. Forces re-download on next launch.++ The saved license key and the GeoIP database in the same dir are kept.+ """from .config import get_cache_dircache_dir = get_cache_dir()if cache_dir.exists():try:- shutil.rmtree(cache_dir)+ for build_dir in cache_dir.glob("chromium-*"):+ shutil.rmtree(build_dir)except OSError:logger.error("Failed to clear cache: %s", cache_dir)raisediff --git a/dotnet/src/CloakBrowser/Download.cs b/dotnet/src/CloakBrowser/Download.csindex a25a213..8a168c5 100644--- a/dotnet/src/CloakBrowser/Download.cs+++ b/dotnet/src/CloakBrowser/Download.cs@@ -1089,13 +1089,17 @@ public static class Download}}- /// <summary>Remove all cached binaries. Forces re-download on next launch.</summary>+ /// <summary>+ /// Remove all cached binaries. Forces re-download on next launch.+ /// The saved license key and the GeoIP database in the same dir are kept.+ /// </summary>public static void ClearCache(){var cacheDir = Config.GetCacheDir();if (Directory.Exists(cacheDir)){- Directory.Delete(cacheDir, recursive: true);+ foreach (var buildDir in Directory.GetDirectories(cacheDir, "chromium-*"))+ Directory.Delete(buildDir, recursive: true);CloakLog.Info("Cache cleared: {0}", cacheDir);}}diff --git a/dotnet/tests/CloakBrowser.Tests/DownloadConfigTests.cs b/dotnet/tests/CloakBrowser.Tests/DownloadConfigTests.csindex 3c90947..97e22cc 100644--- a/dotnet/tests/CloakBrowser.Tests/DownloadConfigTests.cs+++ b/dotnet/tests/CloakBrowser.Tests/DownloadConfigTests.cs@@ -114,6 +114,36 @@ public class WrapperVersionNewerTests}}+/// <summary>+/// <see cref="Download.ClearCache"/> removes cached builds only. In env-serial because it+/// sets CLOAKBROWSER_CACHE_DIR.+/// </summary>+[Collection("env-serial")]+public class ClearCacheTests+{+ [Fact]+ public void ClearCache_removes_binaries_and_keeps_license_key()+ {+ var prevCacheDir = Environment.GetEnvironmentVariable("CLOAKBROWSER_CACHE_DIR");+ var cacheDir = Path.Combine(Path.GetTempPath(), $"cloak-clear-test-{Guid.NewGuid():N}");+ Directory.CreateDirectory(Path.Combine(cacheDir, "chromium-145"));+ File.WriteAllText(Path.Combine(cacheDir, "chromium-145", "chrome"), "binary");+ File.WriteAllText(Path.Combine(cacheDir, "license.key"), "cb_test");+ try+ {+ Environment.SetEnvironmentVariable("CLOAKBROWSER_CACHE_DIR", cacheDir);+ Download.ClearCache();+ Assert.False(Directory.Exists(Path.Combine(cacheDir, "chromium-145")));+ Assert.Equal("cb_test", File.ReadAllText(Path.Combine(cacheDir, "license.key")));+ }+ finally+ {+ Environment.SetEnvironmentVariable("CLOAKBROWSER_CACHE_DIR", prevCacheDir);+ if (Directory.Exists(cacheDir)) Directory.Delete(cacheDir, recursive: true);+ }+ }+}+/// <summary>/// Concurrent first-run callers of <see cref="Download.EnsureBinaryAsync"/> against one/// empty cache, on the keyed download path: the license and latest version are mockeddiff --git a/js/src/download.ts b/js/src/download.tsindex c1ba77b..b719345 100644--- a/js/src/download.ts+++ b/js/src/download.ts@@ -356,11 +356,18 @@ function startClaimPoll(code: string): void {});}-/** Remove all cached binaries. Forces re-download on next launch. */+/**+ * Remove all cached binaries. Forces re-download on next launch.+ * The saved license key and the GeoIP database in the same dir are kept.+ */export function clearCache(): void {const cacheDir = getCacheDir();if (fs.existsSync(cacheDir)) {- fs.rmSync(cacheDir, { recursive: true, force: true });+ for (const entry of fs.readdirSync(cacheDir)) {+ if (entry.startsWith("chromium-")) {+ fs.rmSync(path.join(cacheDir, entry), { recursive: true, force: true });+ }+ }console.error(`[cloakbrowser] Cache cleared: ${cacheDir}`);}}diff --git a/js/tests/update.test.ts b/js/tests/update.test.tsindex d215dd0..e9c08f1 100644--- a/js/tests/update.test.ts+++ b/js/tests/update.test.ts@@ -395,6 +395,24 @@ describe("ensureBinary", () => {});describe("clearCache", () => {+ it("removes binaries and keeps the license key", () => {+ const orig = process.env.CLOAKBROWSER_CACHE_DIR;+ const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloakbrowser-clear-"));+ process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;+ try {+ fs.mkdirSync(path.join(cacheDir, "chromium-145"));+ fs.writeFileSync(path.join(cacheDir, "chromium-145", "chrome"), "binary");+ fs.writeFileSync(path.join(cacheDir, "license.key"), "cb_test");+ clearCache();+ expect(fs.existsSync(path.join(cacheDir, "chromium-145"))).toBe(false);+ expect(fs.readFileSync(path.join(cacheDir, "license.key"), "utf8")).toBe("cb_test");+ } finally {+ if (orig) process.env.CLOAKBROWSER_CACHE_DIR = orig;+ else delete process.env.CLOAKBROWSER_CACHE_DIR;+ fs.rmSync(cacheDir, { recursive: true, force: true });+ }+ });+it("does not throw when cache dir missing", () => {const orig = process.env.CLOAKBROWSER_CACHE_DIR;process.env.CLOAKBROWSER_CACHE_DIR = "/tmp/cloakbrowser-test-nonexistent";diff --git a/tests/test_update.py b/tests/test_update.pyindex 57d77c2..f43518c 100644--- a/tests/test_update.py+++ b/tests/test_update.py@@ -181,13 +181,15 @@ class TestVerifyChecksum:class TestClearCache:- def test_removes_dir(self, tmp_path):+ def test_removes_binaries_keeps_license_key(self, tmp_path):with patch.dict(os.environ, {"CLOAKBROWSER_CACHE_DIR": str(tmp_path)}):# Create some content(tmp_path / "chromium-145").mkdir()(tmp_path / "chromium-145" / "chrome").write_bytes(b"binary")+ (tmp_path / "license.key").write_text("cb_test")clear_cache()- assert not tmp_path.exists()+ assert not (tmp_path / "chromium-145").exists()+ assert (tmp_path / "license.key").read_text() == "cb_test"def test_noop_if_missing(self, tmp_path):nonexistent = tmp_path / "nonexistent"