mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
Re-enable Reverse CHN CIDR build
This commit is contained in:
parent
352ec2b515
commit
0622d5ea71
@ -5,22 +5,12 @@ import { processLineFromReadline } from './lib/process-line';
|
|||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
|
|
||||||
import { exclude } from 'fast-cidr-tools';
|
import { exclude } from 'fast-cidr-tools';
|
||||||
import picocolors from 'picocolors';
|
|
||||||
import { createMemoizedPromise } from './lib/memo-promise';
|
import { createMemoizedPromise } from './lib/memo-promise';
|
||||||
|
import { CN_CIDR_NOT_INCLUDED_IN_CHNROUTE, NON_CN_CIDR_INCLUDED_IN_CHNROUTE } from './constants/cidr';
|
||||||
// https://github.com/misakaio/chnroutes2/issues/25
|
|
||||||
const EXCLUDE_CIDRS = [
|
|
||||||
'223.118.0.0/15',
|
|
||||||
'223.120.0.0/15'
|
|
||||||
];
|
|
||||||
|
|
||||||
const INCLUDE_CIDRS = [
|
|
||||||
'211.99.96.0/19' // wy.com.cn
|
|
||||||
];
|
|
||||||
|
|
||||||
export const getChnCidrPromise = createMemoizedPromise(async () => {
|
export const getChnCidrPromise = createMemoizedPromise(async () => {
|
||||||
const cidr = await processLineFromReadline(await fetchRemoteTextByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt'));
|
const cidr = await processLineFromReadline(await fetchRemoteTextByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt'));
|
||||||
return exclude([...cidr, ...INCLUDE_CIDRS], EXCLUDE_CIDRS, true);
|
return exclude([...cidr, ...CN_CIDR_NOT_INCLUDED_IN_CHNROUTE], NON_CN_CIDR_INCLUDED_IN_CHNROUTE, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
export const buildChnCidr = task(import.meta.path, async (span) => {
|
export const buildChnCidr = task(import.meta.path, async (span) => {
|
||||||
|
|||||||
@ -1,49 +1,27 @@
|
|||||||
import { fetchRemoteTextByLine } from './lib/fetch-text-by-line';
|
|
||||||
import { processLineFromReadline } from './lib/process-line';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
|
|
||||||
import { exclude, merge } from 'fast-cidr-tools';
|
import { exclude, merge } from 'fast-cidr-tools';
|
||||||
import { getChnCidrPromise } from './build-chn-cidr';
|
import { getChnCidrPromise } from './build-chn-cidr';
|
||||||
|
import { NON_CN_CIDR_INCLUDED_IN_CHNROUTE, RESERVED_IPV4_CIDR } from './constants/cidr';
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Reserved_IP_addresses
|
export const buildInternalReverseChnCIDR = task(import.meta.path, async () => {
|
||||||
const RESERVED_IPV4_CIDR = [
|
|
||||||
'0.0.0.0/8',
|
|
||||||
'10.0.0.0/8',
|
|
||||||
'100.64.0.0/10',
|
|
||||||
'127.0.0.0/8',
|
|
||||||
'169.254.0.0/16',
|
|
||||||
'172.16.0.0/12',
|
|
||||||
'192.0.0.0/24',
|
|
||||||
'192.0.2.0/24',
|
|
||||||
// 192.88.99.0 // is currently being broadcast by HE and Comcast
|
|
||||||
'192.168.0.0/16',
|
|
||||||
'198.18.0.0/15',
|
|
||||||
'198.51.100.0/24',
|
|
||||||
'203.0.113.0/24',
|
|
||||||
'224.0.0.0/4',
|
|
||||||
'233.252.0.0/24',
|
|
||||||
'240.0.0.0/4'
|
|
||||||
];
|
|
||||||
|
|
||||||
export const buildInternalReverseChnCIDR = task(import.meta.path, async (span) => {
|
|
||||||
const cidrPromise = getChnCidrPromise();
|
const cidrPromise = getChnCidrPromise();
|
||||||
const peeked = Bun.peek(cidrPromise);
|
const peeked = Bun.peek(cidrPromise);
|
||||||
const cidr: string[] = peeked === cidrPromise
|
const cidr: string[] = peeked === cidrPromise
|
||||||
? await span.traceChildPromise('download chnroutes2', cidrPromise)
|
? await cidrPromise
|
||||||
: (peeked as string[]);
|
: (peeked as string[]);
|
||||||
|
|
||||||
const reversedCidr = span.traceChildSync('build reversed chn cidr', () => merge(
|
const reversedCidr = merge(
|
||||||
exclude(
|
exclude(
|
||||||
['0.0.0.0/0'],
|
['0.0.0.0/0'],
|
||||||
RESERVED_IPV4_CIDR.concat(cidr),
|
RESERVED_IPV4_CIDR.concat(cidr),
|
||||||
true
|
true
|
||||||
).concat([
|
).concat(
|
||||||
// https://github.com/misakaio/chnroutes2/issues/25
|
// https://github.com/misakaio/chnroutes2/issues/25
|
||||||
'223.118.0.0/15',
|
NON_CN_CIDR_INCLUDED_IN_CHNROUTE
|
||||||
'223.120.0.0/15'
|
)
|
||||||
])
|
);
|
||||||
));
|
|
||||||
|
|
||||||
return Bun.write(path.resolve(import.meta.dir, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
|
return Bun.write(path.resolve(import.meta.dir, '../List/internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
|
||||||
});
|
});
|
||||||
|
|||||||
29
Build/constants/cidr.ts
Normal file
29
Build/constants/cidr.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// https://en.wikipedia.org/wiki/Reserved_IP_addresses
|
||||||
|
export const RESERVED_IPV4_CIDR = [
|
||||||
|
'0.0.0.0/8',
|
||||||
|
'10.0.0.0/8',
|
||||||
|
'100.64.0.0/10',
|
||||||
|
'127.0.0.0/8',
|
||||||
|
'169.254.0.0/16',
|
||||||
|
'172.16.0.0/12',
|
||||||
|
'192.0.0.0/24',
|
||||||
|
'192.0.2.0/24',
|
||||||
|
// 192.88.99.0 // is currently being broadcast by HE and Comcast
|
||||||
|
'192.168.0.0/16',
|
||||||
|
'198.18.0.0/15',
|
||||||
|
'198.51.100.0/24',
|
||||||
|
'203.0.113.0/24',
|
||||||
|
'224.0.0.0/4',
|
||||||
|
'233.252.0.0/24',
|
||||||
|
'240.0.0.0/4'
|
||||||
|
];
|
||||||
|
|
||||||
|
// https://github.com/misakaio/chnroutes2/issues/25
|
||||||
|
export const NON_CN_CIDR_INCLUDED_IN_CHNROUTE = [
|
||||||
|
'223.118.0.0/15',
|
||||||
|
'223.120.0.0/15'
|
||||||
|
];
|
||||||
|
|
||||||
|
export const CN_CIDR_NOT_INCLUDED_IN_CHNROUTE = [
|
||||||
|
'211.99.96.0/19' // wy.com.cn
|
||||||
|
];
|
||||||
@ -9,6 +9,7 @@ import { buildChnCidr } from './build-chn-cidr';
|
|||||||
import { buildSpeedtestDomainSet } from './build-speedtest-domainset';
|
import { buildSpeedtestDomainSet } from './build-speedtest-domainset';
|
||||||
import { buildInternalCDNDomains } from './build-internal-cdn-rules';
|
import { buildInternalCDNDomains } from './build-internal-cdn-rules';
|
||||||
// import { buildInternalChnDomains } from './build-internal-chn-domains';
|
// import { buildInternalChnDomains } from './build-internal-chn-domains';
|
||||||
|
import { buildInternalReverseChnCIDR } from './build-internal-reverse-chn-cidr';
|
||||||
import { buildDomesticRuleset } from './build-domestic-ruleset';
|
import { buildDomesticRuleset } from './build-domestic-ruleset';
|
||||||
import { buildStreamService } from './build-stream-service';
|
import { buildStreamService } from './build-stream-service';
|
||||||
|
|
||||||
@ -33,7 +34,6 @@ import { createSpan, printTraceResult } from './trace';
|
|||||||
const rootSpan = createSpan('root');
|
const rootSpan = createSpan('root');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: restore this once Bun has fixed their worker
|
|
||||||
// const buildInternalReverseChnCIDRWorker = new Worker(new URL('./workers/build-internal-reverse-chn-cidr-worker.ts', import.meta.url));
|
// const buildInternalReverseChnCIDRWorker = new Worker(new URL('./workers/build-internal-reverse-chn-cidr-worker.ts', import.meta.url));
|
||||||
|
|
||||||
const downloadPreviousBuildPromise = downloadPreviousBuild(rootSpan);
|
const downloadPreviousBuildPromise = downloadPreviousBuild(rootSpan);
|
||||||
@ -51,17 +51,7 @@ import { createSpan, printTraceResult } from './trace';
|
|||||||
buildCdnConfPromise
|
buildCdnConfPromise
|
||||||
]).then(() => buildInternalCDNDomains(rootSpan));
|
]).then(() => buildInternalCDNDomains(rootSpan));
|
||||||
|
|
||||||
// const buildInternalReverseChnCIDRPromise = new Promise<TaskResult>(resolve => {
|
const buildInternalReverseChnCIDRPromise = buildInternalReverseChnCIDR(rootSpan);
|
||||||
// const handleMessage = (e: MessageEvent<TaskResult>) => {
|
|
||||||
// const { data } = e;
|
|
||||||
|
|
||||||
// buildInternalReverseChnCIDRWorker.postMessage('exit');
|
|
||||||
// buildInternalReverseChnCIDRWorker.removeEventListener('message', handleMessage);
|
|
||||||
// resolve(data);
|
|
||||||
// };
|
|
||||||
// buildInternalReverseChnCIDRWorker.addEventListener('message', handleMessage);
|
|
||||||
// buildInternalReverseChnCIDRWorker.postMessage('build');
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const buildInternalChnDomainsPromise = buildInternalChnDomains();
|
// const buildInternalChnDomainsPromise = buildInternalChnDomains();
|
||||||
const buildDomesticRulesetPromise = downloadPreviousBuildPromise.then(() => buildDomesticRuleset(rootSpan));
|
const buildDomesticRulesetPromise = downloadPreviousBuildPromise.then(() => buildDomesticRuleset(rootSpan));
|
||||||
@ -92,7 +82,8 @@ import { createSpan, printTraceResult } from './trace';
|
|||||||
buildChnCidrPromise,
|
buildChnCidrPromise,
|
||||||
buildSpeedtestDomainSetPromise,
|
buildSpeedtestDomainSetPromise,
|
||||||
buildInternalCDNDomainsPromise,
|
buildInternalCDNDomainsPromise,
|
||||||
// buildInternalReverseChnCIDRPromise,
|
buildInternalReverseChnCIDRPromise,
|
||||||
|
buildInternalReverseChnCIDRPromise,
|
||||||
// buildInternalChnDomainsPromise,
|
// buildInternalChnDomainsPromise,
|
||||||
buildDomesticRulesetPromise,
|
buildDomesticRulesetPromise,
|
||||||
buildRedirectModulePromise,
|
buildRedirectModulePromise,
|
||||||
|
|||||||
@ -114,7 +114,7 @@ export const task = <T>(importMetaPath: string, fn: (span: Span) => T, customnam
|
|||||||
const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath));
|
const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath));
|
||||||
return async (span?: Span) => {
|
return async (span?: Span) => {
|
||||||
if (span) {
|
if (span) {
|
||||||
return span.traceChild(taskName).traceAsyncFn(fn);
|
return span.traceChildAsync(taskName, fn);
|
||||||
}
|
}
|
||||||
return fn(createSpan(taskName));
|
return fn(createSpan(taskName));
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,19 +0,0 @@
|
|||||||
declare const self: Worker;
|
|
||||||
|
|
||||||
import { buildInternalReverseChnCIDR } from '../build-internal-reverse-chn-cidr';
|
|
||||||
|
|
||||||
// preload the task
|
|
||||||
const promise = buildInternalReverseChnCIDR();
|
|
||||||
|
|
||||||
const handleMessage = async (e: MessageEvent<'build' | 'exit'>) => {
|
|
||||||
if (e.data === 'build') {
|
|
||||||
const stat = await promise;
|
|
||||||
postMessage(stat);
|
|
||||||
} else /* if (e.data === 'exit') */ {
|
|
||||||
self.removeEventListener('message', handleMessage);
|
|
||||||
self.unref();
|
|
||||||
self.terminate();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
self.addEventListener('message', handleMessage);
|
|
||||||
Loading…
x
Reference in New Issue
Block a user