smartdns/dist/index.js
2017-07-18 15:45:06 +02:00

145 lines
9.1 KiB
JavaScript

"use strict";
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const q = require("smartq");
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
*/
constructor(dnsProviderArg = 'google') {
this._setDnsProvider(dnsProviderArg);
}
/**
* gets a record
*/
getRecord(recordNameArg, recordTypeArg) {
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) {
let done = q.defer();
plugins.dns.resolveTxt(recordNameArg, (err, recordsArg) => {
if (err) {
done.reject(err);
return;
}
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);
return;
}
let responseArray = [];
for (let recordKey in recordsArg) {
responseArray.push({
name: recordNameArg,
value: recordsArg[recordKey],
type: recordTypeArg
});
}
done.resolve(responseArray);
});
return done.promise;
}
/**
* set the DNS provider
*/
_setDnsProvider(dnsProvider) {
if (dnsProvider === 'google') {
this.dnsServerIp = '8.8.8.8';
this.dnsServerPort = 53;
plugins.dns.setServers(['8.8.8.8', '8.8.4.4']);
}
else {
throw new Error('unknown dns provider');
}
}
}
exports.Dnsly = Dnsly;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsNEJBQTJCO0FBQzNCLDJDQUEwQztBQXFCMUM7O0dBRUc7QUFDSDtJQUdFOztPQUVHO0lBQ0gsWUFBWSxpQkFBK0IsUUFBUTtRQUNqRCxJQUFJLENBQUMsZUFBZSxDQUFDLGNBQWMsQ0FBQyxDQUFBO0lBQ3RDLENBQUM7SUFFRDs7T0FFRztJQUNHLFNBQVMsQ0FBRSxhQUFxQixFQUFFLGFBQTZCOztZQUNuRSxNQUFNLENBQUMsQ0FBQyxhQUFhL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