smartcert/dist/index.js
2016-07-04 08:41:58 +02:00

122 lines
11 KiB
JavaScript

"use strict";
var plugins = require("./cert.plugins");
var paths = require("./cert.paths");
var Cert = (function () {
function Cert(optionsArg) {
var _this = this;
this.sslGitOriginPull = function () {
if (_this._gitOriginRepo) {
plugins.smartgit.pull(_this._sslDir, "origin", "master");
}
};
this.sslGitOriginAddCommitPush = function () {
if (_this._gitOriginRepo) {
plugins.smartgit.add.addAll(_this._sslDir);
plugins.smartgit.commit(_this._sslDir, "added new SSL certificates and deleted obsolete ones.");
plugins.smartgit.push(_this._sslDir, "origin", "master");
}
};
this._cfEmail = optionsArg.cfEmail;
this._cfKey = optionsArg.cfKey;
this._sslDir = optionsArg.sslDir;
this._gitOriginRepo = optionsArg.gitOriginRepo;
this._testMode = optionsArg.testMode;
// write hook config
var config = {
cfEmail: this._cfEmail,
cfKey: this._cfKey
};
plugins.smartfile.memory.toFsSync(JSON.stringify(config), plugins.path.join(__dirname, "assets/config.json"));
// setup Git
if (this._gitOriginRepo) {
plugins.smartgit.init(this._sslDir);
plugins.smartgit.remote.add(this._sslDir, "origin", this._gitOriginRepo);
this.sslGitOriginPull();
}
// setup leSh config;
var leShConfigString;
if (this._testMode) {
leShConfigString = "CA=\"https://acme-staging.api.letsencrypt.org/directory\"\n";
}
else {
leShConfigString = " ";
}
;
plugins.smartfile.memory.toFsSync(leShConfigString, paths.leShConfig);
}
;
Cert.prototype.getDomainCert = function (domainNameArg, optionsArg) {
if (optionsArg === void 0) { optionsArg = { force: false }; }
var done = plugins.q.defer();
this.sslGitOriginPull();
if (!checkDomainsStillValid(domainNameArg, this._sslDir) || optionsArg.force) {
plugins.shelljs.exec("chmod 700 " + paths.letsencryptSh);
plugins.shelljs.exec("chmod 700 " + paths.certHook);
plugins.smartfile.fs.ensureDir(paths.certDir);
plugins.shelljs.exec("bash -c \"" + paths.letsencryptSh + " -c -f " + paths.leShConfig + " -d " + domainNameArg + " -t dns-01 -k " + paths.certHook + " -o " + paths.certDir + "\"");
var fetchedCertsArray = plugins.smartfile.fs.listFoldersSync(paths.certDir);
if (fetchedCertsArray.indexOf(domainNameArg) != -1) {
updateSslDirSync(this._sslDir, domainNameArg);
}
this.sslGitOriginAddCommitPush();
done.resolve();
}
else {
plugins.beautylog.info("certificate for " + domainNameArg + " is still valid! Not fetching new one!");
done.resolve();
}
;
return done.promise;
};
;
Cert.prototype.cleanOldCertificates = function () {
};
;
return Cert;
}());
exports.Cert = Cert;
var Certificate = (function () {
function Certificate() {
}
;
return Certificate;
}());
exports.Certificate = Certificate;
var checkDomainsStillValid = function (domainNameArg, sslDirArg) {
var domainConfigPath = plugins.path.join(sslDirArg, domainNameArg, "config.json");
if (plugins.smartfile.fs.fileExistsSync(domainConfigPath)) {
var domainConfig = plugins.smartfile.fs.toObjectSync(domainConfigPath, "json");
if (Date.now() >= ((domainConfig.expires - 604800) * 1000)) {
return false;
}
else {
return true;
}
}
else {
return false;
}
};
var updateSslDirSync = function (sslDirArg, domainNameArg) {
plugins.smartfile.fs.ensureDirSync(sslDirArg);
var domainCertFolder = plugins.path.join(paths.certDir, domainNameArg);
if (plugins.smartfile.fs.listFoldersSync(paths.certDir).indexOf(domainNameArg) != -1) {
plugins.smartfile.fs.copySync(plugins.path.join(domainCertFolder, "fullchain.pem"), plugins.path.join(sslDirArg, domainNameArg, "fullchain.pem"));
plugins.smartfile.fs.copySync(plugins.path.join(domainCertFolder, "privkey.pem"), plugins.path.join(sslDirArg, domainNameArg, "privkey.pem"));
// create cert config
var certRegex = /.*\-([0-9]*)\.pem/;
var certFileNameWithTime = plugins.smartfile.fs.listFilesSync(domainCertFolder, certRegex)[0];
var certTime = parseInt(certRegex.exec(certFileNameWithTime)[1]);
var certConfig = {
domainName: domainNameArg,
created: certTime,
expires: certTime + 7776000
};
plugins.smartfile.memory.toFsSync(JSON.stringify(certConfig), plugins.path.join(sslDirArg, domainNameArg, "config.json"));
}
;
};
var updateGitOrigin = function (syncDirectionArg) {
};
updateGitOrigin(0 /* to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