build-anti-bogus-domain.js 2.3 KB

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