websocket与RPC

1、websocket

  • new WebSocket() 是固定的语法,可以作为定位的关键词
  • 常见用法
    • ws.send 用于向服务器发送数据
    • ws.onopen 用于指定连接成功后的回调函数
    • ws.onmessage 用于指定收到服务器数据后的回调函数
  • 定位
    • 定位send
      • 可以先搜索new WebSocket(),找到相应代码位置,打上断点
      • 刷新页面,然后对该对象的send方法进行hook,并在hook代码中加入debugger,进行定位
      • 定位好以后,对于hook的send方法重新赋回原函数
    • 定位onmessage
      • 搜索onmessage 或者 addEventListener("message")
        ws.onmessage = function(event) {
          var data = event.data;
        };
         
        ws.addEventListener("message", function(event) {
          var data = event.data;
        });

2、RPC

  • 服务端通用模板

    import asyncio
    import websockets
    
    async def main_logic(websocket, path):
        if path == '/douyin':
            data = [i for i in range(1,100)] # 模拟待处理数据,可以通过方法去实时获取
            for i in data:
                await websocket.send(str(i))
                recv_text = await websocket.recv()
                # 业务处理逻辑相关代码,对响应数据进行处理
                print(recv_text)
    
    start_server = websockets.serve(main_logic, '127.0.0.1', 9999)
    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()

    如果建立多个连接,会异步处理main_logic中的任务

  • 某音直播数据RPC示例

    • 客户端client.js
      !function () {
          var res = s.toObject();
          if (window.flag_) {
              window.ws_.send(JSON.stringify(res));
          } else {
              var ws = new WebSocket("ws://127.0.0.1:9999");
              window.ws_ = ws;
              window.flag_ = true;
              ws.open = function (evt) {
              };
              ws.onmessage = function (evt) {
                  ws.send(JSON.stringify(res));
              }
          }
      }();
    • 服务端server.py
      import asyncio
      import websockets
      
      async def check_permit(websocket):
          # 在这里可以编写相关业务代码逻辑,用于向客户端发送数据
          send_text = 'xxx'
          await websocket.send(send_text)
          return True
      
      async def recv_msg(websocket):
          while 1:
              recv_text = await websocket.recv()
              # 业务处理逻辑相关代码,对响应数据进行处理
              print(recv_text)
      
      
      async def main_logic(websocket, path):
          await check_permit(websocket)
          await recv_msg(websocket)
      
      start_server = websockets.serve(main_logic, '127.0.0.1', 9999)
      asyncio.get_event_loop().run_until_complete(start_server)
      asyncio.get_event_loop().run_forever()
  • 某博登录参数RPC

    • client.js
      // 该代码放到makeRequest函数内部
      !function () {
          if (window.flag_) {
          } else {
              window.weibo_ = makeRequest;
              var ws = new WebSocket("ws://127.0.0.1:9999");
              window.flag_ = true;
              ws.onopen = function (evt) {
              };
              ws.onmessage = function (evt) {
                  var data_ = evt.data;
                  var result = data_.split(",");
                  var res = window.weibo_(result[0], result[1], 7, false);
                  ws.send(JSON.stringify(res));
              }
          }
      }();
    • server.py
      import asyncio
      import websockets
      async
      def check_permit(websocket): # 账号列表 for send_text in [ '11111111111,111', '11111111112,112', '11111111113,113', '11111111114,114' ]: await websocket.send(send_text) return True async def recv_msg(websocket): while 1: recv_text = await websocket.recv() # 拿到相关加密参数 print(recv_text) async def main_logic(websocket, path): await check_permit(websocket) await recv_msg(websocket) start_server = websockets.serve(main_logic, '127.0.0.1', 9999) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever()
posted @ 2024-04-08 01:00  eliwang  阅读(67)  评论(0编辑  收藏  举报