build-sgmodule-redirect.ts 13 KB

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