接口 mock 实战 (二)

标题

雪球app抓包与mock实战

大纲

  • mitmproxy 使用
  • ssl 通信原理

介绍

mitmproxy说明文档

https://docs.mitmproxy.org/stable/

分类:

  1. mitmproxy:交互式工具(不支持 windows )
  2. mitmweb: web UI 代理工具(可视化,功能少)

  1. mitmdump: 可扩展的无 UI 代理工具(扩展功能强,可人为写插件增强)

安装

pip install mitmproxy

证书安装

https://docs.mitmproxy.org/stable/concepts-certificates/

Certificates

mitmproxy 使用

监听 8080 端口,提高显示等级

mitmdump --flow-detail 3

脚本:实现请求计算器,每来一个请求,计算器 + 1 ,并且打印

from mitmproxy import ctx


class Counter:
    def __init__(self):
        self.num = 0

    def request(self, flow):
        self.num = self.num + 1
        ctx.log.info("hello")
        ctx.log.info("We've seen %d flows" % self.num)


addons = [
    Counter()
]

实现 maplocal

from mitmproxy import ctx
from mitmproxy import http


class ABC:
    def __init__(self):
        self.num = 0
    def request(self, flow: http.HTTPFlow) -> None:
        if "quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url:
          with open(r"C:\Users\yuruo\Desktop\tmp.json", 'r', encoding="utf-8") as f:
            flow.response = http.HTTPResponse.make(
                200,  # (optional) status code
                f.read(),  # (optional) content
                {"Content-Type": "application/json"}  # (optional) headers
            ) 

addons = [
    ABC()
]

rsa 握手阶段