validate-gfwlist.ts 3.5 KB

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