greenlock.js-ARCHIVED/lib/common.js

119 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-12-15 15:21:27 +00:00
'use strict';
var fs = require('fs');
2015-12-19 19:59:50 +00:00
var path = require('path');
2015-12-15 15:21:27 +00:00
var PromiseA = require('bluebird');
2015-12-19 19:59:50 +00:00
var homeRe = new RegExp("^~(\\/|\\\|\\" + path.sep + ")");
2015-12-19 10:18:32 +00:00
var re = /^[a-zA-Z0-9\.\-]+$/;
var punycode = require('punycode');
2015-12-19 19:59:50 +00:00
module.exports.isValidDomain = function (domain) {
2015-12-19 10:18:32 +00:00
if (re.test(domain)) {
return domain;
}
domain = punycode.toASCII(domain);
if (re.test(domain)) {
return domain;
}
return '';
};
2015-12-17 08:46:40 +00:00
module.exports.tplConfigDir = function merge(configDir, defaults) {
2015-12-19 19:59:50 +00:00
var homedir = require('homedir')();
2015-12-17 08:46:40 +00:00
Object.keys(defaults).forEach(function (key) {
if ('string' === typeof defaults[key]) {
defaults[key] = defaults[key].replace(':config', configDir).replace(':conf', configDir);
2015-12-19 19:59:50 +00:00
defaults[key] = defaults[key].replace(homeRe, homedir + path.sep);
2015-12-17 08:46:40 +00:00
}
});
};
2015-12-17 08:46:40 +00:00
module.exports.merge = function merge(defaults, args) {
var copy = {};
Object.keys(defaults).forEach(function (key) {
copy[key] = defaults[key];
});
Object.keys(args).forEach(function (key) {
copy[key] = args[key];
});
return copy;
};
module.exports.tplCopy = function merge(copy) {
2015-12-19 19:59:50 +00:00
var homedir = require('homedir')();
var tpls = {
hostname: (copy.domains || [])[0]
, server: (copy.server || '').replace('https://', '').replace(/(\/)$/, '')
, conf: copy.configDir
, config: copy.configDir
};
2015-12-17 08:46:40 +00:00
Object.keys(copy).forEach(function (key) {
if ('string' === typeof copy[key]) {
Object.keys(tpls).sort(function (a, b) {
return b.length - a.length;
}).forEach(function (tplname) {
2015-12-20 10:41:17 +00:00
if (!tpls[tplname]) {
// what can't be templated now may be templatable later
return;
}
copy[key] = copy[key].replace(':' + tplname, tpls[tplname]);
copy[key] = copy[key].replace(homeRe, homedir + path.sep);
});
2015-12-17 08:46:40 +00:00
}
});
//return copy;
};
module.exports.fetchFromDisk = function (args) {
// TODO NO HARD-CODED DEFAULTS
if (!args.fullchainPath || !args.privkeyPath || !args.certPath || !args.chainPath) {
console.warn("missing one or more of args.privkeyPath, args.fullchainPath, args.certPath, args.chainPath");
console.warn("hard-coded conventional pathnames were for debugging and are not a stable part of the API");
}
2015-12-15 15:21:27 +00:00
//, fs.readFileAsync(fullchainPath, 'ascii')
2015-12-17 08:46:40 +00:00
// note: if this ^^ gets added back in, the arrays below must change
return PromiseA.all([
fs.readFileAsync(args.privkeyPath, 'ascii') // 0
, fs.readFileAsync(args.certPath, 'ascii') // 1
, fs.readFileAsync(args.chainPath, 'ascii') // 2
2015-12-15 15:21:27 +00:00
// stat the file, not the link
2015-12-17 08:46:40 +00:00
, fs.statAsync(args.certPath) // 3
2015-12-15 15:21:27 +00:00
]).then(function (arr) {
2015-12-17 08:46:40 +00:00
2015-12-15 15:21:27 +00:00
return {
key: arr[0] // privkey.pem
2015-12-17 08:46:40 +00:00
, privkey: arr[0] // privkey.pem
, fullchain: arr[1] + '\n' + arr[2] // fullchain.pem
, cert: arr[1] // cert.pem
2015-12-17 08:46:40 +00:00
, chain: arr[2] // chain.pem
2015-12-17 08:46:40 +00:00
, ca: arr[2] // chain.pem
2015-12-17 09:05:18 +00:00
, privkeyPath: args.privkeyPath
, fullchainPath: args.fullchainPath
, certPath: args.certPath
, chainPath: args.chainPath
2015-12-17 08:46:40 +00:00
, issuedAt: arr[3].mtime.valueOf() // ??? TODO parse to determine expiresAt and lifetime
2015-12-17 09:05:18 +00:00
, lifetime: args.lifetime
2015-12-15 15:21:27 +00:00
};
2015-12-17 08:46:40 +00:00
}, function (err) {
2015-12-17 09:17:27 +00:00
if (args.debug) {
2016-02-10 20:41:15 +00:00
console.error("[letsencrypt/lib/common.js] fetchFromDisk");
2015-12-17 08:46:40 +00:00
console.error(err.stack);
}
2015-12-15 15:21:27 +00:00
return null;
});
};