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