now requesting in parallel
This commit is contained in:
18
ts/cert.helpers.ts
Normal file
18
ts/cert.helpers.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import * as plugins from "./cert.plugins";
|
||||
import * as paths from "./cert.paths";
|
||||
|
||||
let firstCall = true;
|
||||
let accountKeyPresent = true;
|
||||
export let accountsKeyPresent = () => {
|
||||
let done = plugins.q.defer();
|
||||
if (firstCall) {
|
||||
done.resolve();
|
||||
firstCall = false;
|
||||
setTimeout(() => {accountKeyPresent = true},5000);
|
||||
} else if(accountKeyPresent){
|
||||
done.resolve()
|
||||
} else {
|
||||
setTimeout(done.resolve, 5000);
|
||||
};
|
||||
return done.promise;
|
||||
};
|
@@ -1,12 +1,14 @@
|
||||
import * as plugins from "./cert.plugins";
|
||||
|
||||
//dirs
|
||||
export let certDir = plugins.path.join(__dirname,"assets/certs");
|
||||
export let defaultSslDir = plugins.path.join(__dirname,"assets/defaultSslDir");
|
||||
export let assetDir = plugins.path.join(__dirname,"assets/");
|
||||
export let accountsDir = plugins.path.join(__dirname,"assets/accounts/");
|
||||
|
||||
// files
|
||||
export let certHook = plugins.path.join(__dirname,"cert.hook.js");
|
||||
export let config = plugins.path.join(__dirname,"assets/config.json");
|
||||
export let leShConfig = plugins.path.join(__dirname,"assets/leshconfig.json");
|
||||
export let letsencryptSh = plugins.path.join(__dirname,"assets/letsencrypt.sh");
|
||||
|
||||
//dirs
|
||||
export let certDir = plugins.path.join(__dirname,"assets/certs");
|
||||
export let defaultSslDir = plugins.path.join(__dirname,"assets/defaultSslDir");
|
||||
export let assetDir = plugins.path.join(__dirname,"assets/");
|
@@ -2,6 +2,7 @@ import "typings-global";
|
||||
export import beautylog = require("beautylog");
|
||||
export import cflare = require("cflare");
|
||||
export let fs = require("fs-extra");
|
||||
export import lik = require("lik");
|
||||
export import path = require("path");
|
||||
export import q = require("q");
|
||||
export import shelljs = require("shelljs");
|
||||
|
69
ts/index.ts
69
ts/index.ts
@@ -1,5 +1,6 @@
|
||||
import * as plugins from "./cert.plugins";
|
||||
import * as paths from "./cert.paths";
|
||||
import * as helpers from "./cert.helpers"
|
||||
|
||||
export interface ICertConstructorOptions {
|
||||
cfEmail: string,
|
||||
@@ -7,7 +8,7 @@ export interface ICertConstructorOptions {
|
||||
sslDir?: string,
|
||||
gitOriginRepo?: string,
|
||||
testMode?: boolean
|
||||
};
|
||||
};
|
||||
|
||||
export class Cert {
|
||||
private _cfEmail: string;
|
||||
@@ -15,14 +16,14 @@ export class Cert {
|
||||
private _sslDir: string;
|
||||
private _gitOriginRepo: string;
|
||||
private _testMode: boolean;
|
||||
domainsCurrentlyRequesting: string[] = [];
|
||||
domainsCurrentlyRequesting: plugins.lik.Stringmap = new plugins.lik.Stringmap();
|
||||
certificatesPresent: Certificate[];
|
||||
certificatesValid: Certificate[];
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for Cert object
|
||||
*/
|
||||
constructor(optionsArg:ICertConstructorOptions) {
|
||||
constructor(optionsArg: ICertConstructorOptions) {
|
||||
this._cfEmail = optionsArg.cfEmail;
|
||||
this._cfKey = optionsArg.cfKey;
|
||||
this._sslDir = optionsArg.sslDir;
|
||||
@@ -58,7 +59,6 @@ export class Cert {
|
||||
);
|
||||
plugins.shelljs.exec("chmod 700 " + paths.letsencryptSh);
|
||||
plugins.shelljs.exec("chmod 700 " + paths.certHook);
|
||||
plugins.shelljs.exec(`bash -c "${paths.letsencryptSh}`);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -86,29 +86,40 @@ export class Cert {
|
||||
*/
|
||||
getDomainCert(domainNameArg: string, optionsArg: { force: boolean } = { force: false }) {
|
||||
let done = plugins.q.defer();
|
||||
if (!checkDomainsStillValid(domainNameArg, this._sslDir) || optionsArg.force) {
|
||||
plugins.smartfile.fs.ensureDir(paths.certDir);
|
||||
plugins.beautylog.info(`getting cert for ${domainNameArg}`);
|
||||
plugins.shelljs.exec(
|
||||
`bash -c "${paths.letsencryptSh} -c --no-lock -f ${paths.leShConfig} -d ${domainNameArg} -t dns-01 -k ${paths.certHook} -o ${paths.certDir}"`,
|
||||
{
|
||||
silent: true,
|
||||
async:true
|
||||
},
|
||||
(codeArg, stdoutArg) => {
|
||||
console.log(stdoutArg);
|
||||
let fetchedCertsArray: string[] = plugins.smartfile.fs.listFoldersSync(paths.certDir);
|
||||
if (fetchedCertsArray.indexOf(domainNameArg) != -1) {
|
||||
updateSslDirSync(this._sslDir, domainNameArg);
|
||||
plugins.smartfile.fs.removeSync(plugins.path.join(paths.certDir,domainNameArg));
|
||||
}
|
||||
// make sure no one else requires the same domain at the same time
|
||||
helpers.accountsKeyPresent().then(() => {
|
||||
if (!this.domainsCurrentlyRequesting.checkString(domainNameArg)) {
|
||||
this.domainsCurrentlyRequesting.addString(domainNameArg);
|
||||
if (!checkDomainsStillValid(domainNameArg, this._sslDir) || optionsArg.force) {
|
||||
plugins.smartfile.fs.ensureDir(paths.certDir);
|
||||
plugins.beautylog.info(`getting cert for ${domainNameArg}`);
|
||||
plugins.shelljs.exec(
|
||||
`bash -c "${paths.letsencryptSh} -c --no-lock -f ${paths.leShConfig} -d ${domainNameArg} -t dns-01 -k ${paths.certHook} -o ${paths.certDir}"`,
|
||||
{
|
||||
silent: true,
|
||||
async: true
|
||||
},
|
||||
(codeArg, stdoutArg) => {
|
||||
console.log(stdoutArg);
|
||||
let fetchedCertsArray: string[] = plugins.smartfile.fs.listFoldersSync(paths.certDir);
|
||||
if (fetchedCertsArray.indexOf(domainNameArg) != -1) {
|
||||
updateSslDirSync(this._sslDir, domainNameArg);
|
||||
plugins.smartfile.fs.removeSync(plugins.path.join(paths.certDir, domainNameArg));
|
||||
}
|
||||
this.domainsCurrentlyRequesting.removeString(domainNameArg);
|
||||
done.resolve();
|
||||
}
|
||||
);
|
||||
} else {
|
||||
plugins.beautylog.info("certificate for " + domainNameArg + " is still valid! Not fetching new one!");
|
||||
this.domainsCurrentlyRequesting.removeString(domainNameArg);
|
||||
done.resolve();
|
||||
}
|
||||
);
|
||||
} else {
|
||||
plugins.beautylog.info("certificate for " + domainNameArg + " is still valid! Not fetching new one!");
|
||||
done.resolve();
|
||||
};
|
||||
};
|
||||
} else {
|
||||
plugins.beautylog.warn(`${domainNameArg} is already requesting`);
|
||||
};
|
||||
});
|
||||
|
||||
return done.promise;
|
||||
};
|
||||
cleanOldCertificates() {
|
||||
@@ -132,8 +143,8 @@ interface certConfig {
|
||||
}
|
||||
|
||||
let checkDomainsStillValid = (domainNameArg: string, sslDirArg: string): boolean => {
|
||||
let domainConfigPath = plugins.path.join(sslDirArg, domainNameArg,"config.json");
|
||||
if (plugins.smartfile.fs.fileExistsSync(domainConfigPath)){
|
||||
let domainConfigPath = plugins.path.join(sslDirArg, domainNameArg, "config.json");
|
||||
if (plugins.smartfile.fs.fileExistsSync(domainConfigPath)) {
|
||||
let domainConfig = plugins.smartfile.fs.toObjectSync(
|
||||
domainConfigPath,
|
||||
"json"
|
||||
|
Reference in New Issue
Block a user