validate-domestic.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { readFileByLine } from './lib/fetch-text-by-line';
  2. import { parse } from 'csv-parse/sync';
  3. import { createTrie } from './lib/trie';
  4. import path from 'node:path';
  5. import { processLine } from './lib/process-line';
  6. import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
  7. import { SOURCE_DIR } from './constants/dir';
  8. import { fetchWithRetry } from './lib/fetch-retry';
  9. export const parseDomesticList = async () => {
  10. const trie = createTrie(await parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf'));
  11. const top5000 = new Set<string>();
  12. const res = await (await fetchWithRetry('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000', {
  13. headers: {
  14. accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  15. 'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,es;q=0.5',
  16. 'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
  17. 'sec-ch-ua-mobile': '?0',
  18. 'sec-ch-ua-platform': '"macOS"',
  19. 'sec-fetch-dest': 'document',
  20. 'sec-fetch-mode': 'navigate',
  21. 'sec-fetch-site': 'none',
  22. 'sec-fetch-user': '?1',
  23. 'upgrade-insecure-requests': '1'
  24. }
  25. })).text();
  26. const stream = parse(res);
  27. for await (const [domain] of stream) {
  28. if (trie.has(domain)) {
  29. top5000.add(domain);
  30. }
  31. console.log({ domain });
  32. }
  33. const notIncludedDomestic = new Set<string>(top5000);
  34. const runAgainstRuleset = async (ruleset: string) => {
  35. for await (const l of readFileByLine(ruleset)) {
  36. const line = processLine(l);
  37. if (!line) continue;
  38. const [type, domain] = line.split(',');
  39. if (type === 'DOMAIN-SUFFIX') {
  40. if (top5000.has(domain)) {
  41. notIncludedDomestic.delete(domain);
  42. }
  43. } else if (type === 'DOMAIN-KEYWORD') {
  44. for (const d of top5000) {
  45. if (d.includes(domain)) {
  46. notIncludedDomestic.delete(d);
  47. }
  48. }
  49. }
  50. }
  51. };
  52. // await Promise.all([
  53. await runAgainstRuleset(path.resolve(SOURCE_DIR, 'non_ip/domestic.conf'));
  54. // ]);
  55. console.log(notIncludedDomestic.size, notIncludedDomestic);
  56. };