From 97ab818442d29024816bc72a36261e23750275bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E5=BD=93=E5=BD=93?= Date: Fri, 5 Sep 2014 18:12:50 +0800 Subject: [PATCH 1/7] 404 --- lib/requestHandler.js | 7 ++++++- web/404.html | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 web/404.html diff --git a/lib/requestHandler.js b/lib/requestHandler.js index d4b5e90..a480302 100644 --- a/lib/requestHandler.js +++ b/lib/requestHandler.js @@ -171,7 +171,12 @@ function userRequestHandler(req,userRes){ //send response },function(callback){ - userRes.end(serverResData); + if(404 == statusCode){ + var html404path = pathUtil.join(__dirname, '..', 'web', '404.html'); + userRes.end(fs.readFileSync(html404path)); + }else{ + userRes.end(serverResData); + } callback(); //udpate record info diff --git a/web/404.html b/web/404.html new file mode 100644 index 0000000..1a7adc2 --- /dev/null +++ b/web/404.html @@ -0,0 +1,27 @@ + + + + Anyproxy - 404 NOT FOUND + + + + +

404 NOT FOUND

+ + \ No newline at end of file From 0343eb7aebb6be80fd9074048499e5e34bc5d55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E5=BD=93=E5=BD=93?= Date: Fri, 5 Sep 2014 18:28:38 +0800 Subject: [PATCH 2/7] 404 --- web/404.html | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/web/404.html b/web/404.html index 1a7adc2..fc1b062 100644 --- a/web/404.html +++ b/web/404.html @@ -4,24 +4,25 @@ Anyproxy - 404 NOT FOUND -

404 NOT FOUND

+

404 NOT FOUND

\ No newline at end of file From 946c4ee75566bb3dbc18087a227f5d296cfea93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E5=BD=93=E5=BD=93?= Date: Sat, 6 Sep 2014 09:50:49 +0800 Subject: [PATCH 3/7] userHome default rule --- lib/certMgr.js | 7 ++----- lib/util.js | 5 +++++ proxy.js | 8 +++++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/certMgr.js b/lib/certMgr.js index 686065e..8e048b9 100644 --- a/lib/certMgr.js +++ b/lib/certMgr.js @@ -5,9 +5,10 @@ var exec = require('child_process').exec, os = require("os"), color = require('colorful'), readline = require('readline'), + util = require('./util'), asyncTask = require("async-task-mgr"); -var certDir = path.join(getUserHome(),"/.anyproxy_certs/"), +var certDir = path.join(util.getUserHome(),"/.anyproxy_certs/"), cmdDir = path.join(__dirname,"..","./cert/"), asyncTaskMgr = new asyncTask(); @@ -37,10 +38,6 @@ function getCertificate(hostname,cb){ } } -function getUserHome() { - return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; -} - function createCert(hostname,callback){ console.log(hostname); checkRootCA(); diff --git a/lib/util.js b/lib/util.js index 8f58988..ce4a315 100644 --- a/lib/util.js +++ b/lib/util.js @@ -18,3 +18,8 @@ module.exports.merge = function(baseObj, extendObj){ return baseObj; } + +module.exports.getUserHome = function(){ + return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; +} + diff --git a/proxy.js b/proxy.js index 42b5303..5343cbe 100644 --- a/proxy.js +++ b/proxy.js @@ -19,6 +19,7 @@ var http = require('http'), util = require("./lib/util"), entities = require("entities"), express = require("express"), + path = require("path"), WebSocketServer= require('ws').Server; GLOBAL.recorder = new Recorder(); @@ -31,6 +32,11 @@ var T_TYPE_HTTP = 0, DEFAULT_HOST = "localhost", DEFAULT_TYPE = T_TYPE_HTTP; +var default_rule = require('./lib/rule_default'); +if(fs.existsSync(path.join(util.getUserHome(),"/.anyproxy/rule_default.js"))){ + default_rule = require(path.join(util.getUserHome(),"/.anyproxy/rule_default")); +} + //option //option.type : 'http'(default) or 'https' //option.port : 8001(default) @@ -43,7 +49,7 @@ function proxyServer(option){ proxyType = /https/i.test(option.type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP , proxyPort = option.port || DEFAULT_PORT, proxyHost = option.hostname || DEFAULT_HOST, - proxyRules = option.rule || require('./lib/rule_default'); + proxyRules = option.rule || default_rule; requestHandler.setRules(proxyRules); //TODO : optimize calling for set rule self.httpProxyServer = null; From b173e481fd2d6439323de5c642d926f3ff815277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E5=BD=93=E5=BD=93?= Date: Sat, 6 Sep 2014 10:15:51 +0800 Subject: [PATCH 4/7] decwd fault__rule --- proxy.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/proxy.js b/proxy.js index 5343cbe..81a87e3 100644 --- a/proxy.js +++ b/proxy.js @@ -3,6 +3,7 @@ try{ GLOBAL.util = require('./lib/util'); GLOBAL.util['iconv-lite'] = require("iconv-lite"); GLOBAL.util['colorful'] = require("colorful"); + GLOBAL.util['path'] = require("path"); }catch(e){} var http = require('http'), @@ -36,6 +37,9 @@ var default_rule = require('./lib/rule_default'); if(fs.existsSync(path.join(util.getUserHome(),"/.anyproxy/rule_default.js"))){ default_rule = require(path.join(util.getUserHome(),"/.anyproxy/rule_default")); } +if(fs.existsSync(process.cwd() + '/rule.js')){ + default_rule = require(process.cwd() + '/rule'); +} //option //option.type : 'http'(default) or 'https' From b247ba1b312fe1da6a69c8c6abb23c9a6fc5da5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E5=BD=93=E5=BD=93?= Date: Sat, 6 Sep 2014 10:38:47 +0800 Subject: [PATCH 5/7] relative path --- bin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin.js b/bin.js index ee46f15..6a8ef37 100644 --- a/bin.js +++ b/bin.js @@ -32,7 +32,7 @@ if(program.clear){ try{ //for abs path ruleModule = require(program.rule); }catch(e){ //for relative path - ruleModule = require("./" + program.rule); + ruleModule = require(process.cwd() + '/' + program.rule.replace(/^\.\//,'')); } console.log(color.green("rule file loaded")); }else{ From 2eea76297c1c57d13f0eb210205ef583ea8a8044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E5=BD=93=E5=BD=93?= Date: Sat, 6 Sep 2014 10:47:33 +0800 Subject: [PATCH 6/7] anyproxyHome --- proxy.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/proxy.js b/proxy.js index 81a87e3..371e37e 100644 --- a/proxy.js +++ b/proxy.js @@ -34,8 +34,12 @@ var T_TYPE_HTTP = 0, DEFAULT_TYPE = T_TYPE_HTTP; var default_rule = require('./lib/rule_default'); -if(fs.existsSync(path.join(util.getUserHome(),"/.anyproxy/rule_default.js"))){ - default_rule = require(path.join(util.getUserHome(),"/.anyproxy/rule_default")); +var anyproxyHome = path.join(util.getUserHome(),"/.anyproxy/"); +if(!fs.existsSync(anyproxyHome)){ + fs.mkdirSync(anyproxyHome); +} +if(fs.existsSync(path.join(anyproxyHome,"rule_default.js"))){ + default_rule = require(path.join(anyproxyHome,"rule_default")); } if(fs.existsSync(process.cwd() + '/rule.js')){ default_rule = require(process.cwd() + '/rule'); From db4088ea3c47ee95b583ab30ed884bc8c29dbbdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=A0=E9=87=8C?= Date: Sun, 7 Sep 2014 13:49:39 +0800 Subject: [PATCH 7/7] update doc --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d846903..3605fb8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ anyproxy ========== A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly. -(Some Chinese in this doc is nothing but translation of some key points. Be relax if you dont understand.) +(Chinese in this doc is nothing but translation of some key points. Be relax if you dont understand.) ![](https://i.alipayobjects.com/i/ecmng/png/201409/3NKRCRk2Uf.png_250x.png) @@ -152,12 +152,13 @@ module.exports = { Using https features ---------------- #### step 1 - install openssl -* install [openssl](http://wiki.openssl.org/index.php/Compilation_and_Installation) ,if you want to use HTTPS-related features. After that, the command ``openssl`` should be exposed to your shell +* openssl is availabe here : [http://wiki.openssl.org/index.php/Compilation_and_Installation](http://wiki.openssl.org/index.php/Compilation_and_Installation) +* using ``openssl version -a `` to make sure it is accessible via you command line. #### step 2 - generate a rootCA and trust it * you should do this when it is the first time to start anyproxy * execute ``anyproxy --root`` ,follow the instructions on screen -* you will see some tip like *rootCA generated at : /usr/lib...* . ``cd`` to that directory, add/trust the rootCA.crt file to your system keychain. In OSX, you may do that by open the *crt file directly +* [important!]you will see some tip like *rootCA generated at : /usr/lib...* . ``cd`` to that directory, add/trust the rootCA.crt file to your system keychain. In OSX, you may do that by open the *crt file directly #### step 3 - start a https proxy * ``anyproxy --type https --host my.domain.com``