build-anti-bogus-domain.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // @ts-check
  2. import path from 'path';
  3. import { createRuleset } from './lib/create-file';
  4. import { fetchRemoteTextByLine, readFileByLine } from './lib/fetch-text-by-line';
  5. import { processLine } from './lib/process-line';
  6. import { task } from './lib/trace-runner';
  7. import { SHARED_DESCRIPTION } from './lib/constants';
  8. import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
  9. import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
  10. const getBogusNxDomainIPs = async () => {
  11. const url = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
  12. return fsCache.apply(
  13. url,
  14. async () => {
  15. const result: string[] = [];
  16. for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) {
  17. if (line && line.startsWith('bogus-nxdomain=')) {
  18. const ip = line.slice(15).trim();
  19. if (isProbablyIpv4(ip)) {
  20. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  21. } else if (isProbablyIpv6(ip)) {
  22. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  23. }
  24. }
  25. }
  26. return result;
  27. },
  28. {
  29. ttl: TTL.ONE_WEEK(),
  30. serializer: serializeArray,
  31. deserializer: deserializeArray
  32. }
  33. );
  34. };
  35. export const buildAntiBogusDomain = task(import.meta.path, async () => {
  36. const bogusIpPromise = getBogusNxDomainIPs();
  37. const result: string[] = [];
  38. for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/ip/reject.conf'))) {
  39. const l = processLine(line);
  40. if (l) {
  41. result.push(l);
  42. }
  43. }
  44. result.push(...(await bogusIpPromise));
  45. const description = [
  46. ...SHARED_DESCRIPTION,
  47. '',
  48. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers.',
  49. '',
  50. 'Data from:',
  51. ' - https://github.com/felixonmars/dnsmasq-china-list'
  52. ];
  53. return Promise.all(createRuleset(
  54. 'Sukka\'s Ruleset - Anti Bogus Domain',
  55. description,
  56. new Date(),
  57. result,
  58. 'ruleset',
  59. path.resolve(import.meta.dir, '../List/ip/reject.conf'),
  60. path.resolve(import.meta.dir, '../Clash/ip/reject.txt')
  61. ));
  62. });
  63. if (import.meta.main) {
  64. buildAntiBogusDomain();
  65. }