File size: 8,906 Bytes
5d0a52f
 
 
 
 
 
 
 
347f81b
 
5d0a52f
 
 
 
 
8b777a2
5d0a52f
 
 
 
4a940a5
5d0a52f
4a940a5
 
 
5d0a52f
347f81b
 
 
 
 
 
 
 
 
 
 
 
 
 
5d0a52f
347f81b
5d0a52f
347f81b
5d0a52f
 
 
347f81b
 
 
 
5d0a52f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347f81b
5d0a52f
 
347f81b
 
 
5d0a52f
 
 
 
 
 
 
 
 
 
 
 
 
347f81b
 
 
 
 
 
 
5d0a52f
 
 
 
 
 
 
 
 
 
 
347f81b
0d2f54c
 
 
 
 
 
 
 
 
 
347f81b
0d2f54c
 
347f81b
 
0d2f54c
5d0a52f
 
 
 
347f81b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d0a52f
347f81b
5d0a52f
 
 
 
 
347f81b
 
 
 
 
5d0a52f
 
 
 
 
347f81b
 
 
 
 
 
 
5d0a52f
 
 
 
 
 
 
 
 
347f81b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d0a52f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a940a5
 
5d0a52f
347f81b
 
 
 
 
 
 
 
 
4a940a5
8b777a2
4a940a5
8b777a2
 
5d0a52f
 
 
 
 
4a940a5
 
 
347f81b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d0a52f
 
8b777a2
 
5d0a52f
 
 
 
 
 
 
 
347f81b
5d0a52f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
 * CookieJar β€” per-account cookie storage.
 *
 * Stores cookies (especially cf_clearance from Cloudflare) so that
 * GET endpoints like /codex/usage don't get blocked by JS challenges.
 *
 * Cookies are auto-captured from every ChatGPT API response's Set-Cookie
 * headers, and can also be set manually via the management API.
 *
 * Persistence format v2: includes expiry timestamps.
 */

import {
  readFileSync,
  writeFileSync,
  renameSync,
  existsSync,
  mkdirSync,
} from "fs";
import { resolve, dirname } from "path";
import { getDataDir } from "../paths.js";

function getCookieFile(): string {
  return resolve(getDataDir(), "cookies.json");
}

interface StoredCookie {
  value: string;
  expires: number | null; // Unix ms timestamp, null = session cookie
}

/** v2 persistence format */
interface CookieFileV2 {
  _version: 2;
  accounts: Record<string, Record<string, { value: string; expires: number | null }>>;
}

/** Critical cookie names that trigger immediate persistence on change */
const CRITICAL_COOKIES = new Set(["cf_clearance", "__cf_bm"]);

export class CookieJar {
  private cookies: Map<string, Record<string, StoredCookie>> = new Map();
  private persistTimer: ReturnType<typeof setTimeout> | null = null;
  private cleanupTimer: ReturnType<typeof setInterval>;

  constructor() {
    this.load();
    this.cleanupExpired();
    // Clean up expired cookies every 5 minutes
    this.cleanupTimer = setInterval(() => this.cleanupExpired(), 5 * 60 * 1000);
    if (this.cleanupTimer.unref) this.cleanupTimer.unref();
  }

  /**
   * Set cookies for an account.
   * Accepts "name1=val1; name2=val2" string or a Record.
   * Merges with existing cookies.
   */
  set(accountId: string, cookies: string | Record<string, string>): void {
    const existing = this.cookies.get(accountId) ?? {};

    if (typeof cookies === "string") {
      for (const part of cookies.split(";")) {
        const eq = part.indexOf("=");
        if (eq === -1) continue;
        const name = part.slice(0, eq).trim();
        const value = part.slice(eq + 1).trim();
        if (name) existing[name] = { value, expires: null };
      }
    } else {
      for (const [k, v] of Object.entries(cookies)) {
        existing[k] = { value: v, expires: null };
      }
    }

    this.cookies.set(accountId, existing);
    this.schedulePersist();
  }

  /**
   * Build the Cookie header value for a request.
   * Returns null if no cookies are stored.
   */
  getCookieHeader(accountId: string): string | null {
    const cookies = this.cookies.get(accountId);
    if (!cookies || Object.keys(cookies).length === 0) return null;
    const now = Date.now();
    const pairs: string[] = [];
    for (const [k, c] of Object.entries(cookies)) {
      if (c.expires !== null && c.expires <= now) continue; // skip expired
      pairs.push(`${k}=${c.value}`);
    }
    return pairs.length > 0 ? pairs.join("; ") : null;
  }

  /**
   * Auto-capture Set-Cookie headers from an API response.
   * Call this after every successful fetch to chatgpt.com.
   */
  capture(accountId: string, response: Response): void {
    const setCookies =
      typeof response.headers.getSetCookie === "function"
        ? response.headers.getSetCookie()
        : [];
    this.captureRaw(accountId, setCookies);
  }

