anyproxy-修改返回内容(beforeSendResponse)

前言

fiddler可以抓包打断点后,修改返回的内容,便于模拟各种返回结果。anyproxy也可以通过写rule模块规则,模拟返回状态码、头部、body

beforeSendResponse

beforeSendResponse(requestDetail, responseDetail)

  • AnyProxy向客户端发送请求前,会调用beforeSendResponse,并带上参数requestDetail responseDetail
  • requestDetail 同beforeSendRequest中的参数
  • responseDetail
    response {object} 服务端的返回信息,包括statusCode header body三个字段
    _res {object} 原始的服务端返回对象

举例,请求 http://httpbin.org/get 时,responseDetail参数内容大致如下

{
  response: {
    statusCode: 200,
    header: {
      ‘Content-Type‘: ‘image/gif‘,
      Connection: ‘close‘,
      ‘Cache-Control‘: ‘...‘
    },
    body: ‘...‘
  },
  _res: { /* ... */ }
}

修改返回内容

以下几种返回都是合法的

不做任何处理,返回null

return null;

修改返回的状态码

var newResponse = Object.assign({}, responseDetail.response);
newResponse.statusCode = 404;
return {
  response: newResponse
};

修改返回的内容

var newResponse = Object.assign({}, responseDetail.response);
newResponse.body += ‘--from anyproxy--‘;
return {
  response: newResponse
};

rule案例

这里提供一些样例,来讲解规则模块的常见用法
你可以通过 anyproxy --rule http://....js 来加载模块并体验

用curl发请求测试的方法如下

返回本地json格式body

使用本地数据,拦截发送到 http://httpbin.org 的请求,使用本地数据代替服务端返回,sample_use_local_response.js规则

/*
  sample:
    intercept all requests toward httpbin.org, use a local response
  test:
    curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
*/
module.exports = {
  *beforeSendRequest(requestDetail) {
    const localResponse = {
      statusCode: 200,
      header: { ‘Content-Type‘: ‘application/json‘ },
      body: ‘{"hello": "this is local response"}‘
    };
    if (requestDetail.url.indexOf(‘http://httpbin.org‘) === 0) {
      return {
        response: localResponse
      };
    }
  },
};

加载rule规则的js文件

anyproxy -i --rule sample_use_local_response.js

使用curl测试http://httpbin.org

C:\Users\dell>curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
{"hello": "this is local response"}

模拟返回状态码

模拟返回404状态码,sample_modify_response_statuscode.js规则如下

/*
  sample:
    modify all status code of http://httpbin.org/ to 404
  test:
    curl -I ‘http://httpbin.org/user-agent‘ --proxy http://127.0.0.1:8001
  expected response:
    HTTP/1.1 404 Not Found
*/
module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url.indexOf(‘http://httpbin.org‘) === 0) {
      const newResponse = responseDetail.response;
      newResponse.statusCode = 404;
      return {
        response: newResponse
      };
    }
  }
};

模拟返回头部

修改返回的头部,sample_modify_response_header.js规则如下

/*
  sample:
    modify response header of http://httpbin.org/user-agent
  test:
    curl -I ‘http://httpbin.org/user-agent‘ --proxy http://127.0.0.1:8001
  expected response:
    X-Proxy-By: AnyProxy
*/
module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url.indexOf(‘http://httpbin.org/user-agent‘) === 0) {
      const newResponse = responseDetail.response;
      newResponse.header[‘X-Proxy-By‘] = ‘AnyProxy‘;
      return {
        response: newResponse
      };
    }
  }
};

修改返回body

修改返回的body里面部分内容,sample_modify_response_data.js规则如下

/*
  sample:
    modify response data of http://httpbin.org/user-agent
  test:
    curl ‘http://httpbin.org/user-agent‘ --proxy http://127.0.0.1:8001
  expected response:
    { "user-agent": "curl/7.43.0" } -- AnyProxy Hacked! --
*/

module.exports = {
  *beforeSendResponse(requestDetail, responseDetail) {
    if (requestDetail.url === ‘http://httpbin.org/user-agent‘) {
      const newResponse = responseDetail.response;
      newResponse.body += ‘-- AnyProxy Hacked! --‘;
      return new Promise((resolve, reject) => {
        setTimeout(() => { // delay the response for 5s
          resolve({ response: newResponse });
        }, 5000);
      });
    }
  },
};

