build-reject-ip-list.ts 3.3 KB

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