anyproxy/lib/certMgr.js

144 lines
4.8 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-09-06 09:50:49 +08:00
util = require('./util'),
2014-08-13 17:30:16 +08:00
asyncTask = require("async-task-mgr");
2014-08-11 16:43:14 +08:00
2014-12-08 17:13:41 +08:00
//TODO : unstable in windows
2014-09-06 09:50:49 +08:00
var certDir = path.join(util.getUserHome(),"/.anyproxy_certs/"),
2014-08-13 13:52:31 +08:00
cmdDir = path.join(__dirname,"..","./cert/"),
2014-09-12 10:28:39 +08:00
cmd_genRoot = path.join(cmdDir,"./gen-rootCA"),
cmd_genCert = path.join(cmdDir,"./gen-cer"),
2014-08-13 11:51:39 +08:00
asyncTaskMgr = new asyncTask();
2014-08-11 16:43:14 +08:00
2014-09-12 10:28:39 +08:00
if(!fs.existsSync(certDir)){
2014-11-13 10:05:56 +08:00
try{
fs.mkdirSync(certDir,0777); //may fail in windows
}catch(e){
console.log("===========");
console.log("failed to create cert dir ,please create one by yourself - " + certDir);
console.log("this error will not block main thread unless you use https-related features in anyproxy");
console.log("===========");
}
2014-08-11 16:43:14 +08:00
}
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 createCert(hostname,callback){
2014-08-13 11:51:39 +08:00
checkRootCA();
2014-08-11 16:43:14 +08:00
2014-09-12 10:28:39 +08:00
var cmd = cmd_genCert + " __host __path".replace(/__host/,hostname).replace(/__path/,certDir);
exec(cmd,{ cwd : certDir },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(){
2014-09-12 10:28:39 +08:00
var crtFile = path.join(certDir,"rootCA.crt"),
keyFile = path.join(certDir,"rootCA.key");
2014-08-14 10:59:49 +08:00
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"));
2014-08-14 10:59:49 +08:00
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()){
2014-09-12 10:28:39 +08:00
console.log(color.yellow("rootCA exists at " + certDir));
2014-08-14 10:59:49 +08:00
var rl = readline.createInterface({
2014-09-12 10:28:39 +08:00
input : process.stdin,
2014-08-14 10:59:49 +08:00
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(){
2014-09-12 10:28:39 +08:00
//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 " + certDir)));
console.log(color.green(color.bold("or you may get it via anyproxy webinterface")));
2014-08-14 10:59:49 +08:00
process.exit(0);
2014-09-12 10:28:39 +08:00
}else{
console.log(color.red("fail to generate root CA"));
}
});
2014-08-14 10:59:49 +08:00
});
}
2014-08-13 11:51:39 +08:00
}
function getRootCAFilePath(){
if(isRootCAFileExists()){
return path.join(certDir,"rootCA.crt");
}else{
return "";
}
}
module.exports.getRootCAFilePath = getRootCAFilePath;
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;