build-reject-domainset.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const { promises: fsPromises } = require('fs');
  2. const { resolve: pathResolve } = require('path');
  3. const Piscina = require('piscina');
  4. const { processHosts, processFilterRules } = require('./lib/parse-filter');
  5. const threads = require('os').cpus().length - 1;
  6. (async () => {
  7. /** @type Set<string> */
  8. const domainSets = new Set();
  9. // Parse from remote hosts & domain lists
  10. (await Promise.all([
  11. processHosts('https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext', true),
  12. processHosts('https://raw.githubusercontent.com/hoshsadiq/adblock-nocoin-list/master/hosts.txt'),
  13. processHosts('https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/spy.txt')
  14. ])).forEach(hosts => {
  15. hosts.forEach(host => {
  16. if (host) {
  17. domainSets.add(host);
  18. }
  19. });
  20. });
  21. let previousSize = domainSets.size;
  22. console.log(`Import ${previousSize} rules from hosts files!`);
  23. await fsPromises.readFile(pathResolve(__dirname, '../List/domainset/reject_sukka.conf'), { encoding: 'utf-8' }).then(data => {
  24. data.split('\n').forEach(line => {
  25. const trimmed = line.trim();
  26. if (
  27. line.startsWith('#')
  28. || line.startsWith(' ')
  29. || line.startsWith('\r')
  30. || line.startsWith('\n')
  31. || trimmed === ''
  32. ) {
  33. return;
  34. }
  35. /* if (domainSets.has(line) || domainSets.has(`.${line}`)) {
  36. console.warn(`|${line}| is already in the list!`);
  37. } */
  38. domainSets.add(trimmed);
  39. });
  40. });
  41. previousSize = domainSets.size - previousSize;
  42. console.log(`Import ${previousSize} rules from reject_sukka.conf!`);
  43. // Parse from AdGuard Filters
  44. /** @type Set<string> */
  45. const filterRuleWhitelistDomainSets = new Set([
  46. 'localhost',
  47. 'broadcasthost',
  48. 'ip6-loopback',
  49. 'ip6-localnet',
  50. 'ip6-mcastprefix',
  51. 'ip6-allnodes',
  52. 'ip6-allrouters',
  53. 'ip6-allhosts',
  54. 'mcastprefix',
  55. 'analytics.google.com',
  56. 'msa.cdn.mediaset.net', // Added manually using DOMAIN-KEYWORDS
  57. 'cloud.answerhub.com',
  58. 'ae01.alicdn.com',
  59. 'whoami.akamai.net',
  60. 'whoami.ds.akahelp.net',
  61. 'pxlk9.net.', // This one is malformed from EasyList, which I will manually add instead
  62. 'instant.page', // No, it doesn't violate anyone's privacy. I will whitelist it
  63. 'piwik.pro',
  64. 'mixpanel.com',
  65. 'cdn.mxpnl.com',
  66. 'heapanalytics.com',
  67. 'segment.com',
  68. 'segmentify.com',
  69. 't.co', // pgl yoyo add t.co to the blacklist
  70. 'survicate.com' // AdGuardDNSFilter
  71. ]);
  72. (await Promise.all([
  73. // Easy List
  74. 'https://easylist.to/easylist/easylist.txt',
  75. // AdGuard DNS Filter
  76. 'https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt',
  77. // uBlock Origin Filter List
  78. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters.txt',
  79. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2020.txt',
  80. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2021.txt',
  81. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2022.txt',
  82. // uBlock Origin Badware Risk List
  83. 'https://ublockorigin.github.io/uAssets/filters/badware.txt',
  84. // uBlock Origin Privacy List
  85. 'https://ublockorigin.github.io/uAssets/filters/privacy.txt',
  86. // uBlock Origin Resource Abuse
  87. 'https://ublockorigin.github.io/uAssets/filters/resource-abuse.txt',
  88. // uBlock Origin Unbreak
  89. 'https://ublockorigin.github.io/uAssets/filters/unbreak.txt',
  90. // AdGuard Base Filter
  91. 'https://filters.adtidy.org/extension/ublock/filters/2_without_easylist.txt',
  92. // AdGuard Mobile AD
  93. 'https://filters.adtidy.org/extension/ublock/filters/11.txt',
  94. // AdGuard Tracking Protection
  95. 'https://filters.adtidy.org/extension/ublock/filters/3.txt',
  96. // AdGuard Japanese filter
  97. 'https://filters.adtidy.org/extension/ublock/filters/7.txt',
  98. // AdGuard Chinese filter (EasyList China + AdGuard Chinese filter)
  99. 'https://filters.adtidy.org/extension/ublock/filters/224.txt',
  100. // Easy Privacy
  101. 'https://easylist.to/easylist/easyprivacy.txt',
  102. // Curben's Malware Online UrlHaus
  103. 'https://curben.gitlab.io/malware-filter/urlhaus-filter-agh-online.txt',
  104. // Curben's Phishing Online Filter
  105. 'https://curben.gitlab.io/malware-filter/phishing-filter-agh.txt',
  106. // Curben's PUP List
  107. 'https://curben.gitlab.io/malware-filter/pup-filter-agh.txt',
  108. // GameConsoleAdblockList
  109. 'https://raw.githubusercontent.com/DandelionSprout/adfilt/master/GameConsoleAdblockList.txt',
  110. // PiHoleBlocklist
  111. 'https://raw.githubusercontent.com/Perflyst/PiHoleBlocklist/master/SmartTV-AGH.txt',
  112. ].map(processFilterRules))).forEach(({ white, black }) => {
  113. white.forEach(i => filterRuleWhitelistDomainSets.add(i));
  114. black.forEach(i => domainSets.add(i));
  115. });
  116. previousSize = domainSets.size - previousSize;
  117. console.log(`Import ${previousSize} rules from adguard filters!`);
  118. // Read DOMAIN Keyword
  119. const domainKeywordsSet = new Set();
  120. const domainSuffixSet = new Set();
  121. await fsPromises.readFile(pathResolve(__dirname, '../List/non_ip/reject.conf'), { encoding: 'utf-8' }).then(data => {
  122. data.split('\n').forEach(line => {
  123. if (line.startsWith('DOMAIN-KEYWORD')) {
  124. const [, ...keywords] = line.split(',');
  125. domainKeywordsSet.add(keywords.join(',').trim());
  126. } else if (line.startsWith('DOMAIN-SUFFIX')) {
  127. const [, ...keywords] = line.split(',');
  128. domainSuffixSet.add(keywords.join(',').trim());
  129. }
  130. });
  131. });
  132. console.log(`Import ${domainKeywordsSet.size} black keywords and ${domainSuffixSet.size} black suffixes!`);
  133. previousSize = domainSets.size;
  134. // Dedupe domainSets
  135. console.log(`Start deduping! (${previousSize})`);
  136. const piscina = new Piscina({
  137. filename: pathResolve(__dirname, 'worker/build-reject-domainset-worker.js'),
  138. workerData: domainSets
  139. });
  140. (await Promise.all([
  141. piscina.run(
  142. { keywords: domainKeywordsSet, suffixes: domainSuffixSet },
  143. { name: 'dedupeKeywords' }
  144. ),
  145. piscina.run(
  146. { whiteList: filterRuleWhitelistDomainSets },
  147. { name: 'whitelisted' }
  148. )
  149. ])).forEach(set => {
  150. set.forEach(i => domainSets.delete(i));
  151. });
  152. (await Promise.all(
  153. Array.from(domainSets)
  154. .reduce((result, element, index) => {
  155. const chunk = index % threads;
  156. result[chunk] ??= [];
  157. result[chunk].push(element);
  158. return result;
  159. }, [])
  160. .map(chunk => piscina.run(
  161. { chunk },
  162. { name: 'dedupe' }
  163. ))
  164. )).forEach(set => {
  165. set.forEach(i => domainSets.delete(i));
  166. });
  167. console.log(`Deduped ${previousSize - domainSets.size} rules!`);
  168. return fsPromises.writeFile(
  169. pathResolve(__dirname, '../List/domainset/reject.conf'),
  170. `${[...domainSets].join('\n')}\n`,
  171. { encoding: 'utf-8' });
  172. })();