New build infra

This commit is contained in:
SukkaW
2022-12-08 18:10:59 +08:00
parent 6646a1ec98
commit ffb4e24e8a
14 changed files with 353 additions and 78 deletions

View File

@@ -2,12 +2,16 @@ const { fetchWithRetry } = require('./lib/fetch-retry');
const fs = require('fs');
const path = require('path');
const { isIPv4, isIPv6 } = require('net');
const { compareAndWriteFile } = require('./lib/string-array-compare');
const { withBannerArray } = require('./lib/with-banner');
(async () => {
console.time('Total Time - build-telegram-cidr');
/** @type {Response} */
const resp = await fetchWithRetry('https://core.telegram.org/resources/cidr.txt');
const lastModified = new Date(resp.headers.get('last-modified'));
const lastModified = resp.headers.get('last-modified');
const date = lastModified ? new Date(lastModified) : new Date();
const res = (await resp.text())
.split('\n')
@@ -17,23 +21,31 @@ const { isIPv4, isIPv6 } = require('net');
throw new Error('Failed to fetch data!');
}
await fs.promises.writeFile(
path.resolve(__dirname, '../List/ip/telegram.conf'),
'# Telegram CIDR (https://core.telegram.org/resources/cidr.txt)' + '\n' +
'# Last Updated: ' + lastModified.toISOString() + '\n' +
res.map(ip => {
const [subnet] = ip.split('/');
console.log(' - ' + ip + ': ' + subnet);
if (isIPv4(subnet)) {
return `IP-CIDR,${ip},no-resolve`;
}
if (isIPv6(subnet)) {
return `IP-CIDR6,${ip},no-resolve`;
}
return '';
}).join('\n') + '\n',
'utf-8'
);
await compareAndWriteFile(
withBannerArray(
'Sukka\'s Surge Rules - Telegram IP CIDR',
[
'License: AGPL 3.0',
'Homepage: https://ruleset.skk.moe',
'GitHub: https://github.com/SukkaW/Surge',
'Data from:',
' - https://core.telegram.org/resources/cidr.txt'
],
date,
res.map(ip => {
const [subnet] = ip.split('/');
console.log(' - ' + ip + ': ' + subnet);
if (isIPv4(subnet)) {
return `IP-CIDR,${ip},no-resolve`;
}
if (isIPv6(subnet)) {
return `IP-CIDR6,${ip},no-resolve`;
}
return '';
})
),
path.resolve(__dirname, '../List/ip/telegram.conf')
)
console.timeEnd('Total Time - build-telegram-cidr');
})();