From edaf638f3c003d38399999a278a6aa94008bc15d Mon Sep 17 00:00:00 2001 From: OttoMao Date: Thu, 6 Aug 2015 17:26:22 +0800 Subject: [PATCH] save map config to local file --- bin.js | 4 ++-- lib/rule_default.js | 49 +++++++++++++++++++++++++++++++++++++++++++++ lib/util.js | 18 ++++++++++++++++- package.json | 2 +- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/bin.js b/bin.js index 2cf3a40..6130849 100644 --- a/bin.js +++ b/bin.js @@ -22,7 +22,7 @@ program .option('-i, --intercept', 'intercept(decrypt) https requests when root CA exists') .option('-s, --silent', 'do not print anything into terminal') .option('-c, --clear', 'clear all the tmp certificates') - .option('install', 'install node modules') + .option('install', '[alpha] install node modules') .parse(process.argv); if(program.clear){ @@ -71,7 +71,7 @@ if(program.clear){ //read rule file from a specific position (function(){ try{ - var anyproxyHome = path.join(util.getUserHome(),"/.anyproxy/"); + var anyproxyHome = path.join(util.getAnyProxyHome()); if(fs.existsSync(path.join(anyproxyHome,"rule_default.js"))){ ruleModule = require(path.join(anyproxyHome,"rule_default")); } diff --git a/lib/rule_default.js b/lib/rule_default.js index cdba36f..e09b173 100644 --- a/lib/rule_default.js +++ b/lib/rule_default.js @@ -1,5 +1,6 @@ var utils = require("./util"), bodyParser = require("body-parser"), + path = require("path"), fs = require("fs"); 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' } ] 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 = { summary:function(){ var tip = "the default rule for AnyProxy which supports CORS."; @@ -110,6 +157,8 @@ module.exports = { app.post("/setMapConfig",function(req,res){ mapConfig = req.body; res.json(mapConfig); + + saveMapConfig(mapConfig); }); cb(); diff --git a/lib/util.js b/lib/util.js index 14cc570..28f1912 100644 --- a/lib/util.js +++ b/lib/util.js @@ -21,9 +21,25 @@ module.exports.merge = function(baseObj, extendObj){ return baseObj; } -module.exports.getUserHome = function(){ +function getUserHome(){ 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){ return String(str).replace(regexp || (/\{\{([^{}]+)\}\}/g), function(match, name){ diff --git a/package.json b/package.json index b507ef8..93e235a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anyproxy", - "version": "3.7.2", + "version": "3.7.3Beta", "description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.", "main": "proxy.js", "bin": {