build-anti-bogus-domain.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // @ts-check
  2. const path = require('path');
  3. const { isIPv4, isIPv6 } = require('net');
  4. const { compareAndWriteFile } = require('./lib/string-array-compare');
  5. const { withBannerArray } = require('./lib/with-banner');
  6. const { fetchRemoteTextAndCreateReadlineInterface, readFileByLine } = require('./lib/fetch-remote-text-by-line');
  7. const { minifyRules } = require('./lib/minify-rules');
  8. (async () => {
  9. console.time('Total Time - build-anti-bogus-domain');
  10. console.time('* Download bogus-nxdomain-list');
  11. const rl = await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf');
  12. /** @type {string[]} */
  13. const res = [];
  14. for await (const line of rl) {
  15. if (line.startsWith('bogus-nxdomain=')) {
  16. res.push(line.replace('bogus-nxdomain=', ''));
  17. }
  18. }
  19. console.timeEnd('* Download bogus-nxdomain-list');
  20. const filePath = path.resolve(__dirname, '../Source/ip/reject.conf');
  21. const resultPath = path.resolve(__dirname, '../List/ip/reject.conf');
  22. /** @type {string[]} */
  23. const result = [];
  24. for await (const line of readFileByLine(filePath)) {
  25. if (line === '# --- [Anti Bogus Domain Replace Me] ---') {
  26. res.forEach(ip => {
  27. if (isIPv4(ip)) {
  28. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  29. } else if (isIPv6(ip)) {
  30. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  31. }
  32. });
  33. } else {
  34. result.push(line);
  35. }
  36. }
  37. await compareAndWriteFile(
  38. withBannerArray(
  39. 'Sukka\'s Surge Rules - Anti Bogus Domain',
  40. [
  41. 'License: AGPL 3.0',
  42. 'Homepage: https://ruleset.skk.moe',
  43. 'GitHub: https://github.com/SukkaW/Surge',
  44. '',
  45. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers.',
  46. '',
  47. 'Data from:',
  48. ' - https://github.com/felixonmars/dnsmasq-china-list'
  49. ],
  50. new Date(),
  51. minifyRules(result)
  52. ),
  53. resultPath
  54. );
  55. console.timeEnd('Total Time - build-anti-bogus-domain');
  56. })();