build-sgmodule-redirect.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import path from 'node:path';
  2. import { task } from './trace';
  3. import { compareAndWriteFile } from './lib/create-file';
  4. import { getHostname } from 'tldts-experimental';
  5. import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR } from './constants/dir';
  6. import { escapeRegexp } from 'fast-escape-regexp';
  7. import { fastStringArrayJoin } from 'foxts/fast-string-array-join';
  8. const REDIRECT_MIRROR_HEADER: Array<[from: string, to: string, canUboUriTransform?: boolean]> = [
  9. // Gravatar
  10. // ['gravatar.neworld.org/', 'https://secure.gravatar.com/'],
  11. ['cdn.v2ex.com/gravatar/', 'https://secure.gravatar.com/avatar/', true],
  12. // U.SB
  13. ['cdnjs.loli.net/', 'https://cdnjs.cloudflare.com/', true],
  14. ['fonts.loli.net/', 'https://fonts.googleapis.com/', true],
  15. ['gstatic.loli.net/', 'https://fonts.gstatic.com/', true],
  16. ['themes.loli.net/', 'https://themes.googleusercontent.com/', true],
  17. ['ajax.loli.net/', 'https://ajax.googleapis.com/', true],
  18. ['gravatar.loli.net/', 'https://secure.gravatar.com/', true],
  19. // Geekzu
  20. ['gapis.geekzu.org/ajax/', 'https://ajax.googleapis.com/', true],
  21. ['fonts.geekzu.org/', 'https://fonts.googleapis.com/', true],
  22. ['gapis.geekzu.org/g-fonts/', 'https://fonts.gstatic.com/', true],
  23. ['gapis.geekzu.org/g-themes/', 'https://themes.googleusercontent.com/', true],
  24. ['sdn.geekzu.org/', 'https://secure.gravatar.com/', true],
  25. // libravatar
  26. ['seccdn.libravatar.org/gravatarproxy/', 'https://secure.gravatar.com/', true],
  27. // 7ED Services
  28. ['use.sevencdn.com/css', 'https://fonts.googleapis.com/css', true],
  29. ['use.sevencdn.com/ajax/libs/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
  30. ['use.sevencdn.com/gajax/', 'https://ajax.googleapis.com/ajax/', true],
  31. ['use.sevencdn.com/chart', 'https://chart.googleapis.com/chart', true],
  32. ['use.sevencdn.com/avatar', 'https://secure.gravatar.com/avatar', true],
  33. ['raw.gitmirror.com/', 'https://raw.githubusercontent.com/'],
  34. ['gist.gitmirror.com/', 'https://gist.githubusercontent.com/'],
  35. ['raw.githubusercontents.com/', 'https://raw.githubusercontent.com/'],
  36. ['gist.githubusercontents.com/', 'https://gist.githubusercontent.com/'],
  37. ['cdn.gitmirror.com/', 'https://cdn.statically.io/'],
  38. // FastGit
  39. ['raw.fastgit.org/', 'https://raw.githubusercontent.com/'],
  40. // ['assets.fastgit.org/', 'https://github.githubassets.com/'],
  41. // jsDelivr
  42. ['fastly.jsdelivr.net/', 'https://cdn.jsdelivr.net/', true],
  43. ['gcore.jsdelivr.net/', 'https://cdn.jsdelivr.net/', true],
  44. ['testingcf.jsdelivr.net/', 'https://cdn.jsdelivr.net/', true],
  45. // JSDMirror
  46. ['cdn.jsdmirror.com/', 'https://cdn.jsdelivr.net/', true],
  47. ['cdn.jsdmirror.cn/', 'https://cdn.jsdelivr.net/', true],
  48. // onmicrosoft.cn
  49. ['jsd.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/', true],
  50. ['npm.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/npm/', true],
  51. ['cdnjs.onmicrosoft.cn/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
  52. // KGitHub
  53. ['raw.kgithub.com/', 'https://raw.githubusercontent.com/'],
  54. ['raw.kkgithub.com/', 'https://raw.githubusercontent.com/'],
  55. // cdn.iocdn.cc
  56. ['cdn.iocdn.cc/avatar/', 'https://secure.gravatar.com/avatar/', true],
  57. ['cdn.iocdn.cc/css', 'https://fonts.googleapis.com/css', true],
  58. ['cdn.iocdn.cc/icon', 'https://fonts.googleapis.com/icon', true],
  59. ['cdn.iocdn.cc/earlyaccess', 'https://fonts.googleapis.com/earlyaccess', true],
  60. ['cdn.iocdn.cc/s', 'https://fonts.gstatic.com/s', true],
  61. ['cdn.iocdn.cc/static', 'https://themes.googleusercontent.com/static', true],
  62. ['cdn.iocdn.cc/ajax', 'https://ajax.googleapis.com/ajax', true],
  63. ['cdn.iocdn.cc/', 'https://cdn.jsdelivr.net/', true],
  64. // wp-china-yes
  65. ['googlefonts.admincdn.com/', 'https://fonts.googleapis.com/', true],
  66. ['googleajax.admincdn.com/', 'https://ajax.googleapis.com/', true],
  67. ['cdnjs.admincdn.com/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
  68. // Polyfill
  69. ['polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/', true],
  70. ['polyfill.top/', 'https://cdnjs.cloudflare.com/polyfill/', true],
  71. ['polyfill-js.cn/', 'https://cdnjs.cloudflare.com/polyfill/', true],
  72. ['cdn.polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/', true],
  73. ['fastly-polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/', true],
  74. ['fastly-polyfill.net/', 'https://cdnjs.cloudflare.com/polyfill/', true],
  75. ['polyfill-fastly.net/', 'https://cdnjs.cloudflare.com/polyfill/', true],
  76. // BootCDN has been controlled by a malicious actor and being used to spread malware
  77. ['cdn.bootcss.com/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
  78. ['cdn.bootcdn.net/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
  79. ['cdn.staticfile.net/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
  80. ['cdn.staticfile.org/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
  81. // The UNPKG has not been actively maintained and is finally down (https://github.com/unpkg/unpkg/issues/412)
  82. ['unpkg.com/', 'https://cdn.jsdelivr.net/npm/', true]
  83. ];
  84. const REDIRECT_MIRROR_307: Array<[from: string, to: string, canUboUriTransform?: boolean]> = [
  85. // Redirect Google
  86. ['google.cn/', 'https://google.com/'],
  87. ['www.google.cn/', 'https://www.google.com/'],
  88. ['g.cn/', 'https://google.com/'],
  89. ['ditu.google.cn/', 'https://maps.google.com/'],
  90. ['maps.google.cn/', 'https://maps.google.com/'],
  91. ['www.g.cn/', 'https://www.google.com/'],
  92. // avg.tv/sm114514 -> https://www.nicovideo.jp/watch/sm114514
  93. ['acg.tv/sm', 'https://www.nicovideo.jp/watch/sm'],
  94. ['acg.tv/', 'https://b23.tv/'],
  95. // Minecraft Wiki
  96. ['minecraft.fandom.com/wiki/', 'https://minecraft.wiki/w/', true],
  97. ['minecraft.fandom.com/', 'https://minecraft.wiki/', true],
  98. // Hello, FANZA!
  99. ['missav.com/', 'https://missav.ai/', true],
  100. ['thisav.com/', 'https://thisav.me/', true]
  101. ];
  102. const REDIRECT_FAKEWEBSITES: Array<[from: string, to: string]> = [ // all REDIRECT_FAKEWEBSITES can be transformed by uBO uritransform
  103. // IGN China to IGN Global
  104. ['ign.xn--fiqs8s', 'https://cn.ign.com/ccpref/us'],
  105. // Fuck Makeding
  106. ['abbyychina.com', 'https://www.abbyy.cn'],
  107. ['bartender.cc', 'https://www.seagullscientific.com'],
  108. ['betterzip.net', 'https://macitbetter.com'],
  109. ['beyondcompare.cc', 'https://www.scootersoftware.com'],
  110. ['bingdianhuanyuan.cn', 'https://www.faronics.com'],
  111. ['chemdraw.com.cn', 'https://revvitysignals.com/products/research/chemdraw'],
  112. ['codesoftchina.com', 'https://www.teklynx.com'],
  113. ['coreldrawchina.com', 'https://www.coreldraw.com'],
  114. ['crossoverchina.com', 'https://www.codeweavers.com'],
  115. ['easyrecoverychina.com', 'https://www.ontrack.com'],
  116. ['ediuschina.com', 'https://www.grassvalley.com'],
  117. ['flstudiochina.com', 'https://www.image-line.com/fl-studio'],
  118. ['formysql.com', 'https://www.navicat.com.cn'],
  119. ['guitarpro.cc', 'https://www.guitar-pro.com'],
  120. ['huishenghuiying.com.cn', 'https://www.corel.com'],
  121. ['iconworkshop.cn', 'https://www.axialis.com/iconworkshop'],
  122. ['imindmap.cc', 'https://imindmap.com/zh-cn'],
  123. ['jihehuaban.com.cn', 'https://sketch.io'],
  124. ['keyshot.cc', 'https://www.keyshot.com'],
  125. ['mathtype.cn', 'https://www.wiris.com/en/mathtype'],
  126. ['mindmanager.cc', 'https://www.mindjet.com'],
  127. ['mindmapper.cc', 'https://mindmapper.com'],
  128. ['mycleanmymac.com', 'https://macpaw.com/cleanmymac'],
  129. ['nicelabel.cc', 'https://www.nicelabel.com'],
  130. ['ntfsformac.cc', 'https://www.tuxera.com/products/tuxera-ntfs-for-mac-cn'],
  131. ['ntfsformac.cn', 'https://www.paragon-software.com/ufsdhome/zh/ntfs-mac'],
  132. ['overturechina.com', 'https://sonicscores.com/overture'],
  133. ['passwordrecovery.cn', 'https://cn.elcomsoft.com/aopr.html'],
  134. ['pdfexpert.cc', 'https://pdfexpert.com/zh'],
  135. ['ultraiso.net', 'https://cn.ezbsystems.com/ultraiso'],
  136. ['vegaschina.cn', 'https://www.vegas.com'],
  137. ['xmindchina.net', 'https://www.xmind.cn'],
  138. ['xshellcn.com', 'https://www.netsarang.com/products/xsh_overview.html'],
  139. ['yuanchengxiezuo.com', 'https://www.teamviewer.com/zhcn'],
  140. ['zbrushcn.com', 'https://www.maxon.net/en/zbrush']
  141. ];
  142. export const buildRedirectModule = task(require.main === module, __filename)(async (span) => {
  143. const fullDomains = new Set<string>();
  144. const minimumDomains = new Set<string>();
  145. for (let i = 0, len = REDIRECT_MIRROR_HEADER.length; i < len; i++) {
  146. const [from, , canUboUriTransform] = REDIRECT_MIRROR_HEADER[i];
  147. const hostname = getHostname(from, { detectIp: false });
  148. if (hostname) {
  149. fullDomains.add(hostname);
  150. if (!canUboUriTransform) {
  151. minimumDomains.add(hostname);
  152. }
  153. }
  154. }
  155. for (let i = 0, len = REDIRECT_MIRROR_307.length; i < len; i++) {
  156. const [from, , canUboUriTransform] = REDIRECT_MIRROR_307[i];
  157. const hostname = getHostname(from, { detectIp: false });
  158. if (hostname) {
  159. fullDomains.add(hostname);
  160. if (!canUboUriTransform) {
  161. minimumDomains.add(hostname);
  162. }
  163. }
  164. }
  165. for (let i = 0, len = REDIRECT_FAKEWEBSITES.length; i < len; i++) {
  166. const [from] = REDIRECT_FAKEWEBSITES[i];
  167. const hostname = getHostname(from, { detectIp: false });
  168. if (hostname) {
  169. fullDomains.add(hostname);
  170. // REDIRECT_FAKEWEBSITES all can be transformed by uBO uritransform
  171. }
  172. }
  173. await Promise.all([
  174. compareAndWriteFile(
  175. span,
  176. [
  177. '#!name=[Sukka] URL Redirect',
  178. `#!desc=Last Updated: ${new Date().toISOString()} Size: ${fullDomains.size}`,
  179. '',
  180. '[MITM]',
  181. `hostname = %APPEND% ${fastStringArrayJoin(Array.from(fullDomains), ', ')}`,
  182. '',
  183. '[URL Rewrite]',
  184. ...REDIRECT_MIRROR_HEADER.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 header`),
  185. ...REDIRECT_FAKEWEBSITES.map(([from, to]) => `^https?://(www.)?${(from)} ${to} 307`),
  186. ...REDIRECT_MIRROR_307.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 307`)
  187. ],
  188. path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect.sgmodule')
  189. ),
  190. compareAndWriteFile(
  191. span,
  192. [
  193. '#!name=[Sukka] URL Redirect (Minimum)',
  194. `#!desc=Last Updated: ${new Date().toISOString()} Size: ${minimumDomains.size}`,
  195. '# This module only contains rules that doesn\'t work with/hasn\'t migrated to uBlock Origin\'s "uritransform" filter syntax',
  196. '# uBO/AdGuard filter can be found at https://ruleset.skk.moe/Internal/sukka_ubo_url_redirect_filters.txt',
  197. '# This reduces mitm-hostnames and improves performance, with the tradeoff of uBO/AdGuard filter only cover mostly in browser.',
  198. '',
  199. '[MITM]',
  200. `hostname = %APPEND% ${fastStringArrayJoin(Array.from(minimumDomains), ', ')}`,
  201. '',
  202. '[URL Rewrite]',
  203. ...REDIRECT_MIRROR_HEADER.reduce<string[]>((acc, [from, to, canUboUriTransform]) => {
  204. if (!canUboUriTransform) {
  205. acc.push(`^https?://${escapeRegexp(from)}(.*) ${to}$1 header`);
  206. }
  207. return acc;
  208. }, []),
  209. ...REDIRECT_MIRROR_307.reduce<string[]>((acc, [from, to, canUboUriTransform]) => {
  210. if (!canUboUriTransform) {
  211. acc.push(`^https?://${escapeRegexp(from)}(.*) ${to}$1 307`);
  212. }
  213. return acc;
  214. }, [])
  215. ],
  216. path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect_minimum.sgmodule')
  217. ),
  218. compareAndWriteFile(
  219. span,
  220. [
  221. '! Title: [sukka] Sukka URL Redirect',
  222. `! Last modified: ${new Date().toUTCString()}`,
  223. '! Expires: 4 hours',
  224. '! Description: Redirect requests via uritransform network filter syntax.',
  225. '! License: https://ruleset.skk.moe/LICENSE',
  226. '! Homepage: https://ruleset.skk.moe',
  227. '! GitHub: https://github.com/SukkaW/Surge',
  228. '',
  229. ...REDIRECT_MIRROR_HEADER.reduce<string[]>(uBOUriTransformGenerator, []),
  230. ...REDIRECT_MIRROR_307.reduce<string[]>(uBOUriTransformGenerator, []),
  231. ...REDIRECT_FAKEWEBSITES.reduce<string[]>(uBOUriTransformGeneratorForFakeWebsites, [])
  232. ],
  233. path.join(OUTPUT_INTERNAL_DIR, 'sukka_ubo_url_redirect_filters.txt')
  234. )
  235. ]);
  236. });
  237. function uBOUriTransformGenerator(acc: string[], [from, to, canUboUriTransform]: [from: string, to: string, canUboUriTransform?: boolean]): string[] {
  238. if (!canUboUriTransform) {
  239. return acc;
  240. }
  241. // unlike Surge, which processes rules form top to bottom, uBO treats later rules with higher priority (overriden-like behavior),
  242. // so when doing uBO we need to prepend. Given the the rules count is small, the performance impact is negligible.
  243. acc.unshift(
  244. '||'
  245. + from
  246. + '$all,uritransform=/'
  247. + escapeRegexp(from).replaceAll('/', String.raw`\/`)
  248. + '/'
  249. + to.replace('https://', '').replaceAll('/', String.raw`\/`)
  250. + '/'
  251. );
  252. return acc;
  253. }
  254. function uBOUriTransformGeneratorForFakeWebsites(acc: string[], [from, to]: [from: string, to: string]): string[] {
  255. // unlike Surge, which processes rules form top to bottom, uBO treats later rules with higher priority (overriden-like behavior),
  256. // so when doing uBO we need to prepend. Given the the rules count is small, the performance impact is negligible.
  257. acc.unshift(
  258. '||'
  259. + from
  260. + '$all,uritransform=/'
  261. + String.raw`.*` + escapeRegexp(from).replaceAll('/', String.raw`\/`) + String.raw`\/(.*)`
  262. + '/'
  263. + to.replace('https://', '').replaceAll('/', String.raw`\/`)
  264. + '/'
  265. );
  266. return acc;
  267. }