build-anti-bogus-domain.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 { surgeRulesetToClashClassicalTextRuleset } = require('./lib/clash');
  8. const { processLine } = require('./lib/process-line');
  9. (async () => {
  10. console.time('Total Time - build-anti-bogus-domain');
  11. console.time('* Download bogus-nxdomain-list');
  12. /** @type {string[]} */
  13. const res = [];
  14. for await (const line of await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) {
  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. /** @type {string[]} */
  22. const result = [];
  23. for await (const line of readFileByLine(filePath)) {
  24. if (line === '# --- [Anti Bogus Domain Replace Me] ---') {
  25. res.forEach(ip => {
  26. if (isIPv4(ip)) {
  27. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  28. } else if (isIPv6(ip)) {
  29. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  30. }
  31. });
  32. } else {
  33. const l = processLine(line);
  34. if (l) {
  35. result.push(l);
  36. }
  37. }
  38. }
  39. const description = [
  40. 'License: AGPL 3.0',
  41. 'Homepage: https://ruleset.skk.moe',
  42. 'GitHub: https://github.com/SukkaW/Surge',
  43. '',
  44. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers.',
  45. '',
  46. 'Data from:',
  47. ' - https://github.com/felixonmars/dnsmasq-china-list'
  48. ];
  49. await compareAndWriteFile(
  50. withBannerArray(
  51. 'Sukka\'s Ruleset - Anti Bogus Domain',
  52. description,
  53. new Date(),
  54. surgeRulesetToClashClassicalTextRuleset(result)
  55. ),
  56. path.resolve(__dirname, '../Clash/ip/reject.txt')
  57. );
  58. await Promise.all([
  59. compareAndWriteFile(
  60. withBannerArray(
  61. 'Sukka\'s Ruleset - Anti Bogus Domain',
  62. description,
  63. new Date(),
  64. result
  65. ),
  66. path.resolve(__dirname, '../List/ip/reject.conf')
  67. ),
  68. compareAndWriteFile(
  69. withBannerArray(
  70. 'Sukka\'s Ruleset - Anti Bogus Domain',
  71. description,
  72. new Date(),
  73. surgeRulesetToClashClassicalTextRuleset(result)
  74. ),
  75. path.resolve(__dirname, '../Clash/ip/reject.txt')
  76. )
  77. ]);
  78. console.timeEnd('Total Time - build-anti-bogus-domain');
  79. })();