build-reject-domainset.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. const { promises: fsPromises } = require('fs');
  2. const { resolve: pathResolve } = require('path');
  3. const Piscina = require('piscina');
  4. const { processHosts, processFilterRules, preprocessFullDomainSetBeforeUsedAsWorkerData } = 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. console.time('Total Time - build-reject-domain-set');
  10. /** @type Set<string> */
  11. const domainSets = new Set();
  12. console.log('Downloading hosts file...');
  13. console.time('* Download and process Hosts');
  14. // Parse from remote hosts & domain lists
  15. (await Promise.all([
  16. processHosts('https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext', true),
  17. processHosts('https://raw.githubusercontent.com/hoshsadiq/adblock-nocoin-list/master/hosts.txt'),
  18. processHosts('https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/spy.txt')
  19. ])).forEach(hosts => {
  20. hosts.forEach(host => {
  21. if (host) {
  22. domainSets.add(host);
  23. }
  24. });
  25. });
  26. console.timeEnd('* Download and process Hosts');
  27. let previousSize = domainSets.size;
  28. console.log(`Import ${previousSize} rules from hosts files!`);
  29. await fsPromises.readFile(pathResolve(__dirname, '../List/domainset/reject_sukka.conf'), { encoding: 'utf-8' }).then(data => {
  30. data.split('\n').forEach(line => {
  31. const trimmed = line.trim();
  32. if (
  33. line.startsWith('#')
  34. || line.startsWith(' ')
  35. || line.startsWith('\r')
  36. || line.startsWith('\n')
  37. || trimmed === ''
  38. ) {
  39. return;
  40. }
  41. /* if (domainSets.has(line) || domainSets.has(`.${line}`)) {
  42. console.warn(`|${line}| is already in the list!`);
  43. } */
  44. domainSets.add(trimmed);
  45. });
  46. });
  47. previousSize = domainSets.size - previousSize;
  48. console.log(`Import ${previousSize} rules from reject_sukka.conf!`);
  49. // Parse from AdGuard Filters
  50. /** @type Set<string> */
  51. const filterRuleWhitelistDomainSets = new Set([
  52. 'localhost',
  53. 'broadcasthost',
  54. 'ip6-loopback',
  55. 'ip6-localnet',
  56. 'ip6-mcastprefix',
  57. 'ip6-allnodes',
  58. 'ip6-allrouters',
  59. 'ip6-allhosts',
  60. 'mcastprefix',
  61. 'skk.moe',
  62. 'analytics.google.com',
  63. 'msa.cdn.mediaset.net', // Added manually using DOMAIN-KEYWORDS
  64. 'cloud.answerhub.com',
  65. 'ae01.alicdn.com',
  66. 'whoami.akamai.net',
  67. 'whoami.ds.akahelp.net',
  68. 'pxlk9.net.', // This one is malformed from EasyList, which I will manually add instead
  69. 'instant.page', // No, it doesn't violate anyone's privacy. I will whitelist it
  70. 'piwik.pro',
  71. 'mixpanel.com',
  72. 'cdn.mxpnl.com',
  73. 'heapanalytics.com',
  74. 'segment.com',
  75. 'segmentify.com',
  76. 't.co', // pgl yoyo add t.co to the blacklist
  77. 'survicate.com', // AdGuardDNSFilter
  78. 'perfops.io', // AdGuardDNSFilter
  79. 'd2axgrpnciinw7.cloudfront.net', // ADGuardDNSFilter
  80. 'tb-lb.sb-cd.com', // AdGuard
  81. 'storage.yandexcloud.net', // phishing list
  82. 'login.microsoftonline.com' // phishing list
  83. ]);
  84. console.time('* Download and process AdBlock Filter Rules');
  85. (await Promise.all([
  86. // Easy List
  87. [
  88. 'https://easylist.to/easylist/easylist.txt',
  89. [
  90. 'https://easylist-downloads.adblockplus.org/easylist.txt',
  91. 'https://raw.githubusercontent.com/easylist/easylist/gh-pages/easylist.txt',
  92. 'https://secure.fanboy.co.nz/easylist.txt'
  93. ]
  94. ],
  95. // AdGuard DNS Filter
  96. 'https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt',
  97. // uBlock Origin Filter List
  98. [
  99. 'https://ublockorigin.github.io/uAssets/filters/filters.txt',
  100. [
  101. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters.txt',
  102. 'https://ublockorigin.pages.dev/filters/filters.txt'
  103. ]
  104. ],
  105. [
  106. 'https://ublockorigin.github.io/uAssets/filters/filters-2020.txt',
  107. [
  108. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2020.txt',
  109. 'https://ublockorigin.pages.dev/filters/filters-2020.txt'
  110. ]
  111. ],
  112. [
  113. 'https://ublockorigin.github.io/uAssets/filters/filters-2021.txt',
  114. [
  115. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2021.txt',
  116. 'https://ublockorigin.pages.dev/filters/filters-2021.txt'
  117. ]
  118. ],
  119. [
  120. 'https://ublockorigin.github.io/uAssets/filters/filters-2022.txt',
  121. [
  122. 'https://ublockorigin.github.io/uAssetsCDN/filters/filters-2022.txt',
  123. 'https://ublockorigin.pages.dev/filters/filters-2022.txt'
  124. ]
  125. ],
  126. // uBlock Origin Badware Risk List
  127. [
  128. 'https://ublockorigin.github.io/uAssets/filters/badware.txt',
  129. [
  130. 'https://ublockorigin.github.io/uAssetsCDN/filters/badware.txt',
  131. 'https://ublockorigin.pages.dev/filters/badware.txt'
  132. ]
  133. ],
  134. // uBlock Origin Privacy List
  135. [
  136. 'https://ublockorigin.github.io/uAssets/filters/privacy.txt',
  137. [
  138. 'https://ublockorigin.github.io/uAssetsCDN/filters/privacy.txt',
  139. 'https://ublockorigin.pages.dev/filters/privacy.txt'
  140. ]
  141. ],
  142. // uBlock Origin Resource Abuse
  143. [
  144. 'https://ublockorigin.github.io/uAssets/filters/resource-abuse.txt',
  145. [
  146. 'https://ublockorigin.github.io/uAssetsCDN/filters/resource-abuse.txt',
  147. 'https://ublockorigin.pages.dev/filters/resource-abuse.txt'
  148. ]
  149. ],
  150. // uBlock Origin Unbreak
  151. [
  152. 'https://ublockorigin.github.io/uAssets/filters/unbreak.txt',
  153. [
  154. 'https://ublockorigin.github.io/uAssetsCDN/filters/unbreak.txt',
  155. 'https://ublockorigin.pages.dev/filters/unbreak.txt'
  156. ]
  157. ],
  158. // AdGuard Base Filter
  159. 'https://filters.adtidy.org/extension/ublock/filters/2_without_easylist.txt',
  160. // AdGuard Mobile AD
  161. 'https://filters.adtidy.org/extension/ublock/filters/11.txt',
  162. // AdGuard Tracking Protection
  163. 'https://filters.adtidy.org/extension/ublock/filters/3.txt',
  164. // AdGuard Japanese filter
  165. 'https://filters.adtidy.org/extension/ublock/filters/7.txt',
  166. // AdGuard Chinese filter (EasyList China + AdGuard Chinese filter)
  167. 'https://filters.adtidy.org/extension/ublock/filters/224.txt',
  168. // Easy Privacy
  169. [
  170. 'https://easylist.to/easylist/easyprivacy.txt',
  171. [
  172. 'https://secure.fanboy.co.nz/easyprivacy.txt',
  173. 'https://raw.githubusercontent.com/easylist/easylist/gh-pages/easyprivacy.txt',
  174. 'https://easylist-downloads.adblockplus.org/easyprivacy.txt'
  175. ]
  176. ],
  177. // Curben's UrlHaus Malicious URL Blocklist
  178. [
  179. 'https://curbengh.github.io/urlhaus-filter/urlhaus-filter-agh-online.txt',
  180. [
  181. 'https://urlhaus-filter.pages.dev/urlhaus-filter-agh-online.txt',
  182. // Prefer mirror, since malware-filter.gitlab.io has not been updated for a while
  183. // 'https://malware-filter.gitlab.io/urlhaus-filter/urlhaus-filter-agh-online.txt'
  184. ]
  185. ],
  186. // Curben's Phishing URL Blocklist
  187. [
  188. 'https://curbengh.github.io/phishing-filter/phishing-filter-agh.txt',
  189. [
  190. 'https://phishing-filter.pages.dev/phishing-filter-agh.txt',
  191. // Prefer mirror, since malware-filter.gitlab.io has not been updated for a while
  192. // 'https://malware-filter.gitlab.io/malware-filter/phishing-filter-agh.txt'
  193. ]
  194. ],
  195. // Curben's PUP Domains Blocklist
  196. [
  197. 'https://curbengh.github.io/pup-filter/pup-filter-agh.txt',
  198. [
  199. 'https://pup-filter.pages.dev/pup-filter-agh.txt',
  200. // Prefer mirror, since malware-filter.gitlab.io has not been updated for a while
  201. // 'https://malware-filter.gitlab.io/malware-filter/pup-filter-agh.txt'
  202. ]
  203. ],
  204. // GameConsoleAdblockList
  205. 'https://raw.githubusercontent.com/DandelionSprout/adfilt/master/GameConsoleAdblockList.txt',
  206. // PiHoleBlocklist
  207. 'https://raw.githubusercontent.com/Perflyst/PiHoleBlocklist/master/SmartTV-AGH.txt',
  208. // Spam404
  209. 'https://raw.githubusercontent.com/Spam404/lists/master/adblock-list.txt'
  210. ].map(input => {
  211. if (typeof input === 'string') {
  212. return processFilterRules(input);
  213. }
  214. if (Array.isArray(input) && input.length === 2) {
  215. return processFilterRules(input[0], input[1]);
  216. }
  217. }))).forEach(({ white, black }) => {
  218. white.forEach(i => filterRuleWhitelistDomainSets.add(i));
  219. black.forEach(i => domainSets.add(i));
  220. });
  221. console.timeEnd('* Download and process AdBlock Filter Rules');
  222. previousSize = domainSets.size - previousSize;
  223. console.log(`Import ${previousSize} rules from adguard filters!`);
  224. // Read DOMAIN Keyword
  225. const domainKeywordsSet = new Set();
  226. const domainSuffixSet = new Set();
  227. await fsPromises.readFile(pathResolve(__dirname, '../List/non_ip/reject.conf'), { encoding: 'utf-8' }).then(data => {
  228. data.split('\n').forEach(line => {
  229. if (line.startsWith('DOMAIN-KEYWORD')) {
  230. const [, ...keywords] = line.split(',');
  231. domainKeywordsSet.add(keywords.join(',').trim());
  232. } else if (line.startsWith('DOMAIN-SUFFIX')) {
  233. const [, ...keywords] = line.split(',');
  234. domainSuffixSet.add(keywords.join(',').trim());
  235. }
  236. });
  237. });
  238. // Read Special Phishing Suffix list
  239. await fsPromises.readFile(pathResolve(__dirname, '../List/domainset/reject_phishing.conf'), { encoding: 'utf-8' }).then(data => {
  240. data.split('\n').forEach(line => {
  241. const trimmed = line.trim();
  242. if (
  243. line.startsWith('#')
  244. || line.startsWith(' ')
  245. || line.startsWith('\r')
  246. || line.startsWith('\n')
  247. || trimmed === ''
  248. ) {
  249. return;
  250. }
  251. /* if (domainSets.has(line) || domainSets.has(`.${line}`)) {
  252. console.warn(`|${line}| is already in the list!`);
  253. } */
  254. domainSuffixSet.add(trimmed);
  255. });
  256. });
  257. console.log(`Import ${domainKeywordsSet.size} black keywords and ${domainSuffixSet.size} black suffixes!`);
  258. previousSize = domainSets.size;
  259. // Dedupe domainSets
  260. console.log(`Start deduping from black keywords/suffixes! (${previousSize})`);
  261. console.time(`* Dedupe from black keywords/suffixes`);
  262. for (const domain of domainSets) {
  263. let isTobeRemoved = false;
  264. for (const suffix of domainSuffixSet) {
  265. if (domain.endsWith(suffix)) {
  266. isTobeRemoved = true;
  267. break;
  268. }
  269. }
  270. if (!isTobeRemoved) {
  271. for (const keyword of domainKeywordsSet) {
  272. if (domain.includes(keyword)) {
  273. isTobeRemoved = true;
  274. break;
  275. }
  276. }
  277. }
  278. if (!isTobeRemoved) {
  279. for (const white of filterRuleWhitelistDomainSets) {
  280. if (domain.includes(white) || white.includes(domain)) {
  281. isTobeRemoved = true;
  282. break;
  283. }
  284. }
  285. }
  286. if (isTobeRemoved) {
  287. domainSets.delete(domain);
  288. }
  289. }
  290. console.timeEnd(`* Dedupe from black keywords/suffixes`);
  291. console.log(`Deduped ${previousSize} - ${domainSets.size} = ${previousSize - domainSets.size} from black keywords and suffixes!`);
  292. previousSize = domainSets.size;
  293. // Dedupe domainSets
  294. console.log(`Start deduping! (${previousSize})`);
  295. const START_TIME = Date.now();
  296. const piscina = new Piscina({
  297. filename: pathResolve(__dirname, 'worker/build-reject-domainset-worker.js'),
  298. workerData: preprocessFullDomainSetBeforeUsedAsWorkerData([...domainSets]),
  299. idleTimeout: 50,
  300. minThreads: threads,
  301. maxThreads: threads
  302. });
  303. console.log(`Launching ${threads} threads...`)
  304. const tasksArray = Array.from(domainSets)
  305. .reduce((result, element, index) => {
  306. const chunk = index % threads;
  307. result[chunk] ??= [];
  308. result[chunk].push(element);
  309. return result;
  310. }, []);
  311. (
  312. await Promise.all(
  313. Array.from(domainSets)
  314. .reduce((result, element, index) => {
  315. const chunk = index % threads;
  316. result[chunk] ??= [];
  317. result[chunk].push(element);
  318. return result;
  319. }, [])
  320. .map(chunk => piscina.run({ chunk }, { name: 'dedupe' }))
  321. )
  322. ).forEach((result, taskIndex) => {
  323. const chunk = tasksArray[taskIndex];
  324. result.forEach((value, index) => {
  325. if (value === 1) {
  326. domainSets.delete(chunk[index])
  327. }
  328. })
  329. });
  330. console.log(`* Dedupe from covered subdomain - ${(Date.now() - START_TIME) / 1000}s`);
  331. console.log(`Deduped ${previousSize - domainSets.size} rules!`);
  332. await Promise.all([
  333. fsPromises.writeFile(
  334. pathResolve(__dirname, '../List/domainset/reject.conf'),
  335. `${[...domainSets].join('\n')}\n`,
  336. { encoding: 'utf-8' }
  337. ),
  338. piscina.destroy()
  339. ]);
  340. console.timeEnd('Total Time - build-reject-domain-set');
  341. if (piscina.queueSize === 0) {
  342. process.exit(0);
  343. }
  344. })();