smartdns/ts/index.ts

164 lines
4.0 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './dnsly.plugins';
2016-11-15 20:39:21 +00:00
2019-01-06 23:28:15 +00:00
export type TDnsProvider = 'google' | 'cloudflare';
export type TDnsRecordType =
| 'A'
2017-07-18 13:45:06 +00:00
| 'AAAA'
| 'CNAME'
| 'PTR'
| 'MX'
| 'NAPTR'
| 'NS'
| 'SOA'
| 'SRV'
| 'TXT';
2016-11-15 20:39:21 +00:00
2017-01-26 23:11:13 +00:00
export interface IDnsRecord {
chunked?: string[];
name: string;
type: TDnsRecordType;
value: string;
2017-01-26 23:11:13 +00:00
}
2016-11-15 20:39:21 +00:00
/**
* class dnsly offers methods for working with dns from a dns provider like Google DNS
*/
export class Smartdns {
dnsServerIp: string;
dnsServerPort: number;
2017-07-18 13:45:06 +00:00
/**
* constructor for class dnsly
*/
2019-01-06 23:28:15 +00:00
constructor(dnsProviderArg: TDnsProvider = 'cloudflare') {
this._setDnsProvider(dnsProviderArg);
2017-07-18 13:45:06 +00:00
}
2016-11-15 20:39:21 +00:00
/**
* check a dns record until it has propagated to Google DNS
* should be considerably fast
2019-01-06 23:21:15 +00:00
* @param recordNameArg
* @param recordTypeArg
* @param expectedValue
*/
async checkUntilAvailable(
recordNameArg: string,
recordTypeArg: TDnsRecordType,
expectedValue: string
) {
let cycleArg = 0;
2017-07-18 13:45:06 +00:00
let doCheck = async () => {
if (cycleArg < 30) {
cycleArg++;
2017-07-18 13:45:06 +00:00
try {
let myRecordArray = await this.getRecord(recordNameArg, recordTypeArg);
let myRecord = myRecordArray[0].value[0];
2017-07-18 13:45:06 +00:00
if (myRecord === expectedValue) {
return true;
2017-07-18 13:45:06 +00:00
} else {
2019-01-06 23:21:15 +00:00
await plugins.smartdelay.delayFor(2000);
return await doCheck();
2017-07-18 13:45:06 +00:00
}
} catch (err) {
2019-01-06 23:21:15 +00:00
await plugins.smartdelay.delayFor(2000);
return await doCheck();
2017-01-26 23:11:13 +00:00
}
2017-07-18 13:45:06 +00:00
} else {
console.log('failed permanently...');
return false;
2017-07-18 13:45:06 +00:00
}
};
return await doCheck();
2017-07-18 13:45:06 +00:00
}
2017-01-26 23:11:13 +00:00
2017-07-18 13:45:06 +00:00
/**
* get A Dns Record
*/
async getRecordA(recordNameArg: string): Promise<IDnsRecord[]> {
return await this.getRecord(recordNameArg, 'A');
2017-07-18 13:45:06 +00:00
}
2017-01-26 23:11:13 +00:00
2017-07-18 13:45:06 +00:00
/**
* get AAAA Record
*/
async getRecordAAAA(recordNameArg: string) {
return await this.getRecord(recordNameArg, 'AAAA');
2017-07-18 13:45:06 +00:00
}
2017-01-26 23:11:13 +00:00
2017-07-18 13:45:06 +00:00
/**
* gets a txt record
*/
getRecordTxt(recordNameArg: string): Promise<IDnsRecord[]> {
2019-01-06 23:21:15 +00:00
let done = plugins.smartpromise.defer<IDnsRecord[]>();
2017-07-18 13:45:06 +00:00
plugins.dns.resolveTxt(recordNameArg, (err, recordsArg) => {
if (err) {
done.reject(err);
return;
2017-07-18 13:45:06 +00:00
}
let responseArray: IDnsRecord[] = [];
2017-07-18 13:45:06 +00:00
for (let record of recordsArg) {
let recordAny: any = record; // fix wrong typings
2017-07-18 13:45:06 +00:00
responseArray.push({
chunked: recordAny,
name: recordNameArg,
value: recordAny.join(' '),
type: 'TXT'
});
2017-07-18 13:45:06 +00:00
}
done.resolve(responseArray);
});
return done.promise;
2017-07-18 13:45:06 +00:00
}
2017-01-26 23:11:13 +00:00
2017-07-18 13:45:06 +00:00
/**
* get oridinary record
*/
getRecord(recordNameArg: string, recordTypeArg: TDnsRecordType): Promise<IDnsRecord[]> {
2019-01-06 23:21:15 +00:00
let done = plugins.smartpromise.defer<IDnsRecord[]>();
2017-07-18 13:45:06 +00:00
plugins.dns.resolve(recordNameArg, recordTypeArg, (err, recordsArg) => {
if (err) {
done.reject(err);
return;
2017-07-18 13:45:06 +00:00
}
let responseArray: IDnsRecord[] = [];
2017-07-18 13:45:06 +00:00
for (let recordKey in recordsArg) {
responseArray.push({
name: recordNameArg,
value: recordsArg[recordKey],
type: recordTypeArg
});
}
done.resolve(responseArray);
});
return done.promise;
}
getNameServer(domainNameArg: string) {
2019-01-06 23:21:15 +00:00
const done = plugins.smartpromise.defer();
plugins.dns.resolveNs(domainNameArg, (err, result) => {
if (!err) {
done.resolve(result);
} else {
console.log(err);
done.reject(err);
2017-07-18 13:45:06 +00:00
}
});
2017-07-18 13:45:06 +00:00
}
2016-11-15 20:39:21 +00:00
2017-07-18 13:45:06 +00:00
/**
* set the DNS provider
*/
private _setDnsProvider(dnsProvider: TDnsProvider) {
2017-07-18 13:45:06 +00:00
if (dnsProvider === 'google') {
this.dnsServerIp = '8.8.8.8';
this.dnsServerPort = 53;
plugins.dns.setServers(['8.8.8.8', '8.8.4.4']);
2019-01-06 23:28:15 +00:00
} else if (dnsProvider === 'cloudflare') {
this.dnsServerIp = '1.1.1.1';
this.dnsServerPort = 53;
plugins.dns.setServers(['1.1.1.1', '1.0.0.1']);
2017-07-18 13:45:06 +00:00
} else {
throw new Error('unknown dns provider');
2016-11-15 20:39:21 +00:00
}
2017-07-18 13:45:06 +00:00
}
2017-01-26 23:11:13 +00:00
}