smartcert/ts/index.ts

95 lines
3.3 KiB
TypeScript
Raw Normal View History

2016-06-22 11:22:09 +00:00
import * as plugins from "./cert.plugins";
2016-06-18 13:59:03 +00:00
import * as paths from "./cert.paths";
export class Cert {
2016-06-28 07:32:01 +00:00
private _cfEmail: string;
private _cfKey: string;
private _sslDir: string;
2016-06-23 02:37:18 +00:00
certificatesPresent:Certificate[];
certificatesValid:Certificate[];
2016-06-18 13:59:03 +00:00
gitOriginRepo;
2016-06-23 01:46:37 +00:00
constructor(optionsArg: {
cfEmail: string,
cfKey: string,
sslDir: string,
gitOriginRepo?: string
}) {
2016-06-28 07:32:01 +00:00
this._cfEmail = optionsArg.cfEmail;
this._cfKey = optionsArg.cfKey;
this._sslDir = optionsArg.sslDir;
2016-06-18 13:59:03 +00:00
this.gitOriginRepo = optionsArg.gitOriginRepo;
2016-06-22 11:22:09 +00:00
let config = {
2016-06-28 07:32:01 +00:00
cfEmail: this._cfEmail,
cfKey: this._cfKey
2016-06-22 11:22:09 +00:00
}
2016-06-28 03:53:49 +00:00
plugins.smartfile.memory.toFsSync(JSON.stringify(config),plugins.path.join(__dirname, "assets/config.json"));
2016-06-22 11:22:09 +00:00
};
2016-06-28 03:53:49 +00:00
getDomainCert(domainNameArg: string,optionsArg?:{force:boolean}) {
2016-06-22 11:22:09 +00:00
let done = plugins.q.defer();
2016-06-28 07:32:01 +00:00
if (!checkDomainsStillValid(domainNameArg) || optionsArg.force) {
2016-06-23 01:46:37 +00:00
plugins.shelljs.exec("chmod 700 " + paths.letsencryptSh);
plugins.shelljs.exec("chmod 700 " + paths.certHook);
2016-06-28 03:53:49 +00:00
plugins.shelljs.exec("bash -c \"" + paths.letsencryptSh + " -c -d " + domainNameArg + " -t dns-01 -k " + paths.certHook + " -o " + paths.certDir + "\"");
let fetchedCertsArray:string[] = plugins.smartfile.fs.listFoldersSync(paths.certDir);
if(fetchedCertsArray.indexOf(domainNameArg) != -1){
2016-06-28 07:32:01 +00:00
updateSslDirSync(this._sslDir,domainNameArg);
2016-06-28 03:53:49 +00:00
}
2016-06-23 01:46:37 +00:00
done.resolve();
} else {
plugins.beautylog.info("certificate for " + domainNameArg + " is still valid! Not fetching new one!");
done.resolve();
}
2016-06-22 11:22:09 +00:00
return done.promise;
2016-06-18 13:59:03 +00:00
};
}
2016-06-28 03:53:49 +00:00
export class Certificate {
2016-06-23 01:46:37 +00:00
domainName: string;
creationDate: Date;
expiryDate: Date;
constructor() {
2016-06-18 13:59:03 +00:00
};
}
2016-06-28 07:32:01 +00:00
interface certConfig {
domainName:string;
created:number;
expires:number;
}
let checkDomainsStillValid = (domainNameArg: string): boolean => {
2016-06-23 01:46:37 +00:00
return false;
}
2016-06-28 07:32:01 +00:00
let updateSslDirSync = (sslDirArg:string,domainNameArg:string) => {
plugins.smartfile.fs.ensureDirSync(sslDirArg);
let 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
let certRegex = /.*\-([0-9]*)\.pem/;
let certFileNameWithTime:string = plugins.smartfile.fs.listFilesSync(domainCertFolder,certRegex)[0];
2016-06-28 07:32:01 +00:00
let certTime = parseInt(certRegex.exec(certFileNameWithTime)[1]);
let certConfig:certConfig = {
domainName: domainNameArg,
created: certTime,
expires: certTime + 7776000
};
plugins.smartfile.memory.toFs(
JSON.stringify(certConfig),
plugins.path.join(sslDirArg,domainNameArg,"config.json")
);
};
}
let updateGitOrigin = () => {
2016-06-23 01:46:37 +00:00
2016-06-18 13:59:03 +00:00
}