validate-domestic.ts 2.4 KB

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