Refactor: use UDP DNS to get Telegram backup IP [skip ci]

This commit is contained in:
SukkaW
2026-01-01 23:41:57 +08:00
parent 320c8c9fcc
commit 9b764ffdb5
3 changed files with 33 additions and 47 deletions

View File

@@ -131,10 +131,13 @@ const pool = new Worktank({
const picocolors = __require('picocolors') as typeof import('picocolors'); const picocolors = __require('picocolors') as typeof import('picocolors');
const { fetch } = __require('./fetch-retry') as typeof import('./fetch-retry'); const { fetch } = __require('./fetch-retry') as typeof import('./fetch-retry');
const DNS2 = __require('dns2') as typeof import('dns2');
const dns = __require('node:dns/promises') as typeof import('node:dns/promises');
const { createReadlineInterfaceFromResponse } = __require('./fetch-text-by-line') as typeof import('./fetch-text-by-line'); const { createReadlineInterfaceFromResponse } = __require('./fetch-text-by-line') as typeof import('./fetch-text-by-line');
const { getTelegramBackupIPFromBase64 } = __require('./get-telegram-backup-ip') as typeof import('./get-telegram-backup-ip'); const { getTelegramBackupIPFromBase64 } = __require('./get-telegram-backup-ip') as typeof import('./get-telegram-backup-ip');
const { fastIpVersion } = __require('foxts/fast-ip-version') as typeof import('foxts/fast-ip-version'); const { fastIpVersion } = __require('foxts/fast-ip-version') as typeof import('foxts/fast-ip-version');
const { fastStringArrayJoin } = __require('foxts/fast-string-array-join') as typeof import('foxts/fast-string-array-join');
const resp = await fetch('https://core.telegram.org/resources/cidr.txt'); const resp = await fetch('https://core.telegram.org/resources/cidr.txt');
const lastModified = resp.headers.get('last-modified'); const lastModified = resp.headers.get('last-modified');
@@ -159,33 +162,37 @@ const pool = new Worktank({
// https://github.com/tdlib/td/blob/master/td/telegram/ConfigManager.cpp // https://github.com/tdlib/td/blob/master/td/telegram/ConfigManager.cpp
// Backup IP Source 1 (DoH) const resolvers = ['8.8.8.8', '1.0.0.1'].map((ip) => {
await Promise.all([ const resolver = new dns.Resolver();
DNS2.DOHClient({ dns: 'https://8.8.4.4/dns-query?dns={query}' }), resolver.setServers([ip]);
DNS2.DOHClient({ dns: 'https://1.0.0.1/dns-query?dns={query}' }) return Object.assign(resolver, { server: ip });
].flatMap( });
(client) => [
'apv3.stel.com', // prod
'tapv3.stel.com' // test
].map(async (domain) => {
try {
// tapv3.stel.com was for testing server
const resp = await client(domain, 'TXT');
const strings = resp.answers.map(i => i.data);
const str = strings[0]!.length > strings[1]!.length // Backup IP Source 1 (DNS)
? strings[0]! + strings[1]! await Promise.all(resolvers.flatMap((resolver) => [
: strings[1]! + strings[0]!; 'apv3.stel.com', // prod
'tapv3.stel.com' // test
const ips = getTelegramBackupIPFromBase64(str); ].map(async (domain) => {
ips.forEach(i => backupIPs.add(i.ip)); try {
// tapv3.stel.com was for testing server
console.log('[telegram backup ip]', picocolors.green('DoH TXT'), { domain, ips }); const resp = await resolver.resolveTxt(domain);
} catch (e) { const strings = resp.map(r => fastStringArrayJoin(r, '')); // flatten
console.error('[telegram backup ip]', picocolors.red('DoH TXT error'), { domain }, e); if (strings.length !== 2) {
throw new TypeError(`Unexpected TXT record count: ${strings.length}`);
} }
})
)); const str = strings[0].length > strings[1].length
? strings[0] + strings[1]
: strings[1] + strings[0];
const ips = getTelegramBackupIPFromBase64(str);
ips.forEach(i => backupIPs.add(i.ip));
console.log('[telegram backup ip]', picocolors.green('DNS TXT'), { domain, ips, server: resolver.server });
} catch (e) {
console.error('[telegram backup ip]', picocolors.red('DNS TXT error'), { domain }, e);
}
})));
// Backup IP Source 2: Firebase Realtime Database (test server not supported) // Backup IP Source 2: Firebase Realtime Database (test server not supported)
try { try {

View File

@@ -26,7 +26,6 @@
"ci-info": "^4.3.1", "ci-info": "^4.3.1",
"cli-progress": "^3.12.0", "cli-progress": "^3.12.0",
"csv-parse": "^6.1.0", "csv-parse": "^6.1.0",
"dns2": "github:lsongdev/node-dns#e4fa035aca0b8eb730bde3431fbf0c60a31a09c9",
"domain-alive": "^0.1.14", "domain-alive": "^0.1.14",
"fast-cidr-tools": "^0.3.4", "fast-cidr-tools": "^0.3.4",
"fast-escape-regexp": "^1.0.1", "fast-escape-regexp": "^1.0.1",
@@ -56,7 +55,6 @@
"@swc/core": "1.13.5", "@swc/core": "1.13.5",
"@types/better-sqlite3": "^7.6.13", "@types/better-sqlite3": "^7.6.13",
"@types/cli-progress": "^3.11.6", "@types/cli-progress": "^3.11.6",
"@types/dns2": "^2.0.10",
"@types/mocha": "^10.0.10", "@types/mocha": "^10.0.10",
"@types/node": "^24.10.4", "@types/node": "^24.10.4",
"@types/tar-fs": "^2.0.4", "@types/tar-fs": "^2.0.4",

19
pnpm-lock.yaml generated
View File

@@ -35,9 +35,6 @@ importers:
csv-parse: csv-parse:
specifier: ^6.1.0 specifier: ^6.1.0
version: 6.1.0 version: 6.1.0
dns2:
specifier: github:lsongdev/node-dns#e4fa035aca0b8eb730bde3431fbf0c60a31a09c9
version: https://codeload.github.com/lsongdev/node-dns/tar.gz/e4fa035aca0b8eb730bde3431fbf0c60a31a09c9
domain-alive: domain-alive:
specifier: ^0.1.14 specifier: ^0.1.14
version: 0.1.14 version: 0.1.14
@@ -120,9 +117,6 @@ importers:
'@types/cli-progress': '@types/cli-progress':
specifier: ^3.11.6 specifier: ^3.11.6
version: 3.11.6 version: 3.11.6
'@types/dns2':
specifier: ^2.0.10
version: 2.0.10
'@types/mocha': '@types/mocha':
specifier: ^10.0.10 specifier: ^10.0.10
version: 10.0.10 version: 10.0.10
@@ -667,9 +661,6 @@ packages:
'@types/cli-progress@3.11.6': '@types/cli-progress@3.11.6':
resolution: {integrity: sha512-cE3+jb9WRlu+uOSAugewNpITJDt1VF8dHOopPO4IABFc3SXYL5WE/+PTz/FCdZRRfIujiWW3n3aMbv1eIGVRWA==} resolution: {integrity: sha512-cE3+jb9WRlu+uOSAugewNpITJDt1VF8dHOopPO4IABFc3SXYL5WE/+PTz/FCdZRRfIujiWW3n3aMbv1eIGVRWA==}
'@types/dns2@2.0.10':
resolution: {integrity: sha512-3ezTiJfY6/VlXnvEOVSSbWpFbxtS3zXi0f2mccV6tI7wT+BIvTgLZhEUVHpiE1ycLKMqJyXndmkKw42nX49MQA==}
'@types/estree@1.0.8': '@types/estree@1.0.8':
resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==} resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==}
@@ -1117,10 +1108,6 @@ packages:
resolution: {integrity: sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==} resolution: {integrity: sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==}
engines: {node: '>=0.3.1'} engines: {node: '>=0.3.1'}
dns2@https://codeload.github.com/lsongdev/node-dns/tar.gz/e4fa035aca0b8eb730bde3431fbf0c60a31a09c9:
resolution: {tarball: https://codeload.github.com/lsongdev/node-dns/tar.gz/e4fa035aca0b8eb730bde3431fbf0c60a31a09c9}
version: 2.1.0
dom-serializer@1.4.1: dom-serializer@1.4.1:
resolution: {integrity: sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==} resolution: {integrity: sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==}
@@ -2653,10 +2640,6 @@ snapshots:
dependencies: dependencies:
'@types/node': 24.10.4 '@types/node': 24.10.4
'@types/dns2@2.0.10':
dependencies:
'@types/node': 24.10.4
'@types/estree@1.0.8': {} '@types/estree@1.0.8': {}
'@types/istanbul-lib-coverage@2.0.6': {} '@types/istanbul-lib-coverage@2.0.6': {}
@@ -3062,8 +3045,6 @@ snapshots:
diff@7.0.0: {} diff@7.0.0: {}
dns2@https://codeload.github.com/lsongdev/node-dns/tar.gz/e4fa035aca0b8eb730bde3431fbf0c60a31a09c9: {}
dom-serializer@1.4.1: dom-serializer@1.4.1:
dependencies: dependencies:
domelementtype: 2.3.0 domelementtype: 2.3.0