validate-domestic.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. export const parseDomesticList = async () => {
  9. const trie = createTrie(await parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf'));
  10. const top5000 = new Set<string>();
  11. const res = await (await fetch('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000', {
  12. headers: {
  13. 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',
  14. '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',
  15. 'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
  16. 'sec-ch-ua-mobile': '?0',
  17. 'sec-ch-ua-platform': '"macOS"',
  18. 'sec-fetch-dest': 'document',
  19. 'sec-fetch-mode': 'navigate',
  20. 'sec-fetch-site': 'none',
  21. 'sec-fetch-user': '?1',
  22. 'upgrade-insecure-requests': '1'
  23. }
  24. })).text();
  25. const stream = parse(res);
  26. for await (const [domain] of stream) {
  27. if (trie.has(domain)) {
  28. top5000.add(domain);
  29. }
  30. console.log({ domain });
  31. }
  32. const notIncludedDomestic = new Set<string>(top5000);
  33. const runAgainstRuleset = async (ruleset: string) => {
  34. for await (const l of readFileByLine(ruleset)) {
  35. const line = processLine(l);
  36. if (!line) continue;
  37. const [type, domain] = line.split(',');
  38. if (type === 'DOMAIN-SUFFIX') {
  39. if (top5000.has(domain)) {
  40. notIncludedDomestic.delete(domain);
  41. }
  42. } else if (type === 'DOMAIN-KEYWORD') {
  43. for (const d of top5000) {
  44. if (d.includes(domain)) {
  45. notIncludedDomestic.delete(d);
  46. }
  47. }
  48. }
  49. }
  50. };
  51. // await Promise.all([
  52. await runAgainstRuleset(path.resolve(SOURCE_DIR, 'non_ip/domestic.conf'));
  53. // ]);
  54. console.log(notIncludedDomestic.size, notIncludedDomestic);
  55. };