2016-08-15 17:48:47 +08:00
|
|
|
var logUtil = require('./log');
|
|
|
|
var util = require('./util');
|
|
|
|
var color = require('colorful');
|
|
|
|
var EasyCert = require('node-easy-cert');
|
|
|
|
var exec = require('child_process').exec;
|
|
|
|
var path = require('path');
|
|
|
|
var readline = require('readline');
|
|
|
|
|
|
|
|
var isWin = /^win/.test(process.platform);
|
|
|
|
var options = {
|
|
|
|
rootDirPath: util.getUserHome() + '/.anyproxy_certs',
|
|
|
|
defaultCertAttrs: [
|
|
|
|
{ name: 'countryName', value: 'CN' },
|
|
|
|
{ name: 'organizationName', value: 'AnyProxy' },
|
|
|
|
{ shortName: 'ST', value: 'SH' },
|
|
|
|
{ shortName: 'OU', value: 'AnyProxy SSL Proxy' }
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
var easyCert = new EasyCert(options);
|
|
|
|
var crtMgr = util.merge({}, easyCert);
|
|
|
|
|
|
|
|
// catch specified error, such as ROOT_CA_NOT_EXISTS
|
|
|
|
crtMgr.getCertificate = function (host, cb) {
|
|
|
|
easyCert.getCertificate(host, (error, keyContent, crtContent) => {
|
|
|
|
if (error === 'ROOT_CA_NOT_EXISTS') {
|
|
|
|
util.showRootInstallTip();
|
|
|
|
process.exit(0);
|
|
|
|
return;
|
2015-02-10 12:03:21 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 17:48:47 +08:00
|
|
|
cb(error, keyContent, crtContent);
|
2016-05-02 00:18:35 +08:00
|
|
|
});
|
2016-08-15 17:48:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// set default common name of the cert
|
|
|
|
crtMgr.generateRootCA = function (cb) {
|
|
|
|
doGenerate(false);
|
|
|
|
|
|
|
|
function doGenerate(overwrite) {
|
|
|
|
const rootOptions = {
|
|
|
|
commonName: 'AnyProxy',
|
|
|
|
overwrite: !!overwrite
|
|
|
|
};
|
|
|
|
|
|
|
|
easyCert.generateRootCA(rootOptions, (error, keyPath, crtPath) => {
|
|
|
|
if (!error) {
|
|
|
|
const certDir = path.dirname(keyPath);
|
|
|
|
logUtil.printLog(color.cyan('The cert is generated at "' + certDir + '"'));
|
|
|
|
if(isWin){
|
|
|
|
exec("start .",{ cwd : certDir });
|
|
|
|
}else{
|
|
|
|
exec("open .",{ cwd : certDir });
|
|
|
|
}
|
2014-08-14 10:59:49 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 17:48:47 +08:00
|
|
|
if (error === 'ROOT_CA_EXISTED') {
|
|
|
|
var rl = readline.createInterface({
|
|
|
|
input : process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
2016-05-02 00:18:35 +08:00
|
|
|
|
2016-08-15 17:48:47 +08:00
|
|
|
rl.question("do you really want to generate a new one ?)(yes/NO)", function(answer) {
|
|
|
|
if(/yes/i.test(answer)){
|
|
|
|
doGenerate(true);
|
|
|
|
}else{
|
|
|
|
console.log("will not generate a new one");
|
2016-05-02 00:18:35 +08:00
|
|
|
|
2016-08-15 17:48:47 +08:00
|
|
|
}
|
|
|
|
rl.close();
|
|
|
|
});
|
2016-05-02 00:18:35 +08:00
|
|
|
|
2016-08-15 17:48:47 +08:00
|
|
|
return;
|
2016-05-02 00:18:35 +08:00
|
|
|
}
|
2016-08-15 17:48:47 +08:00
|
|
|
cb(error, keyPath, crtPath);
|
2014-08-14 10:59:49 +08:00
|
|
|
});
|
|
|
|
}
|
2014-08-13 11:51:39 +08:00
|
|
|
|
2016-08-15 17:48:47 +08:00
|
|
|
};
|
2014-11-06 13:55:43 +08:00
|
|
|
|
2016-08-15 17:48:47 +08:00
|
|
|
module.exports = crtMgr;
|