build-reject-ip-list.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 { fetchAssets } 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[]]> = fetchAssets(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. const readLocalRejectIpListPromise = readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'ip/reject.conf'));
  48. export const buildRejectIPList = task(require.main === module, __filename)(async (span) => {
  49. const [bogusNxDomainIPs, botNetIPs] = await Promise.all([
  50. span.traceChildPromise('get bogus nxdomain ips', getBogusNxDomainIPsPromise),
  51. span.traceChildPromise('get botnet ips', getBotNetFilterIPsPromise)
  52. ]);
  53. return new RulesetOutput(span, 'reject', 'ip')
  54. .withTitle('Sukka\'s Ruleset - Anti Bogus Domain')
  55. .withDescription([
  56. ...SHARED_DESCRIPTION,
  57. '',
  58. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers, and botnet controller IPs.',
  59. '',
  60. 'Data from:',
  61. ' - https://github.com/felixonmars/dnsmasq-china-list',
  62. ' - https://github.com/curbengh/botnet-filter'
  63. ])
  64. .addFromRuleset(readLocalRejectIpListPromise)
  65. .bulkAddCIDR4NoResolve(bogusNxDomainIPs[0])
  66. .bulkAddCIDR6NoResolve(bogusNxDomainIPs[1])
  67. .bulkAddCIDR4NoResolve(botNetIPs[0])
  68. .bulkAddCIDR6NoResolve(botNetIPs[1])
  69. .write();
  70. });