validate-gfwlist.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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';
  6. import { readFileByLine } from './lib/fetch-text-by-line';
  7. import path from '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. const top500Gfwed = new Set<string>();
  55. const res = await fetch('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000');
  56. const stream = Readable.fromWeb(res.body!).pipe(parse());
  57. const trie = createTrie(blackSet);
  58. for await (const [domain] of stream) {
  59. if (trie.has(domain)) {
  60. top500Gfwed.add(domain);
  61. }
  62. }
  63. const notIncludedTop500Gfwed = new Set<string>(top500Gfwed);
  64. const runAgainstRuleset = async (ruleset: string) => {
  65. for await (const l of readFileByLine(ruleset)) {
  66. const line = processLine(l);
  67. if (!line) continue;
  68. const [type, domain] = line.split(',');
  69. if (type === 'DOMAIN-SUFFIX') {
  70. if (top500Gfwed.has(domain)) {
  71. notIncludedTop500Gfwed.delete(domain);
  72. }
  73. } else if (type === 'DOMAIN-KEYWORD') {
  74. for (const d of top500Gfwed) {
  75. if (d.includes(domain)) {
  76. notIncludedTop500Gfwed.delete(d);
  77. }
  78. }
  79. }
  80. }
  81. };
  82. await Promise.all([
  83. runAgainstRuleset(path.resolve(import.meta.dir, '../Source/non_ip/global.conf')),
  84. runAgainstRuleset(path.resolve(import.meta.dir, '../Source/non_ip/telegram.conf')),
  85. runAgainstRuleset(path.resolve(import.meta.dir, '../List/non_ip/stream.conf'))
  86. ]);
  87. console.log(notIncludedTop500Gfwed);
  88. return [
  89. whiteSet,
  90. blackSet,
  91. trie,
  92. top500Gfwed
  93. ] as const;
  94. };
  95. if (import.meta.main) {
  96. parseGfwList();
  97. }