日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

anyproxy-初识使用

發布時間:2023/12/19 综合教程 48 生活家
生活随笔 收集整理的這篇文章主要介紹了 anyproxy-初识使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

anyproxy是一款可以高度定制的代理服務器,基于nodejs。

特征

支持https明文代理 支持低網速模擬 支持二次開發,可以用javascript控制代理的全部流程,搭建前端個性化調試環境 提供web版界面,觀測請求情況

設計

anyproxy把http通信過程中的各個階段進行抽離,分解成三個階段:

收到來自客戶端請求之后,允許開發者直接從本地提供返回 在轉發請求到服務器前,允許開發者對發送的請求進行修改 在收到服務器響應之后,允許開發者對響應內容進行修改,再返回給客戶端

對于上述每個階段,anyproxy都提供了API接口,引入開發者編寫自己的規則代碼,實時干預通信過程,以此滿足各類自定義需求。

具體地,我們提供的接口包括:

收到用戶請求之后

shouldUseLocalResponse ,是否在本地直接發送響應(不再向服務器發出請求) dealLocalResponse 如果shouldUseLocalResponse返回true,會調用這個函數來獲取本地響應內容(異步接口) 向服務端發出請求之前

replaceRequestProtocol 替換向服務器發出的請求協議,支持http和https的替換 replaceRequestOption 替換向服務器發出的請求參數,即nodeJS中的 request option replaceRequestData 替換請求的body 向用戶返回服務端的響應之前

replaceResponseStatusCode 替換服務器響應的http狀態碼 replaceResponseHeader 替換服務器響應的http頭 replaceServerResDataAsync 替換服務器響應的數據(異步接口) pauseBeforeSendingResponse 在請求返回給用戶前的延遲時間

快速開始

安裝

安裝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,你會在瀏覽器里看到實時的請求。 要確保是現代瀏覽器

HTTPS相關教程

生成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代理服務器,兩者使用場景完全不同。

AnyProxy規則文件樣例

修改請求頭:防止CDN返回304

以“防止CDN返回304”這個需求為例,最直接的方案是攔截請求,在發送到CDN前刪除header中的if-modified-since字段。在AnyProxy中,配置replaceRequestOption接口,3行代碼就能實現這個自定義功能:

?

1
2
3
4
5
6
7
8
9

//rulefile
module.exports = {
//在向服務器發出請求前,AnyProxy會調用這個接口,可以在此時修改發送請求的參數
replaceRequestOption :function(req,option){
var newOption =option;
deletenewOption.headers['if-modified-since'];
returnnewOption;
}
};

修改響應數據

再舉個例子,如果你想修改響應數據,在所有html文件最后加個”Hello World”,就需要調用replaceServerResDataAsync接口,并結合content-type字段來進行修改,大約需要8行代碼。

?

1
2
3
4
5
6
7
8
9
10
11
12
13

//rulefile
module.exports = {
replaceServerResDataAsync:function(req,res,serverResData,callback){
//append"hello world"toallweb pages
if(/html/i.test(res.headers['content-type'])){
var newDataStr = serverResData.toString();
newDataStr +="hello world!";
callback(newDataStr);
}else{
callback(serverResData);
}
}
};

把所有的響應延遲1500毫秒

?

1
2
3
4
5
6
7
8

module.exports = {

pauseBeforeSendingResponse :function(req,res){
//delayallthe responsefor1500ms
return1500;
}

};

為ajax請求增加跨域頭

?

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

//rulescheme :
// Ref:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

module.exports = {
shouldUseLocalResponse :function(req,reqBody){
//interceptalloptions request
if(req.method =="OPTIONS"){
returntrue;
}else{
returnfalse;
}
},

dealLocalResponse :function(req,reqBody,callback){
if(req.method =="OPTIONS"){
callback(200,mergeCORSHeader(req.headers),"");
}
},

replaceResponseHeader:function(req,res,header){
returnmergeCORSHeader(req.headers, header);
}

};

