build-anti-bogus-domain.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 result = [];
  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. const ip = line.slice(15).trim();
  14. if (isIPv4(ip)) {
  15. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  16. } else if (isIPv6(ip)) {
  17. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  18. }
  19. }
  20. }
  21. return result;
  22. };
  23. const buildAntiBogusDomain = task(__filename, async () => {
  24. const filePath = path.resolve(__dirname, '../Source/ip/reject.conf');
  25. const bogusIpPromise = getBogusNxDomainIPs();
  26. /** @type {string[]} */
  27. const result = [];
  28. for await (const line of readFileByLine(filePath)) {
  29. if (line === '# --- [Anti Bogus Domain Replace Me] ---') {
  30. (await bogusIpPromise).forEach(rule => result.push(rule));
  31. continue;
  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. return Promise.all(createRuleset(
  50. 'Sukka\'s Ruleset - Anti Bogus Domain',
  51. description,
  52. new Date(),
  53. result,
  54. 'ruleset',
  55. path.resolve(__dirname, '../List/ip/reject.conf'),
  56. path.resolve(__dirname, '../Clash/ip/reject.txt')
  57. ));
  58. });
  59. module.exports.buildAntiBogusDomain = buildAntiBogusDomain;
  60. if (require.main === module) {
  61. buildAntiBogusDomain();
  62. }