validate-domestic.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { 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 { SOURCE_DIR } from './constants/dir';
  7. import { $fetch } from './lib/make-fetch-happen';
  8. import { parseFelixDnsmasqFromResp } from './lib/parse-dnsmasq';
  9. async function parseFelixDnsmasq(url: string): Promise<string[]> {
  10. const resp = await $fetch(url);
  11. return parseFelixDnsmasqFromResp(resp);
  12. }
  13. export async function parseDomesticList() {
  14. const trie = createTrie(await parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf'));
  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. await runAgainstRuleset(path.resolve(SOURCE_DIR, 'non_ip/domestic.conf'));
  58. // ]);
  59. console.log(notIncludedDomestic.size, notIncludedDomestic);
  60. }