smartdns/dist/index.js

145 lines
9.1 KiB
JavaScript
Raw Normal View History

2016-11-15 20:39:21 +00:00
"use strict";
2017-01-26 23:11:13 +00:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
2017-07-18 13:45:06 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2017-01-26 23:11:13 +00:00
const q = require("smartq");
2016-11-15 20:39:21 +00:00
const plugins = require("./dnsly.plugins");
/**
* class dnsly offers methods for working with dns from a dns provider like Google DNS
*/
class Dnsly {
/**
* constructor for class dnsly
*/
2017-01-26 23:11:13 +00:00
constructor(dnsProviderArg = 'google') {
2016-11-15 20:39:21 +00:00
this._setDnsProvider(dnsProviderArg);
}
/**
* gets a record
*/
getRecord(recordNameArg, recordTypeArg) {
2017-01-26 23:11:13 +00:00
return __awaiter(this, void 0, void 0, function* () {
switch (recordTypeArg) {
case 'TXT':
return yield this.getRecordTxt(recordNameArg);
case 'A':
return yield this.getRecordA(recordNameArg);
case 'AAAA':
return yield this.getRecordAAAA(recordNameArg);
}
});
}
checkUntilAvailable(recordNameArg, recordTypeArg, expectedValue) {
return __awaiter(this, void 0, void 0, function* () {
let cycleArg = 0;
let doCheck = () => __awaiter(this, void 0, void 0, function* () {
if (cycleArg < 30) {
cycleArg++;
try {
let myRecordArray = yield this.getRecord(recordNameArg, recordTypeArg);
let myRecord = myRecordArray[0].value;
if (myRecord === expectedValue) {
return true;
}
else {
yield plugins.smartdelay.delayFor(500);
return yield doCheck();
}
}
catch (err) {
yield plugins.smartdelay.delayFor(500);
return yield doCheck();
}
}
else {
console.log('failed permanently...');
return false;
}
});
return yield doCheck();
});
}
/**
* get A Dns Record
*/
getRecordA(recordNameArg) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getOrdinaryRecord(recordNameArg, 'A');
});
}
/**
* get AAAA Record
*/
getRecordAAAA(recordNameArg) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getOrdinaryRecord(recordNameArg, 'AAAA');
});
}
/**
* gets a txt record
*/
getRecordTxt(recordNameArg) {
2016-11-15 20:39:21 +00:00
let done = q.defer();
2017-01-26 23:11:13 +00:00
plugins.dns.resolveTxt(recordNameArg, (err, recordsArg) => {
2016-11-15 20:39:21 +00:00
if (err) {
done.reject(err);
2017-01-26 23:37:32 +00:00
return;
2016-11-15 20:39:21 +00:00
}
2017-01-26 23:11:13 +00:00
let responseArray = [];
for (let record of recordsArg) {
let recordAny = record; // fix wrong typings
responseArray.push({
chunked: recordAny,
name: recordNameArg,
value: recordAny.join(' '),
type: 'TXT'
});
}
done.resolve(responseArray);
});
return done.promise;
}
/**
* get oridinary record
*/
getOrdinaryRecord(recordNameArg, recordTypeArg) {
let done = q.defer();
plugins.dns.resolve(recordNameArg, recordTypeArg, (err, recordsArg) => {
if (err) {
done.reject(err);
2017-01-26 23:37:32 +00:00
return;
2017-01-26 23:11:13 +00:00
}
let responseArray = [];
2017-07-18 13:45:06 +00:00
for (let recordKey in recordsArg) {
2017-01-26 23:11:13 +00:00
responseArray.push({
name: recordNameArg,
2017-07-18 13:45:06 +00:00
value: recordsArg[recordKey],
2017-01-26 23:11:13 +00:00
type: recordTypeArg
});
}
done.resolve(responseArray);
2016-11-15 20:39:21 +00:00
});
return done.promise;
}
/**
* set the DNS provider
*/
_setDnsProvider(dnsProvider) {
if (dnsProvider === 'google') {
this.dnsServerIp = '8.8.8.8';
this.dnsServerPort = 53;
2016-11-15 21:40:40 +00:00
plugins.dns.setServers(['8.8.8.8', '8.8.4.4']);
2016-11-15 20:39:21 +00:00
}
else {
throw new Error('unknown dns provider');
}
}
}
exports.Dnsly = Dnsly;
2017-07-18 13:45:06 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