Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

1.8 KB
Raw Blame
// The app's own window signs in to the app's own server with the owner header (server-session.mjs), never a session.
// When that window still reads "signed out", either it loaded before the server held the header's token, or it carries a
// session cookie the server has ended (a code once typed into it, since expired or revoked), and the server reads a
// presented cookie before the header, refusing it on every launch. The server's rule stays; the app drops its own ended
// cookie for its own server and loads the page again.

/** At most one repair a minute, so a page that still can't sign in shows the pairing page instead of reloading forever. */
export const LOCAL_REPAIR_EVERY_MS = 60_000;

/** `cookies`: the window's session cookies (read when asked: Electron's session exists only once the app is ready).
 * `now` is a monotonic clock (performance.now): the wall clock can step back and hold the page in a loop. */
export function createLocalSignInRepair({ ready, origin, port, cookies, window, log = () => {}, now = () => performance.now() }) {
  let lastAt = null;
  return async function repairLocalSignIn() {
    if (!ready()) throw new Error("this computer's server is not ready");
    if (lastAt !== null && now() - lastAt < LOCAL_REPAIR_EVERY_MS) throw new Error("already tried signing this window in again");
    lastAt = now();
    const url = origin(), prefix = `omb_session_${port()}_`, jar = cookies(); // request-auth.ts sessionCookieName
    const ended = (await jar.get({ url })).filter(({ name }) => name.startsWith(prefix));
    for (const { name } of ended) await jar.remove(url, name);
    log(`own window read signed out: ${ended.length} session cookie(s) dropped, loading it again`);
    const win = window();
    if (win && !win.isDestroyed()) win.webContents.reload();
  };
}