build-anti-bogus-domain.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @ts-check
  2. import path from 'path';
  3. import { createRuleset } from './lib/create-file';
  4. import { fetchRemoteTextByLine, readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  5. import { task } from './trace';
  6. import { SHARED_DESCRIPTION } from './lib/constants';
  7. import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
  8. import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem';
  9. const URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
  10. const getBogusNxDomainIPsPromise = fsFetchCache.apply(
  11. URL,
  12. async () => {
  13. const result: string[] = [];
  14. for await (const line of await fetchRemoteTextByLine(URL)) {
  15. if (line.startsWith('bogus-nxdomain=')) {
  16. const ip = line.slice(15).trim();
  17. if (isProbablyIpv4(ip)) {
  18. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  19. } else if (isProbablyIpv6(ip)) {
  20. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  21. }
  22. }
  23. }
  24. return result;
  25. },
  26. {
  27. ttl: TTL.ONE_WEEK(),
  28. serializer: serializeArray,
  29. deserializer: deserializeArray
  30. }
  31. );
  32. export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
  33. const result: string[] = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../Source/ip/reject.conf'));
  34. const peeked = Bun.peek(getBogusNxDomainIPsPromise);
  35. const bogusNxDomainIPs = peeked === getBogusNxDomainIPsPromise
  36. ? await span.traceChildPromise('get bogus nxdomain ips', getBogusNxDomainIPsPromise)
  37. : (peeked as string[]);
  38. result.push(...bogusNxDomainIPs);
  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 createRuleset(
  48. span,
  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. }