简体   繁体   English

是否有可能捕获并记录我自己的Websocket(wss)流量

[英]Is it possible capture and log my own Websocket (wss) traffic

如何使用PhantomJs捕获和记录来自Websocket并发往浏览器的数据流量?

You can try an approach with the following working example for PhantomJS 2.1.1. 您可以使用以下针对PhantomJS 2.1.1的工作示例尝试一种方法。 It opens sample page at http://www.websocket.org/echo.html and prints data traffic (input and output), along with some extra debug information. 它在http://www.websocket.org/echo.html上打开示例页面,并打印数据流量(输入和输出)以及一些其他调试信息。 The traffic itself is not very interesting, but fits for the demonstration purpose. 交通本身不是很有趣,但适合演示目的。 Hope, this helps. 希望这可以帮助。

var webPage = require('webpage');
var page = webPage.create();

page.onConsoleMessage = function(msg) {
    console.log('\n' + msg);
};

page.onError = function() {};

page.onInitialized = function() {
    page.evaluate(function proxyWebSocket() {

        var WS = window.WebSocket;

        var WebSocket = function(url, protocols) {
            var self = this;
            console.log('Creating websocket: ' + url);

            var ws = new WS(url, protocols);

            self.addEventListener = function(type, listener, useCapture, wantsUntrusted) {
                console.log('addEventListener: ' + type + '; ' + listener + '; ' + useCapture + '; ' + wantsUntrusted);

                var newListener = function(event) {
                    console.log('dispatching event: ', JSON.stringify(event));
                    return listener(event);
                };
                return ws.addEventListener.call(ws, type, newListener, useCapture, wantsUntrusted);
            };

            self.dispatchEvent = ws.dispatchEvent.bind(ws);
            self.removeEventListener = ws.removeEventListener.bind(ws);

            self.send = function(data) {
                console.log('websocket send: ' + data);
                return ws.send(data);
            };

            self.close = function(code, reason) {
                self.trace && console.log('websocket close: ' + code + ' ' + reason);
                return ws.close(code, reason);
            };

            // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Attributes
            var properties = [
                { name: "binaryType" },
                { name: "bufferedAmount", readOnly: true },
                { name: "extensions" },
                { name: "onclose", traceData: true },
                { name: "onmessage", traceData: true },
                { name: "onopen", traceData: true },
                { name: "onerror", traceData: true },
                { name: "protocol" },
                { name: "readyState", readOnly: true },
                { name: "url", readOnly: true }
            ];
            properties.forEach(defineProperty, self);

            function defineProperty(prop) {
                var descriptor = {
                    get: function() {
                        var result = ws[prop.name];
                        console.log('websocket get ' + prop.name + ': ' + result);
                        return result;
                    }
                };

                if (!prop.readOnly) {
                    descriptor.set = function(value) {
                        console.log('websocket set ' + prop.name + ': ' + value);
                        if (prop.traceData) {
                            var traceFn = function(event) {
                                console.log('websocket ' + prop.name + (event.data ? ': ' + JSON.stringify(event.data) : ''));
                                return value(event);
                            };
                            ws[prop.name] = traceFn;
                        } else {
                            ws[prop.name] = value;
                        }
                        return value;
                    };
                }

                Object.defineProperty(self, prop.name, descriptor);
            }
        };

        WebSocket.CONNECTING = WS.CONNECTING;
        WebSocket.OPEN = WS.OPEN;
        WebSocket.CLOSING = WS.CLOSING;
        WebSocket.CLOSED = WS.CLOSED;

        window.WebSocket = WebSocket;
    });
};

page.open("http://www.websocket.org/echo.html", function testWebSocket(status) {
    if (status === "success") {
        console.log('open status: ' + status);

        window.setTimeout(function() {
            phantom.exit();
        }, 5000);
    }
});

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

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