build-anti-bogus-domain.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const { fetchWithRetry } = require('./lib/fetch-retry');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { isIPv4, isIPv6 } = require('net');
  5. (async () => {
  6. console.time('Total Time - build-anti-bogus-domain');
  7. console.time('* Download bogus-nxdomain-list')
  8. const res = (await (await fetchWithRetry('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')).text())
  9. .split('\n')
  10. .map(line => {
  11. if (line.startsWith('bogus-nxdomain=')) {
  12. return line.replace('bogus-nxdomain=', '');
  13. }
  14. return null
  15. })
  16. .filter(ip => typeof ip === 'string');
  17. console.timeEnd('* Download bogus-nxdomain-list')
  18. const filePath = path.resolve(__dirname, '../Source/ip/reject.conf');
  19. const resultPath = path.resolve(__dirname, '../List/ip/reject.conf');
  20. const content = (await fs.promises.readFile(filePath, 'utf-8'))
  21. .replace(
  22. '# --- [Anti Bogus Domain Replace Me] ---',
  23. res.map(ip => {
  24. if (isIPv4(ip)) {
  25. return `IP-CIDR,${ip}/32,no-resolve`
  26. }
  27. if (isIPv6(ip)) {
  28. return `IP-CIDR6,${ip}/128,no-resolve`
  29. }
  30. return ''
  31. }).join('\n')
  32. );
  33. await fs.promises.writeFile(resultPath, content, 'utf-8');
  34. console.timeEnd('Total Time - build-anti-bogus-domain');
  35. })();