perf: 抽取api

This commit is contained in:
xiaojunnuo
2021-01-06 23:52:10 +08:00
parent 5312c11472
commit 8cc80deff8
10 changed files with 766 additions and 592 deletions
+654 -57
View File
File diff suppressed because it is too large Load Diff
+3 -1
View File
@@ -10,7 +10,9 @@
"author": "Greper",
"license": "MIT",
"dependencies": {
"@certd/api": "^0.1.2"
"@certd/api": "^0.1.2",
"@alicloud/pop-core": "^1.7.10",
"tencentcloud-sdk-nodejs": "^4.0.44"
},
"devDependencies": {
"chai": "^4.2.0",
@@ -1,8 +1,9 @@
import dnsProviders from './index.js'
export class DnsProviderFactory {
static async createByType (type, options) {
try {
const ProviderModule = await import('./impl/' + type + '.js')
const Provider = ProviderModule.default
const Provider = dnsProviders[type]
return new Provider(options)
} catch (e) {
throw new Error('暂不支持此dnsProvider' + type, e)
@@ -0,0 +1,108 @@
import { AbstractDnsProvider } from '@certd/api'
import Core from '@alicloud/pop-core'
import _ from 'lodash-es'
export class AliyunDnsProvider extends AbstractDnsProvider {
constructor (dnsProviderConfig) {
super()
this.client = new Core({
accessKeyId: dnsProviderConfig.accessKeyId,
accessKeySecret: dnsProviderConfig.accessKeySecret,
endpoint: 'https://alidns.aliyuncs.com',
apiVersion: '2015-01-09'
})
}
static name () {
return 'aliyun'
}
async getDomainList () {
const params = {
RegionId: 'cn-hangzhou'
}
const requestOption = {
method: 'POST'
}
const ret = await this.client.request('DescribeDomains', params, requestOption)
return ret.Domains.Domain
}
async matchDomain (dnsRecord) {
const list = await this.getDomainList()
let domain = null
for (const item of list) {
if (_.endsWith(dnsRecord, item.DomainName)) {
domain = item.DomainName
break
}
}
if (!domain) {
throw new Error('can not find Domain ,' + dnsRecord)
}
return domain
}
async getRecords (domain, rr, value) {
const params = {
RegionId: 'cn-hangzhou',
DomainName: domain,
RRKeyWord: rr
}
if (value) {
params.ValueKeyWord = value
}
const requestOption = {
method: 'POST'
}
const ret = await this.client.request('DescribeDomainRecords', params, requestOption)
return ret.DomainRecords.Record
}
async createRecord ({ fullRecord, type, value }) {
this.logger.info('添加域名解析:', fullRecord, value)
const domain = await this.matchDomain(fullRecord)
const rr = fullRecord.replace('.' + domain, '')
const params = {
RegionId: 'cn-hangzhou',
DomainName: domain,
RR: rr,
Type: type,
Value: value
// Line: 'oversea' // 海外
}
const requestOption = {
method: 'POST'
}
try {
const ret = await this.client.request('AddDomainRecord', params, requestOption)
this.logger.info('添加域名解析成功:', value, value, ret.RecordId)
return ret.RecordId
} catch (e) {
// e.code === 'DomainRecordDuplicate'
this.logger.info('添加域名解析出错', e)
throw e
}
}
async removeRecord ({ fullRecord, type, value, record }) {
const params = {
RegionId: 'cn-hangzhou',
RecordId: record
}
const requestOption = {
method: 'POST'
}
const ret = await this.client.request('DeleteDomainRecord', params, requestOption)
this.logger.info('删除域名解析成功:', fullRecord, value, ret.RecordId)
return ret.RecordId
}
}
@@ -1,7 +1,11 @@
import { AbstractDnsProvider, util } from '@certd/api'
import _ from 'lodash-es'
const request = util.request
export default class DnspodDnsProvider extends AbstractDnsProvider {
export class DnspodDnsProvider extends AbstractDnsProvider {
static name () {
return 'dnspod'
}
constructor (dnsProviderConfig) {
super()
if (!dnsProviderConfig.id || !dnsProviderConfig.token) {
@@ -0,0 +1,6 @@
import { AliyunDnsProvider } from './impl/aliyun.js'
import { DnspodDnsProvider } from './impl/dnspod.js'
export default {
[AliyunDnsProvider.name()]: AliyunDnsProvider,
[DnspodDnsProvider.name()]: DnspodDnsProvider
}
+1 -1
View File
@@ -1 +1 @@
export { DnsProviderFactory } from './dns-provider/dns-provider-factory'
export { DnsProviderFactory } from './dns-provider/dns-provider-factory.js'