websocket與RPC

eliwang發表於2024-04-08

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

  • 某音直播資料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()

相關文章