build-anti-bogus-domain.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 bogusIpPromise = getBogusNxDomainIPs();
  25. /** @type {string[]} */
  26. const result = [];
  27. for await (const line of readFileByLine(path.resolve(__dirname, '../Source/ip/reject.conf'))) {
  28. if (line === '# --- [Anti Bogus Domain Replace Me] ---') {
  29. (await bogusIpPromise).forEach(rule => result.push(rule));
  30. continue;
  31. } else {
  32. const l = processLine(line);
  33. if (l) {
  34. result.push(l);
  35. }
  36. }
  37. }
  38. const description = [
  39. 'License: AGPL 3.0',
  40. 'Homepage: https://ruleset.skk.moe',
  41. 'GitHub: https://github.com/SukkaW/Surge',
  42. '',
  43. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers.',
  44. '',
  45. 'Data from:',
  46. ' - https://github.com/felixonmars/dnsmasq-china-list'
  47. ];
  48. return Promise.all(createRuleset(
  49. 'Sukka\'s Ruleset - Anti Bogus Domain',
  50. description,
  51. new Date(),
  52. result,
  53. 'ruleset',
  54. path.resolve(__dirname, '../List/ip/reject.conf'),
  55. path.resolve(__dirname, '../Clash/ip/reject.txt')
  56. ));
  57. });
  58. module.exports.buildAntiBogusDomain = buildAntiBogusDomain;
  59. if (import.meta.main) {
  60. buildAntiBogusDomain();
  61. }