diff --git a/README.md b/README.md index f7b36d6..4bfc79c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ What this proxy do is to generate and replace a temporary cert for any domain if * generate and intercept https requests for any domain without complaint by browser (after you trust its root CA) * can be used globally or as a nodejs module -## How to use +## Usage ### step 0 - setup env * install NodeJS @@ -56,7 +56,7 @@ npm install anyproxy var proxy = require("anyproxy"); !proxy.isRootCAFileExists() && proxy.generateRootCA(); -proxy.startServer("http","8001", "localhost" ,"path/to/rule/file"); +new proxy.proxyServer("http","8001", "localhost" ,"path/to/rule/file"); ``` diff --git a/bin.js b/bin.js index e7add2e..edd81b1 100644 --- a/bin.js +++ b/bin.js @@ -25,5 +25,5 @@ if(program.clear){ process.exit(0); }); }else{ - proxy.startServer(program.type,program.port, program.host ,program.rule); + new proxy.proxyServer(program.type,program.port, program.host ,program.rule); } \ No newline at end of file diff --git a/package.json b/package.json index cfebb24..0ea3710 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anyproxy", - "version": "1.1.0", + "version": "1.1.1", "description": "a charles/fiddle like web proxy for developers which can intercept https requests without browser warning", "main": "proxy.js", "bin": { @@ -14,7 +14,11 @@ }, "devDependencies": {}, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "nodeunit test.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/ottomao/anyproxy.git" }, "author": "ottomao@gmail.com", "license": "ISC" diff --git a/proxy.js b/proxy.js index c9c0b19..79e5185 100644 --- a/proxy.js +++ b/proxy.js @@ -1,3 +1,5 @@ +//TODO : get free port + var http = require('http'), https = require('https'), fs = require('fs'), @@ -16,13 +18,15 @@ var T_TYPE_HTTP = 0, DEFAULT_HOST = "localhost", DEFAULT_TYPE = T_TYPE_HTTP; -function startServer(type, port, hostname,ruleFile){ - var proxyType = /https/i.test(type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP , +function proxyServer(type, port, hostname,ruleFile){ + var self = this, + proxyType = /https/i.test(type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP , proxyPort = port || DEFAULT_PORT, - proxyHost = hostname || DEFAULT_HOST, - httpProxyServer; + proxyHost = hostname || DEFAULT_HOST; - if(ruleFile){ + self.httpProxyServer = null; + + if(ruleFile){ //TODO : fs.join if(fs.existsSync(ruleFile)){ requestHandler.setRules(require(ruleFile)); console.log(color.green("rule file loaded")); @@ -31,8 +35,8 @@ function startServer(type, port, hostname,ruleFile){ } } - async.series([ - + async.series( + [ //creat proxy server function(callback){ if(proxyType == T_TYPE_HTTPS){ @@ -40,7 +44,7 @@ function startServer(type, port, hostname,ruleFile){ if(err){ callback(err); }else{ - httpProxyServer = https.createServer({ + self.httpProxyServer = https.createServer({ key : keyContent, cert: crtContent },requestHandler.userRequestHandler); @@ -49,15 +53,15 @@ function startServer(type, port, hostname,ruleFile){ }); }else{ - httpProxyServer = http.createServer(requestHandler.userRequestHandler); + self.httpProxyServer = http.createServer(requestHandler.userRequestHandler); callback(null); } }, //listen CONNECT method for https over http function(callback){ - httpProxyServer.on('connect',requestHandler.connectReqHandler); - httpProxyServer.listen(proxyPort); + self.httpProxyServer.on('connect',requestHandler.connectReqHandler); + self.httpProxyServer.listen(proxyPort); callback(null); } @@ -75,8 +79,10 @@ function startServer(type, port, hostname,ruleFile){ } } ); + + return self.httpProxyServer; } -module.exports.startServer = startServer; +module.exports.proxyServer = proxyServer; module.exports.generateRootCA = certMgr.generateRootCA; module.exports.isRootCAFileExists = certMgr.isRootCAFileExists; \ No newline at end of file diff --git a/test.js b/test.js index 079afb5..a0c43a8 100644 --- a/test.js +++ b/test.js @@ -1,16 +1,20 @@ -var https = require("https"); + +var https = require("https"), + http = require("http"); process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; + + var options = { - host: "localhost", - port: 8001, - path: "/", - headers: { - Host: "www.alipay.com" - } + host: "localhost", + port: 8001, + path: "/", + headers: { + Host: "www.alipay.com" + } }; https.get(options, function(res) { - console.log(res); - res.pipe(process.stdout); + console.log(res); + res.pipe(process.stdout); }); \ No newline at end of file