build-domestic-ruleset.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // @ts-check
  2. const path = require('path');
  3. const { DOMESTICS } = require('../Source/non_ip/domestic');
  4. const { readFileByLine } = require('./lib/fetch-remote-text-by-line');
  5. const { processLine } = require('./lib/process-line');
  6. const { withBannerArray } = require('./lib/with-banner');
  7. const { compareAndWriteFile } = require('./lib/string-array-compare');
  8. const domainSorter = require('./lib/stable-sort-domain');
  9. (async () => {
  10. const rl = readFileByLine(path.resolve(__dirname, '../Source/non_ip/domestic.conf'));
  11. const results = [];
  12. for await (const l of rl) {
  13. const line = processLine(l);
  14. if (line) {
  15. results.push(line);
  16. }
  17. }
  18. results.push(
  19. ...Object.entries(DOMESTICS)
  20. .filter(([key]) => key !== 'SYSTEM')
  21. .flatMap(([, { domains }]) => domains)
  22. .sort(domainSorter)
  23. .map((domain) => `DOMAIN-SUFFIX,${domain}`)
  24. );
  25. await Promise.all([
  26. compareAndWriteFile(
  27. withBannerArray(
  28. 'Sukka\'s Surge Rules - Domestic Domain',
  29. [
  30. 'License: AGPL 3.0',
  31. 'Homepage: https://ruleset.skk.moe',
  32. 'GitHub: https://github.com/SukkaW/Surge',
  33. '',
  34. 'This file contains known addresses that are avaliable in the Mainland China.'
  35. ],
  36. new Date(),
  37. results
  38. ),
  39. path.resolve(__dirname, '../List/non_ip/domestic.conf')
  40. ),
  41. compareAndWriteFile(
  42. [
  43. '#!name=[Sukka] Local DNS Mapping',
  44. `#!desc=Last Updated: ${new Date().toISOString()}`,
  45. '',
  46. '[Host]',
  47. ...Object.entries(DOMESTICS)
  48. .flatMap(
  49. ([, { domains, dns }]) => domains.flatMap((domain) => [
  50. `${domain} = server:${dns}`,
  51. `*.${domain} = server:${dns}`
  52. ])
  53. ),
  54. ''
  55. ],
  56. path.resolve(__dirname, '../Modules/sukka_local_dns_mapping.sgmodule')
  57. )
  58. ]);
  59. })();