Exemple #1
0
    def test_address(self):
        self.mock_proxy_server.return_value.address = ('somehost', 12345)
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        self.assertEqual(('somehost', 12345), proxy.address())
Exemple #2
0
def create(addr='127.0.0.1', port=0, options=None):
    """Create a new proxy backend.

    Args:
        addr: The address the proxy server will listen on. Default 127.0.0.1.
        port: The port the proxy server will listen on. Default 0 - which means
            use the first available port.
        options: Additional options to configure the proxy.

    Returns:
        An instance of the proxy backend.
    """
    if options is None:
        options = {}

    backend = MitmProxy(addr, port, options)

    t = threading.Thread(name='Selenium Wire Proxy Server',
                         target=backend.serve_forever)
    t.daemon = not options.get('standalone')
    t.start()

    addr, port, *_ = backend.address()
    log.info('Created proxy listening on %s:%s', addr, port)

    return backend
Exemple #3
0
    def test_shutdown(self):
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        proxy.shutdown()

        self.mock_master.return_value.shutdown.assert_called_once_with()
        self.mock_storage.return_value.cleanup.assert_called_once_with()
Exemple #4
0
    def test_serve_forever(self):
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        proxy.serve_forever()

        self.mock_asyncio.set_event_loop.assert_called_once_with(proxy._event_loop)
        self.mock_master.return_value.run.assert_called_once_with()
Exemple #5
0
    def test_creates_storage(self):
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        self.assertEqual(self.mock_storage.return_value, proxy.storage)
        self.mock_storage.assert_called_once_with(base_dir='/some/dir')
Exemple #6
0
    def test_disable_capture(self):
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
            'disable_capture': True
        })

        self.assertEqual(['$^'], proxy.scopes)
Exemple #7
0
 def test_creates_master(self):
     self.mock_storage.return_value.home_dir = '/some/dir/.seleniumwire'
     proxy = MitmProxy('somehost', 12345, {
         'request_storage_base_dir': '/some/dir',
     })
     self.assertEqual(self.mock_master.return_value, proxy._master)
     self.mock_options.assert_called_once_with(
         confdir='/some/dir/.seleniumwire',
         listen_host='somehost',
         listen_port=12345
     )
     self.mock_master.assert_called_once_with(
         self.mock_asyncio.new_event_loop.return_value,
         self.mock_options.return_value
     )
     self.assertEqual(self.mock_proxy_server.return_value, self.mock_master.return_value.server)
     self.mock_proxy_config.assert_called_once_with(self.mock_options.return_value)
     self.mock_proxy_server.assert_called_once_with(self.mock_proxy_config.return_value)
     self.mock_master.return_value.addons.add.assert_has_calls([
         call(),
         call(self.mock_logger.return_value),
         call(self.mock_handler.return_value)
     ])
     self.mock_addons.default_addons.assert_called_once_with()
     self.mock_handler.assert_called_once_with(proxy)
Exemple #8
0
    def test_new_event_loop(self):
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        self.assertEqual(self.mock_asyncio.new_event_loop.return_value, proxy._event_loop)
        self.mock_asyncio.new_event_loop.assert_called_once_with()
Exemple #9
0
    def test_extracts_cert(self):
        self.mock_storage.return_value.home_dir = '/some/dir/.seleniumwire'
        MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        self.mock_extract_cert_and_key.assert_called_once_with('/some/dir/.seleniumwire')
Exemple #10
0
 def test_upstream_proxy_different(self):
     with self.assertRaises(ValueError):
         MitmProxy('somehost', 12345, {
             'request_storage_base_dir': '/some/dir',
             'proxy': {
                 'http': 'http://proxyserver1:8080',
                 'https': 'https://proxyserver2:8080'
             }
         })
Exemple #11
0
    def test_update_mitmproxy_options(self):
        MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
            'mitm_test': 'foobar'
        })

        self.mock_options.return_value.update.assert_has_calls(
            [self.base_options_update(),
             call(test='foobar')])
Exemple #12
0
def create(addr='127.0.0.1', port=0, options=None):
    """Create a new proxy backend.

    The type of backend created depends on the 'backend' option. Supported types
    are 'default' and 'mitmproxy'. When not specified, the default backend will
    be used. The mitmproxy backend is dependent on the mitmproxy package being
    installed.

    Args:
        addr: The address the proxy server will listen on. Default 127.0.0.1.
        port: The port the proxy server will listen on. Default 0 - which means
            use the first available port.
        options: Additional options to configure the proxy.

    Returns:
        An instance of the proxy backend.
    """
    if options is None:
        options = {}

    backend = options.get('backend', DEFAULT_BACKEND)

    if backend == DEFAULT_BACKEND:
        # Use the default backend
        proxy = MitmProxy(addr, port, options)
    elif backend == 'mitmproxy':
        # Use mitmproxy if installed
        from . import mitmproxy

        proxy = mitmproxy.MitmProxy(addr, port, options)
    else:
        raise TypeError("Invalid backend '{}'. "
                        "Valid values are 'default' or 'mitmproxy'.".format(
                            options['backend']))

    t = threading.Thread(name='Selenium Wire Proxy Server',
                         target=proxy.serve_forever)
    t.daemon = not options.get('standalone')
    t.start()

    addr, port, *_ = proxy.address()
    log.info('Created proxy listening on %s:%s', addr, port)

    return proxy
