build-reject-ip-list.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // @ts-check
  2. import path from 'path';
  3. import { createRuleset } from './lib/create-file';
  4. import { fetchRemoteTextByLine, readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  5. import { task } from './trace';
  6. import { SHARED_DESCRIPTION } from './lib/constants';
  7. import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
  8. import { TTL, deserializeArray, fsFetchCache, serializeArray, createCacheKey } from './lib/cache-filesystem';
  9. import { fetchAssets } from './lib/fetch-assets';
  10. import { processLine } from './lib/process-line';
  11. import { appendArrayInPlace } from './lib/append-array-in-place';
  12. const cacheKey = createCacheKey(__filename);
  13. const BOGUS_NXDOMAIN_URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
  14. const getBogusNxDomainIPsPromise = fsFetchCache.apply(
  15. cacheKey(BOGUS_NXDOMAIN_URL),
  16. async () => {
  17. const result: string[] = [];
  18. for await (const line of await fetchRemoteTextByLine(BOGUS_NXDOMAIN_URL)) {
  19. if (line.startsWith('bogus-nxdomain=')) {
  20. const ip = line.slice(15).trim();
  21. if (isProbablyIpv4(ip)) {
  22. result.push(`IP-CIDR,${ip}/32,no-resolve`);
  23. } else if (isProbablyIpv6(ip)) {
  24. result.push(`IP-CIDR6,${ip}/128,no-resolve`);
  25. }
  26. }
  27. }
  28. return result;
  29. },
  30. {
  31. ttl: TTL.ONE_WEEK(),
  32. serializer: serializeArray,
  33. deserializer: deserializeArray
  34. }
  35. );
  36. const BOTNET_FILTER_URL = 'https://curbengh.github.io/botnet-filter/botnet-filter-dnscrypt-blocked-ips.txt';
  37. const BOTNET_FILTER_MIRROR_URL = [
  38. 'https://curbengh.github.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  39. 'https://malware-filter.gitlab.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt',
  40. 'https://malware-filter.pages.dev/botnet-filter-dnscrypt-blocked-ips.txt'
  41. ];
  42. const getBotNetFilterIPsPromise = fsFetchCache.apply(
  43. cacheKey(BOTNET_FILTER_URL),
  44. async () => {
  45. const text = await fetchAssets(BOTNET_FILTER_URL, BOTNET_FILTER_MIRROR_URL);
  46. return text.split('\n').reduce<string[]>((acc, cur) => {
  47. const ip = processLine(cur);
  48. if (ip) {
  49. if (isProbablyIpv4(ip)) {
  50. acc.push(`IP-CIDR,${ip}/32,no-resolve`);
  51. } else if (isProbablyIpv6(ip)) {
  52. acc.push(`IP-CIDR6,${ip}/128,no-resolve`);
  53. }
  54. }
  55. return acc;
  56. }, []);
  57. },
  58. {
  59. ttl: TTL.TWLVE_HOURS(),
  60. serializer: serializeArray,
  61. deserializer: deserializeArray
  62. }
  63. );
  64. const localRejectIPSourcesPromise = readFileIntoProcessedArray(path.resolve(__dirname, '../Source/ip/reject.conf'));
  65. export const buildRejectIPList = task(require.main === module, __filename)(async (span) => {
  66. const result = await localRejectIPSourcesPromise;
  67. const results = await Promise.all([
  68. span.traceChildPromise('get bogus nxdomain ips', getBogusNxDomainIPsPromise),
  69. span.traceChildPromise('get botnet ips', getBotNetFilterIPsPromise)
  70. ]);
  71. const bogusNxDomainIPs = results[0];
  72. const botNetIPs = results[1];
  73. appendArrayInPlace(result, bogusNxDomainIPs);
  74. appendArrayInPlace(result, botNetIPs);
  75. const description = [
  76. ...SHARED_DESCRIPTION,
  77. '',
  78. 'This file contains known addresses that are hijacking NXDOMAIN results returned by DNS servers, and botnet controller IPs.',
  79. '',
  80. 'Data from:',
  81. ' - https://github.com/felixonmars/dnsmasq-china-list',
  82. ' - https://github.com/curbengh/botnet-filter'
  83. ];
  84. return createRuleset(
  85. span,
  86. 'Sukka\'s Ruleset - Anti Bogus Domain',
  87. description,
  88. new Date(),
  89. result,
  90. 'ruleset',
  91. path.resolve(__dirname, '../List/ip/reject.conf'),
  92. path.resolve(__dirname, '../Clash/ip/reject.txt'),
  93. path.resolve(__dirname, '../sing-box/ip/reject.json')
  94. );
  95. });