validate-domestic.ts 2.4 KB

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