mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 07:01:25 +00:00
fix cert issue
This commit is contained in:
parent
a2b231183e
commit
cb813f8cb5
@ -8,20 +8,18 @@ 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);
|
||||
}
|
||||
|
||||
function getCertificate(hostname,cb){
|
||||
var keyFile = path.join(certDir , "__hostname.key".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,7 +81,7 @@ 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,
|
||||
output: process.stdout
|
||||
@ -107,20 +102,22 @@ 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"));
|
||||
|
||||
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 " + cmdDir)));
|
||||
clearCerts(function(){
|
||||
console.log(color.green("temp certs cleared"));
|
||||
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"));
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user