fix cert issue

This commit is contained in:
加里 2014-09-12 10:28:39 +08:00
parent a2b231183e
commit cb813f8cb5

View File

@ -8,20 +8,18 @@ var exec = require('child_process').exec,
util = require('./util'), util = require('./util'),
asyncTask = require("async-task-mgr"); asyncTask = require("async-task-mgr");
//TODO : move root cert from cmd to cert
var certDir = path.join(util.getUserHome(),"/.anyproxy_certs/"), var certDir = path.join(util.getUserHome(),"/.anyproxy_certs/"),
cmdDir = path.join(__dirname,"..","./cert/"), cmdDir = path.join(__dirname,"..","./cert/"),
cmd_genRoot = path.join(cmdDir,"./gen-rootCA"),
cmd_genCert = path.join(cmdDir,"./gen-cer"),
asyncTaskMgr = new asyncTask(); asyncTaskMgr = new asyncTask();
try{
if(!fs.existsSync(certDir)){ if(!fs.existsSync(certDir)){
fs.mkdirSync(certDir,0777); fs.mkdirSync(certDir,0777);
}else{
fs.chmodSync(certDir,0777);
} }
}catch(e){
console.log(e);
}
function getCertificate(hostname,cb){ function getCertificate(hostname,cb){
var keyFile = path.join(certDir , "__hostname.key".replace(/__hostname/,hostname) ), var keyFile = path.join(certDir , "__hostname.key".replace(/__hostname/,hostname) ),
@ -48,14 +46,11 @@ function getCertificate(hostname,cb){
function createCert(hostname,callback){ function createCert(hostname,callback){
checkRootCA(); checkRootCA();
var cmd = "./gen-cer __host __path".replace(/__host/,hostname).replace(/__path/,certDir); var cmd = cmd_genCert + " __host __path".replace(/__host/,hostname).replace(/__path/,certDir);
console.log(cmd); exec(cmd,{ cwd : certDir },function(err,stdout,stderr){
console.log(cmdDir);
exec(cmd,{ cwd : cmdDir },function(err,stdout,stderr){
if(err){ if(err){
callback && callback(new Error("error when generating certificate"),null); callback && callback(new Error("error when generating certificate"),null);
}else{ }else{
console.log(stdout);
var tipText = "certificate created for __HOST".replace(/__HOST/,hostname); var tipText = "certificate created for __HOST".replace(/__HOST/,hostname);
console.log(color.yellow(color.bold("[internal https]")) + color.yellow(tipText)); console.log(color.yellow(color.bold("[internal https]")) + color.yellow(tipText));
callback(null); callback(null);
@ -68,8 +63,8 @@ function clearCerts(cb){
} }
function isRootCAFileExists(){ function isRootCAFileExists(){
var crtFile = path.join(cmdDir,"rootCA.crt"), var crtFile = path.join(certDir,"rootCA.crt"),
keyFile = path.join(cmdDir,"rootCA.key"); keyFile = path.join(certDir,"rootCA.key");
return (fs.existsSync(crtFile) && fs.existsSync(keyFile)); return (fs.existsSync(crtFile) && fs.existsSync(keyFile));
} }
@ -86,7 +81,7 @@ function checkRootCA(){
function generateRootCA(){ function generateRootCA(){
if(isRootCAFileExists()){ if(isRootCAFileExists()){
console.log(color.yellow("rootCA exists at " + cmdDir)); console.log(color.yellow("rootCA exists at " + certDir));
var rl = readline.createInterface({ var rl = readline.createInterface({
input : process.stdin, input : process.stdin,
output: process.stdout output: process.stdout
@ -107,20 +102,22 @@ function generateRootCA(){
} }
function startGenerating(){ function startGenerating(){
var spawnSteam = spawn("./gen-rootCA",['.'],{cwd:cmdDir,stdio: 'inherit'}); //clear old certs
clearCerts(function(){
console.log(color.green("temp certs cleared"));
var spawnSteam = spawn(cmd_genRoot,['.'],{cwd:certDir,stdio: 'inherit'});
spawnSteam.on('close', function (code) { spawnSteam.on('close', function (code) {
if(code == 0){ if(code == 0){
console.log(color.green("rootCA generated")); console.log(color.green("rootCA generated"));
console.log(color.green(color.bold("please trust the rootCA.crt in " + cmdDir))); console.log(color.green(color.bold("please trust the rootCA.crt in " + certDir)));
clearCerts(function(){
console.log(color.green("temp certs cleared"));
process.exit(0); process.exit(0);
});
}else{ }else{
console.log(color.red("fail to generate root CA")); console.log(color.red("fail to generate root CA"));
} }
}); });
});
} }
} }