validate-domestic.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { fetchRemoteTextByLine, readFileByLine } from './lib/fetch-text-by-line';
  2. import { Readable } from 'stream';
  3. import { parse } from 'csv-parse/sync';
  4. import { createTrie } from './lib/trie';
  5. import path from 'path';
  6. import { processLine } from './lib/process-line';
  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. if (line.startsWith('server=/') && line.endsWith('/114.114.114.114')) {
  11. const domain = line.slice(8, -16);
  12. set.add(domain);
  13. }
  14. }
  15. const trie = createTrie(set);
  16. const top5000 = new Set<string>();
  17. const res = await (await fetch('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000')).text();
  18. const stream = parse(res);
  19. for await (const [domain] of stream) {
  20. if (trie.has(domain)) {
  21. top5000.add(domain);
  22. }
  23. console.log({ domain });
  24. }
  25. const notIncludedDomestic = new Set<string>(top5000);
  26. const runAgainstRuleset = async (ruleset: string) => {
  27. for await (const l of readFileByLine(ruleset)) {
  28. const line = processLine(l);
  29. if (!line) continue;
  30. const [type, domain] = line.split(',');
  31. if (type === 'DOMAIN-SUFFIX') {
  32. if (top5000.has(domain)) {
  33. notIncludedDomestic.delete(domain);
  34. }
  35. } else if (type === 'DOMAIN-KEYWORD') {
  36. for (const d of top5000) {
  37. if (d.includes(domain)) {
  38. notIncludedDomestic.delete(d);
  39. }
  40. }
  41. }
  42. }
  43. };
  44. await Promise.all([
  45. runAgainstRuleset(path.resolve(import.meta.dir, '../List/non_ip/domestic.conf'))
  46. ]);
  47. console.log(notIncludedDomestic.size, notIncludedDomestic);
  48. };
  49. if (import.meta.main) {
  50. parseDomesticList();
  51. }