  /**
   * Capture cookies from raw Set-Cookie header strings (e.g. from curl).
   */
  captureRaw(accountId: string, setCookies: string[]): void {
    if (setCookies.length === 0) return;

    const existing = this.cookies.get(accountId) ?? {};
    let changed = false;
    let hasCritical = false;

    for (const raw of setCookies) {
      const parts = raw.split(";").map((s) => s.trim());
      const pair = parts[0];
      const eq = pair.indexOf("=");
      if (eq === -1) continue;

      const name = pair.slice(0, eq).trim();
      const value = pair.slice(eq + 1).trim();
      if (!name) continue;

      // Parse expiry from attributes
      let expires: number | null = null;
      for (let i = 1; i < parts.length; i++) {
        const attr = parts[i];
        const attrLower = attr.toLowerCase();
        if (attrLower.startsWith("max-age=")) {
          const seconds = parseInt(attr.slice(8), 10);
          if (!isNaN(seconds)) {
            expires = seconds <= 0 ? 0 : Date.now() + seconds * 1000;
          }
          break; // Max-Age takes precedence over Expires
        }
        if (attrLower.startsWith("expires=")) {
          const date = new Date(attr.slice(8));
          if (!isNaN(date.getTime())) {
            expires = date.getTime();
          }
        }
      }

      const prev = existing[name];
      if (!prev || prev.value !== value || prev.expires !== expires) {
        existing[name] = { value, expires };
        changed = true;
        if (CRITICAL_COOKIES.has(name)) hasCritical = true;
      }
    }

    if (changed) {
      this.cookies.set(accountId, existing);
      if (hasCritical) {
        this.persistNow(); // Critical cookie β€” persist immediately
      } else {
        this.schedulePersist();
      }
    }
  }

  /** Get raw cookie record for an account. */
  get(accountId: string): Record<string, string> | null {
    const cookies = this.cookies.get(accountId);
    if (!cookies) return null;
    const result: Record<string, string> = {};
    for (const [k, c] of Object.entries(cookies)) {
      result[k] = c.value;
    }
    return result;
  }

  /** Clear all cookies for an account. */
  clear(accountId: string): void {
    if (this.cookies.delete(accountId)) {
      this.schedulePersist();
    }
  }

  /** Remove expired cookies from all accounts. */
  private cleanupExpired(): void {
    const now = Date.now();
    let changed = false;
    for (const [, cookies] of this.cookies) {
      for (const [name, c] of Object.entries(cookies)) {
        if (c.expires !== null && c.expires <= now) {
          delete cookies[name];
          changed = true;
        }
      }
    }
    if (changed) this.schedulePersist();
  }

  // ── Persistence ──────────────────────────────────────────────────

  private schedulePersist(): void {
    if (this.persistTimer) return;
    this.persistTimer = setTimeout(() => {
      this.persistTimer = null;
      this.persistNow();
    }, 1000);
  }

  persistNow(): void {
    if (this.persistTimer) {
      clearTimeout(this.persistTimer);
      this.persistTimer = null;
    }
    try {
      const cookieFile = getCookieFile();
      const dir = dirname(cookieFile);
      if (!existsSync(dir)) mkdirSync(dir, { recursive: true });

      // Persist v2 format with expiry info
      const data: CookieFileV2 = { _version: 2, accounts: {} };
      for (const [acct, cookies] of this.cookies) {
        data.accounts[acct] = {};
        for (const [k, c] of Object.entries(cookies)) {
          data.accounts[acct][k] = { value: c.value, expires: c.expires };
        }
      }
      const tmpFile = cookieFile + ".tmp";
      writeFileSync(tmpFile, JSON.stringify(data, null, 2), "utf-8");
      renameSync(tmpFile, cookieFile);
    } catch (err) {
      console.warn("[CookieJar] Failed to persist:", err instanceof Error ? err.message : err);
    }
  }

  private load(): void {
    try {
      const cookieFile = getCookieFile();
      if (!existsSync(cookieFile)) return;
      const raw = readFileSync(cookieFile, "utf-8");
      const data = JSON.parse(raw);

      if (data && data._version === 2 && data.accounts) {
        // v2 format: { _version: 2, accounts: { acct: { name: { value, expires } } } }
        for (const [acct, cookies] of Object.entries(data.accounts as Record<string, Record<string, { value: string; expires: number | null }>>)) {
          const record: Record<string, StoredCookie> = {};
          for (const [k, c] of Object.entries(cookies)) {
            record[k] = { value: c.value, expires: c.expires ?? null };
          }
          this.cookies.set(acct, record);
        }
      } else {
        // v1 format: { acct: { name: "value" } } (no expiry)
        for (const [key, val] of Object.entries(data as Record<string, unknown>)) {
          if (key === "_version") continue;
          if (typeof val === "object" && val !== null) {
            const record: Record<string, StoredCookie> = {};
            for (const [k, v] of Object.entries(val as Record<string, string>)) {
              record[k] = { value: v, expires: null };
            }
            this.cookies.set(key, record);
          }
        }
      }
    } catch (err) {
      console.warn("[CookieJar] Failed to load cookies:", err instanceof Error ? err.message : err);
    }
  }

  destroy(): void {
    if (this.persistTimer) {
      clearTimeout(this.persistTimer);
      this.persistTimer = null;
    }
    clearInterval(this.cleanupTimer);
    this.persistNow();
  }
}