build-sgmodule-always-realip.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import path from 'node:path';
  2. import { task } from './trace';
  3. import { compareAndWriteFile } from './lib/create-file';
  4. import { DIRECTS, LAN } from '../Source/non_ip/direct';
  5. import type { DNSMapping } from '../Source/non_ip/direct';
  6. import { DOMESTICS, DOH_BOOTSTRAP } from '../Source/non_ip/domestic';
  7. import * as yaml from 'yaml';
  8. import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR } from './constants/dir';
  9. import { appendArrayInPlace } from 'foxts/append-array-in-place';
  10. import { SHARED_DESCRIPTION } from './constants/description';
  11. import { createGetDnsMappingRule } from './build-domestic-direct-lan-ruleset-dns-mapping-module';
  12. import { ClashDomainSet } from './lib/writing-strategy/clash';
  13. import { FileOutput } from './lib/rules/base';
  14. const HOSTNAMES = [
  15. // Network Detection, Captive Portal
  16. 'dns.msftncsi.com',
  17. // '*.msftconnecttest.com',
  18. // 'network-test.debian.org',
  19. // 'detectportal.firefox.com',
  20. // Handle SNAT conversation properly
  21. '*.srv.nintendo.net',
  22. '*.stun.playstation.net',
  23. 'xbox.*.microsoft.com',
  24. '*.xboxlive.com',
  25. '*.turn.twilio.com',
  26. '*.stun.twilio.com',
  27. 'stun.syncthing.net',
  28. 'stun.*',
  29. // 'controlplane.tailscale.com',
  30. // NTP
  31. // 'time.*.com', 'time.*.gov', 'time.*.edu.cn', 'time.*.apple.com', 'time?.*.com', 'ntp.*.com', 'ntp?.*.com', '*.time.edu.cn', '*.ntp.org.cn', '*.pool.ntp.org'
  32. // 'time*.cloud.tencent.com', 'ntp?.aliyun.com',
  33. // QQ Login
  34. // 'localhost.*.qq.com'
  35. // 'localhost.ptlogin2.qq.com
  36. // 'localhost.sec.qq.com',
  37. // 'localhost.work.weixin.qq.com',
  38. '127.*.*.*.sslip.io',
  39. '127-*-*-*.sslip.io',
  40. '*.127.*.*.*.sslip.io',
  41. '*-127-*-*-*.sslip.io',
  42. '127.*.*.*.nip.io',
  43. '127-*-*-*.nip.io',
  44. '*.127.*.*.*.nip.io',
  45. '*-127-*-*-*.nip.io'
  46. ];
  47. export const buildAlwaysRealIPModule = task(require.main === module, __filename)(async (span) => {
  48. const surge: string[] = [];
  49. const clashFakeIpFilter = new FileOutput(span, 'clash_fake_ip_filter')
  50. .withTitle('Sukka\'s Ruleset - Always Real IP Plus')
  51. .withDescription([
  52. ...SHARED_DESCRIPTION,
  53. '',
  54. 'Clash.Meta fake-ip-filter as ruleset'
  55. ])
  56. .withStrategies([
  57. new ClashDomainSet('domainset')
  58. ]);
  59. // Intranet, Router Setup, and mant more
  60. const dataset = [DIRECTS, LAN, DOMESTICS, DOH_BOOTSTRAP].reduce<DNSMapping[]>((acc, item) => {
  61. Object.values(item).forEach((i: DNSMapping) => {
  62. if (i.realip) {
  63. acc.push(i);
  64. }
  65. });
  66. return acc;
  67. }, []);
  68. const getDnsMappingRuleWithoutWildcard = createGetDnsMappingRule(false);
  69. for (const { domains } of dataset) {
  70. clashFakeIpFilter.addFromRuleset(domains.flatMap(getDnsMappingRuleWithoutWildcard));
  71. }
  72. return Promise.all([
  73. compareAndWriteFile(
  74. span,
  75. [
  76. '#!name=[Sukka] Always Real IP Plus',
  77. `#!desc=Last Updated: ${new Date().toISOString()}`,
  78. '',
  79. '[General]',
  80. `always-real-ip = %APPEND% ${HOSTNAMES.concat(surge).join(', ')}`
  81. ],
  82. path.resolve(OUTPUT_MODULES_DIR, 'sukka_common_always_realip.sgmodule')
  83. ),
  84. compareAndWriteFile(
  85. span,
  86. yaml.stringify(
  87. {
  88. dns: {
  89. 'fake-ip-filter': appendArrayInPlace(
  90. /** clash */
  91. dataset.flatMap(({ domains }) => domains.map((domain) => {
  92. switch (domain[0]) {
  93. case '$':
  94. return domain.slice(1);
  95. case '+':
  96. return '+.' + domain.slice(1);
  97. default:
  98. return domain;
  99. }
  100. })),
  101. HOSTNAMES
  102. )
  103. }
  104. },
  105. { version: '1.1' }
  106. ).split('\n'),
  107. path.join(OUTPUT_INTERNAL_DIR, 'clash_fake_ip_filter.yaml')
  108. )
  109. ]);
  110. });