build-sgmodule-redirect.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // Don't enable URL Redirect minimum, due to its popularity and thus CSP issues
  83. ['unpkg.com/', 'https://cdn.jsdelivr.net/npm/']
  84. ];
  85. const REDIRECT_MIRROR_307: Array<[from: string, to: string, canUboUriTransform?: boolean]> = [
  86. // Redirect Google
  87. ['google.cn/', 'https://google.com/'],
  88. ['www.google.cn/', 'https://www.google.com/'],
  89. ['g.cn/', 'https://google.com/'],
  90. ['ditu.google.cn/', 'https://maps.google.com/'],
  91. ['maps.google.cn/', 'https://maps.google.com/'],
  92. ['www.g.cn/', 'https://www.google.com/'],
  93. // avg.tv/sm114514 -> https://www.nicovideo.jp/watch/sm114514
  94. ['acg.tv/sm', 'https://www.nicovideo.jp/watch/sm'],
  95. ['acg.tv/', 'https://b23.tv/'],
  96. // Minecraft Wiki
  97. ['minecraft.fandom.com/wiki/', 'https://minecraft.wiki/w/', true],
  98. ['minecraft.fandom.com/', 'https://minecraft.wiki/', true],
  99. // Hello, FANZA!
  100. ['missav.com/', 'https://missav.ai/', true],
  101. ['thisav.com/', 'https://thisav.me/', true]
  102. ];
  103. const REDIRECT_FAKEWEBSITES: Array<[from: string, to: string]> = [ // all REDIRECT_FAKEWEBSITES can be transformed by uBO uritransform
  104. // IGN China to IGN Global
  105. ['ign.xn--fiqs8s', 'https://cn.ign.com/ccpref/us'],
  106. // Fuck Makeding
  107. ['abbyychina.com', 'https://www.abbyy.cn'],
  108. ['bartender.cc', 'https://www.seagullscientific.com'],
  109. ['betterzip.net', 'https://macitbetter.com'],
  110. ['beyondcompare.cc', 'https://www.scootersoftware.com'],
  111. ['bingdianhuanyuan.cn', 'https://www.faronics.com'],
  112. ['chemdraw.com.cn', 'https://revvitysignals.com/products/research/chemdraw'],
  113. ['codesoftchina.com', 'https://www.teklynx.com'],
  114. ['coreldrawchina.com', 'https://www.coreldraw.com'],
  115. ['crossoverchina.com', 'https://www.codeweavers.com'],
  116. ['easyrecoverychina.com', 'https://www.ontrack.com'],
  117. ['ediuschina.com', 'https://www.grassvalley.com'],
  118. ['flstudiochina.com', 'https://www.image-line.com/fl-studio'],
  119. ['formysql.com', 'https://www.navicat.com.cn'],
  120. ['guitarpro.cc', 'https://www.guitar-pro.com'],
  121. ['huishenghuiying.com.cn', 'https://www.corel.com'],
  122. ['iconworkshop.cn', 'https://www.axialis.com/iconworkshop'],
  123. ['imindmap.cc', 'https://imindmap.com/zh-cn'],
  124. ['jihehuaban.com.cn', 'https://sketch.io'],
  125. ['keyshot.cc', 'https://www.keyshot.com'],
  126. ['mathtype.cn', 'https://www.wiris.com/en/mathtype'],
  127. ['mindmanager.cc', 'https://www.mindjet.com'],
  128. ['mindmapper.cc', 'https://mindmapper.com'],
  129. ['mycleanmymac.com', 'https://macpaw.com/cleanmymac'],
  130. ['nicelabel.cc', 'https://www.nicelabel.com'],
  131. ['ntfsformac.cc', 'https://www.tuxera.com/products/tuxera-ntfs-for-mac-cn'],
  132. ['ntfsformac.cn', 'https://www.paragon-software.com/ufsdhome/zh/ntfs-mac'],
  133. ['overturechina.com', 'https://sonicscores.com/overture'],
  134. ['passwordrecovery.cn', 'https://cn.elcomsoft.com/aopr.html'],
  135. ['pdfexpert.cc', 'https://pdfexpert.com/zh'],
  136. ['ultraiso.net', 'https://cn.ezbsystems.com/ultraiso'],
  137. ['vegaschina.cn', 'https://www.vegas.com'],
  138. ['xmindchina.net', 'https://www.xmind.cn'],
  139. ['xshellcn.com', 'https://www.netsarang.com/products/xsh_overview.html'],
  140. ['yuanchengxiezuo.com', 'https://www.teamviewer.com/zhcn'],
  141. ['zbrushcn.com', 'https://www.maxon.net/en/zbrush']
  142. ];
  143. export const buildRedirectModule = task(require.main === module, __filename)(async (span) => {
  144. const fullDomains = new Set<string>();
  145. const minimumDomains = new Set<string>();
  146. for (let i = 0, len = REDIRECT_MIRROR_HEADER.length; i < len; i++) {
  147. const [from, , canUboUriTransform] = REDIRECT_MIRROR_HEADER[i];
  148. const hostname = getHostname(from, { detectIp: false });
  149. if (hostname) {
  150. fullDomains.add(hostname);
  151. if (!canUboUriTransform) {
  152. minimumDomains.add(hostname);
  153. }
  154. }
  155. }
  156. for (let i = 0, len = REDIRECT_MIRROR_307.length; i < len; i++) {
  157. const [from, , canUboUriTransform] = REDIRECT_MIRROR_307[i];
  158. const hostname = getHostname(from, { detectIp: false });
  159. if (hostname) {
  160. fullDomains.add(hostname);
  161. if (!canUboUriTransform) {
  162. minimumDomains.add(hostname);
  163. }
  164. }
  165. }
  166. for (let i = 0, len = REDIRECT_FAKEWEBSITES.length; i < len; i++) {
  167. const [from] = REDIRECT_FAKEWEBSITES[i];
  168. const hostname = getHostname(from, { detectIp: false });
  169. if (hostname) {
  170. fullDomains.add(hostname);
  171. // REDIRECT_FAKEWEBSITES all can be transformed by uBO uritransform
  172. }
  173. }
  174. await Promise.all([
  175. compareAndWriteFile(
  176. span,
  177. [
  178. '#!name=[Sukka] URL Redirect',
  179. `#!desc=Last Updated: ${new Date().toISOString()} Size: ${fullDomains.size}`,
  180. '',
  181. '[MITM]',
  182. `hostname = %APPEND% ${fastStringArrayJoin(Array.from(fullDomains), ', ')}`,
  183. '',
  184. '[URL Rewrite]',
  185. ...REDIRECT_MIRROR_HEADER.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 header`),
  186. ...REDIRECT_FAKEWEBSITES.map(([from, to]) => `^https?://(www.)?${(from)} ${to} 307`),
  187. ...REDIRECT_MIRROR_307.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 307`)
  188. ],
  189. path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect.sgmodule')
  190. ),
  191. compareAndWriteFile(
  192. span,
  193. [
  194. '#!name=[Sukka] URL Redirect (Minimum)',
  195. `#!desc=Last Updated: ${new Date().toISOString()} Size: ${minimumDomains.size}`,
  196. '# This module only contains rules that doesn\'t work with/hasn\'t migrated to uBlock Origin\'s "uritransform" filter syntax',
  197. '# uBO/AdGuard filter can be found at https://ruleset.skk.moe/Internal/sukka_ubo_url_redirect_filters.txt',
  198. '# This reduces mitm-hostnames and improves performance, with the tradeoff of uBO/AdGuard filter only cover mostly in browser.',
  199. '',
  200. '[MITM]',
  201. `hostname = %APPEND% ${fastStringArrayJoin(Array.from(minimumDomains), ', ')}`,
  202. '',
  203. '[URL Rewrite]',
  204. ...REDIRECT_MIRROR_HEADER.reduce<string[]>((acc, [from, to, canUboUriTransform]) => {
  205. if (!canUboUriTransform) {
  206. acc.push(`^https?://${escapeRegexp(from)}(.*) ${to}$1 header`);
  207. }
  208. return acc;
  209. }, []),
  210. ...REDIRECT_MIRROR_307.reduce<string[]>((acc, [from, to, canUboUriTransform]) => {
  211. if (!canUboUriTransform) {
  212. acc.push(`^https?://${escapeRegexp(from)}(.*) ${to}$1 307`);
  213. }
  214. return acc;
  215. }, [])
  216. ],
  217. path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect_minimum.sgmodule')
  218. ),
  219. compareAndWriteFile(
  220. span,
  221. [
  222. '! Title: [sukka] Sukka URL Redirect',
  223. `! Last modified: ${new Date().toUTCString()}`,
  224. '! Expires: 4 hours',
  225. '! Description: Redirect requests via uritransform network filter syntax.',
  226. '! License: https://ruleset.skk.moe/LICENSE',
  227. '! Homepage: https://ruleset.skk.moe',
  228. '! GitHub: https://github.com/SukkaW/Surge',
  229. '',
  230. ...REDIRECT_MIRROR_HEADER.reduce<string[]>(uBOUriTransformGenerator, []),
  231. ...REDIRECT_MIRROR_307.reduce<string[]>(uBOUriTransformGenerator, []),
  232. ...REDIRECT_FAKEWEBSITES.reduce<string[]>(uBOUriTransformGeneratorForFakeWebsites, [])
  233. ],
  234. path.join(OUTPUT_INTERNAL_DIR, 'sukka_ubo_url_redirect_filters.txt')
  235. )
  236. ]);
  237. });
  238. function uBOUriTransformGenerator(acc: string[], [from, to, canUboUriTransform]: [from: string, to: string, canUboUriTransform?: boolean]): string[] {
  239. if (!canUboUriTransform) {
  240. return acc;
  241. }
  242. // unlike Surge, which processes rules form top to bottom, uBO treats later rules with higher priority (overriden-like behavior),
  243. // so when doing uBO we need to prepend. Given the the rules count is small, the performance impact is negligible.
  244. acc.unshift(
  245. '||'
  246. + from
  247. + '$all,uritransform=/'
  248. + escapeRegexp(from).replaceAll('/', String.raw`\/`)
  249. + '/'
  250. + to.replace('https://', '').replaceAll('/', String.raw`\/`)
  251. + '/'
  252. );
  253. return acc;
  254. }
  255. function uBOUriTransformGeneratorForFakeWebsites(acc: string[], [from, to]: [from: string, to: string]): string[] {
  256. // unlike Surge, which processes rules form top to bottom, uBO treats later rules with higher priority (overriden-like behavior),
  257. // so when doing uBO we need to prepend. Given the the rules count is small, the performance impact is negligible.
  258. acc.unshift(
  259. '||'
  260. + from
  261. + '$all,uritransform=/'
  262. + String.raw`.*` + escapeRegexp(from).replaceAll('/', String.raw`\/`) + String.raw`\/(.*)`
  263. + '/'
  264. + to.replace('https://', '').replaceAll('/', String.raw`\/`)
  265. + '/'
  266. );
  267. return acc;
  268. }