This commit is contained in:
Philipp Kunz 2017-01-27 00:23:56 +01:00
commit db9d748f99
9 changed files with 307 additions and 46 deletions

View File

@ -1,4 +1,5 @@
import 'typings-global';
import * as beautylog from 'beautylog';
import * as dns from 'dns';
export { beautylog, dns };
import * as smartdelay from 'smartdelay';
export { beautylog, dns, smartdelay };

View File

@ -4,4 +4,6 @@ const beautylog = require("beautylog");
exports.beautylog = beautylog;
const dns = require("dns");
exports.dns = dns;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG5zbHkucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL2Ruc2x5LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUN2Qix1Q0FBc0M7QUFJbEMsOEJBQVM7QUFIYiwyQkFBMEI7QUFJdEIsa0JBQUcifQ==
const smartdelay = require("smartdelay");
exports.smartdelay = smartdelay;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG5zbHkucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL2Ruc2x5LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUN2Qix1Q0FBc0M7QUFLbEMsOEJBQVM7QUFKYiwyQkFBMEI7QUFLdEIsa0JBQUc7QUFKUCx5Q0FBd0M7QUFLcEMsZ0NBQVUifQ==

29
dist/index.d.ts vendored
View File

@ -1,7 +1,11 @@
/// <reference types="q" />
import * as q from 'q';
export declare type TDnsProvider = 'google';
export declare type TDnsRecordType = 'A' | 'AAAA' | 'CNAME' | 'PTR' | 'MX' | 'NAPTR' | 'NS' | 'SOA' | 'SRV' | 'TXT';
export interface IDnsRecord {
chunked?: string[];
name: string;
type: TDnsRecordType;
value: string;
}
/**
* class dnsly offers methods for working with dns from a dns provider like Google DNS
*/
@ -11,11 +15,28 @@ export declare class Dnsly {
/**
* constructor for class dnsly
*/
constructor(dnsProviderArg: TDnsProvider);
constructor(dnsProviderArg?: TDnsProvider);
/**
* gets a record
*/
getRecord(recordNameArg: string, recordTypeArg: TDnsRecordType): q.Promise<{}>;
getRecord(recordNameArg: string, recordTypeArg: TDnsRecordType): Promise<IDnsRecord[]>;
checkUntilAvailable(recordNameArg: string, recordTypeArg: TDnsRecordType, expectedValue: string): Promise<any>;
/**
* get A Dns Record
*/
getRecordA(recordNameArg: string): Promise<IDnsRecord[]>;
/**
* get AAAA Record
*/
getRecordAAAA(recordNameArg: string): Promise<IDnsRecord[]>;
/**
* gets a txt record
*/
getRecordTxt(recordNameArg: string): Promise<IDnsRecord[]>;
/**
* get oridinary record
*/
private getOrdinaryRecord(recordNameArg, recordTypeArg);
/**
* set the DNS provider
*/

110
dist/index.js vendored
View File

@ -1,5 +1,13 @@
"use strict";
const q = require("q");
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());
});
};
const q = require("smartq");
const plugins = require("./dnsly.plugins");
/**
* class dnsly offers methods for working with dns from a dns provider like Google DNS
@ -8,19 +16,111 @@ class Dnsly {
/**
* constructor for class dnsly
*/
constructor(dnsProviderArg) {
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.resolve(recordNameArg, recordTypeArg, (err, addresses) => {
plugins.dns.resolveTxt(recordNameArg, (err, recordsArg) => {
if (err) {
done.reject(err);
}
done.resolve(addresses);
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);
}
let responseArray = [];
for (let record of recordsArg) {
responseArray.push({
name: recordNameArg,
value: record,
type: recordTypeArg
});
}
done.resolve(responseArray);
});
return done.promise;
}
@ -39,4 +139,4 @@ class Dnsly {
}
}
exports.Dnsly = Dnsly;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIj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RyxTQUFTLENBQUE7WUFDNUIsSUFBSSxDQUFDLGFBQWEsR0FBRyxFQUFFLENBQUE7WUFDdkIsT0FBTyxDQUFDLEdBQUcsQ0FBQyxVQUFVLENBQUMsQ0FBQyxTQUFTLEVBQUUsU0FBUyxDQUFDLENBQUMsQ0FBQTtRQUNsRCxDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDSixNQUFNLElBQUksS0FBSyxDQUFDLHNCQUFzQixDQUFDLENBQUE7UUFDM0MsQ0FBQztJQUNMLENBQUM7Q0FDSjtBQTFIRCxzQkEwSEMifQ==

View File

@ -1,6 +1,6 @@
{
"name": "dnsly",
"version": "1.0.7",
"version": "2.0.1",
"description": "smart dns methods written in TypeScript",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
@ -23,14 +23,13 @@
},
"homepage": "https://gitlab.com/pushrocks/dnsly#README",
"dependencies": {
"@types/q": "0.0.32",
"beautylog": "^6.0.0",
"q": "^1.4.1",
"smartdelay": "^1.0.1",
"smartq": "^1.0.4",
"typings-global": "^1.0.14"
},
"devDependencies": {
"@types/should": "^8.1.30",
"should": "^11.1.2",
"smartchai": "^1.0.1",
"typings-test": "^1.0.3"
}
}

