save map config to local file

This commit is contained in:
OttoMao 2015-08-06 17:26:22 +08:00
parent ef19ccf72b
commit edaf638f3c
4 changed files with 69 additions and 4 deletions

4
bin.js
View File

@ -22,7 +22,7 @@ program
.option('-i, --intercept', 'intercept(decrypt) https requests when root CA exists') .option('-i, --intercept', 'intercept(decrypt) https requests when root CA exists')
.option('-s, --silent', 'do not print anything into terminal') .option('-s, --silent', 'do not print anything into terminal')
.option('-c, --clear', 'clear all the tmp certificates') .option('-c, --clear', 'clear all the tmp certificates')
.option('install', 'install node modules') .option('install', '[alpha] install node modules')
.parse(process.argv); .parse(process.argv);
if(program.clear){ if(program.clear){
@ -71,7 +71,7 @@ if(program.clear){
//read rule file from a specific position //read rule file from a specific position
(function(){ (function(){
try{ try{
var anyproxyHome = path.join(util.getUserHome(),"/.anyproxy/"); var anyproxyHome = path.join(util.getAnyProxyHome());
if(fs.existsSync(path.join(anyproxyHome,"rule_default.js"))){ if(fs.existsSync(path.join(anyproxyHome,"rule_default.js"))){
ruleModule = require(path.join(anyproxyHome,"rule_default")); ruleModule = require(path.join(anyproxyHome,"rule_default"));
} }

View File

@ -1,5 +1,6 @@
var utils = require("./util"), var utils = require("./util"),
bodyParser = require("body-parser"), bodyParser = require("body-parser"),
path = require("path"),
fs = require("fs"); fs = require("fs");
var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(), var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(),
@ -8,6 +9,52 @@ var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(),
//e.g. [ { keyword: 'aaa', local: '/Users/Stella/061739.pdf' } ] //e.g. [ { keyword: 'aaa', local: '/Users/Stella/061739.pdf' } ]
var mapConfig = []; var mapConfig = [];
var configFile = "mapConfig.json";
function saveMapConfig(content,cb){
try{
var anyproxyHome = utils.getAnyProxyHome(),
mapCfgPath = path.join(anyproxyHome,configFile);
if(typeof content == "object"){
content = JSON.stringify(content);
}
fs.writeFile(mapCfgPath,content,cb);
}catch(e){
cb && cb(e)
}
}
function getMapConfig(cb){
try{
var anyproxyHome = utils.getAnyProxyHome(),
mapCfgPath = path.join(anyproxyHome,configFile);
fs.readFile(mapCfgPath,{encoding:"utf8"},function(err,content){
if(err){
cb && cb(err)
}else{
try{
var obj = JSON.parse(content);
cb && cb(null,obj);
}catch(e){
cb && cb(e);
}
}
});
}catch(e){
cb && cb(e)
}
}
//load saved config file
getMapConfig(function(err,result){
if(result){
mapConfig = result;
}
});
module.exports = { module.exports = {
summary:function(){ summary:function(){
var tip = "the default rule for AnyProxy which supports CORS."; var tip = "the default rule for AnyProxy which supports CORS.";
@ -110,6 +157,8 @@ module.exports = {
app.post("/setMapConfig",function(req,res){ app.post("/setMapConfig",function(req,res){
mapConfig = req.body; mapConfig = req.body;
res.json(mapConfig); res.json(mapConfig);
saveMapConfig(mapConfig);
}); });
cb(); cb();

View File

@ -21,9 +21,25 @@ module.exports.merge = function(baseObj, extendObj){
return baseObj; return baseObj;
} }
module.exports.getUserHome = function(){ function getUserHome(){
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
} }
module.exports.getUserHome = getUserHome;
module.exports.getAnyProxyHome = function(){
var home = path.join(util.getUserHome(),"/.anyproxy/");
if(!fs.existsSync(home)){
try{
fs.mkdirSync(home,0777);
}catch(e){
return null;
}
}
return home;
}
module.exports.simpleRender = function(str, object, regexp){ module.exports.simpleRender = function(str, object, regexp){
return String(str).replace(regexp || (/\{\{([^{}]+)\}\}/g), function(match, name){ return String(str).replace(regexp || (/\{\{([^{}]+)\}\}/g), function(match, name){

View File

@ -1,6 +1,6 @@
{ {
"name": "anyproxy", "name": "anyproxy",
"version": "3.7.2", "version": "3.7.3Beta",
"description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.", "description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.",
"main": "proxy.js", "main": "proxy.js",
"bin": { "bin": {