mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
Chore: minor changes
This commit is contained in:
parent
6f8d515e7c
commit
6235d9fee0
@ -8,7 +8,7 @@ import { processLine } from './lib/process-line';
|
|||||||
import { RulesetOutput } from './lib/create-file';
|
import { RulesetOutput } from './lib/create-file';
|
||||||
import { SOURCE_DIR } from './constants/dir';
|
import { SOURCE_DIR } from './constants/dir';
|
||||||
import { $$fetch } from './lib/fetch-retry';
|
import { $$fetch } from './lib/fetch-retry';
|
||||||
import { fetchAssetsWithout304 } from './lib/fetch-assets';
|
import { fetchAssets } from './lib/fetch-assets';
|
||||||
|
|
||||||
const BOGUS_NXDOMAIN_URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
|
const BOGUS_NXDOMAIN_URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
|
||||||
const getBogusNxDomainIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = $$fetch(BOGUS_NXDOMAIN_URL).then(async (resp) => {
|
const getBogusNxDomainIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = $$fetch(BOGUS_NXDOMAIN_URL).then(async (resp) => {
|
||||||
@ -37,7 +37,7 @@ const BOTNET_FILTER_MIRROR_URL = [
|
|||||||
// https://curbengh.github.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt
|
// https://curbengh.github.io/malware-filter/botnet-filter-dnscrypt-blocked-ips.txt
|
||||||
];
|
];
|
||||||
|
|
||||||
const getBotNetFilterIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = fetchAssetsWithout304(BOTNET_FILTER_URL, BOTNET_FILTER_MIRROR_URL).then(text => text.split('\n').reduce<[ipv4: string[], ipv6: string[]]>((acc, cur) => {
|
const getBotNetFilterIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = fetchAssets(BOTNET_FILTER_URL, BOTNET_FILTER_MIRROR_URL).then(text => text.split('\n').reduce<[ipv4: string[], ipv6: string[]]>((acc, cur) => {
|
||||||
const ip = processLine(cur);
|
const ip = processLine(cur);
|
||||||
if (ip) {
|
if (ip) {
|
||||||
if (isProbablyIpv4(ip)) {
|
if (isProbablyIpv4(ip)) {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import { $$fetch, defaultRequestInit, ResponseError } from './fetch-retry';
|
import { $$fetch, defaultRequestInit, ResponseError } from './fetch-retry';
|
||||||
import { wait } from 'foxts/wait';
|
import { waitWithAbort } from 'foxts/wait';
|
||||||
|
|
||||||
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
|
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
|
||||||
export class CustomAbortError extends Error {
|
export class CustomAbortError extends Error {
|
||||||
@ -26,29 +26,14 @@ export class CustomNoETagFallbackError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sleepWithAbort(ms: number, signal: AbortSignal) {
|
export async function fetchAssets(url: string, fallbackUrls: null | undefined | string[] | readonly string[]) {
|
||||||
return new Promise<void>((resolve, reject) => {
|
|
||||||
if (signal.aborted) {
|
|
||||||
reject(signal.reason as Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
signal.addEventListener('abort', stop, { once: true });
|
|
||||||
|
|
||||||
wait(ms).then(resolve).catch(reject).finally(() => signal.removeEventListener('abort', stop));
|
|
||||||
|
|
||||||
function stop(this: AbortSignal) { reject(this.reason as Error); }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function fetchAssetsWithout304(url: string, fallbackUrls: null | undefined | string[] | readonly string[]) {
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
|
||||||
const createFetchFallbackPromise = async (url: string, index: number) => {
|
const createFetchFallbackPromise = async (url: string, index: number) => {
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
// Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
|
// Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
|
||||||
try {
|
try {
|
||||||
await sleepWithAbort(50 + (index + 1) * 100, controller.signal);
|
await waitWithAbort(50 + (index + 1) * 100, controller.signal);
|
||||||
} catch {
|
} catch {
|
||||||
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
|
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
|
||||||
throw new CustomAbortError();
|
throw new CustomAbortError();
|
||||||
|
|||||||
@ -95,10 +95,9 @@ setGlobalDispatcher(agent.compose(
|
|||||||
: retryAfter * 1e3; // Retry-After is in seconds
|
: retryAfter * 1e3; // Retry-After is in seconds
|
||||||
}
|
}
|
||||||
|
|
||||||
const retryTimeout
|
const retryTimeout = retryAfter > 0
|
||||||
= retryAfter > 0
|
? Math.min(retryAfter, maxTimeout)
|
||||||
? Math.min(retryAfter, maxTimeout)
|
: Math.min(minTimeout * (timeoutFactor ** (counter - 1)), maxTimeout);
|
||||||
: Math.min(minTimeout * (timeoutFactor ** (counter - 1)), maxTimeout);
|
|
||||||
|
|
||||||
console.log('[fetch retry]', 'schedule retry', { statusCode, retryTimeout, errorCode, url: opts.origin });
|
console.log('[fetch retry]', 'schedule retry', { statusCode, retryTimeout, errorCode, url: opts.origin });
|
||||||
// eslint-disable-next-line sukka/prefer-timer-id -- won't leak
|
// eslint-disable-next-line sukka/prefer-timer-id -- won't leak
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import picocolors from 'picocolors';
|
|||||||
import { fastNormalizeDomain } from '../normalize-domain';
|
import { fastNormalizeDomain } from '../normalize-domain';
|
||||||
import { processLine } from '../process-line';
|
import { processLine } from '../process-line';
|
||||||
import { onBlackFound } from './shared';
|
import { onBlackFound } from './shared';
|
||||||
import { fetchAssetsWithout304 } from '../fetch-assets';
|
import { fetchAssets } from '../fetch-assets';
|
||||||
import type { Span } from '../../trace';
|
import type { Span } from '../../trace';
|
||||||
|
|
||||||
function domainListLineCb(l: string, set: string[], includeAllSubDomain: boolean, meta: string) {
|
function domainListLineCb(l: string, set: string[], includeAllSubDomain: boolean, meta: string) {
|
||||||
@ -32,7 +32,7 @@ export function processDomainLists(
|
|||||||
domainListsUrl: string, mirrors: string[] | null, includeAllSubDomain = false
|
domainListsUrl: string, mirrors: string[] | null, includeAllSubDomain = false
|
||||||
) {
|
) {
|
||||||
return span.traceChildAsync(`process domainlist: ${domainListsUrl}`, async (span) => {
|
return span.traceChildAsync(`process domainlist: ${domainListsUrl}`, async (span) => {
|
||||||
const text = await span.traceChildAsync('download', () => fetchAssetsWithout304(
|
const text = await span.traceChildAsync('download', () => fetchAssets(
|
||||||
domainListsUrl,
|
domainListsUrl,
|
||||||
mirrors
|
mirrors
|
||||||
));
|
));
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import type { Span } from '../../trace';
|
import type { Span } from '../../trace';
|
||||||
import { fetchAssetsWithout304 } from '../fetch-assets';
|
import { fetchAssets } from '../fetch-assets';
|
||||||
import { onBlackFound, onWhiteFound } from './shared';
|
import { onBlackFound, onWhiteFound } from './shared';
|
||||||
import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
|
import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
|
||||||
import { fastNormalizeDomain } from '../normalize-domain';
|
import { fastNormalizeDomain } from '../normalize-domain';
|
||||||
@ -27,7 +27,7 @@ export async function processFilterRules(
|
|||||||
allowThirdParty = false
|
allowThirdParty = false
|
||||||
): Promise<{ white: string[], black: string[] }> {
|
): Promise<{ white: string[], black: string[] }> {
|
||||||
const [white, black, warningMessages] = await parentSpan.traceChild(`process filter rules: ${filterRulesUrl}`).traceAsyncFn(async (span) => {
|
const [white, black, warningMessages] = await parentSpan.traceChild(`process filter rules: ${filterRulesUrl}`).traceAsyncFn(async (span) => {
|
||||||
const text = await span.traceChildAsync('download', () => fetchAssetsWithout304(filterRulesUrl, fallbackUrls));
|
const text = await span.traceChildAsync('download', () => fetchAssets(filterRulesUrl, fallbackUrls));
|
||||||
|
|
||||||
const whitelistDomainSets = new Set<string>();
|
const whitelistDomainSets = new Set<string>();
|
||||||
const blacklistDomainSets = new Set<string>();
|
const blacklistDomainSets = new Set<string>();
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import type { Span } from '../../trace';
|
import type { Span } from '../../trace';
|
||||||
import { fetchAssetsWithout304 } from '../fetch-assets';
|
import { fetchAssets } from '../fetch-assets';
|
||||||
import { fastNormalizeDomain } from '../normalize-domain';
|
import { fastNormalizeDomain } from '../normalize-domain';
|
||||||
import { processLine } from '../process-line';
|
import { processLine } from '../process-line';
|
||||||
import { onBlackFound } from './shared';
|
import { onBlackFound } from './shared';
|
||||||
@ -29,7 +29,7 @@ export function processHosts(
|
|||||||
hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false
|
hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false
|
||||||
) {
|
) {
|
||||||
return span.traceChildAsync(`process hosts: ${hostsUrl}`, async (span) => {
|
return span.traceChildAsync(`process hosts: ${hostsUrl}`, async (span) => {
|
||||||
const text = await span.traceChild('download').traceAsyncFn(() => fetchAssetsWithout304(hostsUrl, mirrors));
|
const text = await span.traceChild('download').traceAsyncFn(() => fetchAssets(hostsUrl, mirrors));
|
||||||
|
|
||||||
const domainSets: string[] = [];
|
const domainSets: string[] = [];
|
||||||
|
|
||||||
|
|||||||
@ -31,7 +31,7 @@
|
|||||||
"fast-cidr-tools": "^0.3.1",
|
"fast-cidr-tools": "^0.3.1",
|
||||||
"fast-fifo": "^1.3.2",
|
"fast-fifo": "^1.3.2",
|
||||||
"fdir": "^6.4.2",
|
"fdir": "^6.4.2",
|
||||||
"foxts": "^1.1.5",
|
"foxts": "^1.1.6",
|
||||||
"hash-wasm": "^4.12.0",
|
"hash-wasm": "^4.12.0",
|
||||||
"json-stringify-pretty-compact": "^3.0.0",
|
"json-stringify-pretty-compact": "^3.0.0",
|
||||||
"picocolors": "^1.1.1",
|
"picocolors": "^1.1.1",
|
||||||
|
|||||||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
@ -53,8 +53,8 @@ importers:
|
|||||||
specifier: ^6.4.2
|
specifier: ^6.4.2
|
||||||
version: 6.4.2(picomatch@4.0.2)
|
version: 6.4.2(picomatch@4.0.2)
|
||||||
foxts:
|
foxts:
|
||||||
specifier: ^1.1.5
|
specifier: ^1.1.6
|
||||||
version: 1.1.5
|
version: 1.1.6
|
||||||
hash-wasm:
|
hash-wasm:
|
||||||
specifier: ^4.12.0
|
specifier: ^4.12.0
|
||||||
version: 4.12.0
|
version: 4.12.0
|
||||||
@ -1134,8 +1134,8 @@ packages:
|
|||||||
resolution: {integrity: sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==}
|
resolution: {integrity: sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==}
|
||||||
engines: {node: '>= 6'}
|
engines: {node: '>= 6'}
|
||||||
|
|
||||||
foxts@1.1.5:
|
foxts@1.1.6:
|
||||||
resolution: {integrity: sha512-s5SRvB7PTTOl5ZWYE4PrBhHXi6sw/0fLrYDDZThFHcHJCmEMaWmZwM0A5Op9APs6y0nQGrUgroV6WFKNdTbgSQ==}
|
resolution: {integrity: sha512-O2UR/MDLo0w4igcFHwLn2KyXUD84P6bE3U4OpVsxvcYrWLFvvDO8zKLBS/o++tFJTCq7p/3USR48E8/dF2vAAQ==}
|
||||||
|
|
||||||
fs-constants@1.0.0:
|
fs-constants@1.0.0:
|
||||||
resolution: {integrity: sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==}
|
resolution: {integrity: sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==}
|
||||||
@ -2932,7 +2932,7 @@ snapshots:
|
|||||||
combined-stream: 1.0.8
|
combined-stream: 1.0.8
|
||||||
mime-types: 2.1.35
|
mime-types: 2.1.35
|
||||||
|
|
||||||
foxts@1.1.5: {}
|
foxts@1.1.6: {}
|
||||||
|
|
||||||
fs-constants@1.0.0: {}
|
fs-constants@1.0.0: {}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user