build-reject-domainset.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 cpuCount = require('os').cpus().length;
  6. const { isCI } = require('ci-info');
  7. const threads = isCI ? cpuCount : cpuCount / 2;
  8. (async () => {
  9. /** @type Set<string> */
  10. const domainSets = new Set();
  11. // Parse from remote hosts & domain lists
  12. (await Promise.all([
  13. processHosts('https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext', true),
  14. processHosts('https://raw.githubusercontent.com/hoshsadiq/adblock-nocoin-list/master/hosts.txt'),
  15. processHosts('https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/spy.txt')
  16. ])).forEach(hosts => {
  17. hosts.forEach(host => {
  18. if (host) {
  19. domainSets.add(host);
  20. }
  21. });
  22. });
  23. let previousSize = domainSets.size;
  24. console.log(`Import ${previousSize} rules from hosts files!`);
  25. await fsPromises.readFile(pathResolve(__dirname, '../List/domainset/reject_sukka.conf'), { encoding: 'utf-8' }).then(data => {
  26. data.split('\n').forEach(line => {
  27. const trimmed = line.trim();
  28. if (
  29. line.startsWith('#')
  30. || line.startsWith(' ')
  31. || line.startsWith('\r')
  32. || line.startsWith('\n')
  33. || trimmed === ''
  34. ) {
  35. return;
  36. }
  37. /* if (domainSets.has(line) || domainSets.has(`.${line}`)) {
  38. console.warn(`|${line}| is already in the list!`);
  39. } */
  40. domainSets.add(trimmed);
  41. });
  42. });
  43. previousSize = domainSets.size - previousSize;
  44. console.log(`Import ${previousSize} rules from reject_sukka.conf!`);
  45. // Parse from AdGuard Filters
  46. /** @type Set<string> */
  47. const filterRuleWhitelistDomainSets = new Set([
  48. 'localhost',
  49. 'broadcasthost',
  50. 'ip6-loopback',
  51. 'ip6-localnet',
  52. 'ip6-mcastprefix',
  53. 'ip6-allnodes',
  54. 'ip6-allrouters',
  55. 'ip6-allhosts',
  56. 'mcastprefix',
  57. 'analytics.google.com',
  58. 'msa.cdn.mediaset.net', // Added manually using DOMAIN-KEYWORDS
  59. 'cloud.answerhub.com',
  60. 'ae01.alicdn.com',
  61. 'whoami.akamai.net',
  62. 'whoami.ds.akahelp.net',
  63. 'pxlk9.net.', // This one is malformed from EasyList, which I will manually add instead
  64. 'instant.page', // No, it doesn't violate anyone's privacy. I will whitelist it
  65. 'piwik.pro',
  66. 'mixpanel.com',
  67. 'cdn.mxpnl.com',
  68. 'heapanalytics.com',
  69. 'segment.com',
  70. 'segmentify.com',
  71. 't.co', // pgl yoyo add t.co to the blacklist
  72. 'survicate.com', // AdGuardDNSFilter
  73. 'perfops.io', // AdGuardDNSFilter
  74. 'd2axgrpnciinw7.cloudfront.net', // ADGuardDNSFilter
  75. 'tb-lb.sb-cd.com', // AdGuard
  76. 'storage.yandexcloud.net' // phishing list
  77. ]);
  78. (await Promise.all([
  79. // Easy List
  80. [
  81. 'https://easylist.to/easylist/easylist.txt',
  82. [
  83. 'https://easylist-downloads.adblockplus.org/easylist.txt',
  84. 'https://secure.fanboy.co.nz/easylist.txt'
  85. ]
  86. ],
  87. // AdGuard DNS Filter
  88. 'https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt',
  89. // uBlock Origin Filter List
  90. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters.txt',
  91. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2020.txt',
  92. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2021.txt',
  93. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2022.txt',
  94. // uBlock Origin Badware Risk List
  95. 'https://ublockorigin.github.io/uAssets/filters/badware.txt',
  96. // uBlock Origin Privacy List
  97. 'https://ublockorigin.github.io/uAssets/filters/privacy.txt',
  98. // uBlock Origin Resource Abuse
  99. 'https://ublockorigin.github.io/uAssets/filters/resource-abuse.txt',
  100. // uBlock Origin Unbreak
  101. 'https://ublockorigin.github.io/uAssets/filters/unbreak.txt',
  102. // AdGuard Base Filter
  103. 'https://filters.adtidy.org/extension/ublock/filters/2_without_easylist.txt',
  104. // AdGuard Mobile AD
  105. 'https://filters.adtidy.org/extension/ublock/filters/11.txt',
  106. // AdGuard Tracking Protection
  107. 'https://filters.adtidy.org/extension/ublock/filters/3.txt',
  108. // AdGuard Japanese filter
  109. 'https://filters.adtidy.org/extension/ublock/filters/7.txt',
  110. // AdGuard Chinese filter (EasyList China + AdGuard Chinese filter)
  111. 'https://filters.adtidy.org/extension/ublock/filters/224.txt',
  112. // Easy Privacy
  113. [
  114. 'https://easylist.to/easylist/easyprivacy.txt',
  115. [
  116. 'https://secure.fanboy.co.nz/easyprivacy.txt',
  117. 'https://easylist-downloads.adblockplus.org/easyprivacy.txt'
  118. ]
  119. ],
  120. // Curben's Malware Online UrlHaus
  121. 'https://malware-filter.gitlab.io/malware-filter/urlhaus-filter-agh-online.txt',
  122. // Curben's Phishing Online Filter
  123. 'https://malware-filter.gitlab.io/malware-filter/phishing-filter-agh.txt',
  124. // Curben's PUP List
  125. 'https://malware-filter.gitlab.io/malware-filter/pup-filter-agh.txt',
  126. // GameConsoleAdblockList
  127. 'https://raw.githubusercontent.com/DandelionSprout/adfilt/master/GameConsoleAdblockList.txt',
  128. // PiHoleBlocklist
  129. 'https://raw.githubusercontent.com/Perflyst/PiHoleBlocklist/master/SmartTV-AGH.txt',
  130. ].map(input => {
  131. if (typeof input === 'string') {
  132. return processFilterRules(input);
  133. }
  134. if (Array.isArray(input) && input.length === 2) {
  135. return processFilterRules(input[0], input[1]);
  136. }
  137. }))).forEach(({ white, black }) => {
  138. white.forEach(i => filterRuleWhitelistDomainSets.add(i));
  139. black.forEach(i => domainSets.add(i));
  140. });
  141. previousSize = domainSets.size - previousSize;
  142. console.log(`Import ${previousSize} rules from adguard filters!`);
  143. // Read DOMAIN Keyword
  144. const domainKeywordsSet = new Set();
  145. const domainSuffixSet = new Set();
  146. await fsPromises.readFile(pathResolve(__dirname, '../List/non_ip/reject.conf'), { encoding: 'utf-8' }).then(data => {
  147. data.split('\n').forEach(line => {
  148. if (line.startsWith('DOMAIN-KEYWORD')) {
  149. const [, ...keywords] = line.split(',');
  150. domainKeywordsSet.add(keywords.join(',').trim());
  151. } else if (line.startsWith('DOMAIN-SUFFIX')) {
  152. const [, ...keywords] = line.split(',');
  153. domainSuffixSet.add(keywords.join(',').trim());
  154. }
  155. });
  156. });
  157. console.log(`Import ${domainKeywordsSet.size} black keywords and ${domainSuffixSet.size} black suffixes!`);
  158. previousSize = domainSets.size;
  159. // Dedupe domainSets
  160. console.log(`Start deduping! (${previousSize})`);
  161. const toBeRemoved = new Set();
  162. for (const domain of domainSets) {
  163. let isTobeRemoved = false;
  164. for (const keyword of domainKeywordsSet) {
  165. if (domain.includes(keyword) || keyword.includes(domain)) {
  166. isTobeRemoved = true;
  167. break;
  168. }
  169. }
  170. if (!isTobeRemoved) {
  171. for (const suffix of domainSuffixSet) {
  172. if (domain.endsWith(suffix)) {
  173. isTobeRemoved = true;
  174. break;
  175. }
  176. }
  177. }
  178. if (!isTobeRemoved) {
  179. for (const white of filterRuleWhitelistDomainSets) {
  180. if (domain.includes(white) || white.includes(domain)) {
  181. isTobeRemoved = true;
  182. break;
  183. }
  184. }
  185. }
  186. if (isTobeRemoved) {
  187. toBeRemoved.add(domain);
  188. }
  189. }
  190. toBeRemoved.forEach((removed) => {
  191. domainSets.delete(removed)
  192. });
  193. // Dedupe domainSets
  194. console.log(`Deduped ${previousSize - domainSets.size} from black keywords and suffixes!`);
  195. previousSize = domainSets.size;
  196. // Dedupe domainSets
  197. console.log(`Start deduping! (${previousSize})`);
  198. const piscina = new Piscina({
  199. filename: pathResolve(__dirname, 'worker/build-reject-domainset-worker.js'),
  200. workerData: [...domainSets]
  201. });
  202. console.log(`Launching ${threads} threads...`)
  203. const tasksArray = Array.from(domainSets)
  204. .reduce((result, element, index) => {
  205. const chunk = index % threads;
  206. result[chunk] ??= [];
  207. result[chunk].push(element);
  208. return result;
  209. }, []);
  210. (await Promise.all(
  211. Array.from(domainSets)
  212. .reduce((result, element, index) => {
  213. const chunk = index % threads;
  214. result[chunk] ??= [];
  215. result[chunk].push(element);
  216. return result;
  217. }, [])
  218. .map(chunk => piscina.run(
  219. { chunk }
  220. ))
  221. )).forEach((result, taskIndex) => {
  222. const chunk = tasksArray[taskIndex];
  223. result.forEach((value, index) => {
  224. if (value === 1) {
  225. domainSets.delete(chunk[index])
  226. }
  227. })
  228. });
  229. console.log(`Deduped ${previousSize - domainSets.size} rules!`);
  230. return fsPromises.writeFile(
  231. pathResolve(__dirname, '../List/domainset/reject.conf'),
  232. `${[...domainSets].join('\n')}\n`,
  233. { encoding: 'utf-8' });
  234. })();