Skip to content

Commit

Permalink
Merge branch 'SukkaW:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
FYLSen authored Feb 8, 2024
2 parents 2eee76b + 2982d74 commit 977de4a
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 96 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- name: Cache cache.db
uses: actions/cache@v4
with:
save-always: true
path: |
.cache
key: ${{ runner.os }}-v1-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:${{ steps.date.outputs.second }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Mock/www-google-analytics-com_ga.js
Mock/www-googletagservices-com_gpt.js
Mock/www-google-analytics-com_analytics.js
Mock/www-googlesyndication-com_adsbygoogle.js
Mock/amazon-adsystem-com_amazon-apstag.js
7 changes: 1 addition & 6 deletions Build/build-chn-cidr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { fetchRemoteTextByLine } from './lib/fetch-text-by-line';
import { resolve as pathResolve } from 'path';
import { compareAndWriteFile, withBannerArray } from './lib/create-file';
import { processLineFromReadline } from './lib/process-line';
import { traceSync } from './lib/trace-runner';
import { task } from './trace';

import { exclude } from 'fast-cidr-tools';
Expand All @@ -21,11 +20,7 @@ const INCLUDE_CIDRS = [

export const getChnCidrPromise = createMemoizedPromise(async () => {
const cidr = await processLineFromReadline(await fetchRemoteTextByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt'));
return traceSync(
picocolors.gray('processing chnroutes2'),
() => exclude([...cidr, ...INCLUDE_CIDRS], EXCLUDE_CIDRS, true),
picocolors.gray
);
return exclude([...cidr, ...INCLUDE_CIDRS], EXCLUDE_CIDRS, true);
});

export const buildChnCidr = task(import.meta.path, async (span) => {
Expand Down
1 change: 0 additions & 1 deletion Build/build-microsoft-cdn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'path';
import { traceAsync } from './lib/trace-runner';
import { task } from './trace';
import { createRuleset } from './lib/create-file';
import { fetchRemoteTextByLine } from './lib/fetch-text-by-line';
Expand Down
3 changes: 2 additions & 1 deletion Build/download-mock-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const ASSETS_LIST = {
'www-google-analytics-com_ga.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/google-analytics-ga.js',
'www-googletagservices-com_gpt.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/googletagservices-gpt.js',
'www-google-analytics-com_analytics.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/google-analytics.js',
'www-googlesyndication-com_adsbygoogle.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/googlesyndication-adsbygoogle.js'
'www-googlesyndication-com_adsbygoogle.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/googlesyndication-adsbygoogle.js',
'amazon-adsystem-com_amazon-apstag.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/amazon-apstag.js'
} as const;

const mockDir = path.resolve(import.meta.dir, '../Mock');
Expand Down
22 changes: 9 additions & 13 deletions Build/download-publicsuffixlist.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { TTL, fsFetchCache } from './lib/cache-filesystem';
import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
import { createMemoizedPromise } from './lib/memo-promise';
import { traceAsync } from './lib/trace-runner';

export const getPublicSuffixListTextPromise = createMemoizedPromise(() => traceAsync(
'obtain public_suffix_list',
() => fsFetchCache.apply(
'https://publicsuffix.org/list/public_suffix_list.dat',
() => fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit).then(r => r.text()),
{
// https://github.com/publicsuffix/list/blob/master/.github/workflows/tld-update.yml
// Though the action runs every 24 hours, the IANA list is updated every 7 days.
// So a 3 day TTL should be enough.
ttl: TTL.THREE_DAYS()
}
)
export const getPublicSuffixListTextPromise = createMemoizedPromise(() => fsFetchCache.apply(
'https://publicsuffix.org/list/public_suffix_list.dat',
() => fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit).then(r => r.text()),
{
// https://github.com/publicsuffix/list/blob/master/.github/workflows/tld-update.yml
// Though the action runs every 24 hours, the IANA list is updated every 7 days.
// So a 3 day TTL should be enough.
ttl: TTL.THREE_DAYS()
}
));
15 changes: 11 additions & 4 deletions Build/lib/cache-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import os from 'os';
import path from 'path';
import { mkdirSync } from 'fs';
import picocolors from 'picocolors';
import { traceSync } from './trace-runner';

const identity = (x: any) => x;

Expand Down Expand Up @@ -72,7 +71,13 @@ export class Cache<S = string> {
type: S extends string ? 'string' : 'buffer';

constructor({
cachePath = path.join(os.tmpdir() || '/tmp', 'hdc'), tbd, tableName = 'cache', type }: CacheOptions<S> = {}) {
cachePath = path.join(os.tmpdir() || '/tmp', 'hdc'),
tbd,
tableName = 'cache',
type
}: CacheOptions<S> = {}) {
const start = Bun.nanoseconds();

this.cachePath = cachePath;
mkdirSync(this.cachePath, { recursive: true });
if (tbd != null) this.tbd = tbd;
Expand Down Expand Up @@ -111,6 +116,9 @@ export class Cache<S = string> {
this.set('__LAST_VACUUM', dateString, 10 * 365 * 60 * 60 * 24 * 1000);
this.db.exec('VACUUM;');
}

const end = Bun.nanoseconds();
console.log(`${picocolors.gray(`[${((end - start) / 1e6).toFixed(3)}ms]`)} cache initialized from ${this.cachePath}`);
}

set(key: string, value: string, ttl = 60 * 1000): void {
Expand Down Expand Up @@ -172,7 +180,6 @@ export class Cache<S = string> {

if (peeked === promise) {
return promise.then((value) => {
const serializer = 'serializer' in opt ? opt.serializer : identity;
this.set(key, serializer(value), ttl);
return value;
});
Expand All @@ -195,7 +202,7 @@ export class Cache<S = string> {
}
}

export const fsFetchCache = traceSync('initializing filesystem cache for fetch', () => new Cache({ cachePath: path.resolve(import.meta.dir, '../../.cache') }));
export const fsFetchCache = new Cache({ cachePath: path.resolve(import.meta.dir, '../../.cache') });
// process.on('exit', () => {
// fsFetchCache.destroy();
// });
Expand Down
10 changes: 4 additions & 6 deletions Build/lib/cached-tld-parse.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { createCache } from './cache-apply';
import type { PublicSuffixList } from '@gorhill/publicsuffixlist';

let gothillGetDomainCache: ReturnType<typeof createCache> | null = null;
let gorhillGetDomainCache: ReturnType<typeof createCache> | null = null;
export const createCachedGorhillGetDomain = (gorhill: PublicSuffixList) => {
gothillGetDomainCache ??= createCache('cached-gorhill-get-domain', true);
return (domain: string) => {
// we do know gothillGetDomainCache exists here
return gothillGetDomainCache!.sync(domain, () => gorhill.getDomain(domain[0] === '.' ? domain.slice(1) : domain));
};
gorhillGetDomainCache ??= createCache('cached-gorhill-get-domain', true);
return (domain: string) => gorhillGetDomainCache! // we do know gothillGetDomainCache exists here
.sync(domain, () => gorhill.getDomain(domain[0] === '.' ? domain.slice(1) : domain));
};
12 changes: 3 additions & 9 deletions Build/lib/clash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,9 @@ const REQUIRE_REWRITE = {
export const surgeRulesetToClashClassicalTextRuleset = (rules: string[] | Set<string>) => {
const trie = Trie.from(rules);

return CLASH_SUPPORTED_RULE_TYPE.flatMap(
type => trie.find(`${type},`)
).concat(
Object.keys(REQUIRE_REWRITE).flatMap(
(type) => {
const found = trie.find(`${type},`);
return found.map(line => `${REQUIRE_REWRITE[type as keyof typeof REQUIRE_REWRITE]}${line.slice(type.length)}`);
}
)
return CLASH_SUPPORTED_RULE_TYPE.flatMap(type => trie.find(`${type},`)).concat(
Object.keys(REQUIRE_REWRITE).flatMap((type) => trie.find(`${type},`)
.map(line => `${REQUIRE_REWRITE[type as keyof typeof REQUIRE_REWRITE]}${line.slice(type.length)}`))
);
};

Expand Down
16 changes: 3 additions & 13 deletions Build/lib/fetch-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@ const sleepWithAbort = (ms: number, signal: AbortSignal) => new Promise<void>((r
return;
}

signal.addEventListener('abort', stop);
Bun.sleep(ms).then(done).catch(doReject);
function stop(this: AbortSignal) { reject(this.reason); }

function done() {
signal.removeEventListener('abort', stop);
resolve();
}
function stop(this: AbortSignal) {
reject(this.reason);
}
function doReject(reason: unknown) {
signal.removeEventListener('abort', stop);
reject(reason);
}
signal.addEventListener('abort', stop, { once: true });
Bun.sleep(ms).then(resolve).catch(reject).finally(() => signal.removeEventListener('abort', stop));
});

export async function fetchAssets(url: string, fallbackUrls: string[] | readonly string[]) {
Expand Down
5 changes: 2 additions & 3 deletions Build/lib/get-gorhill-publicsuffix.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { toASCII } from 'punycode';
import { traceAsync } from './trace-runner';
import { createMemoizedPromise } from './memo-promise';
import { getPublicSuffixListTextPromise } from '../download-publicsuffixlist';

const customFetch = (url: string | URL): Promise<Blob> => Promise.resolve(Bun.file(url));

export const getGorhillPublicSuffixPromise = createMemoizedPromise(() => traceAsync('create gorhill public suffix instance', async () => {
export const getGorhillPublicSuffixPromise = createMemoizedPromise(async () => {
const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
getPublicSuffixListTextPromise(),
import('@gorhill/publicsuffixlist')
Expand All @@ -15,4 +14,4 @@ export const getGorhillPublicSuffixPromise = createMemoizedPromise(() => traceAs
await gorhill.enableWASM({ customFetch });

return gorhill;
}));
});
6 changes: 1 addition & 5 deletions Build/lib/memo-promise.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export const createMemoizedPromise = <T>(fn: () => Promise<T>, preload = true): () => Promise<T> => {
let promise: Promise<T> | null = null;

if (preload) {
promise = fn();
}
let promise: Promise<T> | null = preload ? fn() : null;

return () => {
promise ??= fn();
Expand Down
20 changes: 0 additions & 20 deletions Build/lib/trace-runner.ts

This file was deleted.

24 changes: 11 additions & 13 deletions Build/lib/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ const deepTrieNodeToJSON = (node: TrieNode) => {
return obj;
};

function trieNodeInspectCustom(this: TrieNode) {
return JSON.stringify(deepTrieNodeToJSON(this), null, 2);
}

const createNode = (): TrieNode => {
const node = new Map<string, TrieNode>() as TrieNode;
node[SENTINEL] = false;
node[Bun.inspect.custom] = () => JSON.stringify(deepTrieNodeToJSON(node), null, 2);
node[Bun.inspect.custom] = trieNodeInspectCustom;
return node;
};

Expand Down Expand Up @@ -70,9 +74,7 @@ export const createTrie = (from?: string[] | Set<string> | null) => {
token = suffix[i];

node = node.get(token);
if (!node) {
return false;
}
if (!node) return false;
}

return true;
Expand All @@ -88,9 +90,7 @@ export const createTrie = (from?: string[] | Set<string> | null) => {
token = inputSuffix[i];

node = node.get(token);
if (!node) {
return [];
}
if (!node) return [];
}

const matches: string[] = [];
Expand Down Expand Up @@ -130,9 +130,7 @@ export const createTrie = (from?: string[] | Set<string> | null) => {
token = inputSuffix[i];

node = node.get(token);
if (!node) {
return;
}
if (!node) return;
}

// Performing DFS from prefix
Expand Down Expand Up @@ -213,11 +211,11 @@ export const createTrie = (from?: string[] | Set<string> | null) => {
for (let i = suffix.length - 1; i >= 0; i--) {
const token = suffix[i];

if (node.has(token)) {
node = node.get(token)!;
} else {
if (!node.has(token)) {
return false;
}

node = node.get(token)!;
}

return node[SENTINEL];
Expand Down
2 changes: 1 addition & 1 deletion Modules/google_cn_307.sgmodule
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#!desc=将 google.cn 的请求重定向到 google.com

[URL Rewrite]
^(http|https):\/\/(www.)?(g|google)\.cn https://www.google.com 307
^https?://(www.)?(g|google)\.cn https://www.google.com 307

[MITM]
hostname = %APPEND% g.cn,www.g.cn,google.cn,www.google.cn
1 change: 1 addition & 0 deletions Modules/sukka_enhance_adblock.sgmodule
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ hostname = %APPEND% *.google-analytics.com, *.googletagmanager.com, *.googlesynd
^https?://.*\.doubleclick\.net/instream/ad_status\.js https://ruleset.skk.moe/Mock/doubleclick-net_instream_ad_status.js 302
^https?://cdn\.ampproject\.org/v0.js https://ruleset.skk.moe/Mock/ampproject-org_v0.js 302
^https?://.*\.addthis\.com/addthis_widget.js https://ruleset.skk.moe/Mock/addthis-com_addthis_widget.js 302
^https?://.*\.amazon-adsystem.com/aax2/apstag.js https://ruleset.skk.moe/Mock/amazon-adsystem-com_amazon-apstag.js 302
5 changes: 5 additions & 0 deletions Source/domainset/cdn.conf
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ i.imgflip.com
.yourimageshare.com
.iili.io
img.hcloud.lat
.imglink.win
# imgix custom domain
www.datocms-assets.com
images.pexels.com
Expand Down Expand Up @@ -1348,6 +1349,7 @@ images.opencollective.com
static.figma.com
s3-alpha-sig.figma.com
s3-alpha.figma.com
s3-figma-plugin-images-production-sig.figma.com
cdn.sprig.com
# yande.re
assets.yande.re
Expand Down Expand Up @@ -2379,3 +2381,6 @@ redircdn.com
images7.design-editor.com
images8.design-editor.com
images.design-editor.com
assets.st-note.com
filestore.fortinet.com
.daumcdn.net
Loading

0 comments on commit 977de4a

Please sign in to comment.