Surge_by_SukkaW/Build/build-internal-reverse-chn-cidr.ts
2024-04-09 18:35:53 +08:00

32 lines
937 B
TypeScript

import path from 'path';
import { task } from './trace';
import { exclude, merge } from 'fast-cidr-tools';
import { getChnCidrPromise } from './build-chn-cidr';
import { NON_CN_CIDR_INCLUDED_IN_CHNROUTE, RESERVED_IPV4_CIDR } from './constants/cidr';
export const buildInternalReverseChnCIDR = task(import.meta.path, async () => {
const cidrPromise = getChnCidrPromise();
const peeked = Bun.peek(cidrPromise);
const cidr: string[] = peeked === cidrPromise
? await cidrPromise
: (peeked as string[]);
const reversedCidr = merge(
exclude(
['0.0.0.0/0'],
RESERVED_IPV4_CIDR.concat(cidr),
true
).concat(
// https://github.com/misakaio/chnroutes2/issues/25
NON_CN_CIDR_INCLUDED_IN_CHNROUTE
)
);
return Bun.write(path.resolve(import.meta.dir, '../Internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
});
if (import.meta.main) {
buildInternalReverseChnCIDR();
}