build-anti-bogus-domain.ts 2.1 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. import { SHARED_DESCRIPTION } from './lib/constants';
  9. const getBogusNxDomainIPs = async () => {
  10. /** @type {string[]} */
  11. const result = [];
  12. for await (const line of await fetchRemoteTextAndCreateReadlineInterface('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) {
  13. if (line.startsWith('bogus-nxdomain=')) {
  14. const ip = line.slice(15).trim();
  15. if (isIPv4(ip)) {
  16. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  17. } else if (isIPv6(ip)) {
  18. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  19. }
  20. }
  21. }
  22. return result;
  23. };
  24. export const buildAntiBogusDomain = task(import.meta.path, async () => {
  25. const bogusIpPromise = getBogusNxDomainIPs();
  26. /** @type {string[]} */
  27. const result = [];
  28. for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/ip/reject.conf'))) {
  29. if (line === '# --- [Anti Bogus Domain Replace Me] ---') {
  30. // bogus ip is less than 200, no need to worry about "Maximum call stack size exceeded"
  31. result.push(...(await bogusIpPromise));
  32. continue;
  33. } else {
  34. const l = processLine(line);
  35. if (l) {
  36. result.push(l);
  37. }
  38. }
  39. }
  40. const description = [
  41. ...SHARED_DESCRIPTION,
  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(import.meta.dir, '../List/ip/reject.conf'),
  55. path.resolve(import.meta.dir, '../Clash/ip/reject.txt')
  56. ));
  57. });
  58. if (import.meta.main) {
  59. buildAntiBogusDomain();
  60. }