mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 16:51:29 +00:00
fix cert issue
This commit is contained in:
parent
a2b231183e
commit
cb813f8cb5
@ -8,21 +8,19 @@ 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)){
|
|
||||||
fs.mkdirSync(certDir,0777);
|
|
||||||
}else{
|
|
||||||
fs.chmodSync(certDir,0777);
|
|
||||||
}
|
|
||||||
|
|
||||||
}catch(e){
|
if(!fs.existsSync(certDir)){
|
||||||
console.log(e);
|
fs.mkdirSync(certDir,0777);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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) ),
|
||||||
crtFile = path.join(certDir , "__hostname.crt".replace(/__hostname/,hostname) );
|
crtFile = path.join(certDir , "__hostname.crt".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,9 +81,9 @@ 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,19 +102,21 @@ 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"));
|
||||||
|
|
||||||
spawnSteam.on('close', function (code) {
|
var spawnSteam = spawn(cmd_genRoot,['.'],{cwd:certDir,stdio: 'inherit'});
|
||||||
if(code == 0){
|
spawnSteam.on('close', function (code) {
|
||||||
console.log(color.green("rootCA generated"));
|
if(code == 0){
|
||||||
console.log(color.green(color.bold("please trust the rootCA.crt in " + cmdDir)));
|
console.log(color.green("rootCA generated"));
|
||||||
clearCerts(function(){
|
console.log(color.green(color.bold("please trust the rootCA.crt in " + certDir)));
|
||||||
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"));
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user