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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/**
 * 工业级页面抓取层(CommonJS)
 *
 * 提供代理池、UA/浏览器头轮换、TLS 指纹、指数退避重试、礼貌限速、反爬降级等能力。
 * 供 CI 脚本(check-updates.js / process-regex-updates.js / run-task.js)与后台任务使用。
 *
 * 环境变量:
 *   PROXY_URLS              代理池,逗号分隔,如 "http://user:pass@host:port,http://user:pass@host2:port"
 *   CRAWLER_RESPECT_ROBOTS  是否遵守 robots.txt(默认 false)
 *   CRAWLER_MIN_DELAY_MS    每域名最小间隔(默认 1200)
 *   CRAWLER_MAX_CONCURRENCY 全局并发上限(默认 5)
 *   CRAWLER_RETRIES         失败重试次数(默认 3)
 *   CRAWLER_TIMEOUT_MS      请求超时(默认 15000)
 *   GITHUB_TOKEN, GITHUB_TOKEN_2..5  GitHub API token(轮换)
 */
'use strict';

const { HeaderGenerator } = require('header-generator');
const { randomInt, sleep } = require('./crawler-utils');

// ── 配置 ──
const PROXY_URLS = (process.env.PROXY_URLS || '')
  .split(',')
  .map((s) => s.trim())
  .filter(Boolean);
const RESPECT_ROBOTS = process.env.CRAWLER_RESPECT_ROBOTS === 'true';
const MIN_DELAY_MS = parseInt(process.env.CRAWLER_MIN_DELAY_MS || '1200', 10);
const MAX_CONCURRENCY = parseInt(process.env.CRAWLER_MAX_CONCURRENCY || '5', 10);
const RETRIES = parseInt(process.env.CRAWLER_RETRIES || '3', 10);
const REQUEST_TIMEOUT_MS = parseInt(process.env.CRAWLER_TIMEOUT_MS || '15000', 10);
const BROWSER_UA =
  process.env.CRAWLER_BROWSER_UA ||
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36';

// ── 代理池(轮询 + 失败旋转)──
let proxyCursor = 0;
let lastProxyFailed = false;

function nextProxy() {
  if (PROXY_URLS.length === 0) return undefined;
  // 若上一代理失败,跳过它强制轮换
  if (lastProxyFailed) {
    proxyCursor = (proxyCursor + 1) % PROXY_URLS.length;
    lastProxyFailed = false;
  }
  const proxy = PROXY_URLS[proxyCursor % PROXY_URLS.length];
  proxyCursor += 1;
  return proxy;
}

// ── 浏览器头预设(每次请求随机选一组)──
let headerPresets = null;

function getHeaderPresets() {
  if (headerPresets) return headerPresets;
  // header-generator 同实例 getHeaders() 结果是确定性的,需按组合显式构造不同预设
  const browsers = ['chrome', 'firefox', 'edge'];
  const operatingSystems = ['windows', 'linux', 'macos'];
  const locales = ['zh-CN', 'zh', 'en-US'];
  const presetCount = 12;
  headerPresets = Array.from({ length: presetCount }, (_, i) => {
    const generator = new HeaderGenerator({
      browsers: [{ name: browsers[i % browsers.length], minVersion: 100 }],
      operatingSystems: [operatingSystems[Math.floor(i / 4) % operatingSystems.length]],
      locales: [locales[i % locales.length]],
    });
    const h = generator.getHeaders();
    return {
      'user-agent': h['user-agent'] || BROWSER_UA,
      accept: h.accept || 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
      'accept-language': h['accept-language'] || 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
      'accept-encoding': 'gzip, deflate, br',
      'sec-ch-ua': h['sec-ch-ua'] || '',
      'sec-ch-ua-mobile': h['sec-ch-ua-mobile'] || '?0',
      'sec-ch-ua-platform': h['sec-ch-ua-platform'] || '"Windows"',
      'upgrade-insecure-requests': h['upgrade-insecure-requests'] || '1',
      'sec-fetch-dest': 'document',
      'sec-fetch-mode': 'navigate',
      'sec-fetch-site': 'none',
      'sec-fetch-user': '?1',
      'cache-control': 'max-age=0',
    };
  });
  return headerPresets;
}

function randomHeaders() {
  const presets = getHeaderPresets();
  return { ...presets[randomInt(0, presets.length - 1)] };
}

// ── 礼貌限速:每域名最小间隔 ──
const lastHitByDomain = new Map();

