1
0
mirror of https://github.com/alibaba/anyproxy.git synced 2025-05-10 06:48:26 +00:00

test cert permission issue

This commit is contained in:
加里 2014-09-11 17:50:34 +08:00
parent 7b38c91d9f
commit a2b231183e
4 changed files with 136 additions and 4 deletions

@ -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);

122
lib/old.js Normal file

@ -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;

@ -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'] || "-___-||";

@ -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": {