View File

@ -1,21 +1,34 @@
"use strict";
require("typings-test");
const should = require("should");
const smartchai_1 = require("smartchai");
const dnsly = require("../dist/index");
let testDnsly;
describe('dnsly', function () {
it('should create an instance of Dnsly', function () {
testDnsly = new dnsly.Dnsly('google');
should(testDnsly).be.instanceOf(dnsly.Dnsly);
smartchai_1.expect(testDnsly).to.be.instanceOf(dnsly.Dnsly);
});
it('should, get a dns record for a domain', function (done) {
testDnsly.getRecord('google.com', 'A').then(res => {
console.log(res);
done();
}).catch(err => {
console.log(err);
done(err);
});
it('should get an A DNS Record', function () {
return smartchai_1.expect(testDnsly.getRecordA('dnsly_a.bleu.de')).to.eventually.deep.equal([{
name: 'dnsly_a.bleu.de',
value: '127.0.0.1',
type: 'A'
}]);
});
it('should get an AAAA Record', function () {
return smartchai_1.expect(testDnsly.getRecordAAAA('dnsly_aaaa.bleu.de')).to.eventually.deep.equal([{
name: 'dnsly_aaaa.bleu.de',
value: '::1',
type: 'AAAA'
}]);
});
it('should get a txt record', function () {
return smartchai_1.expect(testDnsly.getRecordTxt('dnsly_txt.bleu.de')).to.eventually.deep.equal([{
chunked: ['sometext_txt'],
name: 'dnsly_txt.bleu.de',
value: 'sometext_txt',
type: 'TXT'
}]);
});
it('should, get a mx record for a domain', function (done) {
testDnsly.getRecord('google.com', 'MX').then(res => {
@ -26,5 +39,12 @@ describe('dnsly', function () {
done(err);
});
});
it('should check until DNS is available', function () {
return smartchai_1.expect(testDnsly.checkUntilAvailable('dnsly_txt.bleu.de', 'TXT', 'sometext_txt')).to.eventually.be.true;
});
it('should check until DNS is available an return false if it fails', function () {
this.timeout(30000);
return smartchai_1.expect(testDnsly.checkUntilAvailable('dnsly_txt.bleu.de', 'TXT', 'sometext_txt2')).to.eventually.be.false;
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQixpQ0FBZ0M7QUFFaEMsdUNBQXNDO0FBRXRDLElBQUksU0FBc0IsQ0FBQTtBQUUxQixRQUFRLENBQUMsT0FBTyxFQUFFO0lBQ2QsRUFBRSxDQUFDLG9DQUFvQyxFQUFFO1FBQ3JDLFNBQVMsR0FBRyxJQUFJLEtBQUssQ0FBQyxLQUFLLENBQUMsUUFBUSxDQUFDLENBQUE7UUFDckMsTUFBTSxDQUFDLFNBQVMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsS0FBSyxDQUFDLEtBQUssQ0FBQyxDQUFBO0lBQ2hELENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLHVDQUF1QyxFQUFFLFV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0sQ0FBQyxrQkFBTSxDQUFDLFNBQVMsQ0FBQyxtQkFBbUIsQ0FBQyxtQkFBbUIsRUFBQyxLQUFLLEVBQUMsZUFBZSxDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLEVBQUUsQ0FBQyxLQUFLLENBQUE7SUFDbEgsQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9

View File

@ -1,5 +1,5 @@
import 'typings-test'
import * as should from 'should'
import { expect } from 'smartchai'
import * as dnsly from '../dist/index'
@ -8,18 +8,32 @@ let testDnsly: dnsly.Dnsly
describe('dnsly', function () {
it('should create an instance of Dnsly', function () {
testDnsly = new dnsly.Dnsly('google')
should(testDnsly).be.instanceOf(dnsly.Dnsly)
expect(testDnsly).to.be.instanceOf(dnsly.Dnsly)
})
it('should, get a dns record for a domain', function (done) {
testDnsly.getRecord('google.com', 'A').then(res => {
console.log(res)
done()
}).catch(err => {
console.log(err)
done(err)
})
it('should get an A DNS Record', function () {
return expect(testDnsly.getRecordA('dnsly_a.bleu.de')).to.eventually.deep.equal([{
name: 'dnsly_a.bleu.de',
value: '127.0.0.1',
type: 'A'
}])
})
it('should get an AAAA Record', function () {
return expect(testDnsly.getRecordAAAA('dnsly_aaaa.bleu.de')).to.eventually.deep.equal([{
name: 'dnsly_aaaa.bleu.de',
value: '::1',
type: 'AAAA'
}])
})
it('should get a txt record', function() {
return expect(testDnsly.getRecordTxt('dnsly_txt.bleu.de')).to.eventually.deep.equal([{
chunked: ['sometext_txt'],
name: 'dnsly_txt.bleu.de',
value: 'sometext_txt',
type: 'TXT'
}])
})
it('should, get a mx record for a domain', function (done) {
@ -32,4 +46,13 @@ describe('dnsly', function () {
})
})
it('should check until DNS is available', function() {
return expect(testDnsly.checkUntilAvailable('dnsly_txt.bleu.de','TXT','sometext_txt')).to.eventually.be.true
})
it('should check until DNS is available an return false if it fails', function() {
this.timeout(30000)
return expect(testDnsly.checkUntilAvailable('dnsly_txt.bleu.de','TXT','sometext_txt2')).to.eventually.be.false
})
})

View File

@ -1,8 +1,10 @@
import 'typings-global'
import * as beautylog from 'beautylog'
import * as dns from 'dns'
import * as smartdelay from 'smartdelay'
export {
beautylog,
dns
dns,
smartdelay
}

View File

@ -1,4 +1,4 @@
import * as q from 'q'
import * as q from 'smartq'
import * as plugins from './dnsly.plugins'
export type TDnsProvider = 'google'
@ -13,6 +13,13 @@ export type TDnsRecordType = 'A'
| 'SRV'
| 'TXT'
export interface IDnsRecord {
chunked?: string[]
name: string
type: TDnsRecordType
value: string
}
/**
* class dnsly offers methods for working with dns from a dns provider like Google DNS
*/
@ -22,20 +29,106 @@ export class Dnsly {
/**
* constructor for class dnsly
*/
constructor(dnsProviderArg: TDnsProvider) {
constructor(dnsProviderArg: TDnsProvider = 'google') {
this._setDnsProvider(dnsProviderArg)
}
/**
* gets a record
*/
getRecord(recordNameArg: string, recordTypeArg: TDnsRecordType) {
let done = q.defer()
plugins.dns.resolve(recordNameArg,recordTypeArg, (err, addresses) => {
async getRecord(recordNameArg: string, recordTypeArg: TDnsRecordType): Promise<IDnsRecord[]> {
switch (recordTypeArg) {
case 'TXT':
return await this.getRecordTxt(recordNameArg)
case 'A':
return await this.getRecordA(recordNameArg)
case 'AAAA':
return await this.getRecordAAAA(recordNameArg)
}
}
async checkUntilAvailable(recordNameArg: string, recordTypeArg: TDnsRecordType, expectedValue: string) {
let cycleArg = 0
let doCheck = async () => {
if (cycleArg < 30) {
cycleArg++
try {
let myRecordArray = await this.getRecord(recordNameArg, recordTypeArg)
let myRecord = myRecordArray[0].value
if (myRecord === expectedValue) {
return true
} else {
await plugins.smartdelay.delayFor(500)
return await doCheck()
}
} catch (err) {
await plugins.smartdelay.delayFor(500)
return await doCheck()
}
} else {
console.log('failed permanently...')
return false
}
}
return await doCheck()
}
/**
* get A Dns Record
*/
async getRecordA(recordNameArg: string): Promise<IDnsRecord[]> {
return await this.getOrdinaryRecord(recordNameArg, 'A')
}
/**
* get AAAA Record
*/
async getRecordAAAA(recordNameArg: string) {
return await this.getOrdinaryRecord(recordNameArg, 'AAAA')
}
/**
* gets a txt record
*/
getRecordTxt(recordNameArg: string): Promise<IDnsRecord[]> {
let done = q.defer<IDnsRecord[]>()
plugins.dns.resolveTxt(recordNameArg, (err, recordsArg) => {
if (err) {
done.reject(err)
}
done.resolve(addresses)
let responseArray: IDnsRecord[] = []
for (let record of recordsArg) {
let recordAny: any = record // fix wrong typings
responseArray.push({
chunked: recordAny,
name: recordNameArg,
value: recordAny.join(' '),
type: 'TXT'
})
}
done.resolve(responseArray)
})
return done.promise
}
/**
* get oridinary record
*/
private getOrdinaryRecord(recordNameArg: string, recordTypeArg: TDnsRecordType): Promise<IDnsRecord[]> {
let done = q.defer<IDnsRecord[]>()
plugins.dns.resolve(recordNameArg, recordTypeArg, (err, recordsArg) => {
if (err) {
done.reject(err)
}
let responseArray: IDnsRecord[] = []
for (let record of recordsArg) {
responseArray.push({
name: recordNameArg,
value: record,
type: recordTypeArg
})
}
done.resolve(responseArray)
})
return done.promise
}
@ -47,7 +140,7 @@ export class Dnsly {
if (dnsProvider === 'google') {
this.dnsServerIp = '8.8.8.8'
this.dnsServerPort = 53
plugins.dns.setServers(['8.8.8.8','8.8.4.4'])
plugins.dns.setServers(['8.8.8.8', '8.8.4.4'])
} else {
throw new Error('unknown dns provider')
}