async function politeDelay(url) {
  let hostname;
  try {
    hostname = new URL(url).hostname;
  } catch {
    return;
  }
  const last = lastHitByDomain.get(hostname) || 0;
  const elapsed = Date.now() - last;
  // 间隔加入 20% 随机抖动,避免固定节奏
  const delay = Math.floor(MIN_DELAY_MS * (0.8 + Math.random() * 0.4));
  if (elapsed < delay) {
    await sleep(delay - elapsed);
  }
  lastHitByDomain.set(hostname, Date.now());
}

// ── 并发控制(信号量)──
class Semaphore {
  constructor(max) {
    this.max = max;
    this.active = 0;
    this.queue = [];
  }
  async acquire() {
    if (this.active < this.max) {
      this.active += 1;
      return;
    }
    await new Promise((resolve) => this.queue.push(resolve));
    this.active += 1;
  }
  release() {
    this.active -= 1;
    const next = this.queue.shift();
    if (next) next();
  }
}
const semaphore = new Semaphore(MAX_CONCURRENCY);

// ── 指数退避 ──
function backoffMs(attempt) {
  const base = Math.min(1000 * Math.pow(2, attempt), 8000);
  return base + randomInt(0, 300); // 抖动
}

// ── robots.txt(可选)──
const robotsCache = new Map();
async function isAllowedByRobots(url) {
  if (!RESPECT_ROBOTS) return true;
  try {
    const u = new URL(url);
    const key = u.origin;
    if (robotsCache.has(key)) return robotsCache.get(key);
    let allowed = true;
    try {
      const res = await fetch(`${key}/robots.txt`, { redirect: 'follow' });
      if (res.ok) {
        const text = await res.text();
        // 极简解析:仅处理 User-agent: * 与 Disallow
        const disallow = [];
        let applyToAll = false;
        for (const line of text.split(/\r?\n/)) {
          const l = line.trim().toLowerCase();
          if (l.startsWith('user-agent:')) {
            applyToAll = l.includes('*');
          } else if (l.startsWith('disallow:') && applyToAll) {
            const p = l.slice('disallow:'.length).trim();
            if (p) disallow.push(p);
          }
        }
        const path = u.pathname;
        allowed = !disallow.some((d) => path.startsWith(d));
      }
    } catch {
      // robots 不可达则不阻断
    }
    robotsCache.set(key, allowed);
    return allowed;
  } catch {
    return true;
  }
}

// ── 反爬判定 ──
function looksLikeChallenge(status, text) {
  const body = (text || '').slice(0, 4000).toLowerCase();
  if (status === 403 || status === 429) return true;
  if (status === 503 && /captcha|cf-browser-verification|cloudflare|检查.*浏览器/.test(body)) return true;
  if (/just a moment|access denied|verify you are human|安全检查|人机验证/.test(body)) return true;
  return false;
}

let gotScrapingInstance = null;
async function getGotScraping() {
  if (gotScrapingInstance) return gotScrapingInstance;
  const mod = await import('got-scraping');
  gotScrapingInstance = mod.gotScraping;
  return gotScrapingInstance;
}

/**
 * 普通 HTTP 抓取(got-scraping:TLS 指纹 + HTTP/2 + 代理 + 自带头)
 * 不做重试/降级,由 fetchPage 统一编排。
 */
async function rawFetch(url, { timeout = REQUEST_TIMEOUT_MS } = {}) {
  const gotScraping = await getGotScraping();
  const headers = randomHeaders();
  const proxyUrl = nextProxy();
  const response = await gotScraping.get(url, {
    headers,
    proxyUrl,
    useHeaderGenerator: false,
    timeout: { request: timeout, response: timeout },
    retry: { limit: 0 },
    decompress: true,
    responseType: 'text',
    http2: true,
  });
  return {
    status: response.statusCode,
    headers: response.headers,
    text: typeof response.body === 'string' ? response.body : String(response.body),
  };
}

let playwrightBrowserPromise = null;
let playwright = null;

async function getPlaywright() {
  if (!playwright) {
    playwright = await import('playwright');
  }
  if (!playwrightBrowserPromise) {
    playwrightBrowserPromise = playwright.chromium.launch({
      headless: true,
      args: [
        '--disable-blink-features=AutomationControlled',
        '--disable-dev-shm-usage',
        '--no-sandbox',
        ...(PROXY_URLS.length ? [`--proxy-server=${nextProxy()}`] : []),
      ],
    });
  }
  return playwrightBrowserPromise;
}

async function closeBrowser() {
  if (!playwrightBrowserPromise) return;
  try {
    const browser = await playwrightBrowserPromise;
    await browser.close();
  } catch {
    // ignore
  } finally {
    playwrightBrowserPromise = null;
  }
}

