build-anti-bogus-domain.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 './trace';
  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 (span) => {
  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 createRuleset(
  54. span,
  55. 'Sukka\'s Ruleset - Anti Bogus Domain',
  56. description,
  57. new Date(),
  58. result,
  59. 'ruleset',
  60. path.resolve(import.meta.dir, '../List/ip/reject.conf'),
  61. path.resolve(import.meta.dir, '../Clash/ip/reject.txt')
  62. );
  63. });
  64. if (import.meta.main) {
  65. buildAntiBogusDomain();
  66. }