build-sgmodule-redirect.ts 14 KB

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