build-sgmodule-always-realip.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 './lib/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. ];
  39. export const buildAlwaysRealIPModule = task(require.main === module, __filename)(async (span) => {
  40. const surge: string[] = [];
  41. const clashFakeIpFilter = new FileOutput(span, 'clash_fake_ip_filter')
  42. .withTitle('Sukka\'s Ruleset - Always Real IP Plus')
  43. .withDescription([
  44. ...SHARED_DESCRIPTION,
  45. '',
  46. 'Clash.Meta fake-ip-filter as ruleset'
  47. ])
  48. .withStrategies([
  49. new ClashDomainSet('domainset')
  50. ]);
  51. // Intranet, Router Setup, and mant more
  52. const dataset = [DIRECTS, LAN, DOMESTICS, DOH_BOOTSTRAP].reduce<DNSMapping[]>((acc, item) => {
  53. Object.values(item).forEach((i: DNSMapping) => {
  54. if (i.realip) {
  55. acc.push(i);
  56. }
  57. });
  58. return acc;
  59. }, []);
  60. const getDnsMappingRuleWithoutWildcard = createGetDnsMappingRule(false);
  61. for (const { domains } of dataset) {
  62. clashFakeIpFilter.addFromRuleset(domains.flatMap(getDnsMappingRuleWithoutWildcard));
  63. }
  64. return Promise.all([
  65. compareAndWriteFile(
  66. span,
  67. [
  68. '#!name=[Sukka] Always Real IP Plus',
  69. `#!desc=Last Updated: ${new Date().toISOString()}`,
  70. '',
  71. '[General]',
  72. `always-real-ip = %APPEND% ${HOSTNAMES.concat(surge).join(', ')}`
  73. ],
  74. path.resolve(OUTPUT_MODULES_DIR, 'sukka_common_always_realip.sgmodule')
  75. ),
  76. compareAndWriteFile(
  77. span,
  78. yaml.stringify(
  79. {
  80. dns: {
  81. 'fake-ip-filter': appendArrayInPlace(
  82. /** clash */
  83. dataset.flatMap(({ domains }) => domains.map((domain) => `+.${domain}`)),
  84. HOSTNAMES
  85. )
  86. }
  87. },
  88. { version: '1.1' }
  89. ).split('\n'),
  90. path.join(OUTPUT_INTERNAL_DIR, 'clash_fake_ip_filter.yaml')
  91. )
  92. ]);
  93. });