更多学习资料参考https://github.com/alibaba/anyproxy/blob/master/docs/cn/src_doc.md

转自:https://www.cnblogs.com/yoyoketang/p/10878193.html

原文地址:https://www.cnblogs.com/dreamhighqiu/p/10990047.html

时间: 2024-07-31 01:54:50

anyproxy-修改返回内容(beforeSendResponse)的相关文章

WPF Paragraph获取或修改文本内容

一.说明 Paragraph继承自Block,Block继承自TextElement,在TextElement中 // // 摘要: // 获取表示元素中内容末尾的 System.Windows.Documents.TextPointer. // // 返回结果: // 表示 System.Windows.Documents.TextElement 中内容末尾的 System.Windows.Documents.TextPointer. public TextPointer ContentEnd

linux下C++修改文件内容

C fwrite在任意位置写入文件,并可修改文件内容 想实现类似迅雷那样下载时可以从文件半中间写入的功能 1 #include<stdio.h> 2 int main() 3 { 4 FILE *fp; 5 fp=fopen("overwrite.bin","rb+"); //使用rb+模式,可以往半中间插入数据,而且是覆盖插入,若使用"ab+"每次都插入到最后面,调用fseek也没用 6 if(NULL != fp) 7 { 8 i

python ssh 连接远程服务器,修改文本内容,调用脚本

今天小编get到一个用python的paramiko库创建ssh对象,连接到远程服务器,并且修改文件内容,调用脚本的好方法!! 主角当然是paramiko库啦,利用paramiko创建一个ssh对象,用于连接远程服务器 import paramiko ssh = paramiko.SSHClient()#创建SSH对象 设置允许连接不在know_hosts的主机,ssh会将 访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts,当下次访问相同计算机时,OpenS

java修改文件内容

文件的读和写,大家都不陌生,但是修改呢?按照普通的读写流去修改的话,只能全部读取出来,在内存中修改好后,全部写进去,这样对于文件内容过多的时,性能很低. 最近在遇到这个问题的时候,发现RandomAccessFile这个类正好能解决我的问题,废话不多说,下面直接贴代码,分享给大家,有不对的地方欢迎指教,谢谢 /**     * 修改文件内容     * @param fileName     * @param oldstr     * @param newStr     * @return  

测试事件响应修改界面内容

package com.swing.demo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing

[转载]sed实现直接修改文件内容

sed实现直接修改文件内容 sed是实现对流的编辑.通常,我们使用sed可以实现内容的编辑后然后保存成另外的一个文件,如果正确的话,才写入到源文件.但是某些时候,我们需要直接修改文件,因为,保存文件到一个文件,然后再覆盖原文件的办法显得很麻烦.其实很简单,只需要一个 -i 参数就可以了.比如,我想替换文件中的 properties 为 property ,可以使用 sed  's/properties/property/g'  build.xml这种方式,其实并没有修改build.xml文件的内

WordPress批量修改文章内容、URL链接、文章摘要

通过SQL语句来批量修改wordpress博客内容,文章中所有语句都使用默认的wp_表前缀,如果您的数据表前缀不是wp_则需要在语句中作相应更改. 方法/步骤 批量修改文章内容 如果您想替换之前写过的所有文章中的某些内容,如更换博客的名称.更换博客的网址.更换文章配图的链接等,您可以使用以下SQL语句: UPDATE wp_postsSET post_content = REPLACE( post_content,'旧的博客名', '新的博客名' ); 该语句的功能是将所有文章中的“旧的博客名”

ASP函数:根据表和ID和字段名,返回内容

'//根据表和ID和字段名,返回内容Function dsf_fieldValueFromTable(fTable,id,fieldName) dim rs,sql set rs=server.createobject("adodb.recordset") sql="select * from " & fTable & " where id=" & id rs.open sql,conn,1,1 if not rs.eof

命令行修改文件文件夹访问权限 cacls, 修改hosts内容方法

背景 日常使用Windows的过程中,hosts(C:\Windows\System32\drivers\etc)文件可能被一些程序串改,因此在网上找到禁止/允许修改hosts文件的bat脚本,但是在使用过程中有遇到了新的麻烦,特记录下相关问题的解决方法. 禁止/允许修改hosts文件的bat脚本 Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库",当用户在浏览器中输入一个需要登录的网址时,系统会首先自动