mirror of https://github.com/certd/certd
chore:
parent
eb8cd53de2
commit
ff10bc05ec
|
@ -1,15 +1,15 @@
|
||||||
import {LocalCache, logger} from '@certd/basic';
|
import {LocalCache, logger} from '@certd/basic';
|
||||||
import dnsSdk, {AnyRecord} from 'dns'
|
import dnsSdk, {AnyRecord} from 'dns'
|
||||||
import { LRUCache } from 'lru-cache';
|
|
||||||
import {LookupAddress} from "node:dns";
|
import {LookupAddress} from "node:dns";
|
||||||
|
|
||||||
const dns = dnsSdk.promises
|
const dns = dnsSdk.promises
|
||||||
|
|
||||||
export class DnsCustom{
|
export class DnsCustom{
|
||||||
private resolver: dnsSdk.promises.Resolver;
|
private resolver: dnsSdk.promises.Resolver;
|
||||||
private cache = new LRUCache<string, any>({
|
// private cache = new LRUCache<string, any>({
|
||||||
max: 1000,
|
// max: 1000,
|
||||||
ttl: 1000 * 60 * 5,
|
// ttl: 1000 * 60 * 5,
|
||||||
});
|
// });
|
||||||
|
|
||||||
constructor(dnsServers:string[]) {
|
constructor(dnsServers:string[]) {
|
||||||
const resolver = new dns.Resolver();
|
const resolver = new dns.Resolver();
|
||||||
|
@ -17,45 +17,21 @@ export class DnsCustom{
|
||||||
this.resolver = resolver;
|
this.resolver = resolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// async lookup(hostname:string,options?:{ family: any, hints: number, all: boolean }):Promise<LookupAddress[]>{
|
||||||
|
// const cacheKey = hostname + JSON.stringify(options)
|
||||||
|
// let res = this.cache.get(cacheKey)
|
||||||
|
// if (res){
|
||||||
|
// return res
|
||||||
|
// }
|
||||||
|
// res = await this.doLookup(hostname,options)
|
||||||
|
// this.cache.set(cacheKey,res)
|
||||||
|
// return res
|
||||||
|
// }
|
||||||
async lookup(hostname:string,options?:{ family: any, hints: number, all: boolean }):Promise<LookupAddress[]>{
|
async lookup(hostname:string,options?:{ family: any, hints: number, all: boolean }):Promise<LookupAddress[]>{
|
||||||
const cacheKey = hostname + JSON.stringify(options)
|
|
||||||
let res = this.cache.get(cacheKey)
|
|
||||||
if (res){
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
res = await this.doLookup(hostname,options)
|
|
||||||
this.cache.set(cacheKey,res)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
async doLookup(hostname:string,options?:{ family: any, hints: number, all: boolean }):Promise<LookupAddress[]>{
|
|
||||||
// { family: undefined, hints: 0, all: true }
|
// { family: undefined, hints: 0, all: true }
|
||||||
let cnameIps:LookupAddress[] = []
|
|
||||||
let v4:LookupAddress[] = []
|
let v4:LookupAddress[] = []
|
||||||
let v6:LookupAddress[] = []
|
let v6:LookupAddress[] = []
|
||||||
let errors = []
|
let errors = []
|
||||||
const resolveCname = async ()=>{
|
|
||||||
let cnames = []
|
|
||||||
try{
|
|
||||||
cnames = await this.resolver.resolveCname(hostname)
|
|
||||||
}catch (e) {
|
|
||||||
errors.push(e)
|
|
||||||
logger.warn("query cname error",e)
|
|
||||||
}
|
|
||||||
// deep
|
|
||||||
if (cnames && cnames.length > 0) {
|
|
||||||
for (let cname of cnames) {
|
|
||||||
try{
|
|
||||||
const cnameIp = await this.lookup(cname,options)
|
|
||||||
if (cnameIp && cnameIp.length > 0) {
|
|
||||||
cnameIps.push(...cnameIp)
|
|
||||||
}
|
|
||||||
}catch (e) {
|
|
||||||
errors.push(e)
|
|
||||||
logger.warn("lookup cname error",e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const queryV6 = async ()=>{
|
const queryV6 = async ()=>{
|
||||||
try{
|
try{
|
||||||
const list = await this.resolver.resolve6(hostname)
|
const list = await this.resolver.resolve6(hostname)
|
||||||
|
@ -89,7 +65,7 @@ export class DnsCustom{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const queries:Promise<any>[] = [resolveCname()]
|
const queries:Promise<any>[] = []
|
||||||
|
|
||||||
|
|
||||||
const {family, all} = options
|
const {family, all} = options
|
||||||
|
@ -105,7 +81,7 @@ export class DnsCustom{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await Promise.all(queries)
|
await Promise.all(queries)
|
||||||
const res = [...v4,...v6,...cnameIps]
|
const res = [...v4,...v6]
|
||||||
if(res.length === 0){
|
if(res.length === 0){
|
||||||
if (errors.length > 0){
|
if (errors.length > 0){
|
||||||
const e = new Error(errors[0])
|
const e = new Error(errors[0])
|
||||||
|
|
Loading…
Reference in New Issue