File size: 2,379 Bytes
2276d59
62527b4
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c48b9be
 
 
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62527b4
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from 'node:fs';
import { logger } from '../infra/logger.util.js';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { secureFetch } from './security.util.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const COOKIES_DIR = path.join(__dirname, '../../../temp/cookies');

export async function downloadCookies(type: string): Promise<string | null> {
  const sanitizedType = path.basename(type).replace(/[^a-zA-Z0-9_-]/g, '');
  const cookiePath = path.join(COOKIES_DIR, `${sanitizedType}_cookies.txt`);

  if (fs.existsSync(cookiePath)) {
    const stats = fs.statSync(cookiePath);
    const ageMs = Date.now() - stats.mtimeMs;
    if (ageMs < 86400000) return cookiePath; // 24h cache
  }

  try {
    const cookiesUrl = process.env.COOKIES_URL;
    if (!cookiesUrl) return null;

    if (!fs.existsSync(COOKIES_DIR))
      fs.mkdirSync(COOKIES_DIR, { recursive: true });

    const response = await secureFetch(`${cookiesUrl}/${type}_cookies.txt`);
    if (!response.ok) {
      let stripped = cookiesUrl;
      while (stripped.endsWith('/')) stripped = stripped.slice(0, -1);
      const fallback = await secureFetch(stripped);
      if (!fallback.ok) return null;
      let text = await fallback.text();
      if (!text.startsWith('# Netscape HTTP Cookie File')) {
        text = text.replace(/^#[^\n]*\n/, '# Netscape HTTP Cookie File\n');
      }
      fs.writeFileSync(cookiePath, text);
      return cookiePath;
    }

    let text = await response.text();
    if (!text.startsWith('# Netscape HTTP Cookie File')) {
      text = text.replace(/^#[^\n]*\n/, '# Netscape HTTP Cookie File\n');
    }
    fs.writeFileSync(cookiePath, text);
    return cookiePath;
  } catch (error) {
    logger.error(
      `[Cookies] Download failed for ${type}:`,
      (error as Error).message
    );
    return null;
  }
}

export function parseCookieFile(filePath: string): string {
  if (!fs.existsSync(filePath)) return '';
  const content = fs.readFileSync(filePath, 'utf8');
  const lines = content.split('\n');
  const pairs: string[] = [];

  for (const line of lines) {
    if (!line.trim() || line.startsWith('#')) continue;
    const parts = line.split('\t');
    if (parts.length >= 7) {
      pairs.push(`${parts[5].trim()}=${parts[6].trim()}`);
    }
  }
  return pairs.join('; ');
}