可以高度定制的代理服务器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
|
//rule filemodule.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 filemodule.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); } }}; |
把所有的响应延迟1500毫秒
|
1
2
3
4
5
6
7
8
|
module.exports = { pauseBeforeSendingResponse : function(req,res){ //delay all the response for 1500ms return 1500; }}; |
为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
|
//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;} |
截获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" 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/"; } }}; |
改变服务端响应的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 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 onevar 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; }}; |
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的更多相关文章
- 利用Python实现高度定制专属RSS
前言 本文转载自个人博客网站,欢迎来访订阅.本篇属于定制RSS系列终极一弹,是三种方式中自由度最高.定制化最强的,也需要一定的编程能力.附上前两篇链接:1.利用Feed43为网站自制RSS源:2.如何 ...
- PLDroidPlayer 是七牛推出的一款免费的适用于 Android 平台的播放器 SDK,采用全自研的跨平台播放内核,拥有丰富的功能和优异的性能,可高度定制化和二次开发。 https://developer.qiniu.com/pili/sdk/…
PLDroidPlayer PLDroidPlayer 是一个适用于 Android 平台的音视频播放器 SDK,可高度定制化和二次开发,为 Android 开发者提供了简单.快捷的接口,帮助开发者在 ...
- Python 高度定制化自己的线程类和进程类代码,获取启动进程或线程方法的结果(兼容Py2和Py3)
#encoding=utf-8 from threading import Thread from multiprocessing import Process import multiprocess ...
- EAIntroView–高度可定制的iOS应用欢迎页通用解决方案
简介 高度可定制的应用欢迎页通用解决方案,可高度定制,不要仅限于现有的demo. 项目主页: EAIntroView 最新示例: 点击下载 入门 安装 安装后,引入” EAIntroView.h”并设 ...
- Tippy.js - 免费开源且高度可定制的气泡提示独立组件
推荐一个非常优秀的 web 气泡提示独立UI组件. 介绍 Tippy.js 是一款用于Web的完整工具提示,弹出菜单,下拉菜单和菜单解决方案.适用于鼠标,键盘和触摸输入. 特点 超轻量的纯 javas ...
- ECharts-基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表
ECharts http://ecomfe.github.com/echarts 基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽重计算 ...
- TaggingJS – 可以灵活定制的 jQuery 标签系统插件
TaggingJS 是一款 jQuery 插件,用来创建高度可定制的前端标签系统.这款插件不到3KB ,支持主流浏览器.有几种方法来定制 TaggingJS 的默认行为:一是使用 custom_op ...
- tips 前端 各个设备的页面尺寸的media query 与页面高度的经验总结
有段时间 扑了一个多月的在一个wifi的前端项目上 快做完时 各种小问题一堆一堆的修复 处理了一些很零散的问题 因为页面有一个所有页面都有一个背景色 有的页面有背景图 主要重点是移动前端的方向 因为现 ...
- Mitmproxy教程
本文是一个较为完整的 mitmproxy教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 python 知识,且已经安装好了一个 python 3 开 ...
随机推荐
- 【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线 二分+SPFA
题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1 ...
- BZOJ4000 TJOI2015棋盘(状压dp+矩阵快速幂)
显然每一行棋子的某种放法是否合法只与上一行有关,状压起来即可.然后n稍微有点大,矩阵快速幂即可. #include<iostream> #include<cstdio> #in ...
- [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume th ...
- 关于wesocket大文件通讯的切片实现方法
关于websocket的实现网上很多资料这里就不详说,这里大概讲我在websocket传输大文件的时的方法,websocket传输单个文件最大不能超过7kg,否则前段自动断掉,当我们用来语音通讯时,通 ...
- PhoneGap API介绍:File
本文将介绍PhoneGap API——File:通过JavaScript截获本地文件系统.File是用于读取.写入和浏览文件系统层次结构的PhoneGap API. 对象: DirectoryEntr ...
- zabbix问题汇总
1. ::110809.577 resuming IPMI checks on host [10.1.3.41]: connection restored :: seconds :: seconds ...
- 求前n项正整数的倒数和
求前n项正整数的倒数和 前n项正整数的和是一个发散的序列,学过高等数学的这个都知道.所以它没有一个精确的公式,但是近似的公式是有的: 1 + 1/2 + 1/3 + …… + 1/n ≍ ln n + ...
- Codeforces Round #407 (Div. 2) D,E
图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- kubernetes--应用程序健康检查
版权声明:本文属于原创,欢迎转载,转载请保留出处:http://blog.csdn.net/liyingke112 http://blog.csdn.net/liyingke112/article/d ...
- Idrac6 to manage dell server
最近idrac6挂了,java已经升级了 1.安装firefox浏览器,只有火狐是支持idrac最好的 2.安装JDK 3.配置configure java, 4.添加security,edit si ...