diff --git a/rule_sample/README.md b/rule_sample/README.md index 7380b3788..cdbfc3ffe 100644 --- a/rule_sample/README.md +++ b/rule_sample/README.md @@ -1,3 +1,77 @@ + +首先我得谈一下代理所必需实现的效果,除了下面提出的三个阶段的对请求及应答的数据进行处理之外,必须要满足常用场景的简单便捷的配置,降低使用的门槛,我这里要做再做几个示例,以针对各种常用场景的实现做参考使用 + + +这里,我们把http通信过程中的各个阶段进行抽离,分解成三个阶段: + +* 收到来自客户端请求之后,允许开发者直接从本地提供返回 +* 在转发请求到服务器前,允许开发者对发送的请求进行修改 +* 在收到服务器响应之后,允许开发者对响应内容进行修改,再返回给客户端 + +对于上述每个阶段,我们都提供了API接口,引入开发者编写自己的规则代码,实时干预通信过程,以此满足各类自定义需求。 + +具体地,我们提供的接口包括: + +* 收到用户请求之后 + * **shouldUseLocalResponse** ,是否在本地直接发送响应(不再向服务器发出请求) + * **dealLocalResponse** 如果shouldUseLocalResponse返回true,会调用这个函数来获取本地响应内容(异步接口) +* 向服务端发出请求之前 + * **replaceRequestProtocol** 替换向服务器发出的请求协议,支持http和https的替换 + * **replaceRequestOption** 替换向服务器发出的请求参数,即nodeJS中的 [request option](http://nodejs.org/api/http.html#http_http_request_options_callback) + * **replaceRequestData** 替换请求的body +* 向用户返回服务端的响应之前 + * **replaceResponseStatusCode** 替换服务器响应的http状态码 + * **replaceResponseHeader** 替换服务器响应的http头 + * **replaceServerResDataAsync** 替换服务器响应的数据(异步接口) + * **pauseBeforeSendingResponse** 在请求返回给用户前的延迟时间 + + +AnyProxy规则文件样例 +--------------- +以“防止CDN返回304”这个需求为例,最直接的方案是拦截请求,在发送到CDN前删除header中的`if-modified-since`字段。在AnyProxy中,配置replaceRequestOption接口,3行代码就能实现这个自定义功能: + +```javascript +//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行代码。 + +```javascript +//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); + } + } +}; +``` + +响应延迟1500ms + +``` +module.exports = { + pauseBeforeSendingResponse : function(req,res){ + //delay all the response for 1500ms + return 1500; + } +}; +``` + + The following are sample rules. * rule__blank.js @@ -29,4 +103,4 @@ The following are sample rules. * 为请求绑定目标ip * rule_use_local_data.js * map some requests to local file - * 把图片响应映射到本地 \ No newline at end of file + * 把图片响应映射到本地 diff --git a/rule_sample/ajax.js b/rule_sample/ajax.js new file mode 100644 index 000000000..0acf68107 --- /dev/null +++ b/rule_sample/ajax.js @@ -0,0 +1,139 @@ + +// 爱抢购测试,重定向域名响应 + +module.exports = { + summary:function(){ + return "重定向ajax"; + }, + + + + //======================= + //收到用户请求之后 + //======================= + + //是否截获https请求 + shouldInterceptHttpsReq :function(req){ + return false; + }, + + //是否在本地直接发送响应(不再向服务器发出请求) + shouldUseLocalResponse : function(req,reqBody){ + if(req.method == "OPTIONS"){ + return true; + }else{ + return false; + } + // return false; + }, + + dealLocalResponse : function(req,reqBody,callback){ + if(req.method == "OPTIONS"){ + callback(200,mergeCORSHeader(req.headers),""); + } + callback(statusCode,resHeader,responseData) + }, + + + + //======================= + //向服务端发出请求之前 + //======================= + + //替换向服务器发出的请求协议(http和https的替换) + replaceRequestProtocol:function(req,protocol){ + var newProtocol = protocol; + return newProtocol; + }, + + //替换向服务器发出的请求参数(option) + replaceRequestOption : function(req,option){ + var newOption = option; + + /* + option scheme: + { + hostname : "www.taobao.com" + port : 80 + path : "/" + method : "GET" + headers : {cookie:""} + } + */ + + //options : http://nodejs.org/api/http.html#http_http_request_options_callback + if(option.hostname == "10.0.0.119" ){ + console.log('已经重定向') + } + if(option.hostname == "10.0.0.119" ){ + if( /^\/api\//.test(option.path) ){ + console.log('这是请求:' + option.path) + // if(newOption.path=="/home.php" ){ + // newOption.hostname = "m.jjhh.com"; + // newOption.path = "/jjhh/home.php"; + // newOption.port = "80"; + // } + newOption.port = "8181"; + newOption.hostname = "dev.iqianggou.lab"; + //TODO: 以下静态资源没有全部正确加载,为什么呢??? + //目前,没正确加载的 mime type 为 text/html,正确加载的为 image/jpeg + // if(/\.(png|gif|jpg|jpeg)$/.test(req.url)){ + // newOption.hostname = "m.jjhh.com"; + // newOption.port = "80"; + } + } + + return newOption; + }, + + //替换请求的body + replaceRequestData: function(req,data){ + return data; + }, + + + + //======================= + //向用户返回服务端的响应之前 + //======================= + + //替换服务器响应的http状态码 + replaceResponseStatusCode: function(req,res,statusCode){ + var newStatusCode = statusCode; + return newStatusCode; + }, + + //替换服务器响应的http头 Here header == res.headers + replaceResponseHeader: function(req,res,header){ + //var newHeader = header || {}; + return mergeCORSHeader(req.headers, header); + //return newHeader; + }, + + //替换服务器响应的数据 + replaceServerResDataAsync: function(req,res,serverResData,callback){ + callback(serverResData); + }, + + //在请求返回给用户前的延迟时间 + pauseBeforeSendingResponse : function(req,res){ + var timeInMS = 1; //delay all requests for 1ms + return timeInMS; + } +}; + +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; +} \ No newline at end of file diff --git a/rule_sample/clear_cache.js b/rule_sample/clear_cache.js new file mode 100644 index 000000000..3f41f0a13 --- /dev/null +++ b/rule_sample/clear_cache.js @@ -0,0 +1,25 @@ + +// 去除响应头里缓存相关的头 + +module.exports = { + summary:function(){ + return "去除响应头里缓存相关的头"; + }, + //在向服务器发出请求前,AnyProxy会调用这个接口,可以在此时修改发送请求的参数 + replaceRequestOption : function(req,option){ + var newOption = option; + delete newOption.headers['if-none-match']; + delete newOption.headers['if-modified-since']; //防止CDN返回304 + + 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; + } +}; \ No newline at end of file diff --git a/rule_sample/limit.js b/rule_sample/limit.js new file mode 100644 index 000000000..02543d629 --- /dev/null +++ b/rule_sample/limit.js @@ -0,0 +1,113 @@ + +// 爱抢购测试,重定向域名响应 + +module.exports = { + summary:function(){ + return "重定向ajax"; + }, + + + + //======================= + //收到用户请求之后 + //======================= + + //是否截获https请求 + shouldInterceptHttpsReq :function(req){ + return false; + }, + + //是否在本地直接发送响应(不再向服务器发出请求) + shouldUseLocalResponse : function(req,reqBody){ + return false; + }, + + dealLocalResponse : function(req,reqBody,callback){ + callback(statusCode,resHeader,responseData) + }, + + + + //======================= + //向服务端发出请求之前 + //======================= + + //替换向服务器发出的请求协议(http和https的替换) + replaceRequestProtocol:function(req,protocol){ + var newProtocol = protocol; + return newProtocol; + }, + + //替换向服务器发出的请求参数(option) + replaceRequestOption : function(req,option){ + var newOption = option; + + /* + option scheme: + { + hostname : "www.taobao.com" + port : 80 + path : "/" + method : "GET" + headers : {cookie:""} + } + */ + + //options : http://nodejs.org/api/http.html#http_http_request_options_callback + if(option.hostname == "h5.yanzhanjun.iqg.iqianggou.lab" ){ + console.log('已经重定向') + } + if(option.hostname == "m.iqianggou.com" ){ + if( !/^\/api\//.test(option.path) ){ + console.log('这是请求:' + option.path) + // if(newOption.path=="/home.php" ){ + // newOption.hostname = "m.jjhh.com"; + // newOption.path = "/jjhh/home.php"; + // newOption.port = "80"; + // } + //newOption.hostname = "h5.yanzhanjun.iqg.iqianggou.lab"; + //TODO: 以下静态资源没有全部正确加载,为什么呢??? + //目前,没正确加载的 mime type 为 text/html,正确加载的为 image/jpeg + // if(/\.(png|gif|jpg|jpeg)$/.test(req.url)){ + // newOption.hostname = "m.jjhh.com"; + // newOption.port = "80"; + } + } + + return newOption; + }, + + //替换请求的body + replaceRequestData: function(req,data){ + return data; + }, + + + + //======================= + //向用户返回服务端的响应之前 + //======================= + + //替换服务器响应的http状态码 + replaceResponseStatusCode: function(req,res,statusCode){ + var newStatusCode = statusCode; + return newStatusCode; + }, + + //替换服务器响应的http头 Here header == res.headers + replaceResponseHeader: function(req,res,header){ + var newHeader = header || {}; + return newHeader; + }, + + //替换服务器响应的数据 + replaceServerResDataAsync: function(req,res,serverResData,callback){ + callback(serverResData); + }, + + //在请求返回给用户前的延迟时间 + pauseBeforeSendingResponse : function(req,res){ + var timeInMS = 1; //delay all requests for 1ms + return timeInMS; + } +}; diff --git a/rule_sample/no_304.js b/rule_sample/no_304.js new file mode 100644 index 000000000..8b03ba936 --- /dev/null +++ b/rule_sample/no_304.js @@ -0,0 +1,13 @@ +// 防止CDN返回304 + +module.exports = { + summary:function(){ + return "防止CDN返回304,但不包括字体"; + }, + //在向服务器发出请求前,AnyProxy会调用这个接口,可以在此时修改发送请求的参数 + replaceRequestOption : function(req,option){ + var newOption = option; + delete newOption.headers['if-modified-since']; + return newOption; + } +}; \ No newline at end of file diff --git a/rule_sample/rule__blank.js b/rule_sample/rule__blank.js index 30d3c5483..ed2e1a78d 100644 --- a/rule_sample/rule__blank.js +++ b/rule_sample/rule__blank.js @@ -6,7 +6,7 @@ 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"; diff --git a/rule_sample/rule_adjust_response_time.js b/rule_sample/rule_adjust_response_time.js index 32ec584be..8f45fb96f 100644 --- a/rule_sample/rule_adjust_response_time.js +++ b/rule_sample/rule_adjust_response_time.js @@ -1,4 +1,5 @@ //rule scheme : +//把所有的响应延迟1500毫秒 module.exports = { diff --git a/rule_sample/rule_allow_CORS.js b/rule_sample/rule_allow_CORS.js index 182152cc0..58b90fc1f 100644 --- a/rule_sample/rule_allow_CORS.js +++ b/rule_sample/rule_allow_CORS.js @@ -1,5 +1,6 @@ //rule scheme : // Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS +// 为ajax请求增加跨域头 module.exports = { shouldUseLocalResponse : function(req,reqBody){ diff --git a/rule_sample/rule_intercept_some_https_requests.js b/rule_sample/rule_intercept_some_https_requests.js index 825e57c73..5355bbbe0 100644 --- a/rule_sample/rule_intercept_some_https_requests.js +++ b/rule_sample/rule_intercept_some_https_requests.js @@ -1,4 +1,5 @@ //rule scheme : +//截获github.com的https请求,再在最后加点文字 module.exports = { diff --git a/rule_sample/rule_remove_cache_header.js b/rule_sample/rule_remove_cache_header.js index 4018ddbfb..080e13755 100644 --- a/rule_sample/rule_remove_cache_header.js +++ b/rule_sample/rule_remove_cache_header.js @@ -1,6 +1,10 @@ //rule scheme : +//去除响应头里缓存相关的头 module.exports = { + summary:function(){ + return "去除响应头里缓存相关的头"; + }, replaceRequestOption : function(req,option){ var newOption = option; delete newOption.headers['if-none-match']; diff --git a/rule_sample/rule_replace_request_option.js b/rule_sample/rule_replace_request_option.js index 6f3930c03..df987617d 100644 --- a/rule_sample/rule_replace_request_option.js +++ b/rule_sample/rule_replace_request_option.js @@ -1,4 +1,5 @@ //rule scheme : +//在请求发送到服务端前对参数做一些调整 module.exports = { diff --git a/rule_sample/rule_replace_response_data.js b/rule_sample/rule_replace_response_data.js index a8ffbe253..e2426928e 100644 --- a/rule_sample/rule_replace_response_data.js +++ b/rule_sample/rule_replace_response_data.js @@ -1,4 +1,5 @@ //rule scheme : +//修改响应数据 module.exports = { diff --git a/rule_sample/rule_replace_response_status_code.js b/rule_sample/rule_replace_response_status_code.js index 24b7777f3..ae8fb40cf 100644 --- a/rule_sample/rule_replace_response_status_code.js +++ b/rule_sample/rule_replace_response_status_code.js @@ -1,4 +1,5 @@ //rule scheme : +//改变服务端响应的http状态码 module.exports = { diff --git a/rule_sample/rule_reverse_proxy.js b/rule_sample/rule_reverse_proxy.js index 4aa456d83..991156948 100644 --- a/rule_sample/rule_reverse_proxy.js +++ b/rule_sample/rule_reverse_proxy.js @@ -2,6 +2,9 @@ read the following wiki before using rule file https://github.com/alibaba/anyproxy/wiki/What-is-rule-file-and-how-to-write-one */ + +//为请求绑定目标ip + module.exports = { summary:function(){ diff --git a/rule_sample/rule_test.js b/rule_sample/rule_test.js new file mode 100644 index 000000000..3b4224e25 --- /dev/null +++ b/rule_sample/rule_test.js @@ -0,0 +1,205 @@ +/* +read the following wiki before using rule file +https://github.com/alibaba/anyproxy/wiki/What-is-rule-file-and-how-to-write-one +*/ + +/* +接口包括: + +收到用户请求之后 + shouldUseLocalResponse ,是否在本地直接发送响应(不再向服务器发出请求) + dealLocalResponse 如果shouldUseLocalResponse返回true,会调用这个函数来获取本地响应内容(异步接口) +向服务端发出请求之前 + replaceRequestProtocol 替换向服务器发出的请求协议,支持http和https的替换 + replaceRequestOption 替换向服务器发出的请求参数,即nodeJS中的 request option + replaceRequestData 替换请求的body +向用户返回服务端的响应之前 + replaceResponseStatusCode 替换服务器响应的http状态码 + replaceResponseHeader 替换服务器响应的http头 + replaceServerResDataAsync 替换服务器响应的数据(异步接口) + pauseBeforeSendingResponse 在请求返回给用户前的延迟时间 +*/ + + +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 "代理设定:变更请求参数"; + }, + + //======================= + //收到用户请求之后 + //======================= + + //是否截获https请求 + shouldInterceptHttpsReq :function(req){ + return false; + }, + + //是否在本地直接发送响应(不再向服务器发出请求) + shouldUseLocalResponse : function(req,reqBody){ + //为ajax请求增加跨域头(1) + // if(req.method == "OPTIONS"){ + // return true; + // }else{ + // return false; + // } + + return false; + }, + + //如果shouldUseLocalResponse返回true,会调用这个函数来获取本地响应内容 + //e.g. callback(200,{"content-type":"text/html"},"hello world") + dealLocalResponse : function(req,reqBody,callback){ + callback(statusCode,resHeader,responseData) + + //为ajax请求增加跨域头(2) + // if(req.method == "OPTIONS"){ + // callback(200,mergeCORSHeader(req.headers),""); + // } + }, + + + + //======================= + //向服务端发出请求之前 + //======================= + + //替换向服务器发出的请求协议(http和https的替换) + //protocol : "http" or "https" + replaceRequestProtocol:function(req,protocol){ + var newProtocol = protocol; + return newProtocol; + }, + + //替换向服务器发出的请求参数(option) + replaceRequestOption : function(req,option){ + var newOption = option; + + //去除响应头里缓存相关的头(1) + delete newOption.headers['if-none-match']; + delete newOption.headers['if-modified-since']; //防止CDN返回304 + + //options : http://nodejs.org/api/http.html#http_http_request_options_callback + // if(newOption.hostname == "www.shuoyiju.com"){ + // newOption.hostname = "m.iqianggou.com"; + // newOption.port = "80"; + // } + + /* + option scheme: + { + hostname : "www.taobao.com" + port : 80 + path : "/" + method : "GET" + headers : {cookie:""} + } + */ + //http://staging.iqianggou.lab/api/wechat/snsapi/userinfo + // if(newOption.hostname == "www.taobao.com" && newOption.path == "/"){ + // option.path = "/about/"; + // } + + return newOption; + }, + + //替换请求的body + replaceRequestData: function(req,data){ + return data; + }, + + + + //======================= + //向用户返回服务端的响应之前 + //======================= + + //替换服务器响应的http状态码 + replaceResponseStatusCode: function(req,res,statusCode){ + var newStatusCode = statusCode; + newStatusCode = 200; + return newStatusCode; + }, + + //替换服务器响应的http头 + //Here header == res.headers + replaceResponseHeader: function(req,res,header){ + //去除响应头里缓存相关的头(3) + //return mergeCORSHeader(req.headers, header); + + var newHeader = header || {}; + + //去除响应头里缓存相关的头(2) + newHeader["Cache-Control"] = "no-cache, no-store, must-revalidate"; + newHeader["Pragma"] = "no-cache"; + newHeader["Expires"] = 0; + + return newHeader; + }, + + //替换服务器响应的数据 + //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); + + if(/\/api\/wechat\/snsapi\/userinfo/i.test(req.url) && /application\/json/i.test(res.headers['content-type'])){ + var newDataStr = serverResData.toString(); + newDataStr = { + "status": { + "code": 10000, + "message": "成功", + "alert": "", + "server_time": 1435299927 + }, + "data": { + "openid": "oNDT-jg2hbx0SGwX-WP-BiaFNWUQ", + "nickname": "晓寒", + "sex": 1, + "language": "zh_CN", + "city": "普陀", + "province": "上海", + "country": "中国", + "headimgurl": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLAyWJdDNJZjR7fSAcZaYJKaBfnEnvvicYMNcBHgdxbK0Ub9HUl2jac0RIXUHMmuia9BetfKPbMCsfeQ/0", + "unionid": "o31SljhvIXvUZT1ydpGgIxT6DEY8" + } + } + callback( JSON.stringify(newDataStr) ); + }else{ + callback(serverResData); + } + }, + + //Deprecated + // replaceServerResData: function(req,res,serverResData){ + // return serverResData; + // }, + + //在请求返回给用户前的延迟时间 + pauseBeforeSendingResponse : function(req,res){ + var timeInMS = 1; //delay all requests for 1ms + return timeInMS; + } + +}; + +//去除响应头里缓存相关的头(4) +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; +} \ No newline at end of file diff --git a/rule_sample/rule_use_local_data.js b/rule_sample/rule_use_local_data.js index f560f6d8b..b9855409c 100644 --- a/rule_sample/rule_use_local_data.js +++ b/rule_sample/rule_use_local_data.js @@ -1,4 +1,6 @@ //replace all the images with local one +//把图片响应映射到本地 + var fs = require("fs"); var LOCAL_IMAGE = "/Users/path/to/image.png"; diff --git a/rule_sample/test.js b/rule_sample/test.js new file mode 100644 index 000000000..ef98fb493 --- /dev/null +++ b/rule_sample/test.js @@ -0,0 +1,110 @@ + +// 重定向ajax + +module.exports = { + summary:function(){ + return "重定向ajax"; + }, + + + + //======================= + //收到用户请求之后 + //======================= + + //是否截获https请求 + shouldInterceptHttpsReq :function(req){ + return false; + }, + + //是否在本地直接发送响应(不再向服务器发出请求) + shouldUseLocalResponse : function(req,reqBody){ + return false; + }, + + dealLocalResponse : function(req,reqBody,callback){ + callback(statusCode,resHeader,responseData) + }, + + + + //======================= + //向服务端发出请求之前 + //======================= + + //替换向服务器发出的请求协议(http和https的替换) + replaceRequestProtocol:function(req,protocol){ + var newProtocol = protocol; + return newProtocol; + }, + + //替换向服务器发出的请求参数(option) + replaceRequestOption : function(req,option){ + var newOption = option; + + /* + option scheme: + { + hostname : "www.taobao.com" + port : 80 + path : "/" + method : "GET" + headers : {cookie:""} + } + */ + + //options : http://nodejs.org/api/http.html#http_http_request_options_callback + if(newOption.hostname == "staging.iqianggou.lab"){ + // && newOption.port == "8000" + // if( /\/api\/wechat\//.test(newOption.path) ){ + // //newOption.hostname = "m.jjhh.com"; + // //newOption.path = "/jjhh/home.php"; + // newOption.port = "80"; + // } + newOption.hostname == "staging.iqianggou.lab" + + //TODO: 以下静态资源没有全部正确加载,为什么呢??? + //目前,没正确加载的 mime type 为 text/html,正确加载的为 image/jpeg + // if(/\.(png|gif|jpg|jpeg)$/.test(req.url)){ + // newOption.hostname = "m.jjhh.com"; + // newOption.port = "80"; + // } + } + + return newOption; + }, + + //替换请求的body + replaceRequestData: function(req,data){ + return data; + }, + + + + //======================= + //向用户返回服务端的响应之前 + //======================= + + //替换服务器响应的http状态码 + replaceResponseStatusCode: function(req,res,statusCode){ + var newStatusCode = statusCode; + return newStatusCode; + }, + + //替换服务器响应的http头 Here header == res.headers + replaceResponseHeader: function(req,res,header){ + var newHeader = header || {}; + return newHeader; + }, + + //替换服务器响应的数据 + replaceServerResDataAsync: function(req,res,serverResData,callback){ + callback(serverResData); + }, + + //在请求返回给用户前的延迟时间 + pauseBeforeSendingResponse : function(req,res){ + var timeInMS = 1; //delay all requests for 1ms + return timeInMS; + } +}; \ No newline at end of file diff --git a/rule_sample/zhangchao.js b/rule_sample/zhangchao.js new file mode 100644 index 000000000..093e3ee66 --- /dev/null +++ b/rule_sample/zhangchao.js @@ -0,0 +1,116 @@ + +// 爱抢购测试,重定向域名响应 + +module.exports = { + summary:function(){ + return "重定向ajax"; + }, + + + + //======================= + //收到用户请求之后 + //======================= + + //是否截获https请求 + shouldInterceptHttpsReq :function(req){ + return false; + }, + + //是否在本地直接发送响应(不再向服务器发出请求) + shouldUseLocalResponse : function(req,reqBody){ + return false; + }, + + dealLocalResponse : function(req,reqBody,callback){ + callback(statusCode,resHeader,responseData) + }, + + + + //======================= + //向服务端发出请求之前 + //======================= + + //替换向服务器发出的请求协议(http和https的替换) + replaceRequestProtocol:function(req,protocol){ + var newProtocol = protocol; + return newProtocol; + }, + + //替换向服务器发出的请求参数(option) + replaceRequestOption : function(req,option){ + var newOption = option; + + /* + option scheme: + { + hostname : "www.taobao.com" + port : 80 + path : "/" + method : "GET" + headers : {cookie:""} + } + */ + + //options : http://nodejs.org/api/http.html#http_http_request_options_callback + if(option.hostname == "m.iqianggou.com" ){ + console.log('已经重定向') + } + if(option.hostname == "m.iqianggou.com" ){ + if( /^\/api\//.test(option.path) ){ + console.log('这是请求:' + option.path) + // if(newOption.path=="/home.php" ){ + // newOption.hostname = "m.jjhh.com"; + // newOption.path = "/jjhh/home.php"; + // newOption.port = "80"; + // } + newOption.hostname = "10.0.0.127"; + newOption.hostname = "staging.iqianggou.com"; + newOption.hostname = "dev.iqianggou.lab"; + newOption.port = "8181"; + //TODO: 以下静态资源没有全部正确加载,为什么呢??? + //目前,没正确加载的 mime type 为 text/html,正确加载的为 image/jpeg + // if(/\.(png|gif|jpg|jpeg)$/.test(req.url)){ + // newOption.hostname = "m.jjhh.com"; + // newOption.port = "80"; + } + } + + return newOption; + }, + + //替换请求的body + replaceRequestData: function(req,data){ + return data; + }, + + + + //======================= + //向用户返回服务端的响应之前 + //======================= + + //替换服务器响应的http状态码 + replaceResponseStatusCode: function(req,res,statusCode){ + var newStatusCode = statusCode; + return newStatusCode; + }, + + //替换服务器响应的http头 Here header == res.headers + replaceResponseHeader: function(req,res,header){ + var newHeader = header || {}; + return newHeader; + }, + + //替换服务器响应的数据 + replaceServerResDataAsync: function(req,res,serverResData,callback){ + callback(serverResData); + }, + + //在请求返回给用户前的延迟时间 + pauseBeforeSendingResponse : function(req,res){ + var timeInMS = 1; //delay all requests for 1ms + return timeInMS; + } +};