async function createStealthContext(browser) {
  const context = await browser.newContext({
    userAgent: BROWSER_UA,
    viewport: { width: 1366, height: 768 },
    locale: 'zh-CN',
    timezoneId: 'Asia/Shanghai',
    colorScheme: 'light',
    extraHTTPHeaders: {
      'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
      'Upgrade-Insecure-Requests': '1',
    },
  });
  await context.addInitScript(() => {
    Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
    Object.defineProperty(navigator, 'languages', { get: () => ['zh-CN', 'zh', 'en-US', 'en'] });
    Object.defineProperty(navigator, 'plugins', {
      get: () => [
        { name: 'Chrome PDF Plugin', filename: 'internal-pdf-viewer' },
        { name: 'Chrome PDF Viewer', filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai' },
        { name: 'Native Client', filename: 'internal-nacl-plugin' },
      ],
    });
    Object.defineProperty(navigator, 'platform', { get: () => 'Win32' });
    Object.defineProperty(window, 'chrome', {
      get: () => ({ runtime: {}, app: { isInstalled: false } }),
    });
  });
  await context.route('**/*', async (route) => {
    const type = route.request().resourceType();
    if (type === 'image' || type === 'media' || type === 'font') {
      await route.abort();
    } else {
      await route.continue();
    }
  });
  return context;
}

/**
 * Playwright stealth 抓取(JS 渲染 / 反爬降级用)
 */
async function browserFetch(url, { timeout = REQUEST_TIMEOUT_MS } = {}) {
  const browser = await getPlaywright();
  let context;
  try {
    context = await createStealthContext(browser);
    const page = await context.newPage();
    await page.goto(url, { waitUntil: 'networkidle', timeout });
    const text = await page.content();
    return { status: 200, headers: {}, text };
  } catch (error) {
    return { status: 0, headers: {}, text: null, error: error.message };
  } finally {
    if (context) await context.close().catch(() => {});
  }
}

/**
 * 抓取一个页面(工业编排:代理/UA/重试/降级)
 * @returns {Promise<{status:number, headers:object, text:string|null, error?:string, viaBrowser?:boolean}>}
 */
async function fetchPage(url, { js = false, timeout = REQUEST_TIMEOUT_MS, retries = RETRIES } = {}) {
  if (!(await isAllowedByRobots(url))) {
    return { status: 0, headers: {}, text: null, error: 'blocked by robots.txt' };
  }

  await semaphore.acquire();
  try {
    let lastError = null;
    for (let attempt = 0; attempt <= retries; attempt += 1) {
      await politeDelay(url);
      let result;
      if (js) {
        result = await browserFetch(url, { timeout });
      } else {
        try {
          result = await rawFetch(url, { timeout });
        } catch (error) {
          lastError = error.message;
          // 网络/超时错误 → 退避重试
          if (attempt < retries) await sleep(backoffMs(attempt));
          continue;
        }
      }

      if (result.error) {
        lastError = result.error;
        if (attempt < retries) await sleep(backoffMs(attempt));
        continue;
      }

      // 反爬响应 → 旋转代理重试;JS 页失败 → 浏览器降级
      if (looksLikeChallenge(result.status, result.text)) {
        lastError = `anti-bot ${result.status}`;
        lastProxyFailed = true;
        if (!js && attempt >= retries) {
          const browserResult = await browserFetch(url, { timeout });
          if (!browserResult.error && browserResult.text) {
            return { ...browserResult, viaBrowser: true };
          }
        }
        if (attempt < retries) await sleep(backoffMs(attempt));
        continue;
      }

      // 5xx → 退避重试
      if (result.status >= 500 && attempt < retries) {
        lastError = `http ${result.status}`;
        await sleep(backoffMs(attempt));
        continue;
      }

      return result;
    }
    return { status: 0, headers: {}, text: null, error: lastError || 'fetch failed' };
  } finally {
    semaphore.release();
  }
}

/**
 * JSON 抓取
 */
async function fetchJson(url, options = {}) {
  const result = await fetchPage(url, { timeout: options.timeout, retries: options.retries ?? 1 });
  if (result.error || result.status === 0) {
    return { status: result.status, body: null, error: result.error || 'fetch failed' };
  }
  if (result.status >= 400) {
    return { status: result.status, body: null, error: `http ${result.status}` };
  }
  try {
    return { status: result.status, body: JSON.parse(result.text), error: null };
  } catch (error) {
    return { status: result.status, body: null, error: `invalid json: ${error.message}` };
  }
}

/**
 * HEAD 请求(ETag 探测用):取响应头,不下载 body
 */
function headRequest(url, etag = null, lastModified = null, { timeout = REQUEST_TIMEOUT_MS } = {}) {
  return new Promise((resolve) => {
    const mod = url.startsWith('https') ? require('https') : require('http');
    const headers = randomHeaders();
    if (etag) headers['If-None-Match'] = etag;
    if (lastModified) headers['If-Modified-Since'] = lastModified;

    let parsedUrl;
    try {
      parsedUrl = new URL(url);
    } catch {
      return resolve({ status: 0, etag: null, lastModified: null, contentLength: null, unchanged: null });
    }
    const options = {
      hostname: parsedUrl.hostname,
      port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80),
      path: parsedUrl.pathname + parsedUrl.search,
      method: 'HEAD',
      headers,
      timeout,
    };
    const req = mod.request(options, (res) => {
      res.resume();
      resolve({
        status: res.statusCode,
        etag: res.headers['etag'] || null,
        lastModified: res.headers['last-modified'] || null,
        contentLength: res.headers['content-length'] || null,
        unchanged: res.statusCode === 304,
      });
    });
    req.on('error', () => resolve({ status: 0, etag: null, lastModified: null, contentLength: null, unchanged: null }));
    req.on('timeout', () => {
      req.destroy();
      resolve({ status: 0, etag: null, lastModified: null, contentLength: null, unchanged: null });
    });
    req.end();
  });
}

