build-sgmodule-redirect.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import path from 'path';
  2. import { task } from './trace';
  3. import { compareAndWriteFile } from './lib/create-file';
  4. import * as tldts from 'tldts';
  5. function escapeRegExp(string = '') {
  6. const reRegExpChar = /[$()*+.?[\\\]^{|}]/g;
  7. const reHasRegExpChar = new RegExp(reRegExpChar.source);
  8. return string && reHasRegExpChar.test(string)
  9. ? string.replaceAll(reRegExpChar, '\\$&')
  10. : string;
  11. }
  12. const REDIRECT = [
  13. // Gravatar
  14. ['gravatar.neworld.org/', 'https://secure.gravatar.com/'],
  15. ['cdn.v2ex.com/gravatar/', 'https://secure.gravatar.com/avatar/'],
  16. // U.SB
  17. ['cdnjs.loli.net/', 'https://cdnjs.cloudflare.com/'],
  18. ['fonts.loli.net/', 'https://fonts.googleapis.com/'],
  19. ['gstatic.loli.net/', 'https://fonts.gstatic.com/'],
  20. ['themes.loli.net/', 'https://themes.googleusercontent.com/'],
  21. ['ajax.loli.net/', 'https://ajax.googleapis.com/'],
  22. ['gravatar.loli.net/', 'https://secure.gravatar.com/'],
  23. // Geekzu
  24. ['gapis.geekzu.org/ajax/', 'https://ajax.googleapis.com/'],
  25. ['fonts.geekzu.org/', 'https://fonts.googleapis.com/'],
  26. ['gapis.geekzu.org/g-fonts/', 'https://fonts.gstatic.com/'],
  27. ['gapis.geekzu.org/g-themes/', 'https://themes.googleusercontent.com/'],
  28. ['sdn.geekzu.org/', 'https://secure.gravatar.com/'],
  29. // cravatar
  30. ['cravatar.cn/', 'https://secure.gravatar.com/'],
  31. // libravatar
  32. ['seccdn.libravatar.org/gravatarproxy/', 'https://secure.gravatar.com/'],
  33. // ghproxy
  34. ['ghproxy.com/', 'https://'],
  35. ['ghps.cc/', 'https://'],
  36. // gh-proxy
  37. ['github.moeyy.xyz/', 'https://'],
  38. // 7ED Services
  39. ['use.sevencdn.com/css', 'https://fonts.googleapis.com/css'],
  40. ['use.sevencdn.com/ajax/libs/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  41. ['use.sevencdn.com/gajax/', 'https://ajax.googleapis.com/ajax/'],
  42. ['use.sevencdn.com/chart', 'https://chart.googleapis.com/chart'],
  43. ['use.sevencdn.com/avatar', 'https://secure.gravatar.com/avatar'],
  44. ['raw.gitmirror.com/', 'https://raw.githubusercontent.com/'],
  45. ['gist.gitmirror.com/', 'https://gist.githubusercontent.com/'],
  46. ['raw.githubusercontents.com/', 'https://raw.githubusercontent.com/'],
  47. ['gist.githubusercontents.com/', 'https://gist.githubusercontent.com/'],
  48. ['cdn.gitmirror.com/', 'https://cdn.statically.io/'],
  49. // FastGit
  50. ['raw.fastgit.org/', 'https://raw.githubusercontent.com/'],
  51. ['assets.fastgit.org/', 'https://github.githubassets.com/'],
  52. // jsDelivr
  53. ['fastly.jsdelivr.net/', 'https://cdn.jsdelivr.net/'],
  54. ['gcore.jsdelivr.net/', 'https://cdn.jsdelivr.net/'],
  55. // ops.ci
  56. ['jsdelivr.ops.ci/', 'https://cdn.jsdelivr.net/'],
  57. ['fonts.ops.ci/', 'https://fonts.googleapis.com/'],
  58. // onmicrosoft.cn
  59. ['jsd.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/'],
  60. ['npm.onmicrosoft.cn/', 'https://unpkg.com/'],
  61. ['cdnjs.onmicrosoft.cn/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
  62. // KGitHub
  63. ['raw.kgithub.com/', 'https://raw.githubusercontent.com/'],
  64. ['raw.kkgithub.com/', 'https://raw.githubusercontent.com/'],
  65. // Polyfill
  66. ['polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
  67. ['fastly-polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
  68. ['fastly-polyfill.net/', 'https://cdnjs.cloudflare.com/polyfill/'],
  69. // Misc
  70. ['pics.javbus.com/', 'https://i0.wp.com/pics.javbus.com/'],
  71. ['googlefonts.wp-china-yes.net/', 'https://fonts.googleapis.com/'],
  72. ['googleajax.wp-china-yes.net/', 'https://ajax.googleapis.com/']
  73. ] as const;
  74. export const buildRedirectModule = task(import.meta.path, async (span) => {
  75. const domains = Array.from(new Set(REDIRECT.map(([from]) => tldts.getHostname(from, { detectIp: false })))).filter(Boolean);
  76. return compareAndWriteFile(
  77. span,
  78. [
  79. '#!name=[Sukka] URL Redirect',
  80. `#!desc=Last Updated: ${new Date().toISOString()}`,
  81. '',
  82. '[MITM]',
  83. `hostname = %APPEND% ${domains.join(', ')}`,
  84. '',
  85. '[URL Rewrite]',
  86. ...REDIRECT.map(([from, to]) => `^https?://${escapeRegExp(from)}(.*) ${to}$1 302`)
  87. ],
  88. path.resolve(import.meta.dir, '../Modules/sukka_url_redirect.sgmodule')
  89. );
  90. });
  91. if (import.meta.main) {
  92. buildRedirectModule();
  93. }