2017-01-29 16:27:48 +00:00
|
|
|
import 'typings-global';
|
|
|
|
import plugins = require('./cflare.plugins')
|
|
|
|
import helpers = require('./cflare.classes.helpers')
|
2017-01-22 18:37:00 +00:00
|
|
|
import * as interfaces from './cflare.interfaces'
|
2016-04-27 00:20:27 +00:00
|
|
|
|
2016-05-15 17:51:48 +00:00
|
|
|
export class CflareAccount {
|
2017-01-29 16:27:48 +00:00
|
|
|
private authEmail: string
|
|
|
|
private authKey: string
|
2017-01-22 18:37:00 +00:00
|
|
|
constructor() {
|
|
|
|
|
2017-01-29 16:27:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 18:37:00 +00:00
|
|
|
auth(optionsArg: { email: string, key: string }) {
|
2017-01-29 16:27:48 +00:00
|
|
|
this.authEmail = optionsArg.email
|
|
|
|
this.authKey = optionsArg.key
|
2016-04-27 01:08:14 +00:00
|
|
|
}
|
2017-01-29 16:27:48 +00:00
|
|
|
|
2017-01-22 18:37:00 +00:00
|
|
|
getZoneId(domainName: string) {
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer()
|
2016-05-16 01:29:29 +00:00
|
|
|
this.listZones(domainName)
|
2017-01-22 18:37:00 +00:00
|
|
|
.then(zoneArrayArg => {
|
|
|
|
let filteredResponse = zoneArrayArg.filter((zoneArg) => {
|
2017-01-29 16:27:48 +00:00
|
|
|
return zoneArg.name === domainName
|
|
|
|
})
|
2017-01-22 18:37:00 +00:00
|
|
|
if (filteredResponse.length >= 1) {
|
2017-01-29 16:27:48 +00:00
|
|
|
done.resolve(filteredResponse[ 0 ].id)
|
2016-05-16 01:29:29 +00:00
|
|
|
} else {
|
2017-01-29 16:27:48 +00:00
|
|
|
plugins.beautylog.error(`the domain ${domainName} does not appear to be in this account!`)
|
|
|
|
done.reject(undefined)
|
2016-05-16 01:29:29 +00:00
|
|
|
}
|
2017-01-29 16:27:48 +00:00
|
|
|
})
|
|
|
|
return done.promise
|
2016-05-16 01:29:29 +00:00
|
|
|
}
|
2017-01-22 18:37:00 +00:00
|
|
|
getRecord(domainNameArg: string, typeArg: string): Promise<interfaces.ICflareRecord> {
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer()
|
2017-01-22 18:37:00 +00:00
|
|
|
let result: interfaces.ICflareRecord
|
|
|
|
|
2017-01-29 16:27:48 +00:00
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg)
|
2016-05-25 05:06:06 +00:00
|
|
|
this.listRecords(domain.zoneName)
|
2017-01-22 18:37:00 +00:00
|
|
|
.then((recordArrayArg) => {
|
|
|
|
let filteredResponse = recordArrayArg.filter((recordArg) => {
|
2017-01-29 16:27:48 +00:00
|
|
|
return (recordArg.type === typeArg && recordArg.name === domainNameArg)
|
2016-05-16 01:29:29 +00:00
|
|
|
})
|
2017-01-29 16:27:48 +00:00
|
|
|
done.resolve(filteredResponse[ 0 ])
|
2016-05-16 01:29:29 +00:00
|
|
|
})
|
2017-01-29 16:27:48 +00:00
|
|
|
return done.promise
|
2016-05-16 01:29:29 +00:00
|
|
|
};
|
2017-01-22 18:37:00 +00:00
|
|
|
createRecord(domainNameArg: string, typeArg: string, contentArg: string) {
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg)
|
2016-05-25 05:06:06 +00:00
|
|
|
this.getZoneId(domain.zoneName)
|
2017-01-22 18:37:00 +00:00
|
|
|
.then((domainIdArg) => {
|
2016-05-24 21:36:06 +00:00
|
|
|
let dataObject = {
|
2016-05-25 04:26:48 +00:00
|
|
|
name: domain.fullName,
|
2016-05-24 21:36:06 +00:00
|
|
|
type: typeArg,
|
|
|
|
content: contentArg
|
2017-01-29 16:27:48 +00:00
|
|
|
}
|
|
|
|
this.request('POST', '/zones/' + domainIdArg + '/dns_records', dataObject)
|
2017-01-22 18:37:00 +00:00
|
|
|
.then(function (responseArg) {
|
2017-01-29 16:27:48 +00:00
|
|
|
done.resolve(responseArg)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return done.promise
|
2016-04-27 01:08:14 +00:00
|
|
|
};
|
2017-01-22 18:37:00 +00:00
|
|
|
removeRecord(domainNameArg: string, typeArg: string) {
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg)
|
2017-01-22 18:37:00 +00:00
|
|
|
this.getRecord(domain.fullName, typeArg)
|
2016-05-25 04:26:48 +00:00
|
|
|
.then((responseArg) => {
|
2017-01-22 18:37:00 +00:00
|
|
|
if (responseArg) {
|
2017-01-29 16:27:48 +00:00
|
|
|
let requestRoute: string = '/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id
|
|
|
|
this.request('DELETE', requestRoute)
|
2016-06-21 17:04:43 +00:00
|
|
|
.then((responseArg) => {
|
2017-01-29 16:27:48 +00:00
|
|
|
done.resolve(responseArg)
|
|
|
|
})
|
2016-06-21 17:04:43 +00:00
|
|
|
} else {
|
2017-01-29 16:27:48 +00:00
|
|
|
done.reject()
|
2016-06-21 17:04:43 +00:00
|
|
|
}
|
2017-01-29 16:27:48 +00:00
|
|
|
})
|
|
|
|
return done.promise
|
2016-04-27 01:08:14 +00:00
|
|
|
};
|
2017-01-22 18:37:00 +00:00
|
|
|
updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg)
|
|
|
|
return done.promise
|
2016-05-16 01:29:29 +00:00
|
|
|
};
|
2017-01-22 18:37:00 +00:00
|
|
|
listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer<interfaces.ICflareRecord[]>()
|
2017-01-22 18:37:00 +00:00
|
|
|
let result: interfaces.ICflareRecord[] = []
|
|
|
|
|
2017-01-29 16:27:48 +00:00
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg)
|
2016-05-25 05:06:06 +00:00
|
|
|
this.getZoneId(domain.zoneName)
|
2017-01-22 18:37:00 +00:00
|
|
|
.then((domainIdArg) => {
|
2017-01-29 16:27:48 +00:00
|
|
|
this.request('GET', '/zones/' + domainIdArg + '/dns_records?per_page=100')
|
2017-01-22 18:37:00 +00:00
|
|
|
.then(function (responseArg: any) {
|
|
|
|
result = responseArg.result
|
2017-01-29 16:27:48 +00:00
|
|
|
done.resolve(result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return done.promise
|
2016-04-27 01:08:14 +00:00
|
|
|
}
|
2017-01-22 18:37:00 +00:00
|
|
|
listZones(domainName?: string): Promise<interfaces.ICflareZone[]> { // TODO: handle pagination
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer<interfaces.ICflareZone[]>()
|
|
|
|
let requestRoute = '/zones?per_page=50'
|
|
|
|
if (domainName) requestRoute = requestRoute + '&name=' + domainName
|
|
|
|
let result = []
|
|
|
|
this.request('GET', requestRoute)
|
2017-01-22 18:37:00 +00:00
|
|
|
.then((responseArg: any) => {
|
2017-01-29 16:27:48 +00:00
|
|
|
result = responseArg.result
|
|
|
|
done.resolve(result)
|
|
|
|
})
|
|
|
|
return done.promise
|
2016-04-27 01:08:14 +00:00
|
|
|
};
|
2017-01-22 18:37:00 +00:00
|
|
|
request(methodArg: string, routeArg: string, dataArg = {}) {
|
2017-01-29 16:27:48 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
let jsonArg: string = JSON.stringify(dataArg)
|
|
|
|
let options: plugins.smartrequest.ISmartRequestOptions = {
|
2017-01-22 18:37:00 +00:00
|
|
|
method: methodArg,
|
|
|
|
headers: {
|
2017-01-29 16:27:48 +00:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Email': this.authEmail,
|
|
|
|
'X-Auth-Key': this.authKey
|
2016-05-16 01:29:29 +00:00
|
|
|
},
|
2017-01-29 16:27:48 +00:00
|
|
|
requestBody: jsonArg
|
|
|
|
}
|
|
|
|
// console.log(options);
|
2017-01-22 18:37:00 +00:00
|
|
|
let retryCount = 0
|
|
|
|
|
2017-01-29 16:27:48 +00:00
|
|
|
let makeRequest = async () => {
|
|
|
|
let response: any = await plugins.smartrequest.request(
|
|
|
|
`https://api.cloudflare.com/client/v4${routeArg}`,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
done.resolve(response.body)
|
|
|
|
} else if (response.statusCode === 429) {
|
|
|
|
console.log('rate limited! Waiting for retry!')
|
|
|
|
retryRequest()
|
|
|
|
} else {
|
|
|
|
console.log(response.status)
|
|
|
|
console.log(response.messages)
|
|
|
|
console.log(response.errors)
|
|
|
|
done.reject(new Error('request failed'))
|
|
|
|
}
|
2017-01-22 18:37:00 +00:00
|
|
|
}
|
2017-01-29 16:27:48 +00:00
|
|
|
let retryRequest = async (delayTimeArg = 6000) => {
|
2017-01-22 18:37:00 +00:00
|
|
|
console.log(`retry started and waiting for ${delayTimeArg} ms`)
|
|
|
|
await plugins.smartdelay.delayFor(delayTimeArg)
|
2017-01-29 16:27:48 +00:00
|
|
|
if (retryCount < 10) {
|
2017-01-22 18:37:00 +00:00
|
|
|
retryCount++
|
2017-01-29 16:27:48 +00:00
|
|
|
return await makeRequest()
|
2017-01-22 18:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
makeRequest()
|
2017-01-29 16:27:48 +00:00
|
|
|
return done.promise
|
|
|
|
}
|
|
|
|
|
|
|
|
private authCheck() {
|
|
|
|
return (this.authEmail && this.authKey) // check if auth is available
|
2016-04-27 01:08:14 +00:00
|
|
|
}
|
2017-01-29 16:27:48 +00:00
|
|
|
}
|