Provide fallback IP addrs for core services
This commit is contained in:
parent
51069dd3e0
commit
ef830e0432
1 changed files with 69 additions and 39 deletions
|
@ -8,11 +8,40 @@ import type {
|
||||||
lookup as nodeLookup,
|
lookup as nodeLookup,
|
||||||
} from 'dns';
|
} from 'dns';
|
||||||
import { ipcRenderer, net } from 'electron';
|
import { ipcRenderer, net } from 'electron';
|
||||||
import type { ResolvedHost } from 'electron';
|
import type { ResolvedHost, ResolvedEndpoint } from 'electron';
|
||||||
|
|
||||||
import { strictAssert } from './assert';
|
import { strictAssert } from './assert';
|
||||||
import { drop } from './drop';
|
import { drop } from './drop';
|
||||||
|
|
||||||
|
const FALLBACK_ADDRS: ReadonlyMap<
|
||||||
|
string,
|
||||||
|
ReadonlyArray<ResolvedEndpoint>
|
||||||
|
> = new Map([
|
||||||
|
[
|
||||||
|
'cdsi.signal.org',
|
||||||
|
[
|
||||||
|
{ family: 'ipv4', address: '40.122.45.194' },
|
||||||
|
{ family: 'ipv6', address: '2603:1030:7::1' },
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'chat.signal.org',
|
||||||
|
[
|
||||||
|
{ family: 'ipv4', address: '13.248.212.111' },
|
||||||
|
{ family: 'ipv4', address: '76.223.92.165' },
|
||||||
|
{ family: 'ipv6', address: '2600:9000:a507:ab6d:4ce3:2f58:25d7:9cbf' },
|
||||||
|
{ family: 'ipv6', address: '2600:9000:a61f:527c:d5eb:a431:5239:3232' },
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'sfu.voip.signal.org',
|
||||||
|
[
|
||||||
|
{ family: 'ipv4', address: '34.49.5.136' },
|
||||||
|
{ family: 'ipv6', address: '2600:1901:0:9c39::' },
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
function lookupAll(
|
function lookupAll(
|
||||||
hostname: string,
|
hostname: string,
|
||||||
opts: LookupOneOptions | LookupAllOptions,
|
opts: LookupOneOptions | LookupAllOptions,
|
||||||
|
@ -27,9 +56,8 @@ function lookupAll(
|
||||||
strictAssert(typeof callback === 'function', 'missing callback');
|
strictAssert(typeof callback === 'function', 'missing callback');
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
let result: ResolvedHost;
|
let result: Pick<ResolvedHost, 'endpoints'>;
|
||||||
|
|
||||||
try {
|
|
||||||
let queryType: 'A' | 'AAAA' | undefined;
|
let queryType: 'A' | 'AAAA' | undefined;
|
||||||
if (opts.family === 4) {
|
if (opts.family === 4) {
|
||||||
queryType = 'A';
|
queryType = 'A';
|
||||||
|
@ -37,6 +65,7 @@ function lookupAll(
|
||||||
queryType = 'AAAA';
|
queryType = 'AAAA';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
if (net) {
|
if (net) {
|
||||||
// Main process
|
// Main process
|
||||||
result = await net.resolveHost(hostname, {
|
result = await net.resolveHost(hostname, {
|
||||||
|
@ -50,6 +79,16 @@ function lookupAll(
|
||||||
queryType
|
queryType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const fallback = FALLBACK_ADDRS.get(hostname);
|
||||||
|
if (fallback) {
|
||||||
|
result = { endpoints: fallback.slice() };
|
||||||
|
} else {
|
||||||
|
callback(error, []);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const addresses = result.endpoints.map(({ address, family }) => {
|
const addresses = result.endpoints.map(({ address, family }) => {
|
||||||
let numericFamily = -1;
|
let numericFamily = -1;
|
||||||
if (family === 'ipv4') {
|
if (family === 'ipv4') {
|
||||||
|
@ -64,15 +103,9 @@ function lookupAll(
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!opts.all) {
|
if (!opts.all) {
|
||||||
const random = addresses.at(
|
const random = addresses.at(Math.floor(Math.random() * addresses.length));
|
||||||
Math.floor(Math.random() * addresses.length)
|
|
||||||
);
|
|
||||||
if (random === undefined) {
|
if (random === undefined) {
|
||||||
callback(
|
callback(new Error(`Hostname: ${hostname} cannot be resolved`), '', -1);
|
||||||
new Error(`Hostname: ${hostname} cannot be resolved`),
|
|
||||||
'',
|
|
||||||
-1
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
callback(null, random.address, random.family);
|
callback(null, random.address, random.family);
|
||||||
|
@ -80,9 +113,6 @@ function lookupAll(
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, addresses);
|
callback(null, addresses);
|
||||||
} catch (error) {
|
|
||||||
callback(error, []);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
drop(run());
|
drop(run());
|
||||||
|
|
Loading…
Reference in a new issue