雪球app抓包与mock实战2

socks http socket websocket区别

socks 应用

charles 开启 socks 代理

image

socket

只是一个抽象接口,不是协议

websocket

处于应用层,是一个协议,和 http 协议同级

区别

  • websocket
    应用层的一个协议
  • socks 代理
    一种技术
  • socket
    抽象接口

map

map local

map remote


url 解析

https://book.douban.com/subject/34971214/?icn=index-latestbook-subject

  • https:使用的协议是 https
  • book.douban.com:主机的地址(DNS)
  • /subject/34971214:目录地址
  • ?icn=index-latestbook-subject: 参数是 icn ,值 index-latestbook-subject
  • 端口:http(80),https(443)

map 例子

使用 map local 实现一个小型的服务器

image

image

map remote 实现请求转发
image

mirror

jq

https://stedolan.github.io/jq/

数据

{
  "data": {
    "items": [
      {
        "market": {
          "status_id": 8,
          "region": "US",
          "status": "休市",
          "time_zone": "America/New_York",
          "time_zone_desc": null
        },
        "quote": {
          "symbol": "AAPL",
          "code": "AAPL",
          "exchange": "NASDAQ",
          "name": "苹果",
          "type": 0,
          "sub_type": "257",
          "status": 1,
          "current": 349.72,
          "currency": "USD",
          "percent": -0.57,
          "chg": -2.01,
          "timestamp": 1592596801933,
          "time": 1592596801933,
          "lot_size": 1,
          "tick_size": 0.01,
          "open": 354.635,
          "last_close": 351.73,
          "high": 356.56,
          "low": 345.15,
          "avg_price": 350.582,
          "volume": 66118952,
          "amount": 23180114430,
          "turnover_rate": 1.53,
          "amplitude": 3.24,
          "market_capital": 1515803636200,
          "float_market_capital": null,
          "total_shares": 4334335000,
          "float_shares": null,
          "issue_date": 345398400000,
          "lock_set": 1,
          "current_year_percent": 19.7,
          "high52w": 356.56,
          "low52w": 192.58,
          "variable_tick_size": "0.0001 1 0.01",
          "volume_ratio": 1.88,
          "eps": 13.200410212870025,
          "pe_ttm": 26.493,
          "pe_lyr": 27.4324,
          "navps": 18.1372,
          "pb": 19.328066766974818,
          "dividend": 3.1894989542801837,
          "dividend_yield": 0.9120150275306483,
          "psr": 5.61,
          "short_ratio": 0.85,
          "inst_hld": 49.46,
          "beta": 1.1758,
          "timestamp_ext": 1592611199159,
          "current_ext": 347.135,
          "percent_ext": -0.74,
          "chg_ext": -2.585,
          "contract_size": 100,
          "pe_forecast": 22.634,
          "profit_forecast": 66970000000,
          "profit": 55256000000,
          "profit_four": 57215000000,
          "pledge_ratio": null,
          "goodwill_in_net_assets": null,
          "shareholder_funds": 78425000000
        },
        "tags": []
      },
      {
        "market": {
          "status_id": 8,
          "region": "US",
          "status": "休市",
          "time_zone": "America/New_York",
          "time_zone_desc": null
        },
        "quote": {
          "symbol": "BA",
          "code": "BA",
          "exchange": "NYSE",
          "name": "波音",
          "type": 0,
          "sub_type": "257",
          "status": 1,
          "current": 187.02,
          "currency": "USD",
          "percent": -2.74,
          "chg": -5.27,
          "timestamp": 1592596802416,
          "time": 1592596802416,
          "lot_size": 1,
          "tick_size": 0.01,
          "open": 197.9,
          "last_close": 192.29,
          "high": 197.96,
          "low": 184.571,
          "avg_price": 190.115,
          "volume": 67262082,
          "amount": 12787530719,
          "turnover_rate": 11.92,
          "amplitude": 6.96,
          "market_capital": 105540125835,
          "float_market_capital": null,
          "total_shares": 564325344,
          "float_shares": null,
          "issue_date": -1114761600000,
          "lock_set": 1,
          "current_year_percent": -42.25,
          "high52w": 391,
          "low52w": 89,
          "variable_tick_size": "0.0001 1 0.01",
          "volume_ratio": 0.94,
          "eps": -6.047929685043527,
          "pe_ttm": -30.923,
          "pe_lyr": -165.9436,
          "navps": -17.127,
          "pb": -10.919826780651837,
          "dividend": 6.149259243077696,
          "dividend_yield": 3.2880222666440457,
          "psr": 1.2,
          "short_ratio": 0.34,
          "inst_hld": 57.36,
          "beta": 1.4371,
          "timestamp_ext": 1592611199924,
          "current_ext": 185.7,
          "percent_ext": -0.71,
          "chg_ext": -1.32,
          "contract_size": 100,
          "pe_forecast": -42.014,
          "profit_forecast": -2512000000,
          "profit": -636000000,
          "profit_four": -3413000000,
          "pledge_ratio": null,
          "goodwill_in_net_assets": -83.3626487325401,
          "shareholder_funds": -9665000000
        },
        "tags": []
      },
      {
        "market": {
          "status_id": 8,
          "region": "US",
          "status": "休市",
          "time_zone": "America/New_York",
          "time_zone_desc": null
        },
        "quote": {
          "symbol": "LK",
          "code": "LK",
          "exchange": "NASDAQ",
          "name": "瑞幸咖啡",
          "type": 0,
          "sub_type": "1536",
          "status": 1,
          "current": 3.82,
          "currency": "USD",
          "percent": -3.54,
          "chg": -0.14,
          "timestamp": 1592596800432,
          "time": 1592596800432,
          "lot_size": 1,
          "tick_size": 0.01,
          "open": 3.99,
          "last_close": 3.96,
          "high": 4.12,
          "low": 3.77,
          "avg_price": 3.911,
          "volume": 13902080,
          "amount": 54371035,
          "turnover_rate": 5.52,
          "amplitude": 8.84,
          "market_capital": 961863967,
          "float_market_capital": null,
          "total_shares": 251796850,
          "float_shares": null,
          "issue_date": 1558022400000,
          "lock_set": null,
          "current_year_percent": -90.29,
          "high52w": 51.38,
          "low52w": 1.33,
          "variable_tick_size": "0.0001 1 0.01",
          "volume_ratio": 0.55,
          "eps": -0.19331140685212128,
          "pe_ttm": -2.797,
          "pe_lyr": -4.2047,
          "navps": 3.785951663767624,
          "pb": 1.0571463184442627,
          "dividend": null,
          "dividend_yield": null,
          "psr": 1.73,
          "short_ratio": 0.43,
          "inst_hld": null,
          "beta": null,
          "timestamp_ext": 1592611182702,
          "current_ext": 3.85,
          "percent_ext": 0.79,
          "chg_ext": 0.03,
          "contract_size": 100,
          "pe_forecast": -2.893,
          "profit_forecast": -332785767.8220412,
          "profit": -228975153.0835914,
          "profit_four": -344196965.197347,
          "pledge_ratio": null,
          "goodwill_in_net_assets": null,
          "shareholder_funds": 909868341.0405442
        },
        "tags": []
      },
      {
        "market": {
          "status_id": 8,
          "region": "US",
          "status": "休市",
          "time_zone": "America/New_York",
          "time_zone_desc": null
        },
        "quote": {
          "symbol": "PDD",
          "code": "PDD",
          "exchange": "NASDAQ",
          "name": "拼多多",
          "type": 0,
          "sub_type": "1536",
          "status": 1,
          "current": 87.58,
          "currency": "USD",
          "percent": 6.26,
          "chg": 5.16,
          "timestamp": 1592596800439,
          "time": 1592596800439,
          "lot_size": 1,
          "tick_size": 0.01,
          "open": 85.89,
          "last_close": 82.42,
          "high": 89.25,
          "low": 84.41,
          "avg_price": 87.342,
          "volume": 21769627,
          "amount": 1901402761,
          "turnover_rate": 1.82,
          "amplitude": 5.87,
          "market_capital": 104888106712,
          "float_market_capital": null,
          "total_shares": 1197626247,
          "float_shares": null,
          "issue_date": 1532534400000,
          "lock_set": 1,
          "current_year_percent": 131.57,
          "high52w": 89.25,
          "low52w": 19.105,
          "variable_tick_size": "0.0001 1 0.01",
          "volume_ratio": 2.18,
          "eps": -0.15378118604279528,
          "pe_ttm": -80.585,
          "pe_lyr": -106.5108,
          "navps": 2.640914683297272,
          "pb": 34.16464150286346,
          "dividend": null,
          "dividend_yield": null,
          "psr": 22.55,
          "short_ratio": 4.49,
          "inst_hld": null,
          "beta": null,
          "timestamp_ext": 1592611184088,
          "current_ext": 87.68,
          "percent_ext": 0.11,
          "chg_ext": 0.1,
          "contract_size": 100,
          "pe_forecast": -45.039,
          "profit_forecast": -2330167861.6378884,
          "profit": -985335511.1507077,
          "profit_four": -1302338183.9265764,
          "pledge_ratio": null,
          "goodwill_in_net_assets": null,
          "shareholder_funds": 3070077779.1919446
        },
        "tags": []
      },
      {
        "market": {
          "status_id": 8,
          "region": "CN",
          "status": "休市",
          "time_zone": "Asia/Shanghai",
          "time_zone_desc": null
        },
        "quote": {
          "symbol": "SH600009",
          "code": "600009",
          "exchange": "SH",
          "name": "上海机场",
          "type": 11,
          "sub_type": "ASH",
          "status": 1,
          "current": 71.52,
          "currency": "CNY",
          "percent": 1.49,
          "chg": 1.05,
          "timestamp": 1592550000000,
          "time": 1592550000000,
          "lot_size": 100,
          "tick_size": 0.01,
          "open": 70.6,
          "last_close": 70.47,
          "high": 71.87,
          "low": 70.6,
          "avg_price": 71.296,
          "volume": 10478755,
          "amount": 747089884,
          "turnover_rate": 0.96,
          "amplitude": 1.8,
          "market_capital": 137816068201,
          "float_market_capital": 78205431913,
          "total_shares": 1926958448,
          "float_shares": 1093476397,
          "issue_date": 887731200000,
          "lock_set": null,
          "current_year_percent": -9.18,
          "high52w": 88.9,
          "low52w": 58,
          "limit_up": 77.52,
          "limit_down": 63.42,
          "volume_ratio": 0.74,
          "eps": 1.93,
          "pe_ttm": 37.051,
          "pe_forecast": 427.591,
          "pe_lyr": 27.398,
          "navps": 16.65,
          "pb": 4.295,
          "dividend": 0.79,
          "dividend_yield": 1.105,
          "profit": 5030210052.62,
          "profit_four": 3719591541.27,
          "profit_forecast": 322308230,
          "pledge_ratio": null,
          "goodwill_in_net_assets": null
        },
        "tags": []
      },
      {
        "market": {
          "status_id": 8,
          "region": "CN",
          "status": "休市",
          "time_zone": "Asia/Shanghai",
          "time_zone_desc": null
        },
        "quote": {
          "symbol": "SH603986",
          "code": "603986",
          "exchange": "SH",
          "name": "兆易创新",
          "type": 11,
          "sub_type": "ASH",
          "status": 1,
          "current": 226.3,
          "currency": "CNY",
          "percent": 2.25,
          "chg": 4.99,
          "timestamp": 1592550000000,
          "time": 1592550000000,
          "lot_size": 100,
          "tick_size": 0.01,
          "open": 222.1,
          "last_close": 221.31,
          "high": 232.1,
          "low": 219.03,
          "avg_price": 226.006,
          "volume": 12129178,
          "amount": 2741262975,
          "turnover_rate": 3.06,
          "amplitude": 5.91,
          "market_capital": 106537661548,
          "float_market_capital": 89746500490,
          "total_shares": 470780652,
          "float_shares": 396581973,
          "issue_date": 1471449600000,
          "lock_set": null,
          "current_year_percent": 54.82,
          "high52w": 305.4719,
          "low52w": 56.2959,
          "limit_up": 243.44,
          "limit_down": 199.18,
          "volume_ratio": 1.02,
          "eps": 1.56,
          "pe_ttm": 144.916,
          "pe_forecast": 158.614,
          "pe_lyr": 175.538,
          "navps": 20.65,
          "pb": 10.959,
          "dividend": 0.2591288079952785,
          "dividend_yield": 0.115,
          "profit": 606922090.23,
          "profit_four": 735166977.13,
          "profit_forecast": 671679773,
          "pledge_ratio": 0,
          "goodwill_in_net_assets": 24.051480739324553
        },
        "tags": []
      }
    ],
    "items_size": 6
  },
  "error_code": 0,
  "error_description": ""
}

