build-sgmodule-always-realip.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Steam LAN Cache
  30. //
  31. // Steam will DNS lookup this domain, trying to find Local LAN Cache server
  32. // If one is found, Steam client will try to connect with this IP with original CDN domain in
  33. // HTTP Host header, while in HTTP plain HTTP/1.1. It is up to the HTTP server to handle this.
  34. //
  35. // By having lancache.steamcontent.com in Real IP, we can avoid Steam client accidentally mistaking
  36. // the Fake IP as a local LAN cache. This also helps real LAN cache to work properly.
  37. 'lancache.steamcontent.com'
  38. // 'controlplane.tailscale.com',
  39. // NTP
  40. // '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'
  41. // 'time*.cloud.tencent.com', 'ntp?.aliyun.com',
  42. // QQ Login
  43. // 'localhost.*.qq.com'
  44. // 'localhost.ptlogin2.qq.com
  45. // 'localhost.sec.qq.com',
  46. // 'localhost.work.weixin.qq.com'
  47. ];
  48. export const buildAlwaysRealIPModule = task(require.main === module, __filename)(async (span) => {
  49. const surge: string[] = [];
  50. const clashFakeIpFilter = new FileOutput(span, 'clash_fake_ip_filter')
  51. .withTitle('Sukka\'s Ruleset - Always Real IP Plus')
  52. .withDescription([
  53. ...SHARED_DESCRIPTION,
  54. '',
  55. 'Clash.Meta fake-ip-filter as ruleset'
  56. ])
  57. .withStrategies([
  58. new ClashDomainSet('domainset')
  59. ]);
  60. // Intranet, Router Setup, and mant more
  61. const dataset = [DIRECTS, LAN, DOMESTICS, DOH_BOOTSTRAP].reduce<DNSMapping[]>((acc, item) => {
  62. Object.values(item).forEach((i: DNSMapping) => {
  63. if (i.realip) {
  64. acc.push(i);
  65. }
  66. });
  67. return acc;
  68. }, []);
  69. const getDnsMappingRuleWithoutWildcard = createGetDnsMappingRule(false);
  70. for (const { domains } of dataset) {
  71. clashFakeIpFilter.addFromRuleset(domains.flatMap(getDnsMappingRuleWithoutWildcard));
  72. }
  73. return Promise.all([
  74. compareAndWriteFile(
  75. span,
  76. [
  77. '#!name=[Sukka] Always Real IP Plus',
  78. `#!desc=Last Updated: ${new Date().toISOString()}`,
  79. '',
  80. '[General]',
  81. `always-real-ip = %APPEND% ${HOSTNAMES.concat(surge).join(', ')}`
  82. ],
  83. path.resolve(OUTPUT_MODULES_DIR, 'sukka_common_always_realip.sgmodule')
  84. ),
  85. compareAndWriteFile(
  86. span,
  87. yaml.stringify(
  88. {
  89. dns: {
  90. 'fake-ip-filter': appendArrayInPlace(
  91. /** clash */
  92. dataset.flatMap(({ domains }) => domains.map((domain) => {
  93. switch (domain[0]) {
  94. case '$':
  95. return domain.slice(1);
  96. case '+':
  97. return '+.' + domain.slice(1);
  98. default:
  99. return domain;
  100. }
  101. })),
  102. HOSTNAMES
  103. )
  104. }
  105. },
  106. { version: '1.1' }
  107. ).split('\n'),
  108. path.join(OUTPUT_INTERNAL_DIR, 'clash_fake_ip_filter.yaml')
  109. )
  110. ]);
  111. });