mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-03 12:31:54 +08:00
New sgmodule: URL Redirect Minimum
This commit is contained in:
@@ -2,35 +2,34 @@ import path from 'node:path';
|
|||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { compareAndWriteFile } from './lib/create-file';
|
import { compareAndWriteFile } from './lib/create-file';
|
||||||
import { getHostname } from 'tldts-experimental';
|
import { getHostname } from 'tldts-experimental';
|
||||||
import { isTruthy } from 'foxts/guard';
|
import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR } from './constants/dir';
|
||||||
import { OUTPUT_MODULES_DIR } from './constants/dir';
|
|
||||||
import { escapeRegexp } from 'fast-escape-regexp';
|
import { escapeRegexp } from 'fast-escape-regexp';
|
||||||
|
|
||||||
const REDIRECT_MIRROR_HEADER = [
|
const REDIRECT_MIRROR_HEADER = [
|
||||||
// Gravatar
|
// Gravatar
|
||||||
['gravatar.neworld.org/', 'https://secure.gravatar.com/'],
|
// ['gravatar.neworld.org/', 'https://secure.gravatar.com/'],
|
||||||
['cdn.v2ex.com/gravatar/', 'https://secure.gravatar.com/avatar/'],
|
['cdn.v2ex.com/gravatar/', 'https://secure.gravatar.com/avatar/', true],
|
||||||
// U.SB
|
// U.SB
|
||||||
['cdnjs.loli.net/', 'https://cdnjs.cloudflare.com/'],
|
['cdnjs.loli.net/', 'https://cdnjs.cloudflare.com/', true],
|
||||||
['fonts.loli.net/', 'https://fonts.googleapis.com/'],
|
['fonts.loli.net/', 'https://fonts.googleapis.com/', true],
|
||||||
['gstatic.loli.net/', 'https://fonts.gstatic.com/'],
|
['gstatic.loli.net/', 'https://fonts.gstatic.com/', true],
|
||||||
['themes.loli.net/', 'https://themes.googleusercontent.com/'],
|
['themes.loli.net/', 'https://themes.googleusercontent.com/', true],
|
||||||
['ajax.loli.net/', 'https://ajax.googleapis.com/'],
|
['ajax.loli.net/', 'https://ajax.googleapis.com/', true],
|
||||||
['gravatar.loli.net/', 'https://secure.gravatar.com/'],
|
['gravatar.loli.net/', 'https://secure.gravatar.com/', true],
|
||||||
// Geekzu
|
// Geekzu
|
||||||
['gapis.geekzu.org/ajax/', 'https://ajax.googleapis.com/'],
|
['gapis.geekzu.org/ajax/', 'https://ajax.googleapis.com/', true],
|
||||||
['fonts.geekzu.org/', 'https://fonts.googleapis.com/'],
|
['fonts.geekzu.org/', 'https://fonts.googleapis.com/', true],
|
||||||
['gapis.geekzu.org/g-fonts/', 'https://fonts.gstatic.com/'],
|
['gapis.geekzu.org/g-fonts/', 'https://fonts.gstatic.com/', true],
|
||||||
['gapis.geekzu.org/g-themes/', 'https://themes.googleusercontent.com/'],
|
['gapis.geekzu.org/g-themes/', 'https://themes.googleusercontent.com/', true],
|
||||||
['sdn.geekzu.org/', 'https://secure.gravatar.com/'],
|
['sdn.geekzu.org/', 'https://secure.gravatar.com/', true],
|
||||||
// libravatar
|
// libravatar
|
||||||
['seccdn.libravatar.org/gravatarproxy/', 'https://secure.gravatar.com/'],
|
['seccdn.libravatar.org/gravatarproxy/', 'https://secure.gravatar.com/', true],
|
||||||
// 7ED Services
|
// 7ED Services
|
||||||
['use.sevencdn.com/css', 'https://fonts.googleapis.com/css'],
|
['use.sevencdn.com/css', 'https://fonts.googleapis.com/css', true],
|
||||||
['use.sevencdn.com/ajax/libs/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
['use.sevencdn.com/ajax/libs/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
|
||||||
['use.sevencdn.com/gajax/', 'https://ajax.googleapis.com/ajax/'],
|
['use.sevencdn.com/gajax/', 'https://ajax.googleapis.com/ajax/', true],
|
||||||
['use.sevencdn.com/chart', 'https://chart.googleapis.com/chart'],
|
['use.sevencdn.com/chart', 'https://chart.googleapis.com/chart', true],
|
||||||
['use.sevencdn.com/avatar', 'https://secure.gravatar.com/avatar'],
|
['use.sevencdn.com/avatar', 'https://secure.gravatar.com/avatar', true],
|
||||||
['raw.gitmirror.com/', 'https://raw.githubusercontent.com/'],
|
['raw.gitmirror.com/', 'https://raw.githubusercontent.com/'],
|
||||||
['gist.gitmirror.com/', 'https://gist.githubusercontent.com/'],
|
['gist.gitmirror.com/', 'https://gist.githubusercontent.com/'],
|
||||||
['raw.githubusercontents.com/', 'https://raw.githubusercontent.com/'],
|
['raw.githubusercontents.com/', 'https://raw.githubusercontent.com/'],
|
||||||
@@ -40,49 +39,47 @@ const REDIRECT_MIRROR_HEADER = [
|
|||||||
['raw.fastgit.org/', 'https://raw.githubusercontent.com/'],
|
['raw.fastgit.org/', 'https://raw.githubusercontent.com/'],
|
||||||
// ['assets.fastgit.org/', 'https://github.githubassets.com/'],
|
// ['assets.fastgit.org/', 'https://github.githubassets.com/'],
|
||||||
// jsDelivr
|
// jsDelivr
|
||||||
['fastly.jsdelivr.net/', 'https://cdn.jsdelivr.net/'],
|
['fastly.jsdelivr.net/', 'https://cdn.jsdelivr.net/', true],
|
||||||
['gcore.jsdelivr.net/', 'https://cdn.jsdelivr.net/'],
|
['gcore.jsdelivr.net/', 'https://cdn.jsdelivr.net/', true],
|
||||||
['testingcf.jsdelivr.net/', 'https://cdn.jsdelivr.net/'],
|
['testingcf.jsdelivr.net/', 'https://cdn.jsdelivr.net/', true],
|
||||||
// JSDMirror
|
// JSDMirror
|
||||||
['cdn.jsdmirror.com/', 'https://cdn.jsdelivr.net/'],
|
['cdn.jsdmirror.com/', 'https://cdn.jsdelivr.net/', true],
|
||||||
['cdn.jsdmirror.cn/', 'https://cdn.jsdelivr.net/'],
|
['cdn.jsdmirror.cn/', 'https://cdn.jsdelivr.net/', true],
|
||||||
// onmicrosoft.cn
|
// onmicrosoft.cn
|
||||||
['jsd.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/'],
|
['jsd.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/', true],
|
||||||
['npm.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/npm/'],
|
['npm.onmicrosoft.cn/', 'https://cdn.jsdelivr.net/npm/', true],
|
||||||
['cdnjs.onmicrosoft.cn/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
['cdnjs.onmicrosoft.cn/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
|
||||||
// KGitHub
|
// KGitHub
|
||||||
['raw.kgithub.com/', 'https://raw.githubusercontent.com/'],
|
['raw.kgithub.com/', 'https://raw.githubusercontent.com/'],
|
||||||
['raw.kkgithub.com/', 'https://raw.githubusercontent.com/'],
|
['raw.kkgithub.com/', 'https://raw.githubusercontent.com/'],
|
||||||
// cdn.iocdn.cc
|
// cdn.iocdn.cc
|
||||||
['cdn.iocdn.cc/avatar/', 'https://secure.gravatar.com/avatar/'],
|
['cdn.iocdn.cc/avatar/', 'https://secure.gravatar.com/avatar/', true],
|
||||||
['cdn.iocdn.cc/css', 'https://fonts.googleapis.com/css'],
|
['cdn.iocdn.cc/css', 'https://fonts.googleapis.com/css', true],
|
||||||
['cdn.iocdn.cc/icon', 'https://fonts.googleapis.com/icon'],
|
['cdn.iocdn.cc/icon', 'https://fonts.googleapis.com/icon', true],
|
||||||
['cdn.iocdn.cc/earlyaccess', 'https://fonts.googleapis.com/earlyaccess'],
|
['cdn.iocdn.cc/earlyaccess', 'https://fonts.googleapis.com/earlyaccess', true],
|
||||||
['cdn.iocdn.cc/s', 'https://fonts.gstatic.com/s'],
|
['cdn.iocdn.cc/s', 'https://fonts.gstatic.com/s', true],
|
||||||
['cdn.iocdn.cc/static', 'https://themes.googleusercontent.com/static'],
|
['cdn.iocdn.cc/static', 'https://themes.googleusercontent.com/static', true],
|
||||||
['cdn.iocdn.cc/ajax', 'https://ajax.googleapis.com/ajax'],
|
['cdn.iocdn.cc/ajax', 'https://ajax.googleapis.com/ajax', true],
|
||||||
['cdn.iocdn.cc/', 'https://cdn.jsdelivr.net/'],
|
['cdn.iocdn.cc/', 'https://cdn.jsdelivr.net/', true],
|
||||||
// wp-china-yes
|
// wp-china-yes
|
||||||
['googlefonts.admincdn.com/', 'https://fonts.googleapis.com/'],
|
['googlefonts.admincdn.com/', 'https://fonts.googleapis.com/', true],
|
||||||
['googleajax.admincdn.com/', 'https://ajax.googleapis.com/'],
|
['googleajax.admincdn.com/', 'https://ajax.googleapis.com/', true],
|
||||||
['cdnjs.admincdn.com/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
['cdnjs.admincdn.com/', 'https://cdnjs.cloudflare.com/ajax/libs/', true],
|
||||||
// Polyfill
|
// Polyfill
|
||||||
['polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
|
['polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/', true],
|
||||||
['polyfill.top/', 'https://cdnjs.cloudflare.com/polyfill/'],
|
['polyfill.top/', 'https://cdnjs.cloudflare.com/polyfill/', true],
|
||||||
['polyfill-js.cn/', 'https://cdnjs.cloudflare.com/polyfill/'],
|
['polyfill-js.cn/', 'https://cdnjs.cloudflare.com/polyfill/', true],
|
||||||
['cdn.polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
|
['cdn.polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/', true],
|
||||||
['fastly-polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/'],
|
['fastly-polyfill.io/', 'https://cdnjs.cloudflare.com/polyfill/', true],
|
||||||
['fastly-polyfill.net/', 'https://cdnjs.cloudflare.com/polyfill/'],
|
['fastly-polyfill.net/', 'https://cdnjs.cloudflare.com/polyfill/', true],
|
||||||
['polyfill-fastly.net/', 'https://cdnjs.cloudflare.com/polyfill/'],
|
['polyfill-fastly.net/', 'https://cdnjs.cloudflare.com/polyfill/', true],
|
||||||
// BootCDN has been controlled by a malicious actor and being used to spread malware
|
// BootCDN has been controlled by a malicious actor and being used to spread malware
|
||||||
['cdn.bootcss.com/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
['cdn.bootcss.com/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
||||||
['cdn.bootcdn.net/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
['cdn.bootcdn.net/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
||||||
['cdn.staticfile.net/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
['cdn.staticfile.net/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
||||||
['cdn.staticfile.org/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
['cdn.staticfile.org/', 'https://cdnjs.cloudflare.com/ajax/libs/'],
|
||||||
// The UNPKG has not been actively maintained and is finally down (https://github.com/unpkg/unpkg/issues/412)
|
// The UNPKG has not been actively maintained and is finally down (https://github.com/unpkg/unpkg/issues/412)
|
||||||
['unpkg.com/', 'https://cdn.jsdelivr.net/npm/'],
|
['unpkg.com/', 'https://cdn.jsdelivr.net/npm/', true]
|
||||||
// Misc
|
|
||||||
['pics.javbus.com/', 'https://i0.wp.com/pics.javbus.com/']
|
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const REDIRECT_MIRROR_307 = [
|
const REDIRECT_MIRROR_307 = [
|
||||||
@@ -146,20 +143,45 @@ const REDIRECT_FAKEWEBSITES = [
|
|||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export const buildRedirectModule = task(require.main === module, __filename)(async (span) => {
|
export const buildRedirectModule = task(require.main === module, __filename)(async (span) => {
|
||||||
const domains = Array.from(new Set([
|
const fullDomains: string[] = [];
|
||||||
...REDIRECT_MIRROR_HEADER.map(([from]) => getHostname(from, { detectIp: false })),
|
const minimumDomains: string[] = [];
|
||||||
...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `*.${from}`]),
|
|
||||||
...REDIRECT_MIRROR_307.map(([from]) => getHostname(from, { detectIp: false }))
|
|
||||||
])).filter(isTruthy);
|
|
||||||
|
|
||||||
return compareAndWriteFile(
|
for (let i = 0, len = REDIRECT_MIRROR_HEADER.length; i < len; i++) {
|
||||||
|
const [from, , canUboUriTransform] = REDIRECT_MIRROR_HEADER[i];
|
||||||
|
const hostname = getHostname(from, { detectIp: false });
|
||||||
|
if (hostname) {
|
||||||
|
fullDomains.push(hostname);
|
||||||
|
if (!canUboUriTransform) {
|
||||||
|
minimumDomains.push(hostname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let i = 0, len = REDIRECT_MIRROR_307.length; i < len; i++) {
|
||||||
|
const [from] = REDIRECT_MIRROR_307[i];
|
||||||
|
const hostname = getHostname(from, { detectIp: false });
|
||||||
|
if (hostname) {
|
||||||
|
fullDomains.push(hostname);
|
||||||
|
minimumDomains.push(hostname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let i = 0, len = REDIRECT_FAKEWEBSITES.length; i < len; i++) {
|
||||||
|
const [from] = REDIRECT_FAKEWEBSITES[i];
|
||||||
|
const hostname = getHostname(from, { detectIp: false });
|
||||||
|
if (hostname) {
|
||||||
|
fullDomains.push(hostname);
|
||||||
|
minimumDomains.push(hostname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
compareAndWriteFile(
|
||||||
span,
|
span,
|
||||||
[
|
[
|
||||||
'#!name=[Sukka] URL Redirect',
|
'#!name=[Sukka] URL Redirect',
|
||||||
`#!desc=Last Updated: ${new Date().toISOString()} Size: ${domains.length}`,
|
`#!desc=Last Updated: ${new Date().toISOString()} Size: ${fullDomains.length}`,
|
||||||
'',
|
'',
|
||||||
'[MITM]',
|
'[MITM]',
|
||||||
`hostname = %APPEND% ${domains.join(', ')}`,
|
`hostname = %APPEND% ${fullDomains.join(', ')}`,
|
||||||
'',
|
'',
|
||||||
'[URL Rewrite]',
|
'[URL Rewrite]',
|
||||||
...REDIRECT_MIRROR_HEADER.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 header`),
|
...REDIRECT_MIRROR_HEADER.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 header`),
|
||||||
@@ -167,5 +189,60 @@ export const buildRedirectModule = task(require.main === module, __filename)(asy
|
|||||||
...REDIRECT_MIRROR_307.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 307`)
|
...REDIRECT_MIRROR_307.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 307`)
|
||||||
],
|
],
|
||||||
path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect.sgmodule')
|
path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect.sgmodule')
|
||||||
|
),
|
||||||
|
compareAndWriteFile(
|
||||||
|
span,
|
||||||
|
[
|
||||||
|
'#!name=[Sukka] URL Redirect (Minimum)',
|
||||||
|
`#!desc=Last Updated: ${new Date().toISOString()} Size: ${minimumDomains.length}`,
|
||||||
|
'# This module only contains rules that doesn\'t work with/hasn\'t migrated to uBlock Origin\'s "uritransform" filter syntax',
|
||||||
|
'# uBO/AdGuard filter can be found at https://ruleset.skk.moe/Internal/sukka_ubo_url_redirect_filters.txt',
|
||||||
|
'',
|
||||||
|
'[MITM]',
|
||||||
|
`hostname = %APPEND% ${minimumDomains.join(', ')}`,
|
||||||
|
'',
|
||||||
|
'[URL Rewrite]',
|
||||||
|
...REDIRECT_MIRROR_HEADER.reduce<string[]>((acc, [from, to, canUboUriTransform]) => {
|
||||||
|
if (!canUboUriTransform) {
|
||||||
|
acc.push(`^https?://${escapeRegexp(from)}(.*) ${to}$1 header`);
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, []),
|
||||||
|
...REDIRECT_FAKEWEBSITES.map(([from, to]) => `^https?://(www.)?${(from)} ${to} 307`),
|
||||||
|
...REDIRECT_MIRROR_307.map(([from, to]) => `^https?://${escapeRegexp(from)}(.*) ${to}$1 307`)
|
||||||
|
],
|
||||||
|
path.join(OUTPUT_MODULES_DIR, 'sukka_url_redirect_minimum.sgmodule')
|
||||||
|
),
|
||||||
|
compareAndWriteFile(
|
||||||
|
span,
|
||||||
|
[
|
||||||
|
'! Title: [sukka] Sukka URL Redirect',
|
||||||
|
`! Last modified: ${new Date().toUTCString()}`,
|
||||||
|
'! Expires: 4 hours',
|
||||||
|
'! Description: Redirect requests via uritransform network filter syntax.',
|
||||||
|
'! License: https://ruleset.skk.moe/LICENSE',
|
||||||
|
'! Homepage: https://ruleset.skk.moe',
|
||||||
|
'! GitHub: https://github.com/SukkaW/Surge',
|
||||||
|
'',
|
||||||
|
...REDIRECT_MIRROR_HEADER.reduce<string[]>((acc, [from, to, canUboUriTransform]) => {
|
||||||
|
if (!canUboUriTransform) {
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
acc.push(
|
||||||
|
'||'
|
||||||
|
+ from
|
||||||
|
+ '$all,uritransform=/'
|
||||||
|
+ escapeRegexp(from).replaceAll('/', String.raw`\/`)
|
||||||
|
+ '/'
|
||||||
|
+ to.replace('https://', '').replaceAll('/', String.raw`\/`)
|
||||||
|
+ '/'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, [])
|
||||||
|
],
|
||||||
|
path.join(OUTPUT_INTERNAL_DIR, 'sukka_ubo_url_redirect_filters.txt')
|
||||||
|
)
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user