From a2b231183e986d42fb1b306f0e12e7109a90d9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=A0=E9=87=8C?= Date: Thu, 11 Sep 2014 17:50:34 +0800 Subject: [PATCH] test cert permission issue --- lib/certMgr.js | 14 ++++- lib/old.js | 122 ++++++++++++++++++++++++++++++++++++++++++++ lib/rule_default.js | 2 +- package.json | 2 +- 4 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 lib/old.js diff --git a/lib/certMgr.js b/lib/certMgr.js index 7426a2e..f078ad7 100644 --- a/lib/certMgr.js +++ b/lib/certMgr.js @@ -12,8 +12,15 @@ var certDir = path.join(util.getUserHome(),"/.anyproxy_certs/"), cmdDir = path.join(__dirname,"..","./cert/"), asyncTaskMgr = new asyncTask(); -if(!fs.existsSync(certDir)){ - fs.mkdirSync(certDir); +try{ + if(!fs.existsSync(certDir)){ + fs.mkdirSync(certDir,0777); + }else{ + fs.chmodSync(certDir,0777); + } + +}catch(e){ + console.log(e); } function getCertificate(hostname,cb){ @@ -42,10 +49,13 @@ 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){ 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); diff --git a/lib/old.js b/lib/old.js new file mode 100644 index 0000000..8e048b9 --- /dev/null +++ b/lib/old.js @@ -0,0 +1,122 @@ +var exec = require('child_process').exec, + spawn = require('child_process').spawn, + path = require("path"), + fs = require("fs"), + os = require("os"), + color = require('colorful'), + readline = require('readline'), + util = require('./util'), + asyncTask = require("async-task-mgr"); + +var certDir = path.join(util.getUserHome(),"/.anyproxy_certs/"), + cmdDir = path.join(__dirname,"..","./cert/"), + asyncTaskMgr = new asyncTask(); + +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)){ + asyncTaskMgr.addTask(hostname,function(cb){ + createCert(hostname,function(err){ + cb(err ? err : null); + }); + },function(err){ + 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){ + console.log(hostname); + checkRootCA(); + + var cmd = "./gen-cer __host __path".replace(/__host/,hostname).replace(/__path/,certDir); + exec(cmd,{ cwd : cmdDir },function(err,stdout,stderr){ + if(err){ + callback && callback(new Error("error when generating certificate"),null); + }else{ + var tipText = "certificate created for __HOST".replace(/__HOST/,hostname); + console.log(color.yellow(color.bold("[internal https]")) + color.yellow(tipText)); + callback(null); + } + }); +} + +function clearCerts(cb){ + exec("rm *.key *.csr *.crt",{cwd : certDir},cb); +} + +function isRootCAFileExists(){ + var crtFile = path.join(cmdDir,"rootCA.crt"), + keyFile = path.join(cmdDir,"rootCA.key"); + + return (fs.existsSync(crtFile) && fs.existsSync(keyFile)); +} + +function checkRootCA(){ + if(!isRootCAFileExists()){ + console.log(color.red("can not find rootCA.crt or rootCA.key")); + 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); + } +} + +function generateRootCA(){ + 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"); + process.exit(0); + } + + 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")); + } + }); + } +} + +module.exports.generateRootCA = generateRootCA; +module.exports.getCertificate = getCertificate; +module.exports.createCert = createCert; +module.exports.clearCerts = clearCerts; +module.exports.isRootCAFileExists = isRootCAFileExists; \ No newline at end of file diff --git a/lib/rule_default.js b/lib/rule_default.js index b4a527f..ca811e5 100644 --- a/lib/rule_default.js +++ b/lib/rule_default.js @@ -54,7 +54,7 @@ function mergeCORSHeader(reqHeader,originHeader){ delete targetObj["Access-Control-Allow-Headers"]; targetObj["access-control-allow-credentials"] = "true"; - targetObj["access-control-allow-origin"] = reqHeader['origin'] || "-___-||"; + targetObj["access-control-allow-origin"] = reqHeader['origin'] || reqHeader['Origin'] || "-___-||"; targetObj["access-control-allow-methods"] = "GET, POST, PUT"; targetObj["access-control-allow-headers"] = reqHeader['access-control-request-headers'] || "-___-||"; diff --git a/package.json b/package.json index 7003897..034b4dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anyproxy", - "version": "2.3.6", + "version": "2.3.9", "description": "a charles/fiddler like proxy written in NodeJs, which can handle HTTPS requests and CROS perfectly.", "main": "proxy.js", "bin": {