validate-domestic.ts 2.4 KB

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