Ejemplo n.º 1
0
def test_save_flows(tmpdir):
    flows = [
        tflow.tflow(req=True, resp=None),
        tflow.tflow(req=True, resp=True)
    ]
    static_viewer.save_flows(tmpdir, flows)
    assert tmpdir.join('flows.json').check(file=1)
    assert tmpdir.join('flows.json').read() == json.dumps(
        [flow_to_json(f) for f in flows])
Ejemplo n.º 2
0
def test_generate_tflow_js(tdata):
    tf_http = tflow.tflow(resp=True, err=True, ws=True)
    tf_http.id = "d91165be-ca1f-4612-88a9-c0f8696f3e29"
    tf_http.client_conn.id = "4a18d1a0-50a1-48dd-9aa6-d45d74282939"
    tf_http.server_conn.id = "f087e7b2-6d0a-41a8-a8f0-e1a4761395f8"
    tf_http.server_conn.certificate_list = [
        certs.Cert.from_pem(
            Path(
                tdata.path(
                    "mitmproxy/net/data/verificationcerts/self-signed.pem")).
            read_bytes())
    ]
    tf_http.request.trailers = Headers(trailer="qvalue")
    tf_http.response.trailers = Headers(trailer="qvalue")
    tf_http.comment = "I'm a comment!"

    tf_tcp = tflow.ttcpflow(err=True)
    tf_tcp.id = "2ea7012b-21b5-4f8f-98cd-d49819954001"
    tf_tcp.client_conn.id = "8be32b99-a0b3-446e-93bc-b29982fe1322"
    tf_tcp.server_conn.id = "e33bb2cd-c07e-4214-9a8e-3a8f85f25200"

    # language=TypeScript
    content = (
        "/** Auto-generated by test_app.py:test_generate_tflow_js */\n"
        "import {HTTPFlow, TCPFlow} from '../../flow';\n"
        "export function THTTPFlow(): Required<HTTPFlow> {\n"
        "    return %s\n"
        "}\n"
        "export function TTCPFlow(): Required<TCPFlow> {\n"
        "    return %s\n"
        "}" % (
            textwrap.indent(
                json.dumps(app.flow_to_json(tf_http), indent=4,
                           sort_keys=True), "    "),
            textwrap.indent(
                json.dumps(app.flow_to_json(tf_tcp), indent=4, sort_keys=True),
                "    "),
        ))
    content = content.replace(": null", ": undefined")

    (Path(__file__).parent /
     "../../../../web/src/js/__tests__/ducks/_tflow.ts").write_bytes(
         content.encode())
Ejemplo n.º 3
0
def test_proxy_broadcasts_request(io_loop):
    broadcast = Mock()
    proxy = Proxy(io_loop, broadcast)
    flow = tflow(client_conn=True,
                 server_conn=True,
                 req=True,
                 resp=True,
                 err=True)
    proxy.request(flow)
    broadcast.assert_called_with(flow_to_json(flow))
Ejemplo n.º 4
0
 def _test_generate_tflow_js(self):
     _tflow = app.flow_to_json(tflow.tflow(resp=True, err=True))
     # Set some value as constant, so that _tflow.js would not change every time.
     _tflow['client_conn']['id'] = "4a18d1a0-50a1-48dd-9aa6-d45d74282939"
     _tflow['id'] = "d91165be-ca1f-4612-88a9-c0f8696f3e29"
     _tflow['error']['timestamp'] = 1495370312.4814785
     _tflow['response']['timestamp_end'] = 1495370312.4814625
     _tflow['response']['timestamp_start'] = 1495370312.481462
     _tflow['server_conn']['id'] = "f087e7b2-6d0a-41a8-a8f0-e1a4761395f8"
     tflow_json = _json.dumps(_tflow, indent=4, sort_keys=True)
     here = os.path.abspath(os.path.dirname(__file__))
     web_root = os.path.join(here, os.pardir, os.pardir, os.pardir, os.pardir, 'web')
     tflow_path = os.path.join(web_root, 'src/js/__tests__/ducks/_tflow.js')
     content = """export default function(){{\n    return {tflow_json}\n}}""".format(tflow_json=tflow_json)
     with open(tflow_path, 'w', newline="\n") as f:
         f.write(content)
