validate-gfwlist.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { processLine } from './lib/process-line';
  2. import { normalizeDomain } from './lib/normalize-domain';
  3. import { createTrie } from './lib/trie';
  4. // import { Readable } from 'stream';
  5. import { parse } from 'csv-parse/sync';
  6. import { readFileByLine } from './lib/fetch-text-by-line';
  7. import path from 'node:path';
  8. import { SOURCE_DIR } from './constants/dir';
  9. import { $fetch } from './lib/make-fetch-happen';
  10. export async function parseGfwList() {
  11. const whiteSet = new Set<string>();
  12. const blackSet = new Set<string>();
  13. const text = await (await $fetch('https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt')).text();
  14. for (const l of atob(text).split('\n')) {
  15. const line = processLine(l);
  16. if (!line) continue;
  17. if (line[0] === '[') {
  18. continue;
  19. }
  20. if (line.includes('.*')) {
  21. continue;
  22. }
  23. if (line.includes('*')) {
  24. continue;
  25. }
  26. if (line.startsWith('@@||')) {
  27. whiteSet.add(line.slice(4));
  28. continue;
  29. }
  30. if (line.startsWith('@@|http://')) {
  31. whiteSet.add(line.slice(8));
  32. continue;
  33. }
  34. if (line.startsWith('@@|https://')) {
  35. whiteSet.add(line.slice(9));
  36. continue;
  37. }
  38. if (line.startsWith('||')) {
  39. blackSet.add(line.slice(2));
  40. continue;
  41. }
  42. if (line.startsWith('|')) {
  43. blackSet.add(line.slice(1));
  44. continue;
  45. }
  46. if (line.startsWith('.')) {
  47. blackSet.add(line.slice(1));
  48. continue;
  49. }
  50. const d = normalizeDomain(line);
  51. if (d) {
  52. blackSet.add(d);
  53. continue;
  54. }
  55. }
  56. for (const l of (await (await $fetch('https://raw.githubusercontent.com/Loyalsoldier/cn-blocked-domain/release/domains.txt')).text()).split('\n')) {
  57. blackSet.add(l);
  58. }
  59. const top500Gfwed = new Set<string>();
  60. const res = await (await $fetch('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000', {
  61. headers: {
  62. 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',
  63. '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',
  64. 'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
  65. 'sec-ch-ua-mobile': '?0',
  66. 'sec-ch-ua-platform': '"macOS"',
  67. 'sec-fetch-dest': 'document',
  68. 'sec-fetch-mode': 'navigate',
  69. 'sec-fetch-site': 'none',
  70. 'sec-fetch-user': '?1',
  71. 'upgrade-insecure-requests': '1'
  72. }
  73. })).text();
  74. const topDomains = parse(res);
  75. const trie = createTrie(blackSet);
  76. for await (const [domain] of topDomains) {
  77. if (trie.has(domain)) {
  78. top500Gfwed.add(domain);
  79. }
  80. }
  81. const notIncludedTop500Gfwed = new Set<string>(top500Gfwed);
  82. const runAgainstRuleset = async (ruleset: string) => {
  83. for await (const l of readFileByLine(ruleset)) {
  84. const line = processLine(l);
  85. if (!line) continue;
  86. const [type, domain] = line.split(',');
  87. if (type === 'DOMAIN-SUFFIX') {
  88. if (top500Gfwed.has(domain)) {
  89. notIncludedTop500Gfwed.delete(domain);
  90. }
  91. } else if (type === 'DOMAIN-KEYWORD') {
  92. for (const d of top500Gfwed) {
  93. if (d.includes(domain)) {
  94. notIncludedTop500Gfwed.delete(d);
  95. }
  96. }
  97. }
  98. }
  99. };
  100. await Promise.all([
  101. runAgainstRuleset(path.join(SOURCE_DIR, 'non_ip/global.conf')),
  102. runAgainstRuleset(path.join(SOURCE_DIR, 'non_ip/telegram.conf')),
  103. runAgainstRuleset(path.resolve(SOURCE_DIR, 'non_ip/stream.conf'))
  104. ]);
  105. console.log(notIncludedTop500Gfwed);
  106. return [
  107. whiteSet,
  108. blackSet,
  109. trie,
  110. top500Gfwed
  111. ] as const;
  112. }