validate-domestic.ts 1.8 KB

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