Exemple #13
0
    def test_disable_capture(self):
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
            'disable_capture': True
        })

        self.assertEqual(['$^'], proxy.scopes)
        self.mock_options.return_value.update.assert_has_calls(
            [self.base_options_update(),
             call(ignore_hosts=['.*'])])
    def test_event_loop_closed(self):
        self.mock_asyncio.get_event_loop.return_value.is_closed.return_value = True
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        self.assertEqual(self.mock_asyncio.new_event_loop.return_value,
                         proxy._event_loop)
        self.mock_asyncio.get_event_loop.assert_called_once_with()
        self.mock_asyncio.new_event_loop.assert_called_once_with()
        self.mock_asyncio.set_event_loop.assert_called_once_with(
            proxy._event_loop)
    def test_event_loop_exception(self):
        self.mock_asyncio.get_event_loop.side_effect = RuntimeError
        self.mock_asyncio.get_event_loop.return_value.is_closed.return_value = False
        proxy = MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
        })

        self.assertEqual(self.mock_asyncio.new_event_loop.return_value,
                         proxy._event_loop)
        self.mock_asyncio.get_event_loop.assert_called_once_with()
        self.mock_asyncio.new_event_loop.assert_called_once_with()
        self.mock_asyncio.set_event_loop.assert_called_once_with(
            proxy._event_loop)
Exemple #16
0
    def test_upstream_proxy_single(self):
        MitmProxy(
            'somehost', 12345, {
                'request_storage_base_dir': '/some/dir',
                'proxy': {
                    'http': 'http://proxyserver:8080',
                }
            })

        self.mock_options.return_value.update.assert_has_calls([
            self.base_options_update(mode='upstream:http://proxyserver:8080'),
            call()
        ])
Exemple #17
0
    def test_update_mitmproxy_options(self):
        MitmProxy('somehost', 12345, {
            'request_storage_base_dir': '/some/dir',
            'mitm_test': 'foobar'
        })

        self.mock_options.return_value.update.assert_has_calls([
            call(
                ssl_insecure=True,
                upstream_cert=False,
                stream_websockets=True,
                suppress_connection_errors=True,
            ),
            call(test='foobar')
        ])
Exemple #18
0
    def test_upstream_proxy_custom_auth(self):
        MitmProxy(
            'somehost', 12345, {
                'request_storage_base_dir': '/some/dir',
                'proxy': {
                    'https': 'https://proxyserver:8080',
                    'custom_authorization': 'Bearer 12345'
                }
            })

        self.mock_options.return_value.update.assert_has_calls([
            self.base_options_update(mode='upstream:https://proxyserver:8080',
                                     upstream_custom_auth='Bearer 12345'),
            call()
        ])
Exemple #19
0
    def test_upstream_proxy_no_proxy(self):
        MitmProxy(
            'somehost', 12345, {
                'request_storage_base_dir': '/some/dir',
                'proxy': {
                    'https': 'https://proxyserver:8080',
                    'no_proxy': 'localhost:9090, example.com'
                }
            })

        self.mock_options.return_value.update.assert_has_calls([
            self.base_options_update(
                mode='upstream:https://proxyserver:8080',
                no_proxy=['localhost:9090', 'example.com']),
            call()
        ])
Exemple #20
0
    def test_upstream_proxy_single(self):
        MitmProxy(
            'somehost', 12345, {
                'request_storage_base_dir': '/some/dir',
                'proxy': {
                    'http': 'http://proxyserver:8080',
                }
            })

        self.mock_options.return_value.update.assert_has_calls([
            call(ssl_insecure=True,
                 upstream_cert=False,
                 stream_websockets=True,
                 suppress_connection_errors=True,
                 mode='upstream:http://proxyserver:8080'),
            call()
        ])
Exemple #21
0
    def test_upstream_proxy(self):
        MitmProxy(
            'somehost',
            12345,
            {
                'request_storage_base_dir': '/some/dir',
                'proxy': {
                    'http': 'http://proxyserver:8080',
                    # We pick https when both are specified and the same
                    'https': 'https://proxyserver:8080'
                }
            })

        self.mock_options.return_value.update.assert_has_calls([
            self.base_options_update(mode='upstream:https://proxyserver:8080'),
            call()
        ])