build-anti-bogus-domain.js 2.0 KB

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