refactor: 1

master
xiaojunnuo 2021-01-08 17:14:15 +08:00
parent 137e043dfe
commit 576f7db978
4 changed files with 157 additions and 47 deletions

View File

@ -30,6 +30,19 @@
"js-tokens": "^4.0.0" "js-tokens": "^4.0.0"
} }
}, },
"@certd/acme-client": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/@certd/acme-client/-/acme-client-0.1.6.tgz",
"integrity": "sha512-XffzB/QHRj61gUHXor1B8R2TVt7HnklJpjAbwQi8vHjBGloU8I3occJxIunoh1AShhc4wsxnc+h/D5yRIUp17A==",
"requires": {
"axios": "0.21.1",
"backo2": "^1.0.0",
"bluebird": "^3.5.0",
"debug": "^4.1.1",
"log4js": "^6.3.0",
"node-forge": "^0.10.0"
}
},
"@eslint/eslintrc": { "@eslint/eslintrc": {
"version": "0.2.2", "version": "0.2.2",
"resolved": "https://registry.npm.taobao.org/@eslint/eslintrc/download/@eslint/eslintrc-0.2.2.tgz?cache=0&sync_timestamp=1607145629875&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40eslint%2Feslintrc%2Fdownload%2F%40eslint%2Feslintrc-0.2.2.tgz", "resolved": "https://registry.npm.taobao.org/@eslint/eslintrc/download/@eslint/eslintrc-0.2.2.tgz?cache=0&sync_timestamp=1607145629875&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40eslint%2Feslintrc%2Fdownload%2F%40eslint%2Feslintrc-0.2.2.tgz",
@ -186,6 +199,19 @@
"integrity": "sha1-SDFDxWeu7UeFdZwIZXhtx319LjE=", "integrity": "sha1-SDFDxWeu7UeFdZwIZXhtx319LjE=",
"dev": true "dev": true
}, },
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"backo2": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz",
"integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc="
},
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npm.taobao.org/balanced-match/download/balanced-match-1.0.0.tgz", "resolved": "https://registry.npm.taobao.org/balanced-match/download/balanced-match-1.0.0.tgz",
@ -198,6 +224,11 @@
"integrity": "sha1-MPpAyef+B9vIlWeM0ocCTeokHdk=", "integrity": "sha1-MPpAyef+B9vIlWeM0ocCTeokHdk=",
"dev": true "dev": true
}, },
"bluebird": {
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz",
"integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg=="
},
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"resolved": "https://registry.npm.taobao.org/brace-expansion/download/brace-expansion-1.1.11.tgz?cache=0&sync_timestamp=1601898189928&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fbrace-expansion%2Fdownload%2Fbrace-expansion-1.1.11.tgz", "resolved": "https://registry.npm.taobao.org/brace-expansion/download/brace-expansion-1.1.11.tgz?cache=0&sync_timestamp=1601898189928&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fbrace-expansion%2Fdownload%2Fbrace-expansion-1.1.11.tgz",
@ -381,11 +412,24 @@
"which": "^2.0.1" "which": "^2.0.1"
} }
}, },
"date-format": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/date-format/-/date-format-3.0.0.tgz",
"integrity": "sha512-eyTcpKOcamdhWJXj56DpQMo1ylSQpcGtGKXcU0Tb97+K56/CF5amAqqqNj0+KvA0iw2ynxtHWFsPDSClCxe48w=="
},
"dayjs": { "dayjs": {
"version": "1.10.2", "version": "1.10.2",
"resolved": "https://registry.npm.taobao.org/dayjs/download/dayjs-1.10.2.tgz?cache=0&sync_timestamp=1609889274763&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdayjs%2Fdownload%2Fdayjs-1.10.2.tgz", "resolved": "https://registry.npm.taobao.org/dayjs/download/dayjs-1.10.2.tgz?cache=0&sync_timestamp=1609889274763&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdayjs%2Fdownload%2Fdayjs-1.10.2.tgz",
"integrity": "sha1-jzpCTOuUSoGTUGgEsARadz0tBnI=" "integrity": "sha1-jzpCTOuUSoGTUGgEsARadz0tBnI="
}, },
"debug": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz",
"integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==",
"requires": {
"ms": "2.1.2"
}
},
"decamelize": { "decamelize": {
"version": "1.2.0", "version": "1.2.0",
"resolved": "https://registry.npm.taobao.org/decamelize/download/decamelize-1.2.0.tgz", "resolved": "https://registry.npm.taobao.org/decamelize/download/decamelize-1.2.0.tgz",
@ -927,6 +971,26 @@
} }
} }
}, },
"flatted": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.2.tgz",
"integrity": "sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA=="
},
"follow-redirects": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
},
"fs-extra": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
"integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"fs.realpath": { "fs.realpath": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npm.taobao.org/fs.realpath/download/fs.realpath-1.0.0.tgz", "resolved": "https://registry.npm.taobao.org/fs.realpath/download/fs.realpath-1.0.0.tgz",
@ -1001,8 +1065,7 @@
"graceful-fs": { "graceful-fs": {
"version": "4.2.4", "version": "4.2.4",
"resolved": "https://registry.npm.taobao.org/graceful-fs/download/graceful-fs-4.2.4.tgz", "resolved": "https://registry.npm.taobao.org/graceful-fs/download/graceful-fs-4.2.4.tgz",
"integrity": "sha1-Ila94U02MpWMRl68ltxGfKB6Kfs=", "integrity": "sha1-Ila94U02MpWMRl68ltxGfKB6Kfs="
"dev": true
}, },
"growl": { "growl": {
"version": "1.10.5", "version": "1.10.5",
@ -1229,6 +1292,14 @@
"minimist": "^1.2.0" "minimist": "^1.2.0"
} }
}, },
"jsonfile": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"requires": {
"graceful-fs": "^4.1.6"
}
},
"levn": { "levn": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npm.taobao.org/levn/download/levn-0.4.1.tgz", "resolved": "https://registry.npm.taobao.org/levn/download/levn-0.4.1.tgz",
@ -1332,6 +1403,18 @@
} }
} }
}, },
"log4js": {
"version": "6.3.0",
"resolved": "https://registry.npmjs.org/log4js/-/log4js-6.3.0.tgz",
"integrity": "sha512-Mc8jNuSFImQUIateBFwdOQcmC6Q5maU0VVvdC2R6XMb66/VnT+7WS4D/0EeNMZu1YODmJe5NIn2XftCzEocUgw==",
"requires": {
"date-format": "^3.0.0",
"debug": "^4.1.1",
"flatted": "^2.0.1",
"rfdc": "^1.1.4",
"streamroller": "^2.2.4"
}
},
"lru-cache": { "lru-cache": {
"version": "6.0.0", "version": "6.0.0",
"resolved": "https://registry.npm.taobao.org/lru-cache/download/lru-cache-6.0.0.tgz?cache=0&sync_timestamp=1599054167787&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Flru-cache%2Fdownload%2Flru-cache-6.0.0.tgz", "resolved": "https://registry.npm.taobao.org/lru-cache/download/lru-cache-6.0.0.tgz?cache=0&sync_timestamp=1599054167787&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Flru-cache%2Fdownload%2Flru-cache-6.0.0.tgz",
@ -1480,6 +1563,11 @@
} }
} }
}, },
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"nanoid": { "nanoid": {
"version": "3.1.12", "version": "3.1.12",
"resolved": "https://registry.npm.taobao.org/nanoid/download/nanoid-3.1.12.tgz?cache=0&sync_timestamp=1606833958647&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fnanoid%2Fdownload%2Fnanoid-3.1.12.tgz", "resolved": "https://registry.npm.taobao.org/nanoid/download/nanoid-3.1.12.tgz?cache=0&sync_timestamp=1606833958647&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fnanoid%2Fdownload%2Fnanoid-3.1.12.tgz",
@ -1781,6 +1869,11 @@
"integrity": "sha1-SrzYUq0y3Xuqv+m0DgCjbbXzkuY=", "integrity": "sha1-SrzYUq0y3Xuqv+m0DgCjbbXzkuY=",
"dev": true "dev": true
}, },
"rfdc": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.1.4.tgz",
"integrity": "sha512-5C9HXdzK8EAqN7JDif30jqsBzavB7wLpaubisuQIGHWf2gUXSpzy6ArX/+Da8RjFpagWsCn+pIgxTMAmKw9Zug=="
},
"rimraf": { "rimraf": {
"version": "3.0.2", "version": "3.0.2",
"resolved": "https://registry.npm.taobao.org/rimraf/download/rimraf-3.0.2.tgz?cache=0&sync_timestamp=1599054104695&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Frimraf%2Fdownload%2Frimraf-3.0.2.tgz", "resolved": "https://registry.npm.taobao.org/rimraf/download/rimraf-3.0.2.tgz?cache=0&sync_timestamp=1599054104695&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Frimraf%2Fdownload%2Frimraf-3.0.2.tgz",
@ -1910,6 +2003,23 @@
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
"dev": true "dev": true
}, },
"streamroller": {
"version": "2.2.4",
"resolved": "https://registry.npmjs.org/streamroller/-/streamroller-2.2.4.tgz",
"integrity": "sha512-OG79qm3AujAM9ImoqgWEY1xG4HX+Lw+yY6qZj9R1K2mhF5bEmQ849wvrb+4vt4jLMLzwXttJlQbOdPOQVRv7DQ==",
"requires": {
"date-format": "^2.1.0",
"debug": "^4.1.1",
"fs-extra": "^8.1.0"
},
"dependencies": {
"date-format": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/date-format/-/date-format-2.1.0.tgz",
"integrity": "sha512-bYQuGLeFxhkxNOF3rcMtiZxvCBAquGzZm6oWA1oZ0g2THUzivaRhv8uOhdr19LmoobSOLoIAxeUK2RdbM8IFTA=="
}
}
},
"string-width": { "string-width": {
"version": "4.2.0", "version": "4.2.0",
"resolved": "https://registry.npm.taobao.org/string-width/download/string-width-4.2.0.tgz", "resolved": "https://registry.npm.taobao.org/string-width/download/string-width-4.2.0.tgz",
@ -2051,6 +2161,11 @@
"integrity": "sha1-CeJJ696FHTseSNJ8EFREZn8XuD0=", "integrity": "sha1-CeJJ696FHTseSNJ8EFREZn8XuD0=",
"dev": true "dev": true
}, },
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
},
"uri-js": { "uri-js": {
"version": "4.4.0", "version": "4.4.0",
"resolved": "https://registry.npm.taobao.org/uri-js/download/uri-js-4.4.0.tgz", "resolved": "https://registry.npm.taobao.org/uri-js/download/uri-js-4.4.0.tgz",

View File

@ -11,7 +11,6 @@ export class Certd {
this.options = options this.options = options
this.email = options.cert.email this.email = options.cert.email
this.domains = options.cert.domains this.domains = options.cert.domains
this.domain = this.getMainDomain(options.cert.domains)
if (!(options.store instanceof Store)) { if (!(options.store instanceof Store)) {
this.store = new FileStore(options.store || {}) this.store = new FileStore(options.store || {})
@ -19,33 +18,11 @@ export class Certd {
this.certStore = new CertStore({ this.certStore = new CertStore({
store: this.store, store: this.store,
email: options.cert.email, email: options.cert.email,
domain: this.domain domains: this.domains
}) })
this.acme = new AcmeService(this.store) this.acme = new AcmeService(this.store)
} }
getMainDomain (domains) {
if (domains == null) {
return null
}
if (typeof domains === 'string') {
return domains
}
if (domains.length > 0) {
return domains[0]
}
}
//
// buildDomainFileName (domains) {
// const domain = this.getMainDomain(domains)
// return domain.replace(/\*/g, '_')
// }
//
// buildCertDir (email, domains) {
// const domainFileName = this.buildDomainFileName(domains)
// return path.join(email, '/certs/', domainFileName)
// }
async certApply () { async certApply () {
let oldCert let oldCert
try { try {
@ -114,7 +91,7 @@ export class Certd {
return null return null
} }
const { detail, expires } = this.getCrtDetail(cert.crt) const { detail, expires } = this.getCrtDetail(cert.crt)
const domain = this.getMainDomain(this.options.cert.domains) const domain = this.certStore.getMainDomain(this.options.cert.domains)
return { return {
...cert, detail, expires, domain, domains: this.domains, email: this.email ...cert, detail, expires, domain, domains: this.domains, email: this.email
} }

View File

@ -1,29 +1,47 @@
import dayjs from 'dayjs' import dayjs from 'dayjs'
import crypto from 'crypto'
function md5 (content) {
return crypto.createHash('md5').update(content).digest('hex')
}
export class CertStore { export class CertStore {
constructor ({ store, email, domain }) { constructor ({ store, email, domains }) {
this.store = store this.store = store
this.email = email this.email = email
this.domain = domain this.domains = domains
this.safetyDomain = this.getSafetyDomain(this.domain) this.domain = this.getMainDomain(this.domains)
this.domainDir = this.getSafetyDomain(this.domain) + '-' + md5(this.getDomainStr(this.domains))
this.certsRootPath = this.store.buildKey(this.email, 'certs') this.certsRootPath = this.store.buildKey(this.email, 'certs')
this.currentRootPath = this.store.buildKey(this.certsRootPath, this.safetyDomain, 'current') this.currentRootPath = this.store.buildKey(this.certsRootPath, this.domainDir, 'current')
} }
// getAccountConfig () { getMainDomain (domains) {
// return this.store.get(this.accountConfigKey) if (domains == null) {
// } return null
// }
// setAccountConfig (email, account) { if (typeof domains === 'string') {
// return this.store.set(this.accountConfigKey, account) return domains
// } }
if (domains.length > 0) {
return domains[0]
}
}
getDomainStr (domains) {
if (domains == null) {
return null
}
if (typeof domains === 'string') {
return domains
}
return domains.join(',')
}
buildNewCertRootPath (dir) { buildNewCertRootPath (dir) {
if (dir == null) { if (dir == null) {
dir = dayjs().format('YYYY.MM.DD.HHmmss') dir = dayjs().format('YYYY.MM.DD.HHmmss')
} }
return this.store.buildKey(this.certsRootPath, this.safetyDomain, dir) return this.store.buildKey(this.certsRootPath, this.domainDir, dir)
} }
formatCert (pem) { formatCert (pem) {
@ -36,9 +54,9 @@ export class CertStore {
async writeCert (cert) { async writeCert (cert) {
const newDir = this.buildNewCertRootPath() const newDir = this.buildNewCertRootPath()
const crtKey = this.buildKey(newDir, this.safetyDomain + '.crt') const crtKey = this.buildKey(newDir, this.domainDir + '.crt')
const priKey = this.buildKey(newDir, this.safetyDomain + '.key') const priKey = this.buildKey(newDir, this.domainDir + '.key')
const csrKey = this.buildKey(newDir, this.safetyDomain + '.csr') const csrKey = this.buildKey(newDir, this.domainDir + '.csr')
await this.store.set(crtKey, this.formatCert(cert.crt.toString())) await this.store.set(crtKey, this.formatCert(cert.crt.toString()))
await this.store.set(priKey, this.formatCert(cert.key.toString())) await this.store.set(priKey, this.formatCert(cert.key.toString()))
await this.store.set(csrKey, cert.csr.toString()) await this.store.set(csrKey, cert.csr.toString())
@ -52,9 +70,9 @@ export class CertStore {
if (dir == null) { if (dir == null) {
dir = this.currentRootPath dir = this.currentRootPath
} }
const crtKey = this.buildKey(dir, this.safetyDomain + '.crt') const crtKey = this.buildKey(dir, this.domainDir + '.crt')
const priKey = this.buildKey(dir, this.safetyDomain + '.key') const priKey = this.buildKey(dir, this.domainDir + '.key')
const csrKey = this.buildKey(dir, this.safetyDomain + '.csr') const csrKey = this.buildKey(dir, this.domainDir + '.csr')
const crt = await this.store.get(crtKey) const crt = await this.store.get(crtKey)
if (crt == null) { if (crt == null) {
return null return null

View File

@ -68,7 +68,7 @@ describe('Certd', function () {
const certd = new Certd(options) const certd = new Certd(options)
const currentRootPath = certd.certStore.currentRootPath const currentRootPath = certd.certStore.currentRootPath
console.log('rootDir', currentRootPath) console.log('rootDir', currentRootPath)
expect(currentRootPath).match(/xiaojunnuo@qq.com\\certs\\_.docmirror.club\\current/) expect(currentRootPath).match(/xiaojunnuo@qq.com\\certs\\_.docmirror.club-\w+\\current/)
}) })
it('#writeAndReadCert', async function () { it('#writeAndReadCert', async function () {
const options = createOptions() const options = createOptions()