build-anti-bogus-domain.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // @ts-check
  2. import path from 'path';
  3. import { isIPv4, isIPv6 } from 'net';
  4. import { createRuleset } from './lib/create-file';
  5. import { fetchRemoteTextAndCreateReadlineInterface, readFileByLine } from './lib/fetch-remote-text-by-line';
  6. import { processLine } from './lib/process-line';
  7. import { task } from './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. export 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. if (import.meta.main) {
  59. buildAntiBogusDomain();
  60. }