build-reject-ip-list.ts 3.1 KB

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