Ejemplo n.º 5
0
 def test_generate_tflow_js(self):
     _tflow = app.flow_to_json(tflow.tflow(resp=True, err=True))
     # Set some value as constant, so that _tflow.js would not change every time.
     _tflow['client_conn']['id'] = "4a18d1a0-50a1-48dd-9aa6-d45d74282939"
     _tflow['id'] = "d91165be-ca1f-4612-88a9-c0f8696f3e29"
     _tflow['error']['timestamp'] = 1495370312.4814785
     _tflow['response']['timestamp_end'] = 1495370312.4814625
     _tflow['response']['timestamp_start'] = 1495370312.481462
     _tflow['server_conn']['id'] = "f087e7b2-6d0a-41a8-a8f0-e1a4761395f8"
     tflow_json = _json.dumps(_tflow, indent=4, sort_keys=True)
     here = os.path.abspath(os.path.dirname(__file__))
     web_root = os.path.join(here, os.pardir, os.pardir, os.pardir, os.pardir, 'web')
     tflow_path = os.path.join(web_root, 'src/js/__tests__/ducks/_tflow.js')
     content = """export default function(){{\n    return {tflow_json}\n}}""".format(tflow_json=tflow_json)
     with open(tflow_path, 'w', newline="\n") as f:
         f.write(content)
Ejemplo n.º 6
0
    def send_message(self, message_type, intercept, proxy):

        message = {
            "type": message_type,
            "flow": flow_to_json(intercept.flow),
            "proxy_address": self.proxy_address
        }

        # Hack for now to handle disconnect CONNECT events.
        if message_type == "connect-ended":
            message["flow"]["status"] = "disconnected"

        if intercept.user_id:
            message["user_id"] = intercept.user_id

        if intercept.session_id:
            message["session_id"] = intercept.session_id

        if intercept.flow.response:
            data = intercept.flow.response.raw_content
            if data is not None:
                encoded = b64encode(data).decode("utf-8")
                message["response"] = encoded
            else:
                message["response"] = ""

        if intercept.flow.request:
            data = intercept.flow.request.raw_content
            if data is not None:
                encoded = b64encode(data).decode("utf-8")
                message["request"] = encoded
            else:
                message["request"] = ""

        async def send_message_wrapper(redis_pool, message, proxy):
            sent = await send_worker_message(redis_pool, message)
            if not sent:
                logging.info("message error {}".format(message))
                proxy.worker_error(message)

        self.loop.create_task(
            send_message_wrapper(self.connection_pool, message, proxy))
Ejemplo n.º 7
0
 def _test_generate_tflow_js(self):
     _tflow = app.flow_to_json(tflow.tflow(resp=True, err=True))
     # Set some value as constant, so that _tflow.js would not change every time.
     _tflow['client_conn']['id'] = "4a18d1a0-50a1-48dd-9aa6-d45d74282939"
     _tflow['id'] = "d91165be-ca1f-4612-88a9-c0f8696f3e29"
     _tflow['server_conn']['id'] = "f087e7b2-6d0a-41a8-a8f0-e1a4761395f8"
     _tflow["request"]["trailers"] = [["trailer", "qvalue"]]
     _tflow["response"]["trailers"] = [["trailer", "qvalue"]]
     tflow_json = _json.dumps(_tflow, indent=4, sort_keys=True)
     here = os.path.abspath(os.path.dirname(__file__))
     web_root = os.path.join(here, os.pardir, os.pardir, os.pardir, os.pardir, 'web')
     tflow_path = os.path.join(web_root, 'src/js/__tests__/ducks/_tflow.js')
     content = (
         f"/** Auto-generated by test_app.py:TestApp._test_generate_tflow_js */\n"
         f"export default function(){{\n"
         f"    return {tflow_json}\n"
         f"}}"
     )
     with open(tflow_path, 'w', newline="\n") as f:
         f.write(content)
Ejemplo n.º 8
0
 def _sig_view_update(self, view, flow):
     app.ClientConnection.broadcast(
         resource="flows",
         cmd="update",
         data=app.flow_to_json(flow)
     )
Ejemplo n.º 9
0
def save_flows(path: pathlib.Path, flows: typing.Iterable[flow.Flow]) -> None:
    with open(str(path / 'flows.json'), 'w') as f:
        json.dump(
            [flow_to_json(f) for f in flows],
            f
        )
Ejemplo n.º 10
0
def test_save_flows(tmpdir):
    flows = [tflow.tflow(req=True, resp=None), tflow.tflow(req=True, resp=True)]
    static_viewer.save_flows(tmpdir, flows)
    assert tmpdir.join('flows.json').check(file=1)
    assert tmpdir.join('flows.json').read() == json.dumps([flow_to_json(f) for f in flows])
Ejemplo n.º 11
0
def save_flows(path: pathlib.Path, flows: typing.Iterable[flow.Flow]) -> None:
    with open(str(path / 'flows.json'), 'w') as f:
        json.dump(
            [flow_to_json(f) for f in flows],
            f
        )