smartcert/ts/cert.hook.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

2016-06-22 11:22:09 +00:00
#!/usr/bin/env node
// the shebang line above makes sure this script will get interpreted by node
2016-06-21 19:08:04 +00:00
2016-06-22 11:22:09 +00:00
import * as plugins from "./cert.plugins";
import * as paths from "./cert.paths";
let smartcli = new plugins.smartcli.Smartcli();
2016-06-28 03:53:49 +00:00
let config = plugins.smartfile.fs.toObjectSync(paths.config);
2016-06-22 11:22:09 +00:00
let cflare = new plugins.cflare.CflareAccount();
cflare.auth({
email: config.cfEmail,
key: config.cfKey
});
let setChallenge = (domainNameArg: string, challengeArg: string) => {
let done = plugins.q.defer();
2016-06-28 03:53:49 +00:00
plugins.beautylog.log("setting challenge for " + domainNameArg);
2016-06-22 11:22:09 +00:00
cflare.createRecord(prefixName(domainNameArg), "TXT", challengeArg).then(() => {
2016-06-28 03:53:49 +00:00
plugins.beautylog.ok("Challenge has been set!");
plugins.beautylog.info("We need to cool down to let DNS propagate to edge locations!");
2016-06-22 11:22:09 +00:00
cooldown().then(() => {
done.resolve();
});
});
return done.promise;
}
let cleanChallenge = (domainNameArg) => {
let done = plugins.q.defer();
2016-06-28 03:53:49 +00:00
plugins.beautylog.log("cleaning challenge for " + domainNameArg);
2016-06-22 11:22:09 +00:00
cflare.removeRecord(prefixName(domainNameArg), "TXT");
2016-07-24 17:56:32 +00:00
cooldown().then(() => {
done.resolve();
});
2016-06-22 11:22:09 +00:00
return done.promise;
}
let cooldown = () => {
let done = plugins.q.defer();
2016-07-24 17:56:32 +00:00
let cooldowntime = 60000;
2016-06-28 03:53:49 +00:00
let passedTime = 0;
plugins.beautylog.log("Cooling down! " + (cooldowntime/1000).toString() + " seconds left");
let coolDownCounter = () => {
setTimeout(() => {
if(cooldowntime <= passedTime){
plugins.beautylog.ok("Cooled down!");
done.resolve();
} else {
passedTime = passedTime + 5000;
plugins.beautylog.log("Cooling down! " + ((cooldowntime - passedTime)/1000).toString() + " seconds left");
coolDownCounter();
}
}, 5000);
}
coolDownCounter();
2016-06-22 11:22:09 +00:00
return done.promise;
}
let prefixName = (domainNameArg: string): string => {
return "_acme-challenge." + domainNameArg;
}
smartcli.addCommand({
commandName: "deploy_challenge"
}).then((argv) => {
setChallenge(argv._[1], argv._[3]);
});
smartcli.addCommand({
commandName: "clean_challenge"
}).then((argv) => {
cleanChallenge(argv._[1]);
});
smartcli.startParse();