build-anti-bogus-domain.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. (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. ...SHARED_DESCRIPTION,
  41. '',
  42. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers.',
  43. '',
  44. 'Data from:',
  45. ' - https://github.com/felixonmars/dnsmasq-china-list'
  46. ];
  47. return Promise.all(createRuleset(
  48. 'Sukka\'s Ruleset - Anti Bogus Domain',
  49. description,
  50. new Date(),
  51. result,
  52. 'ruleset',
  53. path.resolve(import.meta.dir, '../List/ip/reject.conf'),
  54. path.resolve(import.meta.dir, '../Clash/ip/reject.txt')
  55. ));
  56. });
  57. if (import.meta.main) {
  58. buildAntiBogusDomain();
  59. }