内容提取

 cat quote.json%3f_t%3d1UNKNOWN18445a009224a2113be35314c80ada75.2917714727.1592724238515.1592724318184%26_s%3da78c10%26x%3d0.246%26symbol%3dAAPL%252CBA%252CLK%252CPDD%252CSH600009%252CSH603986%26extend%3ddetail | jq .data.items[].quote.name

image

对象内容重组

cat quote.json%3f_t%3d1UNKNOWN18445a009224a2113be35314c80ada75.2917714727.1592724238515.1592724318184%26_s%3da78c10%26x%3d0.246%26symbol%3dAAPL%252CBA%252CLK%252CPDD%252CSH600009%252CSH603986%26extend%3ddetail | jq .data.items[]| jq '{"name": .quote.name, "value": 12345}' 

image

数组内容重组

cat quote.json%3f_t%3d1UNKNOWN18445a009224a2113be35314c80ada75.2917714727.1592724238515.1592724318184%26_s%3da78c10%26x%3d0.246%26symbol%3dAAPL%252CBA%252CLK%252CPDD%252CSH600009%252CSH603986%26extend%3ddetail | jq '[.dat
a.items[]|.quote.name]'

image

课堂作业

实现 maplocal 和 mapremote

mitmproxt

安装
pip install pipx
pipx install mitmproxy
https://docs.mitmproxy.org/stable/overview-installation/

