Skip to content

Commit

Permalink
feat: 支持解析 ipv6 地址
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed May 5, 2020
1 parent aa79c93 commit b96489b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
25 changes: 20 additions & 5 deletions lib/utils/dns.ts
Original file line number Diff line number Diff line change
@@ -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 './';
Expand All @@ -23,12 +23,27 @@ export const resolveDomain = async (domain: string): Promise<ReadonlyArray<strin
}

logger.debug(`try to resolve domain ${domain}`);
const records = await resolver.resolve4(domain, { ttl: true });
const records = await resolve4And6(domain);
logger.debug(`resolved domain ${domain}: ${JSON.stringify(records)}`);

const address = records.map(item => 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<ReadonlyArray<RecordWithTtl>> => {
function onErr(): ReadonlyArray<never> {
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];
};
8 changes: 7 additions & 1 deletion test/utils/dns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
});

0 comments on commit b96489b

Please sign in to comment.