From cb813f8cb5aed3415be799f27057f8febdf969dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=A0=E9=87=8C?= Date: Fri, 12 Sep 2014 10:28:39 +0800 Subject: [PATCH] fix cert issue --- lib/certMgr.js | 53 ++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/lib/certMgr.js b/lib/certMgr.js index f078ad7..4ff55c7 100644 --- a/lib/certMgr.js +++ b/lib/certMgr.js @@ -8,21 +8,19 @@ var exec = require('child_process').exec, util = require('./util'), asyncTask = require("async-task-mgr"); +//TODO : move root cert from cmd to cert var certDir = path.join(util.getUserHome(),"/.anyproxy_certs/"), cmdDir = path.join(__dirname,"..","./cert/"), + cmd_genRoot = path.join(cmdDir,"./gen-rootCA"), + cmd_genCert = path.join(cmdDir,"./gen-cer"), asyncTaskMgr = new asyncTask(); -try{ - if(!fs.existsSync(certDir)){ - fs.mkdirSync(certDir,0777); - }else{ - fs.chmodSync(certDir,0777); - } -}catch(e){ - console.log(e); +if(!fs.existsSync(certDir)){ + fs.mkdirSync(certDir,0777); } + function getCertificate(hostname,cb){ var keyFile = path.join(certDir , "__hostname.key".replace(/__hostname/,hostname) ), crtFile = path.join(certDir , "__hostname.crt".replace(/__hostname/,hostname) ); @@ -48,14 +46,11 @@ function getCertificate(hostname,cb){ function createCert(hostname,callback){ checkRootCA(); - var cmd = "./gen-cer __host __path".replace(/__host/,hostname).replace(/__path/,certDir); - console.log(cmd); - console.log(cmdDir); - exec(cmd,{ cwd : cmdDir },function(err,stdout,stderr){ + var cmd = cmd_genCert + " __host __path".replace(/__host/,hostname).replace(/__path/,certDir); + exec(cmd,{ cwd : certDir },function(err,stdout,stderr){ if(err){ callback && callback(new Error("error when generating certificate"),null); }else{ - console.log(stdout); var tipText = "certificate created for __HOST".replace(/__HOST/,hostname); console.log(color.yellow(color.bold("[internal https]")) + color.yellow(tipText)); callback(null); @@ -68,8 +63,8 @@ function clearCerts(cb){ } function isRootCAFileExists(){ - var crtFile = path.join(cmdDir,"rootCA.crt"), - keyFile = path.join(cmdDir,"rootCA.key"); + var crtFile = path.join(certDir,"rootCA.crt"), + keyFile = path.join(certDir,"rootCA.key"); return (fs.existsSync(crtFile) && fs.existsSync(keyFile)); } @@ -86,9 +81,9 @@ function checkRootCA(){ function generateRootCA(){ if(isRootCAFileExists()){ - console.log(color.yellow("rootCA exists at " + cmdDir)); + console.log(color.yellow("rootCA exists at " + certDir)); var rl = readline.createInterface({ - input: process.stdin, + input : process.stdin, output: process.stdout }); @@ -107,19 +102,21 @@ function generateRootCA(){ } function startGenerating(){ - var spawnSteam = spawn("./gen-rootCA",['.'],{cwd:cmdDir,stdio: 'inherit'}); + //clear old certs + clearCerts(function(){ + console.log(color.green("temp certs cleared")); - spawnSteam.on('close', function (code) { - if(code == 0){ - console.log(color.green("rootCA generated")); - console.log(color.green(color.bold("please trust the rootCA.crt in " + cmdDir))); - clearCerts(function(){ - console.log(color.green("temp certs cleared")); + var spawnSteam = spawn(cmd_genRoot,['.'],{cwd:certDir,stdio: 'inherit'}); + spawnSteam.on('close', function (code) { + if(code == 0){ + console.log(color.green("rootCA generated")); + console.log(color.green(color.bold("please trust the rootCA.crt in " + certDir))); process.exit(0); - }); - }else{ - console.log(color.red("fail to generate root CA")); - } + }else{ + console.log(color.red("fail to generate root CA")); + } + }); + }); } }