证书安装

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

  • 开启 mitmdump 代理
  • 访问 mitm.it 安装证书

录制与回放

录制:mitmdump -w tmp
回放:mitmdump -nC tmp

过滤表达式

过滤:mitmdump -nr tmp -w tmp2 “~s hogwarts”
回放:mitmdump -nC tmp2

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

使用 mitmproxy 实现 maplocal :

"""
This example shows how to send a reply from the proxy immediately
without sending any data to the remote server.
"""
from mitmproxy import http


def request(flow: http.HTTPFlow) -> None:
    # pretty_url takes the "Host" header of the request into account, which
    # is useful in transparent mode where we usually only have the IP otherwise.

    if flow.request.pretty_url == "https://www.baidu.com/":
        flow.response = http.HTTPResponse.make(
            200,  # (optional) status code
            b"Hello World12345",  # (optional) content
            {"Content-Type": "text/html"}  # (optional) headers
        )

脚本

mitmdump -p 8070 -s .\maplocal.py

实现 maplocal 修改雪球

"""
This example shows how to send a reply from the proxy immediately
without sending any data to the remote server.
"""
from mitmproxy import http


def request(flow: http.HTTPFlow) -> None:
    # pretty_url takes the "Host" header of the request into account, which
    # is useful in transparent mode where we usually only have the IP otherwise.

    if "quote.json" in flow.request.pretty_url:
        with open("C:/Users/yuruo/Desktop/quote.json", encoding="utf-8") as f:
            flow.response = http.HTTPResponse.make(
                200,  # (optional) status code
                f.read(),  # (optional) content
                {"Content-Type": "application/json"}  # (optional) headers
            )

map response

import json


def response(flow):
    if "quote.json" in flow.request.pretty_url:
        data = json.loads(flow.response.content)
        data['data']['items'][0]['quote']['name'] = "hogwarts00001"
        data['data']['items'][1]['quote']['name'] = "hogwarts00002"
        data['data']['items'][1]['quote']['current'] = 123000
        flow.response.text = json.dumps(data)

map local

map remote

补交课堂作业:实现map local 和map remote

  • map local

map remote


map local

关闭