build-reject-ip-list.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 './constants/description';
  6. import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
  7. import { processLine } from './lib/process-line';
  8. import { RulesetOutput } from './lib/create-file';
  9. import { SOURCE_DIR } from './constants/dir';
  10. import { $$fetch } from './lib/fetch-retry';
  11. import { fetchAssetsWithout304 } from './lib/fetch-assets';
  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, true)) {
  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: Promise<[ipv4: string[], ipv6: string[]]> = fetchAssetsWithout304(BOTNET_FILTER_URL, BOTNET_FILTER_MIRROR_URL).then(text => text.split('\n').reduce<[ipv4: string[], ipv6: string[]]>((acc, cur) => {
  37. const ip = processLine(cur);
  38. if (ip) {
  39. if (isProbablyIpv4(ip)) {
  40. acc[0].push(ip);
  41. } else if (isProbablyIpv6(ip)) {
  42. acc[1].push(ip);
  43. }
  44. }
  45. return acc;
  46. }, [[], []]));
  47. export const buildRejectIPList = task(require.main === module, __filename)(async (span) => {
  48. const [bogusNxDomainIPs, botNetIPs] = await Promise.all([
  49. span.traceChildPromise('get bogus nxdomain ips', getBogusNxDomainIPsPromise),
  50. span.traceChildPromise('get botnet ips', getBotNetFilterIPsPromise)
  51. ]);
  52. return new RulesetOutput(span, 'reject', 'ip')
  53. .withTitle('Sukka\'s Ruleset - Anti Bogus Domain')
  54. .withDescription([
  55. ...SHARED_DESCRIPTION,
  56. '',
  57. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers, and botnet controller IPs.',
  58. '',
  59. 'Data from:',
  60. ' - https://github.com/felixonmars/dnsmasq-china-list',
  61. ' - https://github.com/curbengh/botnet-filter'
  62. ])
  63. .addFromRuleset(await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'ip/reject.conf')))
  64. .bulkAddCIDR4NoResolve(bogusNxDomainIPs[0])
  65. .bulkAddCIDR6NoResolve(bogusNxDomainIPs[1])
  66. .bulkAddCIDR4NoResolve(botNetIPs[0])
  67. .bulkAddCIDR6NoResolve(botNetIPs[1])
  68. .write();
  69. });