// ── GitHub API(限流感知 + 多 token 轮换)──
const GITHUB_TOKENS = [];
for (let i = 1; i <= 5; i += 1) {
  const t = process.env[`GITHUB_TOKEN${i === 1 ? '' : `_${i}`}`];
  if (t) GITHUB_TOKENS.push(t);
}
let githubTokenCursor = 0;

function nextGithubToken() {
  if (GITHUB_TOKENS.length === 0) return '';
  const token = GITHUB_TOKENS[githubTokenCursor % GITHUB_TOKENS.length];
  githubTokenCursor += 1;
  return token;
}

/**
 * GitHub API 请求:403 限流时等待 x-ratelimit-reset 或切换 token 重试
 */
async function githubApi(path, { token, retries = 3 } = {}) {
  const apiToken = token || nextGithubToken();
  const headers = {
    Accept: 'application/vnd.github+json',
    'X-GitHub-Api-Version': '2022-11-28',
    'User-Agent': 'logup-scraper',
  };
  if (apiToken) headers.Authorization = `Bearer ${apiToken}`;

  const url = path.startsWith('http') ? path : `https://api.github.com${path}`;
  let lastError = null;

  for (let attempt = 0; attempt <= retries; attempt += 1) {
    const resp = await fetch(url, { headers }).catch((e) => {
      lastError = e.message;
      return null;
    });
    if (!resp) {
      if (attempt < retries) await sleep(backoffMs(attempt));
      continue;
    }

    if (resp.ok) {
      const body = resp.status === 204 ? null : await resp.json().catch(() => null);
      return { status: resp.status, body, error: null };
    }

    // 限流:等待重置或切换 token
    if (resp.status === 403 || resp.status === 429) {
      const retryAfter = resp.headers.get('retry-after');
      const reset = resp.headers.get('x-ratelimit-reset');
      lastError = `rate-limited ${resp.status}`;
      if (GITHUB_TOKENS.length > 1) {
        // 有多 token → 直接切换,不等
        githubTokenCursor += 1;
        headers.Authorization = `Bearer ${nextGithubToken()}`;
        continue;
      }
      if (retryAfter) {
        await sleep(Number(retryAfter) * 1000);
        continue;
      }
      if (reset) {
        const waitMs = Number(reset) * 1000 - Date.now();
        if (waitMs > 0 && waitMs < 60000) {
          await sleep(waitMs);
          continue;
        }
      }
      if (attempt < retries) {
        await sleep(backoffMs(attempt));
        continue;
      }
    }

    const text = await resp.text().catch(() => '');
    return { status: resp.status, body: null, error: `http ${resp.status}: ${text.slice(0, 200)}` };
  }
  return { status: 0, body: null, error: lastError || 'github api failed' };
}

module.exports = {
  fetchPage,
  fetchJson,
  headRequest,
  githubApi,
  closeBrowser,
  nextProxy,
  getProxyPool: () => PROXY_URLS.slice(),
  getHeaderPresets,
  randomHeaders,
};