build-reject-ip-list.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // @ts-check
  2. import path from 'node:path';
  3. import { createReadlineInterfaceFromResponse, readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  4. import { task } from './trace';
  5. import { SHARED_DESCRIPTION } from './lib/constants';
  6. import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
  7. import { TTL, fsFetchCache, createCacheKey, getFileContentHash } from './lib/cache-filesystem';
  8. import { fetchAssets } from './lib/fetch-assets';
  9. import { processLine } from './lib/process-line';
  10. import { RulesetOutput } from './lib/create-file';
  11. import { SOURCE_DIR } from './constants/dir';
  12. const cacheKey = createCacheKey(__filename);
  13. const BOGUS_NXDOMAIN_URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
  14. const getBogusNxDomainIPsPromise = fsFetchCache.applyWithHttp304(
  15. BOGUS_NXDOMAIN_URL,
  16. getFileContentHash(__filename),
  17. async (resp) => {
  18. const ipv4: string[] = [];
  19. const ipv6: string[] = [];
  20. for await (const line of createReadlineInterfaceFromResponse(resp)) {
  21. if (line.startsWith('bogus-nxdomain=')) {
  22. const ip = line.slice(15).trim();
  23. if (isProbablyIpv4(ip)) {
  24. ipv4.push(ip);
  25. } else if (isProbablyIpv6(ip)) {
  26. ipv6.push(ip);
  27. }
  28. }
  29. }
  30. return [ipv4, ipv6] as const;
  31. },
  32. {
  33. serializer: JSON.stringify,
  34. deserializer: JSON.parse
  35. }
  36. );
  37. const BOTNET_FILTER_URL = 'https://curbengh.github.io/botnet-filter/botnet-filter-dnscrypt-blocked-ips.txt';
  38. const BOTNET_FILTER_MIRROR_URL = [
  39. 'https://curbengh.github.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  40. 'https://malware-filter.gitlab.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  41. 'https://malware-filter.pages.dev/botnet-filter-dnscrypt-blocked-ips.txt'
  42. ];
  43. const getBotNetFilterIPsPromise = fsFetchCache.apply<[ipv4: string[], ipv6: string[]]>(
  44. cacheKey(BOTNET_FILTER_URL),
  45. async () => {
  46. const text = await fetchAssets(BOTNET_FILTER_URL, BOTNET_FILTER_MIRROR_URL);
  47. return text.split('\n').reduce<[ipv4: string[], ipv6: string[]]>((acc, cur) => {
  48. const ip = processLine(cur);
  49. if (ip) {
  50. if (isProbablyIpv4(ip)) {
  51. acc[0].push(ip);
  52. } else if (isProbablyIpv6(ip)) {
  53. acc[1].push(ip);
  54. }
  55. }
  56. return acc;
  57. }, [[], []]);
  58. },
  59. {
  60. ttl: TTL.TWLVE_HOURS(),
  61. serializer: JSON.stringify,
  62. deserializer: JSON.parse
  63. }
  64. );
  65. export const buildRejectIPList = task(require.main === module, __filename)(async (span) => {
  66. const [bogusNxDomainIPs, botNetIPs] = await Promise.all([
  67. span.traceChildPromise('get bogus nxdomain ips', getBogusNxDomainIPsPromise),
  68. span.traceChildPromise('get botnet ips', getBotNetFilterIPsPromise)
  69. ]);
  70. return new RulesetOutput(span, 'reject', 'ip')
  71. .withTitle('Sukka\'s Ruleset - Anti Bogus Domain')
  72. .withDescription([
  73. ...SHARED_DESCRIPTION,
  74. '',
  75. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers, and botnet controller IPs.',
  76. '',
  77. 'Data from:',
  78. ' - https://github.com/felixonmars/dnsmasq-china-list',
  79. ' - https://github.com/curbengh/botnet-filter'
  80. ])
  81. .addFromRuleset(await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'ip/reject.conf')))
  82. .bulkAddCIDR4NoResolve(bogusNxDomainIPs[0])
  83. .bulkAddCIDR6NoResolve(bogusNxDomainIPs[1])
  84. .bulkAddCIDR4NoResolve(botNetIPs[0])
  85. .bulkAddCIDR6NoResolve(botNetIPs[1])
  86. .write();
  87. });