mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
// @ts-check
|
|
import { createReadlineInterfaceFromResponse } from './lib/fetch-text-by-line';
|
|
import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
|
|
import { processLine } from './lib/process-line';
|
|
import { task } from './trace';
|
|
import { SHARED_DESCRIPTION } from './lib/constants';
|
|
import { createMemoizedPromise } from './lib/memo-promise';
|
|
import { RulesetOutput } from './lib/create-file';
|
|
import { $fetch } from './lib/make-fetch-happen';
|
|
|
|
export const getTelegramCIDRPromise = createMemoizedPromise(async () => {
|
|
const resp = await $fetch('https://core.telegram.org/resources/cidr.txt');
|
|
const lastModified = resp.headers.get('last-modified');
|
|
const date = lastModified ? new Date(lastModified) : new Date();
|
|
|
|
const ipcidr: string[] = [];
|
|
const ipcidr6: string[] = [];
|
|
|
|
for await (const line of createReadlineInterfaceFromResponse(resp)) {
|
|
const cidr = processLine(line);
|
|
if (!cidr) continue;
|
|
|
|
const [subnet] = cidr.split('/');
|
|
if (isProbablyIpv4(subnet)) {
|
|
ipcidr.push(cidr);
|
|
}
|
|
if (isProbablyIpv6(subnet)) {
|
|
ipcidr6.push(cidr);
|
|
}
|
|
}
|
|
|
|
return { date, ipcidr, ipcidr6 };
|
|
});
|
|
|
|
export const buildTelegramCIDR = task(require.main === module, __filename)(async (span) => {
|
|
const { date, ipcidr, ipcidr6 } = await span.traceChildAsync('get telegram cidr', getTelegramCIDRPromise);
|
|
|
|
if (ipcidr.length + ipcidr6.length === 0) {
|
|
throw new Error('Failed to fetch data!');
|
|
}
|
|
|
|
const description = [
|
|
...SHARED_DESCRIPTION,
|
|
'Data from:',
|
|
' - https://core.telegram.org/resources/cidr.txt'
|
|
];
|
|
|
|
return new RulesetOutput(span, 'telegram', 'ip')
|
|
.withTitle('Sukka\'s Ruleset - Telegram IP CIDR')
|
|
.withDescription(description)
|
|
.withDate(date)
|
|
.bulkAddCIDR4NoResolve(ipcidr)
|
|
.bulkAddCIDR6NoResolve(ipcidr6)
|
|
.write();
|
|
});
|