简体   繁体   English

同时检查多个WebSocket连接

[英]Inspecting multiple WebSocket connections at the same time

I am using solutions provided in following topics to inspect WebSockets traffic (messages) on the web page, which I do not own (solely for learning purposes): 我正在使用以下主题中提供的解决方案来检查我不拥有的WebSockets流量(消息)(仅出于学习目的):

Like this: 像这样:

(function(){
var ws = window.WebSocket;

window.WebSocket = function (a, b, c) {
   var that = c ? new ws(a, b, c) : b ? new ws(a, b) : new ws(a);
   that.addEventListener('open', console.info.bind(console, 'socket open'));
   that.addEventListener('close', console.info.bind(console, 'socket close'));
        that.addEventListener('message', console.info.bind(console, 'socket msg'));
        return that;
    };

    window.WebSocket.prototype=ws.prototype; 
}());

The issue with the provided solutions is that they are listening on only 1 of 3 WebSocket connections ("wss://..."). 提供的解决方案的问题在于,它们仅侦听3个WebSocket连接中的1个(“ wss:// ...”)。 I am able to see in the console the messages that I receive or send, but only for one connection.. Is there something I am missing? 我可以在控制台中看到我收到或发送的消息,但仅用于一个连接。.是否缺少某些内容? Is it possible that two other service are any different and prohibiting the use of prototype extension technique? 其他两项服​​务是否可能有所不同,并禁止使用原型扩展技术?

ps I will not provide an URL to the web resource that I am doing my tests on, in order to avoid possible bans or legal questions. ps为了避免可能的禁令或法律问题,我不会提供正在测试的Web资源的URL。

Okay, since it's been weeks and no answers, then I will post a solution which I ended up using. 好的,因为已经过去了数周,没有答案,所以我将发布一个最终使用的解决方案。

I have built my own Chrome extension that listens to WebSocket connections and forwards all requests and responses to my own WebSocket server (which I happen to run in C# ). 我已经构建了自己的Chrome扩展程序,该扩展程序可以侦听WebSocket连接并将所有请求和响应转发到我自己的WebSocket server (我恰巧在C#运行)。

There are some limitations to this approach. 这种方法有一些局限性。 You are not seeing the request header or who is sending the packets.. You are only able to see the payload and that is it. 您没有看到request header或谁在发送数据包。您只能看到payload ,仅此而已。 Also you are not able to modify the contents in any way or send your own requests (remember - you have no access to header metadata ). 另外,您将无法以任何方式修改内容或发送自己的请求(请记住-您无权访问header metadata )。 Naturally, another limitation is that you have to be running Chrome ( devtools APIs are used).. 自然,另一个限制是您必须运行Chrome (使用devtools APIs )。

Some instructions. 一些指示。

Here is how you attach debugger to listen to network packets: 这是连接调试器以侦听网络数据包的方法:

    chrome.debugger.attach({ tabId: tabId }, "1.2", function () {
        chrome.debugger.sendCommand({ tabId: tabId }, "Network.enable");
        chrome.debugger.onEvent.addListener(onTabDebuggerEvent);
    });

Here is how you catch them: 这是捕获它们的方法:

function onTabDebuggerEvent(debuggeeId, message, params) {
    var debugeeTabId = debuggeeId.tabId;

    chrome.tabs.get(debugeeTabId, function (targetTab) {
        var tabUrl = targetTab.url;

        if (message == "Network.webSocketFrameSent") {

        }
        else if (message == "Network.webSocketFrameReceived") {
            var payloadData = params.response.payloadData;

            var request = {
                source: tabUrl,
                payload: params.response.payloadData
            };

            websocket.send(JSON.stringify(request));
        }
    });
}

Here is how you create a websocket client : 这是创建websocket client

    var websocket = new WebSocket("ws://127.0.0.1:13529");

    setTimeout(() => {
        if (websocket.readyState !== 1) {
            console.log("Unable to connect to a WebsocketServer.");
            websocket = null;
        }
        else {
            console.log("WebsocketConnection started", websocket);

            websocket.onclose = function (evt) {
                console.log("WebSocket connection got closed!");

                if (evt.code == 3001) {
                    console.log('ws closed');
                } else {
                    console.log('ws connection error');
                }

                websocket = null;
            };

            websocket.onerror = function (evt) {
                console.log('ws normal error: ' + evt.type);
                websocket = null;
            };

        }
    }, 3000);

Creating the server is outside the scope of this question. 创建server不在此问题的范围内。 You can use one in Node.js , C# or Java , whatever is preferable for you.. 您可以在Node.jsC#Java使用一个,更适合您。

This is certainly not the most convenient approach, but unlike java-script injection method - it works in all cases. 这当然不是最方便的方法,但是与java-script注入方法不同-它在所有情况下都有效。

Edit: totally forgot to mention. 编辑:完全忘记提及。 There seems to be another way of solving this, BUT I have not dig into that topic therefore maybe this is false info in some way. 似乎有另一种方法可以解决此问题,但是我没有深入探讨该主题,因此也许这在某种程度上是错误的信息。 It should be possible to catch packets on a network interface level, through packet sniffing utilities. 通过数据包嗅探实用程序,应该可以在网络接口级别捕获数据包。 Such as Wireshark or pcap . Wiresharkpcap Maybe something I will investigate further in the future :) 也许将来我会进一步调查:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM