Простой HTTP dump

Discussion in 'Javascript' started by ckpunmkug, 15 Apr 2019 at 5:37 PM.

  1. ckpunmkug

    ckpunmkug Member

    Joined:
    20 Mar 2017
    Messages:
    24
    Likes Received:
    10
    Reputations:
    1
    Запускаем chromium --remote-debugging-port=9222 --incognito about:blank &
    Запускаем в терминале nodejs index.js
    Лазаем в браузере и смотрим в терминал.
    index.js
    Code:
    const CDP = require('chrome-remote-interface');
    global.HTTPRequests = {};
    
    CDP((client) => {
        const {Page, Runtime, Network} = client;
    
        Promise.all([
            Page.enable(), 
            Network.enable()
        ]);
       
        Network.requestWillBeSent((parameters) => {
       
            var loaderId = parameters.loaderId;
            var requestId = parameters.requestId;
           
            if (typeof(HTTPRequests[loaderId]) == 'undefined') 
                HTTPRequests[loaderId] = {};
           
            if (typeof(HTTPRequests[loaderId][requestId]) == 'undefined')
                HTTPRequests[loaderId][requestId] = {};
           
            HTTPRequests[loaderId][requestId]  = {
                wallTime: parameters.wallTime,
                documentURL: parameters.documentURL,
                request: {
                    url: parameters.request.url, 
                    method: parameters.request.method, 
                    postData: parameters.request.postData, 
                },
                response: {}
            };
        });
       
        Network.responseReceived((parameters) => {
           
            var loaderId = parameters.loaderId;
            var requestId = parameters.requestId;
           
            HTTPRequests[loaderId][requestId].request.headersText = parameters.response.requestHeadersText;
           
            HTTPRequests[loaderId][requestId].response = {
                url: parameters.response.url, 
                remoteIPAddress: parameters.response.remoteIPAddress, 
                remotePort: parameters.response.remotePort, 
                status: parameters.response.status,
                headersText: parameters.response.headersText,
                mimeType: parameters.response.mimeType
            };
           
            Network.getResponseBody({requestId: parameters.requestId}).then(parameters => {
                HTTPRequests[loaderId][requestId].response.responseBody = parameters;
                console.log(HTTPRequests[loaderId][requestId]);
                HTTPRequests[loaderId][requestId] = undefined;
            });
        });
    
    }).on('error', (err) => {
        console.error('Cannot connect to browser:', err);
    });
    
     
    BillyBons likes this.