2019-11-15 03:00:07 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
proxy.py
|
|
|
|
~~~~~~~~
|
2019-11-19 04:45:51 +00:00
|
|
|
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
|
|
|
|
Network monitoring, controls & Application development, testing, debugging.
|
2019-11-15 03:00:07 +00:00
|
|
|
|
|
|
|
:copyright: (c) 2013-present by Abhinav Singh and contributors.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import time
|
2022-01-16 13:04:33 +00:00
|
|
|
from typing import TYPE_CHECKING, Any, Dict
|
2019-11-15 03:00:07 +00:00
|
|
|
|
2022-01-20 10:04:54 +00:00
|
|
|
from ..websocket import WebsocketFrame
|
2019-12-02 06:46:00 +00:00
|
|
|
from ...core.event import eventNames
|
2022-01-20 10:04:54 +00:00
|
|
|
from ...common.utils import bytes_
|
|
|
|
from ...common.constants import (
|
|
|
|
PROXY_PY_START_TIME, DEFAULT_DEVTOOLS_DOC_URL, DEFAULT_DEVTOOLS_FRAME_ID,
|
|
|
|
DEFAULT_DEVTOOLS_LOADER_ID,
|
|
|
|
)
|
2019-11-15 03:00:07 +00:00
|
|
|
|
2022-01-16 13:04:33 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING: # pragma: no cover
|
|
|
|
from ..connection import HttpClientConnection
|
|
|
|
|
2019-11-15 03:00:07 +00:00
|
|
|
|
2019-12-02 06:46:00 +00:00
|
|
|
class CoreEventsToDevtoolsProtocol:
|
2021-11-06 08:31:13 +00:00
|
|
|
"""Open in Chrome
|
2019-11-15 03:00:07 +00:00
|
|
|
|
2021-11-16 23:34:29 +00:00
|
|
|
``devtools://devtools/bundled/inspector.html?ws=localhost:8899/devtools``
|
2021-11-06 08:31:13 +00:00
|
|
|
"""
|
2019-11-15 03:00:07 +00:00
|
|
|
|
|
|
|
RESPONSES: Dict[str, bytes] = {}
|
|
|
|
|
|
|
|
@staticmethod
|
2021-11-04 11:28:36 +00:00
|
|
|
def transformer(
|
2022-01-16 13:04:33 +00:00
|
|
|
client: 'HttpClientConnection',
|
2021-11-04 11:28:36 +00:00
|
|
|
event: Dict[str, Any],
|
|
|
|
) -> None:
|
2019-11-15 03:00:07 +00:00
|
|
|
event_name = event['event_name']
|
|
|
|
if event_name == eventNames.REQUEST_COMPLETE:
|
|
|
|
data = CoreEventsToDevtoolsProtocol.request_complete(event)
|
|
|
|
elif event_name == eventNames.RESPONSE_HEADERS_COMPLETE:
|
|
|
|
data = CoreEventsToDevtoolsProtocol.response_headers_complete(
|
2021-11-04 11:28:36 +00:00
|
|
|
event,
|
|
|
|
)
|
2019-11-15 03:00:07 +00:00
|
|
|
elif event_name == eventNames.RESPONSE_CHUNK_RECEIVED:
|
|
|
|
data = CoreEventsToDevtoolsProtocol.response_chunk_received(event)
|
|
|
|
elif event_name == eventNames.RESPONSE_COMPLETE:
|
|
|
|
data = CoreEventsToDevtoolsProtocol.response_complete(event)
|
|
|
|
else:
|
|
|
|
# drop core events unrelated to Devtools
|
|
|
|
return
|
|
|
|
client.queue(
|
2021-11-04 11:28:36 +00:00
|
|
|
memoryview(
|
|
|
|
WebsocketFrame.text(
|
|
|
|
bytes_(
|
|
|
|
json.dumps(data),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2019-11-15 03:00:07 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def request_complete(event: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
now = time.time()
|
|
|
|
return {
|
2020-10-11 04:56:36 +00:00
|
|
|
'method': 'Network.requestWillBeSent',
|
2021-11-06 08:31:13 +00:00
|
|
|
'params': {
|
|
|
|
'requestId': event['request_id'],
|
|
|
|
'frameId': DEFAULT_DEVTOOLS_FRAME_ID,
|
|
|
|
'loaderId': DEFAULT_DEVTOOLS_LOADER_ID,
|
|
|
|
'documentURL': DEFAULT_DEVTOOLS_DOC_URL,
|
|
|
|
'timestamp': now - PROXY_PY_START_TIME,
|
|
|
|
'wallTime': now,
|
|
|
|
'hasUserGesture': False,
|
|
|
|
'type': event['event_payload']['headers']['content-type']
|
|
|
|
if 'content-type' in event['event_payload']['headers']
|
|
|
|
else 'Other',
|
|
|
|
'request': {
|
|
|
|
'url': event['event_payload']['url'],
|
|
|
|
'method': event['event_payload']['method'],
|
|
|
|
'headers': event['event_payload']['headers'],
|
|
|
|
'postData': event['event_payload']['body'],
|
|
|
|
'initialPriority': 'High',
|
|
|
|
'urlFragment': '',
|
|
|
|
'mixedContentType': 'none',
|
|
|
|
},
|
|
|
|
'initiator': {
|
|
|
|
'type': 'other',
|
|
|
|
},
|
2019-11-15 03:00:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def response_headers_complete(event: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
return {
|
2021-11-06 08:31:13 +00:00
|
|
|
'method': 'Network.responseReceived',
|
|
|
|
'params': {
|
|
|
|
'requestId': event['request_id'],
|
|
|
|
'frameId': DEFAULT_DEVTOOLS_FRAME_ID,
|
|
|
|
'loaderId': DEFAULT_DEVTOOLS_LOADER_ID,
|
|
|
|
'timestamp': time.time(),
|
|
|
|
'type': event['event_payload']['headers']['content-type']
|
|
|
|
if event['event_payload']['headers'].has_header('content-type')
|
|
|
|
else 'Other',
|
|
|
|
'response': {
|
|
|
|
'url': '',
|
|
|
|
'status': '',
|
|
|
|
'statusText': '',
|
|
|
|
'headers': '',
|
|
|
|
'headersText': '',
|
|
|
|
'mimeType': '',
|
|
|
|
'connectionReused': True,
|
|
|
|
'connectionId': '',
|
|
|
|
'encodedDataLength': '',
|
|
|
|
'fromDiskCache': False,
|
|
|
|
'fromServiceWorker': False,
|
|
|
|
'timing': {
|
|
|
|
'requestTime': '',
|
|
|
|
'proxyStart': -1,
|
|
|
|
'proxyEnd': -1,
|
|
|
|
'dnsStart': -1,
|
|
|
|
'dnsEnd': -1,
|
|
|
|
'connectStart': -1,
|
|
|
|
'connectEnd': -1,
|
|
|
|
'sslStart': -1,
|
|
|
|
'sslEnd': -1,
|
|
|
|
'workerStart': -1,
|
|
|
|
'workerReady': -1,
|
|
|
|
'sendStart': 0,
|
|
|
|
'sendEnd': 0,
|
|
|
|
'receiveHeadersEnd': 0,
|
|
|
|
},
|
|
|
|
'requestHeaders': '',
|
|
|
|
'remoteIPAddress': '',
|
|
|
|
'remotePort': '',
|
2019-11-15 03:00:07 +00:00
|
|
|
},
|
2021-11-04 11:28:36 +00:00
|
|
|
},
|
2019-11-15 03:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def response_chunk_received(event: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
return {
|
2021-11-06 08:31:13 +00:00
|
|
|
'method': 'Network.dataReceived',
|
|
|
|
'params': {
|
|
|
|
'requestId': event['request_id'],
|
|
|
|
'timestamp': time.time(),
|
|
|
|
'dataLength': event['event_payload']['chunk_size'],
|
|
|
|
'encodedDataLength': event['event_payload']['encoded_chunk_size'],
|
|
|
|
},
|
2019-11-15 03:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def response_complete(event: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
return {
|
2021-11-06 08:31:13 +00:00
|
|
|
'method': 'Network.loadingFinished',
|
|
|
|
'params': {
|
|
|
|
'requestId': event['request_id'],
|
|
|
|
'timestamp': time.time(),
|
|
|
|
'encodedDataLength': event['event_payload']['encoded_response_size'],
|
|
|
|
'shouldReportCorbBlocking': False,
|
|
|
|
},
|
2019-11-15 03:00:07 +00:00
|
|
|
}
|