From b96489b6f92daa9e87f3863fb80e97b4af225fa6 Mon Sep 17 00:00:00 2001 From: Roy Li Date: Tue, 5 May 2020 14:44:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=A7=A3=E6=9E=90=20?= =?UTF-8?q?ipv6=20=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/dns.ts | 25 ++++++++++++++++++++----- test/utils/dns.test.ts | 8 +++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/utils/dns.ts b/lib/utils/dns.ts index 75138d4dd..33a075c25 100644 --- a/lib/utils/dns.ts +++ b/lib/utils/dns.ts @@ -1,4 +1,4 @@ -import { promises as dns } from 'dns'; +import { promises as dns, RecordWithTtl } from 'dns'; import LRU from 'lru-cache'; import { createLogger } from '@surgio/logger'; import { isHeroku, isNow } from './'; @@ -23,12 +23,27 @@ export const resolveDomain = async (domain: string): Promise item.address); + if (records.length) { + const address = records.map(item => item.address); + DomainCache.set(domain, address, records[0].ttl * 1000); + return address; + } + + return []; +}; + +export const resolve4And6 = async (domain: string): Promise> => { + function onErr(): ReadonlyArray { + return []; + } - DomainCache.set(domain, address, records[0].ttl * 1000); + const [ipv4, ipv6] = await Promise.all([ + resolver.resolve4(domain, { ttl: true }).catch(onErr), + resolver.resolve6(domain, { ttl: true }).catch(onErr), + ]); - return address; + return [...ipv4, ...ipv6]; }; diff --git a/test/utils/dns.test.ts b/test/utils/dns.test.ts index 174d0d3f1..782f15a46 100644 --- a/test/utils/dns.test.ts +++ b/test/utils/dns.test.ts @@ -2,7 +2,13 @@ import test from 'ava'; import { isIp } from '../../lib/utils'; import * as dns from '../../lib/utils/dns'; -test('resolveDomain', async t => { +test('resolveDomain ipv4', async t => { const ips = await dns.resolveDomain('gstatic.com'); t.true(isIp(ips[0])); }); + +test('resolveDomain ipv6', async t => { + const ips = await dns.resolveDomain('ipv6.lookup.test-ipv6.com'); + t.true(isIp(ips[0])); +}); +