build-anti-bogus-domain.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. const { task } = require('./lib/trace-runner');
  8. const getBogusNxDomainIPs = async () => {
  9. /** @type {string[]} */
  10. const res = [];
  11. for await (const line of await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) {
  12. if (line.startsWith('bogus-nxdomain=')) {
  13. res.push(line.replace('bogus-nxdomain=', ''));
  14. }
  15. }
  16. return res;
  17. };
  18. const buildAntiBogusDomain = task(__filename, async () => {
  19. const filePath = path.resolve(__dirname, '../Source/ip/reject.conf');
  20. const bogusIpPromise = getBogusNxDomainIPs();
  21. /** @type {string[]} */
  22. const result = [];
  23. for await (const line of readFileByLine(filePath)) {
  24. if (line === '# --- [Anti Bogus Domain Replace Me] ---') {
  25. (await bogusIpPromise).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. continue;
  33. } else {
  34. const l = processLine(line);
  35. if (l) {
  36. result.push(l);
  37. }
  38. }
  39. }
  40. const description = [
  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. return Promise.all(createRuleset(
  51. 'Sukka\'s Ruleset - Anti Bogus Domain',
  52. description,
  53. new Date(),
  54. result,
  55. 'ruleset',
  56. path.resolve(__dirname, '../List/ip/reject.conf'),
  57. path.resolve(__dirname, '../Clash/ip/reject.txt')
  58. ));
  59. });
  60. module.exports.buildAntiBogusDomain = buildAntiBogusDomain;
  61. if (require.main === module) {
  62. buildAntiBogusDomain();
  63. }