build-reject-ip-list.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 { compareAndWriteFile, RulesetOutput } from './lib/create-file';
  7. import { OUTPUT_INTERNAL_DIR, 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. import { AUGUST_ASN } from '../Source/ip/august';
  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. const v = fastIpVersion(ip);
  20. if (v === 4) {
  21. ipv4.push(ip);
  22. } else if (v === 6) {
  23. ipv6.push(ip);
  24. }
  25. }
  26. }
  27. return [ipv4, ipv6] as const;
  28. });
  29. const BOTNET_FILTER_URL = 'https://malware-filter.pages.dev/botnet-filter-dnscrypt-blocked-ips.txt';
  30. const BOTNET_FILTER_MIRROR_URL = [
  31. 'https://botnet-filter.pages.dev/botnet-filter-dnscrypt-blocked-ips.txt',
  32. 'https://malware-filter.gitlab.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  33. 'https://malware-filter.gitlab.io/botnet-filter/botnet-filter-dnscrypt-blocked-ips.txt'
  34. // 'https://curbengh.github.io/botnet-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  35. // https://curbengh.github.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt
  36. ];
  37. 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) => {
  38. const v = fastIpVersion(ip);
  39. if (v === 4) {
  40. acc[0].push(ip);
  41. } else if (v === 6) {
  42. acc[1].push(ip);
  43. }
  44. return acc;
  45. }, [[], []]));
  46. const readLocalRejectIpListPromise = readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'ip/reject.conf'));
  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 Promise.all([
  53. 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. .bulkAddIPASN(AUGUST_ASN)
  70. .write(),
  71. compareAndWriteFile(span, [AUGUST_ASN.join(' ')], path.join(OUTPUT_INTERNAL_DIR, 'august_asn.txt'))
  72. ]);
  73. });