build-sgmodule-redirect.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import path from 'node:path';
  2. import { task } from './trace';
  3. import { compareAndWriteFile } from './lib/create-file';
  4. import { getHostname } from 'tldts';
  5. import { isTruthy } from './lib/misc';
  6. import { OUTPUT_MODULES_DIR } from './constants/dir';
  7. function escapeRegExp(string = '') {
  8. const reRegExpChar = /[$()*+.?[\\\]^{|}]/g;
  9. const reHasRegExpChar = new RegExp(reRegExpChar.source);
  10. return string && reHasRegExpChar.test(string)
  11. ? string.replaceAll(reRegExpChar, String.raw`\$&`)
  12. : string;
  13. }
  14. const REDIRECT_MIRROR_HEADER = [
  15. // Gravatar
  16. ['gravatar.neworld.org/', 'https://secure.gravatar.com/'],
  17. ['cdn.v2ex.com/gravatar/', 'https://secure.gravatar.com/avatar/'],
  18. // U.SB
  19. ['cdnjs.loli.net/', 'https://cdnjs.cloudflare.com/'],
  20. ['fonts.loli.net/', 'https://fonts.googleapis.com/'],
  21. ['gstatic.loli.net/', 'https://fonts.gstatic.com/'],
  22. ['themes.loli.net/', 'https://themes.googleusercontent.com/'],
  23. ['ajax.loli.net/', 'https://ajax.googleapis.com/'],
  24. ['gravatar.loli.net/', 'https://secure.gravatar.com/'],
  25. // Geekzu
  26. ['gapis.geekzu.org/ajax/', 'https://ajax.googleapis.com/'],
  27. ['fonts.geekzu.org/', 'https://fonts.googleapis.com/'],
  28. ['gapis.geekzu.org/g-fonts/', 'https://fonts.gstatic.com/'],
  29. ['gapis.geekzu.org/g-themes/', 'https://themes.googleusercontent.com/'],
  30. ['sdn.geekzu.org/', 'https://secure.gravatar.com/'],
  31. // cravatar
  32. ['cravatar.cn/', 'https://secure.gravatar.com/'],
  33. // libravatar
  34. ['seccdn.libravatar.org/gravatarproxy/', 'https://secure.gravatar.com/'],
  35. // ghproxy
  36. ['ghproxy.com/', 'https://'],
  37. ['ghps.cc/', 'https://'],
  38. // gh-proxy
  39. ['github.moeyy.xyz/', 'https://'],
  40. // 7ED Services
  41. ['use.sevencdn.com/css', 'https://fonts.googleapis.com/css'],
  42. ['use.sevencdn.com/ajax/libs/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  43. ['use.sevencdn.com/gajax/', 'https://ajax.googleapis.com/ajax/'],
  44. ['use.sevencdn.com/chart', 'https://chart.googleapis.com/chart'],
  45. ['use.sevencdn.com/avatar', 'https://secure.gravatar.com/avatar'],
  46. ['raw.gitmirror.com/', 'https://raw.githubusercontent.com/'],
  47. ['gist.gitmirror.com/', 'https://gist.githubusercontent.com/'],
  48. ['raw.githubusercontents.com/', 'https://raw.githubusercontent.com/'],
  49. ['gist.githubusercontents.com/', 'https://gist.githubusercontent.com/'],
  50. ['cdn.gitmirror.com/', 'https://cdn.statically.io/'],
  51. // FastGit
  52. ['raw.fastgit.org/', 'https://raw.githubusercontent.com/'],
  53. ['assets.fastgit.org/', 'https://github.githubassets.com/'],
  54. // jsDelivr
  55. ['fastly.jsdelivr.net/', 'https://cdn.jsdelivr.net/'],
  56. ['gcore.jsdelivr.net/', 'https://cdn.jsdelivr.net/'],
  57. // ops.ci
  58. ['jsdelivr.ops.ci/', 'https://cdn.jsdelivr.net/'],
  59. ['fonts.ops.ci/', 'https://fonts.googleapis.com/'],
  60. // onmicrosoft.cn
  61. ['jsd.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/'],
  62. ['npm.onmicrosoft.cn/', 'https://unpkg.com/'],
  63. ['cdnjs.onmicrosoft.cn/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  64. // KGitHub
  65. ['raw.kgithub.com/', 'https://raw.githubusercontent.com/'],
  66. ['raw.kkgithub.com/', 'https://raw.githubusercontent.com/'],
  67. // Polyfill
  68. ['polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
  69. ['polyfill.top/', 'https://cdnjs.cloudflare.com/polyfill/'],
  70. ['polyfill-js.cn/', 'https://cdnjs.cloudflare.com/polyfill/'],
  71. ['cdn.polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
  72. ['fastly-polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
  73. ['fastly-polyfill.net/', 'https://cdnjs.cloudflare.com/polyfill/'],
  74. // BootCDN has been controlled by a malicious actor and being used to spread malware
  75. ['cdn.bootcss.com/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  76. ['cdn.bootcdn.net/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  77. ['cdn.bootcdn.com/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  78. ['cdn.staticfile.net/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  79. ['cdn.staticfile.org/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  80. // Misc
  81. ['pics.javbus.com/', 'https://i0.wp.com/pics.javbus.com/'],
  82. ['googlefonts.wp-china-yes.net/', 'https://fonts.googleapis.com/'],
  83. ['googleajax.wp-china-yes.net/', 'https://ajax.googleapis.com/']
  84. ] as const;
  85. const REDIRECT_MIRROR_307 = [
  86. // Redirect Google
  87. ['google.cn/', 'https://google.com/'],
  88. ['www.google.cn/', 'https://www.google.com/'],
  89. ['g.cn/', 'https://google.com/'],
  90. ['www.g.cn/', 'https://www.google.com/'],
  91. // avg.tv/sm114514 -> https://www.nicovideo.jp/watch/sm114514
  92. ['acg.tv/sm', 'https://www.nicovideo.jp/watch/sm'],
  93. ['acg.tv/', 'https://b23.tv/']
  94. ];
  95. const REDIRECT_FAKEWEBSITES = [
  96. // IGN China to IGN Global
  97. ['ign.xn--fiqs8s', 'https://cn.ign.com/ccpref/us'],
  98. // Fuck Makeding
  99. ['abbyychina.com', 'https://www.abbyy.cn'],
  100. ['bartender.cc', 'https://cn.seagullscientific.com'],
  101. ['betterzip.net', 'https://macitbetter.com'],
  102. ['beyondcompare.cc', 'https://www.scootersoftware.com'],
  103. ['bingdianhuanyuan.cn', 'https://www.faronics.com'],
  104. ['chemdraw.com.cn', 'https://revvitysignals.com/products/research/chemdraw'],
  105. ['codesoftchina.com', 'https://www.teklynx.com'],
  106. ['coreldrawchina.com', 'https://www.coreldraw.com'],
  107. ['crossoverchina.com', 'https://www.codeweavers.com'],
  108. ['easyrecoverychina.com', 'https://www.ontrack.com'],
  109. ['ediuschina.com', 'https://www.grassvalley.com'],
  110. ['flstudiochina.com', 'https://www.image-line.com/fl-studio'],
  111. ['formysql.com', 'https://www.navicat.com.cn'],
  112. ['guitarpro.cc', 'https://www.guitar-pro.com'],
  113. ['huishenghuiying.com.cn', 'https://www.corel.com'],
  114. ['iconworkshop.cn', 'https://www.axialis.com/iconworkshop'],
  115. ['imindmap.cc', 'https://imindmap.com/zh-cn'],
  116. ['jihehuaban.com.cn', 'https://sketch.io'],
  117. ['keyshot.cc', 'https://www.keyshot.com'],
  118. ['mathtype.cn', 'https://www.wiris.com/en/mathtype'],
  119. ['mindmanager.cc', 'https://www.mindjet.com'],
  120. ['mindmapper.cc', 'https://mindmapper.com'],
  121. ['mycleanmymac.com', 'https://macpaw.com/cleanmymac'],
  122. ['nicelabel.cc', 'https://www.nicelabel.com'],
  123. ['ntfsformac.cc', 'https://www.tuxera.com/products/tuxera-ntfs-for-mac-cn'],
  124. ['ntfsformac.cn', 'https://www.paragon-software.com/ufsdhome/zh/ntfs-mac'],
  125. ['overturechina.com', 'https://sonicscores.com/overture'],
  126. ['passwordrecovery.cn', 'https://cn.elcomsoft.com/aopr.html'],
  127. ['pdfexpert.cc', 'https://pdfexpert.com/zh'],
  128. ['ultraiso.net', 'https://cn.ezbsystems.com/ultraiso'],
  129. ['vegaschina.cn', 'https://www.vegas.com'],
  130. ['xmindchina.net', 'https://www.xmind.cn'],
  131. ['xshellcn.com', 'https://www.netsarang.com/products/xsh_overview.html'],
  132. ['yuanchengxiezuo.com', 'https://www.teamviewer.com/zhcn'],
  133. ['zbrushcn.com', 'https://www.maxon.net/en/zbrush']
  134. ] as const;
  135. export const buildRedirectModule = task(require.main === module, __filename)(async (span) => {
  136. const domains = Array.from(new Set([
  137. ...REDIRECT_MIRROR_HEADER.map(([from]) => getHostname(from, { detectIp: false })),
  138. ...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`]),
  139. ...REDIRECT_MIRROR_307.map(([from]) => getHostname(from, { detectIp: false }))
  140. ])).filter(isTruthy);
  141. return compareAndWriteFile(
  142. span,
  143. [
  144. '#!name=[Sukka] URL Redirect',
  145. `#!desc=Last Updated: ${new Date().toISOString()}`,
  146. '',
  147. '[MITM]',
  148. `hostname = %APPEND% ${domains.join(', ')}`,
  149. '',
  150. '[URL Rewrite]',
  151. ...REDIRECT_MIRROR_HEADER.map(([from, to]) => `^https?://${escapeRegExp(from)}(.*) ${to}$1 header`),
  152. ...REDIRECT_FAKEWEBSITES.map(([from, to]) => `^https?://(www.)?${escapeRegExp(from)} ${to} 307`),
  153. ...REDIRECT_MIRROR_307.map(([from, to]) => `^https?://${escapeRegExp(from)}(.*) ${to}$1 307`)
  154. ],
  155. path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect.sgmodule')
  156. );
  157. });