anyproxy/README.md

102 lines
4.0 KiB
Markdown
Raw Normal View History

2014-08-11 16:43:14 +08:00
anyproxy
2014-08-08 17:50:56 +08:00
==========
2014-08-10 15:55:36 +08:00
## Intro
2014-08-14 10:59:49 +08:00
While there are lots of proxy written by nodejs in github, most of them can not handle users' HTTPS requests perfectly. A typical problem is that the browser will throw warning like INVALID_CERTIFICATE when they want to intercept some https requests.
2014-08-10 15:55:36 +08:00
A simple and fast solution is to short the traffic between the user and the target server. That is to say, what the proxy do is to forward all the traffic of both sides, without intercepting or looking inside.
2014-08-14 10:59:49 +08:00
This is useful when you want to establish a standard proxy and forwarding data. But this can also be useless when being used as a debug tool.
2014-08-10 15:55:36 +08:00
2014-08-14 10:59:49 +08:00
To work as a debug tool of HTTPS, the proxy itself should do two things: intercept the request and cheat the browser with a valid certificate,aka the man-in-the-middle(MITM) attack.
2014-08-10 15:58:32 +08:00
2014-08-14 10:59:49 +08:00
In order to have a browser-trusted certificate, we would sign certificates dynamically. The first thing to do is to generate a self-signed root CA and import to the system keychain. After trusting this CA, all child certs inherit from root CA can be naturally trusted by the browser.
2014-08-10 15:58:32 +08:00
2014-08-14 10:59:49 +08:00
What this proxy do is to generate and replace a temporary cert for any domain if neccessary. Using it, we can intercept any requests for debug. BTW, this is also what the charlse/fiddler do when you check the enable_ssl_proxy in preference.
2014-08-10 15:55:36 +08:00
## Feature
2014-08-14 10:59:49 +08:00
* work as http or https proxy
* 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
2014-08-08 17:50:56 +08:00
2014-08-14 22:58:30 +08:00
## Usage
2014-08-09 11:41:02 +08:00
### step 0 - setup env
2014-08-13 17:30:16 +08:00
2014-08-09 11:41:02 +08:00
* install NodeJS
2014-08-13 17:30:16 +08:00
* install [openssl](http://wiki.openssl.org/index.php/Compilation_and_Installation) , i.e. the command ``openssl`` should be exposed to your shell
### step 1 - install
* ``npm install -g anyproxy`` , may need ``sudo``
2014-08-08 17:50:56 +08:00
2014-08-09 11:41:02 +08:00
### step 2 - generate a rootCA and trust it
2014-08-13 17:30:16 +08:00
* execute ``anyproxy --root`` ,follow the instructions on screen
* you will see some tip like *rootCA generated at : /usr/lib...* , just cd to that position, add the rootCA.crt file to your system keychain and trust. In OSX, you may do that by open the *crt file directly
### step 3 - start server
2014-08-13 17:32:45 +08:00
#### start with default settings
2014-08-13 17:30:16 +08:00
* ``anyproxy``
2014-08-13 17:44:09 +08:00
#### start with a specific port
2014-08-13 17:30:16 +08:00
* ``anyproxy --port 8001``
2014-08-09 11:41:02 +08:00
2014-08-13 17:30:16 +08:00
#### start a https proxy
* ``anyproxy --type https --host my.domain.com``
* the param ``host`` is required with https proxy and it should be kept exactly what it it when you config your browser. Otherwise, you may get some warning about security.
2014-08-09 11:41:02 +08:00
2014-08-13 17:30:16 +08:00
### others
2014-08-09 11:41:02 +08:00
2014-08-14 10:59:49 +08:00
#### work as a module
```
npm install anyproxy
```
```javascript
var proxy = require("anyproxy");
!proxy.isRootCAFileExists() && proxy.generateRootCA();
2014-08-14 22:58:30 +08:00
new proxy.proxyServer("http","8001", "localhost" ,"path/to/rule/file");
2014-08-14 10:59:49 +08:00
```
2014-08-13 17:30:16 +08:00
#### clear all the temperary certificates
* ``anyproxy --clear``
2014-08-13 17:44:09 +08:00
#### map file to local
2014-08-14 10:59:49 +08:00
* ``anyproxy --rule /path/to/ruleFile.js``
* actually ruleFile.js is a module for Nodejs
2014-08-13 17:30:16 +08:00
* a sample schema of ruls.js is as follows
```javascript
var rules = {
"map" :[
{
"host" :/./, //regExp
"path" :/\/path\/test/, //regExp
"localFile" :"", //this file will be returned to user when host and path pattern both meets the request
"localDir" :"~/" //find the file of same name in localdir. anyproxy will not read localDir settings unless localFile is falsy
}
,{
"host" :/./,
2014-08-14 10:59:49 +08:00
"path" :/\.(png|gif|jpg|jpeg)/,
2014-08-13 17:30:16 +08:00
"localFile" :"/Users/Stella/tmp/test.png",
"localDir" :"~/"
}
]
,"httpsConfig":{
"bypassAll" : false, //by setting this to true, anyproxy will not intercept any https request
"interceptDomains":[/www\.a\.com/,/www\.b\.com/] //by setting bypassAll:false, requests towards these domains will be intercepted, and try to meet the map rules above
}
}
module.exports = rules;
```
2014-08-09 11:41:02 +08:00
## Contact
2014-08-13 17:30:16 +08:00
Author : Otto Mao, from Shanghai,China
2014-08-08 17:50:56 +08:00
ottomao@gmail.com
2014-08-13 17:30:16 +08:00
Please feel free to raise any issue about this project, or give me some advice on this poor english doc. :)