mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-13 01:30:37 +08:00
Perf/Refactor: faster ip version
This commit is contained in:
parent
07419a7942
commit
e19d7989c3
@ -147,7 +147,7 @@ async function transformRuleset(parentSpan: Span, sourcePath: string, relativePa
|
|||||||
if (res === $skip) return;
|
if (res === $skip) return;
|
||||||
|
|
||||||
const id = basename;
|
const id = basename;
|
||||||
const type = relativePath.slice(0, -extname.length).split(path.sep)[0];
|
const type = relativePath.split(path.sep)[0];
|
||||||
|
|
||||||
if (type !== 'ip' && type !== 'non_ip') {
|
if (type !== 'ip' && type !== 'non_ip') {
|
||||||
throw new TypeError(`Invalid type: ${type}`);
|
throw new TypeError(`Invalid type: ${type}`);
|
||||||
|
|||||||
@ -3,11 +3,11 @@ import path from 'node:path';
|
|||||||
import { createReadlineInterfaceFromResponse, readFileIntoProcessedArray } from './lib/fetch-text-by-line';
|
import { createReadlineInterfaceFromResponse, readFileIntoProcessedArray } from './lib/fetch-text-by-line';
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { SHARED_DESCRIPTION } from './constants/description';
|
import { SHARED_DESCRIPTION } from './constants/description';
|
||||||
import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
|
|
||||||
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 { fetchAssets } from './lib/fetch-assets';
|
import { fetchAssets } from './lib/fetch-assets';
|
||||||
|
import { fastIpVersion } from './lib/misc';
|
||||||
|
|
||||||
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) => {
|
||||||
@ -17,9 +17,10 @@ const getBogusNxDomainIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = $$
|
|||||||
for await (const line of createReadlineInterfaceFromResponse(resp, true)) {
|
for await (const line of createReadlineInterfaceFromResponse(resp, true)) {
|
||||||
if (line.startsWith('bogus-nxdomain=')) {
|
if (line.startsWith('bogus-nxdomain=')) {
|
||||||
const ip = line.slice(15).trim();
|
const ip = line.slice(15).trim();
|
||||||
if (isProbablyIpv4(ip)) {
|
const v = fastIpVersion(ip);
|
||||||
|
if (v === 4) {
|
||||||
ipv4.push(ip);
|
ipv4.push(ip);
|
||||||
} else if (isProbablyIpv6(ip)) {
|
} else if (v === 6) {
|
||||||
ipv6.push(ip);
|
ipv6.push(ip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,9 +38,10 @@ const BOTNET_FILTER_MIRROR_URL = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const getBotNetFilterIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = fetchAssets(BOTNET_FILTER_URL, BOTNET_FILTER_MIRROR_URL, true).then(arr => arr.reduce<[ipv4: string[], ipv6: string[]]>((acc, ip) => {
|
const getBotNetFilterIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = fetchAssets(BOTNET_FILTER_URL, BOTNET_FILTER_MIRROR_URL, true).then(arr => arr.reduce<[ipv4: string[], ipv6: string[]]>((acc, ip) => {
|
||||||
if (isProbablyIpv4(ip)) {
|
const v = fastIpVersion(ip);
|
||||||
|
if (v === 4) {
|
||||||
acc[0].push(ip);
|
acc[0].push(ip);
|
||||||
} else if (isProbablyIpv6(ip)) {
|
} else if (v === 6) {
|
||||||
acc[1].push(ip);
|
acc[1].push(ip);
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import { createReadlineInterfaceFromResponse } from './lib/fetch-text-by-line';
|
import { createReadlineInterfaceFromResponse } from './lib/fetch-text-by-line';
|
||||||
import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
|
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { SHARED_DESCRIPTION } from './constants/description';
|
import { SHARED_DESCRIPTION } from './constants/description';
|
||||||
import { createMemoizedPromise } from './lib/memo-promise';
|
import { createMemoizedPromise } from './lib/memo-promise';
|
||||||
import { RulesetOutput } from './lib/create-file';
|
import { RulesetOutput } from './lib/create-file';
|
||||||
import { $$fetch } from './lib/fetch-retry';
|
import { $$fetch } from './lib/fetch-retry';
|
||||||
|
import { fastIpVersion } from './lib/misc';
|
||||||
|
|
||||||
export const getTelegramCIDRPromise = createMemoizedPromise(async () => {
|
export const getTelegramCIDRPromise = createMemoizedPromise(async () => {
|
||||||
const resp = await $$fetch('https://core.telegram.org/resources/cidr.txt');
|
const resp = await $$fetch('https://core.telegram.org/resources/cidr.txt');
|
||||||
@ -20,11 +20,10 @@ export const getTelegramCIDRPromise = createMemoizedPromise(async () => {
|
|||||||
const ipcidr6: string[] = [];
|
const ipcidr6: string[] = [];
|
||||||
|
|
||||||
for await (const cidr of createReadlineInterfaceFromResponse(resp, true)) {
|
for await (const cidr of createReadlineInterfaceFromResponse(resp, true)) {
|
||||||
const [subnet] = cidr.split('/');
|
const v = fastIpVersion(cidr);
|
||||||
if (isProbablyIpv4(subnet)) {
|
if (v === 4) {
|
||||||
ipcidr.push(cidr);
|
ipcidr.push(cidr);
|
||||||
}
|
} else if (v === 6) {
|
||||||
if (isProbablyIpv6(subnet)) {
|
|
||||||
ipcidr6.push(cidr);
|
ipcidr6.push(cidr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -199,7 +199,7 @@ export const deserializeSet = (str: string) => new Set(str.split(separator));
|
|||||||
export const serializeArray = (arr: string[]) => fastStringArrayJoin(arr, separator);
|
export const serializeArray = (arr: string[]) => fastStringArrayJoin(arr, separator);
|
||||||
export const deserializeArray = (str: string) => str.split(separator);
|
export const deserializeArray = (str: string) => str.split(separator);
|
||||||
|
|
||||||
export const getFileContentHash = (filename: string) => simpleStringHash(fs.readFileSync(filename, 'utf-8'));
|
const getFileContentHash = (filename: string) => simpleStringHash(fs.readFileSync(filename, 'utf-8'));
|
||||||
export function createCacheKey(filename: string) {
|
export function createCacheKey(filename: string) {
|
||||||
const fileHash = getFileContentHash(filename);
|
const fileHash = getFileContentHash(filename);
|
||||||
return (key: string) => key + '$' + fileHash + '$';
|
return (key: string) => key + '$' + fileHash + '$';
|
||||||
|
|||||||
@ -69,3 +69,7 @@ export function isDirectoryEmptySync(path: PathLike) {
|
|||||||
directoryHandle.closeSync();
|
directoryHandle.closeSync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function fastIpVersion(ip: string) {
|
||||||
|
return ip.includes(':') ? 6 : (ip.includes('.') ? 4 : 0);
|
||||||
|
}
|
||||||
|
|||||||
@ -7,7 +7,8 @@ import type { SingboxSourceFormat } from '../singbox';
|
|||||||
import { RuleOutput } from './base';
|
import { RuleOutput } from './base';
|
||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import { normalizeDomain } from '../normalize-domain';
|
import { normalizeDomain } from '../normalize-domain';
|
||||||
import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
|
import { isProbablyIpv4 } from 'foxts/is-probably-ip';
|
||||||
|
import { fastIpVersion } from '../misc';
|
||||||
|
|
||||||
type Preprocessed = [domain: string[], domainSuffix: string[], sortedDomainRules: string[]];
|
type Preprocessed = [domain: string[], domainSuffix: string[], sortedDomainRules: string[]];
|
||||||
|
|
||||||
@ -93,10 +94,11 @@ export class RulesetOutput extends RuleOutput<Preprocessed> {
|
|||||||
if (value.includes('/')) {
|
if (value.includes('/')) {
|
||||||
return `SRC-IP-CIDR,${value}`;
|
return `SRC-IP-CIDR,${value}`;
|
||||||
}
|
}
|
||||||
if (isProbablyIpv4(value)) {
|
const v = fastIpVersion(value);
|
||||||
|
if (v === 4) {
|
||||||
return `SRC-IP-CIDR,${value}/32`;
|
return `SRC-IP-CIDR,${value}/32`;
|
||||||
}
|
}
|
||||||
if (isProbablyIpv6(value)) {
|
if (v === 6) {
|
||||||
return `SRC-IP-CIDR6,${value}/128`;
|
return `SRC-IP-CIDR6,${value}/128`;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
@ -148,10 +150,14 @@ export class RulesetOutput extends RuleOutput<Preprocessed> {
|
|||||||
source_ip_cidr: [...this.sourceIpOrCidr].reduce<string[]>((acc, cur) => {
|
source_ip_cidr: [...this.sourceIpOrCidr].reduce<string[]>((acc, cur) => {
|
||||||
if (cur.includes('/')) {
|
if (cur.includes('/')) {
|
||||||
acc.push(cur);
|
acc.push(cur);
|
||||||
} else if (isProbablyIpv4(cur)) {
|
} else {
|
||||||
acc.push(cur + '/32');
|
const v = fastIpVersion(cur);
|
||||||
} else if (isProbablyIpv6(cur)) {
|
|
||||||
acc.push(cur + '/128');
|
if (v === 4) {
|
||||||
|
acc.push(cur + '/32');
|
||||||
|
} else if (v === 6) {
|
||||||
|
acc.push(cur + '/128');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
@ -245,11 +251,13 @@ export class RulesetOutput extends RuleOutput<Preprocessed> {
|
|||||||
|
|
||||||
for (const i of urlRegexResults) {
|
for (const i of urlRegexResults) {
|
||||||
for (const processed of i.processed) {
|
for (const processed of i.processed) {
|
||||||
if (normalizeDomain(
|
if (
|
||||||
processed
|
normalizeDomain(
|
||||||
.replaceAll('*', 'a')
|
processed
|
||||||
.replaceAll('?', 'b')
|
.replaceAll('*', 'a')
|
||||||
)) {
|
.replaceAll('?', 'b')
|
||||||
|
)
|
||||||
|
) {
|
||||||
parsed.push([i.origin, processed]);
|
parsed.push([i.origin, processed]);
|
||||||
} else if (!isProbablyIpv4(processed)) {
|
} else if (!isProbablyIpv4(processed)) {
|
||||||
parsedFailures.push([i.origin, processed]);
|
parsedFailures.push([i.origin, processed]);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user