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. const { runner } = require('./lib/trace-runner');
  8. const buildAntiBogusDomain = 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. const filePath = path.resolve(__dirname, '../Source/ip/reject.conf');
  17. /** @type {string[]} */
  18. const result = [];
  19. for await (const line of readFileByLine(filePath)) {
  20. if (line === '# --- [Anti Bogus Domain Replace Me] ---') {
  21. res.forEach(ip => {
  22. if (isIPv4(ip)) {
  23. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  24. } else if (isIPv6(ip)) {
  25. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  26. }
  27. });
  28. } else {
  29. const l = processLine(line);
  30. if (l) {
  31. result.push(l);
  32. }
  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(createRuleset(
  46. 'Sukka\'s Ruleset - Anti Bogus Domain',
  47. description,
  48. new Date(),
  49. result,
  50. 'ruleset',
  51. path.resolve(__dirname, '../List/ip/reject.conf'),
  52. path.resolve(__dirname, '../Clash/ip/reject.txt')
  53. ));
  54. };
  55. module.exports.buildAntiBogusDomain = buildAntiBogusDomain;
  56. if (require.main === module) {
  57. runner(__filename, buildAntiBogusDomain);
  58. }