From dd7ff1fe5b60dbecbf44caf073dc61b03ea34926 Mon Sep 17 00:00:00 2001 From: avwo Date: Fri, 18 Dec 2020 19:16:08 +0800 Subject: [PATCH] feat: download certificate --- biz/webui/cgi-bin/create-cert.js | 19 +++++++------------ lib/https/ca.js | 11 +++++++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/biz/webui/cgi-bin/create-cert.js b/biz/webui/cgi-bin/create-cert.js index a79ca627c..cbacf3f2d 100644 --- a/biz/webui/cgi-bin/create-cert.js +++ b/biz/webui/cgi-bin/create-cert.js @@ -1,3 +1,4 @@ +var Zip = require('node-native-zip2'); var createCertificate = require('../../../lib/https/ca').createCertificate; module.exports = function(req, res) { @@ -6,16 +7,10 @@ module.exports = function(req, res) { if (!domain || domain.length > 64) { return res.status(400); } - try { - var cert = createCertificate(domain); - res.json({ - ec: 0, - cert: cert - }); - } catch (e) { - res.json({ - ec: 2, - em: e.message - }); - } + var cert = createCertificate(domain); + var zip = new Zip(); + var dir = domain + '/' + domain; + zip.add(dir + '.crt', cert.cert); + zip.add(dir + '.key', cert.key); + res.attachment(domain + '.zip').send(zip.toBuffer()); }; diff --git a/lib/https/ca.js b/lib/https/ca.js index 20038e4af..bf7003a50 100644 --- a/lib/https/ca.js +++ b/lib/https/ca.js @@ -27,6 +27,7 @@ var customCertsInfo = {}; var customCertsFiles = {}; var customCertCount = 0; var cachePairs = new LRU({max: 5120}); +var certsCache = new LRU({max: 256}); var ILEGAL_CHAR_RE = /[^a-z\d-]/i; var RANDOM_SERIAL = '.' + Date.now() + '.' + Math.floor(Math.random() * 10000); var PORT_RE = /:\d*$/; @@ -112,7 +113,7 @@ exports.hasCustomCerts = function() { }; function getCertificate(hostname) { - return customPairs[hostname] || cachePairs.get(hostname); + return customPairs[hostname] || cachePairs.get(hostname) || certsCache.get(hostname); } function _createCertificate(hostname) { @@ -159,7 +160,13 @@ function createCertificate(hostname) { } exports.createCertificate = function(hostname) { - return getCertificate(hostname) || _createCertificate(hostname); + hostname = getDomain(hostname); + var cert = getCertificate(hostname); + if (!cert) { + cert = _createCertificate(hostname); + certsCache.set(hostname, cert); + } + return cert; }; function loadCustomCerts(certDir) {