anyproxy是一款能夠高度定製的代理服務器,基於nodejs。javascript
支持https明文代理 支持低網速模擬 支持二次開發,能夠用javascript控制代理的所有流程,搭建前端個性化調試環境 提供web版界面,觀測請求狀況html
anyproxy把http通訊過程當中的各個階段進行抽離,分解成三個階段:前端
收到來自客戶端請求以後,容許開發者直接從本地提供返回 在轉發請求到服務器前,容許開發者對發送的請求進行修改 在收到服務器響應以後,容許開發者對響應內容進行修改,再返回給客戶端java
對於上述每一個階段,anyproxy都提供了API接口,引入開發者編寫本身的規則代碼,實時干預通訊過程,以此知足各種自定義需求。node
具體地,咱們提供的接口包括:python
收到用戶請求以後git
shouldUseLocalResponse ,是否在本地直接發送響應(再也不向服務器發出請求) dealLocalResponse 若是shouldUseLocalResponse返回true,會調用這個函數來獲取本地響應內容(異步接口) 向服務端發出請求以前github
replaceRequestProtocol 替換向服務器發出的請求協議,支持http和https的替換 replaceRequestOption 替換向服務器發出的請求參數,即nodeJS中的 request option replaceRequestData 替換請求的body 向用戶返回服務端的響應以前web
replaceResponseStatusCode 替換服務器響應的http狀態碼 replaceResponseHeader 替換服務器響應的http頭 replaceServerResDataAsync 替換服務器響應的數據(異步接口) pauseBeforeSendingResponse 在請求返回給用戶前的延遲時間ajax
安裝Nodejs npm install -g anyproxy,有可能須要sudo python可選安裝
默認啓動anyproxy 定製啓動端口anyproxy --port 8001 使用某個規則文件anyproxy --rule ./rule_sample/rule_allow_CORS.js 代理https請求anyproxy --intercept(須要安裝證書,詳情見下文) 其餘命令能夠經過anyproxy -h查看
訪問https://127.0.0.1:8002,你會在瀏覽器裏看到實時的請求。 要確保是現代瀏覽器
生成RootCA
命令行執行 sudo anyproxy –root 找到RootCA文件
方法一: 執行完成以後,會打開證書的安裝路徑,便可看到 rootCA.crt 文件 方法二: 啓動anyproxy,瀏覽器打開 https://localhost:8002/fetchCrtFile ,也能獲取rootCA.crt文件 方法三:啓動anyproxy,https://localhost:8002/qr_root 能夠獲取證書路徑的二維碼,移動端安裝時會比較便捷 打開上述rootCA.crt文件 根據操做系統提示,信任rootCA其餘
若是在訪問時出現UNAUTHORIZED_CERTIFICATE一類的安全警告,請從新檢查證書的安裝狀況 證書只需生成一次,使用前每一個終端都須要信任它
明文解析HTTPS
須要解析HTTPS時,用intercept參數來啓動anyproxy anyproxy --intercept 爲終端設置代理,在UI界面就能看到明文的HTTPS請求數據了,帶把小鎖的就是HTTPS請求
進階 - 用rule來手動處理https請求(如:代理文件到本地)
AnyProxy默認不會解析https請求,你須要引入shouldInterceptHttpsReq這個函數來顯式指定解析哪一個請求。具體能夠參照這份sample : rule_intercept_some_https_requests.js
其餘
anyproxy --clear能夠清除全部已生成的證書。清除後,各終端須要從新安裝證書。 平常開發中,不要使用anyproxy --type https來調試。AnyProxy使用https over http的方法來進行代理,而這條命令啓動的是一個https代理服務器,二者使用場景徹底不一樣。
以「防止CDN返回304」這個需求爲例,最直接的方案是攔截請求,在發送到CDN前刪除header中的if-modified-since字段。在AnyProxy中,配置replaceRequestOption接口,3行代碼就能實現這個自定義功能:
1
2
3
4
5
6
7
8
9
|
//
rule
file
module.exports = {
//在向服務器發出請求前,AnyProxy會調用這個接口,能夠在此時修改發送請求的參數
replaceRequestOption :
function
(req,
option
){
var newOption =
option
;
delete
newOption.headers[
'if-modified-since'
];
return
newOption;
}
};
|
再舉個例子,若是你想修改響應數據,在全部html文件最後加個」Hello World」,就須要調用replaceServerResDataAsync接口,並結合content-type字段來進行修改,大約須要8行代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//
rule
file
module.exports = {
replaceServerResDataAsync:
function
(req,res,serverResData,callback){
//append
"hello world"
to
all
web pages
if(/html/i.test(res.headers[
'content-type'
])){
var newDataStr = serverResData.toString();
newDataStr +=
"hello world!"
;
callback(newDataStr);
}
else
{
callback(serverResData);
}
}
};
|
1
2
3
4
5
6
7
8
|
module.exports = {
pauseBeforeSendingResponse :
function
(req,res){
//delay
all
the response
for
1500ms
return
1500;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
//
rule
scheme :
module.exports = {
shouldUseLocalResponse :
function
(req,reqBody){
//intercept
all
options request
if(req.method ==
"OPTIONS"
){
return
true
;
}
else
{
return
false
;
}
},
dealLocalResponse :
function
(req,reqBody,callback){
if(req.method ==
"OPTIONS"
){
callback(200,mergeCORSHeader(req.headers),
""
);
}
},
replaceResponseHeader:
function
(req,res,header){
return
mergeCORSHeader(req.headers, header);
}
};
function
mergeCORSHeader(reqHeader,originHeader){
var targetObj = originHeader || {};
delete
targetObj[
"Access-Control-Allow-Credentials"
];
delete
targetObj[
"Access-Control-Allow-Origin"
];
delete
targetObj[
"Access-Control-Allow-Methods"
];
delete
targetObj[
"Access-Control-Allow-Headers"
];
targetObj[
"access-control-allow-credentials"
] =
"true"
;
targetObj[
"access-control-allow-origin"
] = reqHeader[
'origin'
] ||
"-___-||"
;
targetObj[
"access-control-allow-methods"
] =
"GET, POST, PUT"
;
targetObj[
"access-control-allow-headers"
] = reqHeader[
'access-control-request-headers'
] ||
"-___-||"
;
return
targetObj;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
module.exports = {
replaceServerResDataAsync:
function
(req,res,serverResData,callback){
//
add
"hello github"
to
all
github pages
if(req.headers.host ==
"github.com"
){
serverResData +=
"hello github"
;
}
callback(serverResData);
},
shouldInterceptHttpsReq :
function
(req){
//intercept https://github.com/
//otherwise,
all
the https traffic will
not
go through this proxy
//
return
true
;
if(req.headers.host ==
"github.com"
){
return
true
;
}
else
{
return
false
;
}
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//
rule
scheme :
module.exports = {
replaceRequestOption :
function
(req,
option
){
var newOption =
option
;
delete
newOption.headers[
'if-none-match'
];
delete
newOption.headers[
'if-modified-since'
];
return
newOption;
},
replaceResponseHeader:
function
(req,res,header){
header = header || {};
header[
"Cache-Control"
] =
"no-cache, no-store, must-revalidate"
;
header[
"Pragma"
] =
"no-cache"
;
header[
"Expires"
] = 0;
return
header;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
module.exports = {
replaceRequestOption :
function
(req,
option
){
/*
option scheme:
{
hostname : "www.taobao.com"
port : 80
path : "/"
method : "GET"
headers : {cookie:""}
}
*/
if(
option
.hostname ==
"www.taobao.com"
&&
option
.path ==
"/"
){
option
.path =
"/about/"
;
}
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
module.exports = {
replaceResponseStatusCode:
function
(req,res,statusCode){
//redirect requests toward https://www.taobao.com/*
//using 302
if(req.headers.host ==
"www.taobao.com"
){
statusCode = 302;
}
return
statusCode;
},
replaceResponseHeader:
function
(req,res,header){
if(req.headers.host ==
"www.taobao.com"
){
}
return
header;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//
replace
all
the images
with
local
one
var fs = require(
"fs"
);
var LOCAL_IMAGE =
"/Users/path/to/image.png"
;
module.exports = {
summary:
function
(){
return
"replace all the images with local one"
;
},
//mark if use
local
response
shouldUseLocalResponse :
function
(req,reqBody){
if(/\.(png|gif|jpg|jpeg)$/.test(req.url)){
req.replaceLocalFile =
true
;
return
true
;
}
else
{
return
false
;
}
},
dealLocalResponse :
function
(req,reqBody,callback){
if(req.replaceLocalFile){
callback(200, {
"content-type"
:
"image/png"
}, fs.readFileSync(LOCAL_IMAGE) );
}
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
read the following wiki before using rule file
*/
module.exports = {
/*
These functions will overwrite the default ones, write your own when necessary.
Comments in Chinese are nothing but a translation of key points. Be relax if you dont understand.
致中文用戶:中文註釋都是隻摘要,必要時請參閱英文文檔。歡迎提出修改建議。
*/
summary:
function
(){
return
"this is a blank rule for AnyProxy"
;
},
//=======================
//
when
getting a request
from
user
//收到用戶請求以後
//=======================
//是否截獲https請求
//should intercept https request,
or
it will be forwarded
to
real
server
shouldInterceptHttpsReq :
function
(req){
return
false
;
},
//是否在本地直接發送響應(再也不向服務器發出請求)
//whether
to
intercept this request
by
local
logic
//if the
return
value
is
true
, anyproxy will call dealLocalResponse
to
get response data
and
will
not
send request
to
remote server anymore
//req
is
the
user
's request sent to the proxy server
shouldUseLocalResponse : function(req,reqBody){
return false;
},
//若是shouldUseLocalResponse返回true,會調用這個函數來獲取本地響應內容
//you may deal the response locally instead of sending it to server
//this function be called when shouldUseLocalResponse returns true
//callback(statusCode,resHeader,responseData)
//e.g. callback(200,{"content-type":"text/html"},"hello world")
dealLocalResponse : function(req,reqBody,callback){
callback(statusCode,resHeader,responseData)
},
//=======================
//when ready to send a request to server
//向服務端發出請求以前
//=======================
//替換向服務器發出的請求協議(http和https的替換)
//replace the request protocol when sending to the real server
//protocol : "http" or "https"
replaceRequestProtocol:function(req,protocol){
var newProtocol = protocol;
return newProtocol;
},
//替換向服務器發出的請求參數(option)
//option is the configuration of the http request sent to remote server. You may refers to https://nodejs.org/api/http.html#http_http_request_options_callback
//you may return a customized option to replace the original one
//you should not overwrite content-length header in options, since anyproxy will handle it for you
replaceRequestOption : function(req,option){
var newOption = option;
return newOption;
},
//替換請求的body
//replace the request body
replaceRequestData: function(req,data){
return data;
},
//=======================
//when ready to send the response to user after receiving response from server
//向用戶返回服務端的響應以前
//=======================
//替換服務器響應的http狀態碼
//replace the statusCode before it'
s sent
to
the
user
replaceResponseStatusCode:
function
(req,res,statusCode){
var newStatusCode = statusCode;
return
newStatusCode;
},
//替換服務器響應的http頭
//
replace
the httpHeader before it
's sent to the user
//Here header == res.headers
replaceResponseHeader: function(req,res,header){
var newHeader = header;
return newHeader;
},
//替換服務器響應的數據
//replace the response from the server before it'
s sent
to
the
user
//you may
return
either a Buffer
or
a string
//serverResData
is
a Buffer.
for
those non-unicode reponse , serverResData.toString() should
not
be your
first
choice.
replaceServerResDataAsync:
function
(req,res,serverResData,callback){
callback(serverResData);
},
//Deprecated
// replaceServerResData:
function
(req,res,serverResData){
//
return
serverResData;
// },
//在請求返回給用戶前的延遲時間
//
add
a pause before sending response
to
user
pauseBeforeSendingResponse :
function
(req,res){
var timeInMS = 1; //delay
all
requests
for
1ms
return
timeInMS;
}
};
|
支持Https的中間人(man-in-the-middle)代理,同時提供便捷的根證書安裝路徑,方便移動端導入證書 低網速網速模擬,協助調試2G/3G下的表現 能夠導出全部請求記錄,供後期數據分析使用 能夠進行模塊化調用,作二次封裝,合併到現有的前端集成開發環境中,個性化搭建本身的調試環境
HTTPS相關配置的中文文檔
What is rule file and how to write one
代理服務器的新輪子:anyproxy
轉載:https://www.2cto.com/kf/201707/654139.html