build-reject-ip-list.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 './lib/constants';
  6. import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
  7. import { fsFetchCache, getFileContentHash } from './lib/cache-filesystem';
  8. import { processLine } from './lib/process-line';
  9. import { RulesetOutput } from './lib/create-file';
  10. import { SOURCE_DIR } from './constants/dir';
  11. const BOGUS_NXDOMAIN_URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
  12. const getBogusNxDomainIPsPromise = fsFetchCache.applyWithHttp304(
  13. BOGUS_NXDOMAIN_URL,
  14. getFileContentHash(__filename),
  15. async (resp) => {
  16. const ipv4: string[] = [];
  17. const ipv6: string[] = [];
  18. for await (const line of createReadlineInterfaceFromResponse(resp)) {
  19. if (line.startsWith('bogus-nxdomain=')) {
  20. const ip = line.slice(15).trim();
  21. if (isProbablyIpv4(ip)) {
  22. ipv4.push(ip);
  23. } else if (isProbablyIpv6(ip)) {
  24. ipv6.push(ip);
  25. }
  26. }
  27. }
  28. return [ipv4, ipv6] as const;
  29. },
  30. {
  31. serializer: JSON.stringify,
  32. deserializer: JSON.parse
  33. }
  34. );
  35. const BOTNET_FILTER_URL = 'https://malware-filter.pages.dev/botnet-filter-dnscrypt-blocked-ips.txt';
  36. const BOTNET_FILTER_MIRROR_URL = [
  37. 'https://botnet-filter.pages.dev/botnet-filter-dnscrypt-blocked-ips.txt',
  38. 'https://malware-filter.gitlab.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  39. 'https://malware-filter.gitlab.io/botnet-filter/botnet-filter-dnscrypt-blocked-ips.txt'
  40. // 'https://curbengh.github.io/botnet-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  41. // https://curbengh.github.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt
  42. ];
  43. const getBotNetFilterIPsPromise = fsFetchCache.applyWithHttp304AndMirrors<[ipv4: string[], ipv6: string[]]>(
  44. BOTNET_FILTER_URL,
  45. BOTNET_FILTER_MIRROR_URL,
  46. getFileContentHash(__filename),
  47. (text) => text.split('\n').reduce<[ipv4: string[], ipv6: string[]]>((acc, cur) => {
  48. const ip = processLine(cur);
  49. if (ip) {
  50. if (isProbablyIpv4(ip)) {
  51. acc[0].push(ip);
  52. } else if (isProbablyIpv6(ip)) {
  53. acc[1].push(ip);
  54. }
  55. }
  56. return acc;
  57. }, [[], []]),
  58. {
  59. serializer: JSON.stringify,
  60. deserializer: JSON.parse
  61. }
  62. );
  63. export const buildRejectIPList = task(require.main === module, __filename)(async (span) => {
  64. const [bogusNxDomainIPs, botNetIPs] = await Promise.all([
  65. span.traceChildPromise('get bogus nxdomain ips', getBogusNxDomainIPsPromise),
  66. span.traceChildPromise('get botnet ips', getBotNetFilterIPsPromise)
  67. ]);
  68. return new RulesetOutput(span, 'reject', 'ip')
  69. .withTitle('Sukka\'s Ruleset - Anti Bogus Domain')
  70. .withDescription([
  71. ...SHARED_DESCRIPTION,
  72. '',
  73. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers, and botnet controller IPs.',
  74. '',
  75. 'Data from:',
  76. ' - https://github.com/felixonmars/dnsmasq-china-list',
  77. ' - https://github.com/curbengh/botnet-filter'
  78. ])
  79. .addFromRuleset(await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'ip/reject.conf')))
  80. .bulkAddCIDR4NoResolve(bogusNxDomainIPs[0])
  81. .bulkAddCIDR6NoResolve(bogusNxDomainIPs[1])
  82. .bulkAddCIDR4NoResolve(botNetIPs[0])
  83. .bulkAddCIDR6NoResolve(botNetIPs[1])
  84. .write();
  85. });