anyproxy/lib/certMgr.js

125 lines
4.1 KiB
JavaScript
Raw Normal View History

2014-08-11 16:43:14 +08:00
var exec = require('child_process').exec,
2014-08-13 11:51:39 +08:00
spawn = require('child_process').spawn,
2014-08-11 16:43:14 +08:00
path = require("path"),
fs = require("fs"),
os = require("os"),
2014-08-13 11:51:39 +08:00
color = require('colorful'),
2014-08-14 10:59:49 +08:00
readline = require('readline'),
2014-08-13 17:30:16 +08:00
asyncTask = require("async-task-mgr");
2014-08-11 16:43:14 +08:00
var certDir = path.join(getUserHome(),"/.anyproxy_certs/"),
2014-08-13 13:52:31 +08:00
cmdDir = path.join(__dirname,"..","./cert/"),
2014-08-13 11:51:39 +08:00
asyncTaskMgr = new asyncTask();
2014-08-11 16:43:14 +08:00
if(!fs.existsSync(certDir)){
fs.mkdirSync(certDir);
}
function getCertificate(hostname,cb){
var keyFile = path.join(certDir , "__hostname.key".replace(/__hostname/,hostname) ),
crtFile = path.join(certDir , "__hostname.crt".replace(/__hostname/,hostname) );
if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
2014-08-13 17:30:16 +08:00
asyncTaskMgr.addTask(hostname,function(cb){
createCert(hostname,function(err){
cb(err ? err : null);
});
},function(err){
2014-08-11 16:43:14 +08:00
if(!err){
cb(null , fs.readFileSync(keyFile) , fs.readFileSync(crtFile) );
}else{
cb(err);
}
});
}else{
cb(null , fs.readFileSync(keyFile) , fs.readFileSync(crtFile) );
}
}
function getUserHome() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}
function createCert(hostname,callback){
2014-08-13 11:51:39 +08:00
console.log(hostname);
checkRootCA();
2014-08-11 16:43:14 +08:00
var cmd = "./gen-cer __host __path".replace(/__host/,hostname).replace(/__path/,certDir);
2014-08-13 11:51:39 +08:00
exec(cmd,{ cwd : cmdDir },function(err,stdout,stderr){
2014-08-11 16:43:14 +08:00
if(err){
callback && callback(new Error("error when generating certificate"),null);
}else{
2014-08-13 11:51:39 +08:00
var tipText = "certificate created for __HOST".replace(/__HOST/,hostname);
console.log(color.yellow(color.bold("[internal https]")) + color.yellow(tipText));
2014-08-11 16:43:14 +08:00
callback(null);
}
});
}
function clearCerts(cb){
exec("rm *.key *.csr *.crt",{cwd : certDir},cb);
}
2014-08-14 10:59:49 +08:00
function isRootCAFileExists(){
var crtFile = path.join(cmdDir,"rootCA.crt"),
keyFile = path.join(cmdDir,"rootCA.key");
return (fs.existsSync(crtFile) && fs.existsSync(keyFile));
}
2014-08-13 11:51:39 +08:00
2014-08-14 10:59:49 +08:00
function checkRootCA(){
if(!isRootCAFileExists()){
2014-08-13 11:51:39 +08:00
console.log(color.red("can not find rootCA.crt or rootCA.key"));
2014-08-14 10:59:49 +08:00
console.log(color.red("you may generate one by the following methods"));
console.log(color.red("\twhen using globally : anyproxy --root"));
console.log(color.red("\twhen using as a module : require(\"anyproxy\").generateRootCA();"));
process.exit(0);
2014-08-13 11:51:39 +08:00
}
}
function generateRootCA(){
2014-08-14 10:59:49 +08:00
if(isRootCAFileExists()){
console.log(color.yellow("rootCA exists at " + certDir));
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("do you really want to generate a new one ?)(yes/NO)", function(answer) {
if(/yes/i.test(answer)){
startGenerating();
}else{
console.log("will not generate a new one");
2014-08-13 11:51:39 +08:00
process.exit(0);
2014-08-14 10:59:49 +08:00
}
rl.close();
});
}else{
startGenerating();
}
function startGenerating(){
var spawnSteam = spawn("./gen-rootCA",['.'],{cwd:cmdDir,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"));
process.exit(0);
});
}else{
console.log(color.red("fail to generate root CA"));
}
});
}
2014-08-13 11:51:39 +08:00
}
2014-08-14 10:59:49 +08:00
module.exports.generateRootCA = generateRootCA;
module.exports.getCertificate = getCertificate;
module.exports.createCert = createCert;
module.exports.clearCerts = clearCerts;
module.exports.isRootCAFileExists = isRootCAFileExists;