functionmergeCORSHeader(reqHeader,originHeader){
var targetObj = originHeader || {};

deletetargetObj["Access-Control-Allow-Credentials"];
deletetargetObj["Access-Control-Allow-Origin"];
deletetargetObj["Access-Control-Allow-Methods"];
deletetargetObj["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'] ||"-___-||";

returntargetObj;
}

截獲github.com的https請求,再在最后加點文字

?

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"toallgithub pages
if(req.headers.host =="github.com"){
serverResData +="hello github";
}
callback(serverResData);
},

shouldInterceptHttpsReq :function(req){
//intercepthttps://github.com/
//otherwise,allthe https traffic willnotgo through this proxy

//returntrue;
if(req.headers.host =="github.com"){
returntrue;
}else{
returnfalse;
}
}
};

去除響應頭里緩存相關的頭

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//rulescheme :

module.exports = {
replaceRequestOption :function(req,option){
var newOption =option;
deletenewOption.headers['if-none-match'];
deletenewOption.headers['if-modified-since'];

returnnewOption;
},

replaceResponseHeader:function(req,res,header){
header = header || {};
header["Cache-Control"] ="no-cache, no-store, must-revalidate";
header["Pragma"] ="no-cache";
header["Expires"] = 0;

returnheader;
}
};

在請求發送到服務端前對參數做一些調整

?

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){
//replacerequest towardshttps://www.taobao.com
//tohttps://www.taobao.com/about/

/*
option scheme:
{
hostname : "www.taobao.com"
port : 80
path : "/"
method : "GET"
headers : {cookie:""}
}
*/
if(option.hostname =="www.taobao.com"&&option.path =="/"){
option.path ="/about/";
}
}
};

改變服務端響應的http狀態碼

?

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 towardhttps://www.taobao.com/*
//tohttps://www.etao.com
//using 302

if(req.headers.host =="www.taobao.com"){
statusCode = 302;
}

returnstatusCode;
},

replaceResponseHeader:function(req,res,header){
if(req.headers.host =="www.taobao.com"){
header.location ="https://www.etao.com";
}

returnheader;
}
};

把響應映射到本地

?

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

//replaceallthe imageswithlocalone
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 uselocalresponse
shouldUseLocalResponse :function(req,reqBody){
if(/.(png|gif|jpg|jpeg)$/.test(req.url)){
req.replaceLocalFile =true;
returntrue;
}else{
returnfalse;
}
},

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
https://github.com/alibaba/anyproxy/wiki/What-is-rule-file-and-how-to-write-one
*/
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";
},




//=======================
//whengetting a requestfromuser
//收到用戶請求之后
//=======================

//是否截獲https請求
//should intercept https request,orit will be forwardedtorealserver
shouldInterceptHttpsReq :function(req){
returnfalse;
},

//是否在本地直接發送響應(不再向服務器發出請求)
//whethertointercept this requestbylocallogic
//if thereturnvalueistrue, anyproxy will call dealLocalResponsetoget response dataandwillnotsend requesttoremote server anymore
//reqistheuser'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 tohttps://nodejs.org/api/http.html
//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 senttotheuser
replaceResponseStatusCode:function(req,res,statusCode){
var newStatusCode = statusCode;
returnnewStatusCode;
},

//替換服務器響應的http頭
//replacethe 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 senttotheuser
//you mayreturneither a Bufferora string
//serverResDataisa Buffer.forthose non-unicode reponse , serverResData.toString() shouldnotbe yourfirstchoice.
replaceServerResDataAsync:function(req,res,serverResData,callback){
callback(serverResData);
},

//Deprecated
// replaceServerResData:function(req,res,serverResData){
//returnserverResData;
// },

//在請求返回給用戶前的延遲時間
//adda pause before sending responsetouser
pauseBeforeSendingResponse :function(req,res){
var timeInMS = 1; //delayallrequestsfor1ms
returntimeInMS;
}

};

AnyProxy其他特性

支持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

總結

以上是生活随笔為你收集整理的anyproxy-初识使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。