validate-gfwlist.ts 3.5 KB

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