Skip to content

Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.

15.5 KB
Raw Blame
import { describe, it, expect, afterEach, vi } from "vitest";
import fs from "node:fs";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import {
  COUNTRY_LOCALE_MAP,
  DEFAULT_GEOIP_TIMEOUT_MS,
  isPrivateIp,
  maybeResolveGeoip,
  resolveProxyGeo,
  resolveProxyIp,
} from "../src/geoip.js";
import Stream from "node:stream";

const tempDirs: string[] = [];

afterEach(() => {
  vi.restoreAllMocks();
  delete process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS;
  delete process.env.CLOAKBROWSER_CACHE_DIR;
  for (const dir of tempDirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
});

describe("resolveProxyIp", () => {
  it("returns literal IPv4 from proxy URL", async () => {
    expect(await resolveProxyIp("http://10.0.0.5:8888")).toBe("10.0.0.5");
  });

  it("handles proxy URL with credentials", async () => {
    expect(await resolveProxyIp("http://user:[email protected]:8888")).toBe(
      "10.0.0.5"
    );
  });

  it("resolves localhost", async () => {
    const ip = await resolveProxyIp("http://localhost:8888");
    expect(ip).toBeTruthy();
    expect(["127.0.0.1", "::1"]).toContain(ip);
  });

  it("returns null for invalid URL", async () => {
    expect(await resolveProxyIp("not-a-url")).toBeNull();
  });

  it("returns null for empty string", async () => {
    expect(await resolveProxyIp("")).toBeNull();
  });

  it("returns null for schemeless proxy (shows why normalization is needed)", async () => {
    // no scheme — new URL() gives empty hostname for both bare formats
    expect(await resolveProxyIp("user:[email protected]:8888")).toBeNull();
    expect(await resolveProxyIp("10.0.0.5:8888")).toBeNull();
  });

  it("extracts IP after normalization (http:// prepended by maybeResolveGeoip)", async () => {
    expect(await resolveProxyIp("http://user:[email protected]:8888")).toBe("10.0.0.5");
    expect(await resolveProxyIp("http://10.0.0.5:8888")).toBe("10.0.0.5");
  });
});

describe("isPrivateIp", () => {
  it("classifies loopback, private and link-local ranges", () => {
    for (const ip of ["127.0.0.1", "10.0.0.1", "172.16.5.4", "192.168.1.1", "100.100.1.1", "169.254.1.1", "::1", "fd00::1", "fe80::1"]) {
      expect(isPrivateIp(ip), ip).toBe(true);
    }
    for (const ip of ["8.8.8.8", "172.32.0.1", "2001:4860:4860::8888", "not-an-ip"]) {
      expect(isPrivateIp(ip), ip).toBe(false);
    }
  });
});

describe("maybeResolveGeoip", () => {
  it("uses a 20-second default timeout", () => {
    expect(DEFAULT_GEOIP_TIMEOUT_MS).toBe(20_000);
  });

  it("forwards separate HTTP proxy credentials to CONNECT requests", async () => {
    const authorizationHeaders: Array<string | undefined> = [];
    const sockets = new Set<Stream.Duplex>();
    const proxy = http.createServer();
    proxy.on("connect", (request, socket) => {
      sockets.add(socket);
      socket.once("close", () => sockets.delete(socket));
      authorizationHeaders.push(request.headers["proxy-authorization"]);
      socket.end("HTTP/1.1 407 Proxy Authentication Required\r\nContent-Length: 0\r\n\r\n");
    });
    await new Promise<void>((resolve) => proxy.listen(0, "127.0.0.1", resolve));

    try {
      const address = proxy.address();
      if (address === null || typeof address === "string") throw new Error("Missing proxy address");
      process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.2";

      await maybeResolveGeoip({
        geoip: true,
        timezone: "America/New_York",
        locale: "en-US",
        proxy: {
          server: `http://127.0.0.1:${address.port}`,
          username: "user",
          password: "pass",
        },
      });

      expect(authorizationHeaders).not.toHaveLength(0);
      expect(authorizationHeaders).toEqual(
        authorizationHeaders.map(() => "Basic dXNlcjpwYXNz"),
      );
    } finally {
      for (const socket of sockets) socket.destroy();
      await new Promise<void>((resolve, reject) =>
        proxy.close((error) => (error === undefined ? resolve() : reject(error))),
      );
    }
  });

  it("sends CONNECT for a proxy URL with a raw % in the password", async () => {
    const authorizationHeaders: Array<string | undefined> = [];
    const sockets = new Set<Stream.Duplex>();
    const proxy = http.createServer();
    proxy.on("connect", (request, socket) => {
      sockets.add(socket);
      socket.once("close", () => sockets.delete(socket));
      authorizationHeaders.push(request.headers["proxy-authorization"]);
      socket.end("HTTP/1.1 407 Proxy Authentication Required\r\nContent-Length: 0\r\n\r\n");
    });
    await new Promise<void>((resolve) => proxy.listen(0, "127.0.0.1", resolve));
    const { port } = proxy.address() as { port: number };
    process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.2";

    try {
      await maybeResolveGeoip({
        geoip: true,
        timezone: "America/New_York",
        locale: "en-US",
        proxy: `http://user:100%[email protected]:${port}`,
      });
    } finally {
      for (const socket of sockets) socket.destroy();
      await new Promise<void>((resolve) => proxy.close(() => resolve()));
    }

    expect(authorizationHeaders).not.toHaveLength(0);
    expect(authorizationHeaders[0]).toBe(
      "Basic " + Buffer.from("user:100%sure").toString("base64"),
    );
  });

  it("does not throw for a SOCKS5 proxy URL with a raw % in the password", async () => {
    process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.2";
    // Nothing listens on port 9: the lookup fails quietly instead of throwing URIError.
    await expect(
      maybeResolveGeoip({
        geoip: true,
        timezone: "America/New_York",
        locale: "en-US",
        proxy: "socks5://user:100%[email protected]:9",
      }),
    ).resolves.toEqual({ timezone: "America/New_York", locale: "en-US", exitIp: undefined });
  });

  it("does not apply the GeoIP resolution timeout to first-use database download", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-download-"));
    tempDirs.push(cacheDir);
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;
    process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.001";

    const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue({
      ok: true,
      body: new ReadableStream({
        start(controller) {
          controller.enqueue(new Uint8Array([1, 2, 3]));
          controller.close();
        },
      }),
    } as Response);

    await expect(resolveProxyGeo("http://203.0.113.10:8080")).rejects.toThrow(
      "GeoIP resolution",
    );

    expect(fetchSpy).toHaveBeenCalledOnce();
    expect(fetchSpy.mock.calls[0][1]).toEqual({ redirect: "follow" });
  });

  it("GeoIP database download writes nothing to stdout", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-stdout-"));
    tempDirs.push(cacheDir);
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;
    process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.001";

    vi.spyOn(globalThis, "fetch").mockResolvedValue({
      ok: true,
      body: new ReadableStream({
        start(controller) {
          controller.enqueue(new Uint8Array([1, 2, 3]));
          controller.close();
        },
      }),
    } as Response);
    vi.spyOn(console, "error").mockImplementation(() => {});
    const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
    const stdoutWrite = vi.spyOn(process.stdout, "write");

    await expect(resolveProxyGeo("http://203.0.113.10:8080")).rejects.toThrow();

    expect(fs.existsSync(path.join(cacheDir, "geoip", "GeoLite2-City.mmdb"))).toBe(true);
    expect(consoleLog).not.toHaveBeenCalled();
    expect(stdoutWrite).not.toHaveBeenCalled();
  });

  it("throws when the GeoIP database is unavailable", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-db-failure-"));
    tempDirs.push(cacheDir);
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;

    vi.spyOn(globalThis, "fetch")
      .mockRejectedValueOnce(new Error("database unavailable"))
      .mockResolvedValue({ ok: true, text: async () => "5.6.7.8" } as Response);

    await expect(resolveProxyGeo(null)).rejects.toThrow("database is unavailable");
  });

  it("does not fall back to a private proxy address when the exit IP is unknown", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-private-proxy-"));
    tempDirs.push(cacheDir);
    const geoipDir = path.join(cacheDir, "geoip");
    fs.mkdirSync(geoipDir, { recursive: true });
    fs.writeFileSync(path.join(geoipDir, "GeoLite2-City.mmdb"), "stub");
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;

    // A local forwarding proxy that is not listening: exit-IP discovery fails.
    const server = http.createServer();
    await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
    const { port } = server.address() as { port: number };
    await new Promise<void>((resolve) => server.close(() => resolve()));

    await expect(resolveProxyGeo(`http://127.0.0.1:${port}`)).rejects.toThrow(
      "could not discover the egress IP"
    );
  });

  it("throws when the GeoIP database lookup fails", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-lookup-failure-"));
    tempDirs.push(cacheDir);
    const geoipDir = path.join(cacheDir, "geoip");
    fs.mkdirSync(geoipDir, { recursive: true });
    fs.writeFileSync(path.join(geoipDir, "GeoLite2-City.mmdb"), "invalid database");
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;

    vi.spyOn(globalThis, "fetch").mockResolvedValue({
      ok: true,
      text: async () => "5.6.7.8",
    } as Response);

    await expect(resolveProxyGeo(null)).rejects.toThrow("GeoIP lookup failed");
  });

  it("falls back to en-US for a country missing from the locale map (#591)", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-unmapped-"));
    tempDirs.push(cacheDir);
    const geoipDir = path.join(cacheDir, "geoip");
    fs.mkdirSync(geoipDir, { recursive: true });
    fs.writeFileSync(path.join(geoipDir, "GeoLite2-City.mmdb"), "stub");
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;

    class Reader {
      get() {
        return { location: { time_zone: "Indian/Mahe" }, country: { iso_code: "XX" } };
      }
    }
    vi.doMock("mmdb-lib", () => ({ default: { Reader }, Reader }));
    vi.spyOn(globalThis, "fetch").mockResolvedValue({
      ok: true,
      text: async () => "5.6.7.8",
    } as Response);

    try {
      await expect(resolveProxyGeo(null)).resolves.toEqual({
        timezone: "Indian/Mahe",
        locale: "en-US",
        exitIp: "5.6.7.8",
      });
    } finally {
      vi.doUnmock("mmdb-lib");
    }
  });

  it("downloads the database once under concurrent first-use launches (#458)", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-concurrent-"));
    tempDirs.push(cacheDir);
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;
    // Short resolution budget so the post-download reader on the fake DB bails
    // fast (the download itself is exempt from this timeout).
    process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.2";

    // Slow response so all launches queue behind the first download.
    const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(
      () =>
        new Promise<Response>((resolve) =>
          setTimeout(
            () =>
              resolve({
                ok: true,
                body: new ReadableStream({
                  start(controller) {
                    controller.enqueue(new Uint8Array([1, 2, 3]));
                    controller.close();
                  },
                }),
              } as Response),
            50,
          ),
        ),
    );

    const results = await Promise.allSettled(
      Array.from({ length: 5 }, () => resolveProxyGeo("http://203.0.113.10:8080")),
    );

    expect(results.every(result => result.status === "rejected")).toBe(true);
    // Only one launch actually fetched the shared ~70 MB file.
    expect(fetchSpy).toHaveBeenCalledOnce();
  });

  it("no proxy + both explicit: skips the exit-IP fetch entirely", async () => {
    // With no proxy the WebRTC IP would just be the real connection IP the site
    // already sees (a no-op), so maybeResolveGeoip must not call the echo services.
    const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue({
      ok: true,
      text: async () => "5.6.7.8",
    } as Response);

    const result = await maybeResolveGeoip({
      geoip: true,
      timezone: "Europe/Berlin",
      locale: "de-DE",
    });

    expect(result).toEqual({ timezone: "Europe/Berlin", locale: "de-DE" });
    expect(fetchSpy).not.toHaveBeenCalled();
    fetchSpy.mockRestore();
  });

  it("promotes raw tz/locale flags in args: no proxy + both raw-flagged skips fetch", async () => {
    // A raw --fingerprint-timezone/--fingerprint-locale in args counts as explicit,
    // so geoip must not clobber them (and, with no proxy, skips the echo fetch).
    const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue({
      ok: true,
      text: async () => "5.6.7.8",
    } as Response);

    const result = await maybeResolveGeoip({
      geoip: true,
      args: ["--fingerprint-timezone=Asia/Tokyo", "--fingerprint-locale=ja-JP"],
    });

    expect(result).toEqual({ timezone: "Asia/Tokyo", locale: "ja-JP" });
    expect(fetchSpy).not.toHaveBeenCalled();
    fetchSpy.mockRestore();
  });

  it("promoted raw flags survive a GeoIP timeout (with proxy)", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-rawflag-"));
    tempDirs.push(cacheDir);
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;
    process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.025";

    const result = await maybeResolveGeoip({
      geoip: true,
      proxy: "http://203.0.113.10:8080",
      args: ["--fingerprint-timezone=Asia/Tokyo", "--lang=ja-JP"],
    });

    // --lang promotes to locale; both explicit → exit-IP-only path (times out → undefined).
    expect(result.timezone).toBe("Asia/Tokyo");
    expect(result.locale).toBe("ja-JP");
  });

  it("explicit timezone option beats a differing raw flag", async () => {
    const result = await maybeResolveGeoip({
      geoip: true,
      timezone: "America/New_York",
      locale: "en-US",
      args: ["--fingerprint-timezone=Asia/Tokyo"],
    });
    expect(result.timezone).toBe("America/New_York");
    expect(result.locale).toBe("en-US");
  });

  it("returns quickly when GeoIP resolution times out", async () => {
    const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), "cloak-geoip-timeout-"));
    tempDirs.push(cacheDir);
    process.env.CLOAKBROWSER_CACHE_DIR = cacheDir;
    process.env.CLOAKBROWSER_GEOIP_TIMEOUT_SECONDS = "0.025";

    const start = performance.now();
    const result = await maybeResolveGeoip({
      geoip: true,
      proxy: "http://203.0.113.10:8080",
      timezone: "Europe/Paris",
      locale: "fr-FR",
    });
    const elapsed = performance.now() - start;

    expect(result).toEqual({ timezone: "Europe/Paris", locale: "fr-FR", exitIp: undefined });
    expect(elapsed).toBeLessThan(500);
  });
});


describe("COUNTRY_LOCALE_MAP", () => {
  it("contains common countries", () => {
    for (const code of ["US", "GB", "DE", "FR", "JP", "BR", "IL", "RU"]) {
      expect(COUNTRY_LOCALE_MAP[code]).toBeDefined();
    }
  });

  it("maps Seychelles (issue #591)", () => {
    expect(COUNTRY_LOCALE_MAP["SC"]).toBe("en-SC");
  });

  it("values are BCP 47 language-REGION format", () => {
    for (const [code, locale] of Object.entries(COUNTRY_LOCALE_MAP)) {
      const parts = locale.split("-");
      expect(parts).toHaveLength(2);
      expect(parts[0]).toMatch(/^[a-z]{2,3}$/);
      expect(parts[1]).toMatch(/^[A-Z]{2}$/);
    }
  });
});