コード例 #1
0
 def _setUp(self, mocker: MockerFixture) -> None:
     self.mock_fromfd = mocker.patch('socket.fromfd')
     self.mock_selector = mocker.patch('selectors.DefaultSelector')
     self.fileno = 10
     self._addr = ('127.0.0.1', 54382)
     self._conn = self.mock_fromfd.return_value
     # Setup a static directory
     self.static_server_dir = os.path.join(tempfile.gettempdir(), 'static')
     self.index_file_path = os.path.join(
         self.static_server_dir, 'index.html',
     )
     self.html_file_content = b'''<html><head></head><body><h1>Proxy.py Testing</h1></body></html>'''
     os.makedirs(self.static_server_dir, exist_ok=True)
     with open(self.index_file_path, 'wb') as f:
         f.write(self.html_file_content)
     #
     flags = FlagParser.initialize(
         enable_static_server=True,
         static_server_dir=self.static_server_dir,
         threaded=True,
     )
     flags.plugins = Plugins.load([
         bytes_(PLUGIN_HTTP_PROXY),
         bytes_(PLUGIN_WEB_SERVER),
     ])
     self.protocol_handler = HttpProtocolHandler(
         HttpClientConnection(self._conn, self._addr),
         flags=flags,
     )
     self.protocol_handler.initialize()
コード例 #2
0
 async def test_proxy_authentication_failed(self) -> None:
     self._conn = self.mock_fromfd.return_value
     mock_selector_for_client_read(self)
     flags = FlagParser.initialize(
         auth_code=base64.b64encode(b'user:pass'),
         threaded=True,
     )
     flags.plugins = Plugins.load([
         bytes_(PLUGIN_HTTP_PROXY),
         bytes_(PLUGIN_WEB_SERVER),
         bytes_(PLUGIN_PROXY_AUTH),
     ])
     self.protocol_handler = HttpProtocolHandler(
         HttpClientConnection(self._conn, self._addr),
         flags=flags,
     )
     self.protocol_handler.initialize()
     self._conn.recv.return_value = CRLF.join([
         b'GET http://abhinavsingh.com HTTP/1.1',
         b'Host: abhinavsingh.com',
         CRLF,
     ])
     await self.protocol_handler._run_once()
     self.assertEqual(
         self.protocol_handler.work.buffer[0],
         PROXY_AUTH_FAILED_RESPONSE_PKT,
     )
コード例 #3
0
 def _setUp(self, request: Any, mocker: MockerFixture) -> None:
     self.mock_fromfd = mocker.patch('socket.fromfd')
     self.mock_selector = mocker.patch('selectors.DefaultSelector')
     self.fileno = 10
     self._addr = ('127.0.0.1', 54382)
     self._conn = self.mock_fromfd.return_value
     self.pac_file = request.param
     if isinstance(self.pac_file, str):
         with open(self.pac_file, 'rb') as f:
             self.expected_response = f.read()
     else:
         self.expected_response = PAC_FILE_CONTENT
     self.flags = FlagParser.initialize(
         pac_file=self.pac_file, threaded=True,
     )
     self.flags.plugins = Plugins.load([
         bytes_(PLUGIN_HTTP_PROXY),
         bytes_(PLUGIN_WEB_SERVER),
         bytes_(PLUGIN_PAC_FILE),
     ])
     self.protocol_handler = HttpProtocolHandler(
         HttpClientConnection(self._conn, self._addr),
         flags=self.flags,
     )
     self.protocol_handler.initialize()
     self._conn.recv.return_value = CRLF.join([
         b'GET / HTTP/1.1',
         CRLF,
     ])
     mock_selector_for_client_read(self)
コード例 #4
0
    async def test_authenticated_proxy_http_get(self) -> None:
        self._conn = self.mock_fromfd.return_value
        mock_selector_for_client_read(self)

        server = self.mock_server_connection.return_value
        server.connect.return_value = True
        server.buffer_size.return_value = 0

        flags = FlagParser.initialize(
            auth_code=base64.b64encode(b'user:pass'),
            threaded=True,
        )
        flags.plugins = Plugins.load([
            bytes_(PLUGIN_HTTP_PROXY),
            bytes_(PLUGIN_WEB_SERVER),
        ])

        self.protocol_handler = HttpProtocolHandler(
            HttpClientConnection(self._conn, self._addr),
            flags=flags,
        )
        self.protocol_handler.initialize()
        assert self.http_server_port is not None

        self._conn.recv.return_value = b'GET http://localhost:%d HTTP/1.1' % self.http_server_port
        await self.protocol_handler._run_once()
        self.assertEqual(
            self.protocol_handler.request.state,
            httpParserStates.INITIALIZED,
        )

        self._conn.recv.return_value = CRLF
        await self.protocol_handler._run_once()
        self.assertEqual(
            self.protocol_handler.request.state,
            httpParserStates.LINE_RCVD,
        )

        assert self.http_server_port is not None
        self._conn.recv.return_value = CRLF.join([
            b'User-Agent: proxy.py/%s' % bytes_(__version__),
            b'Host: localhost:%d' % self.http_server_port,
            b'Accept: */*',
            httpHeaders.PROXY_CONNECTION + b': Keep-Alive',
            httpHeaders.PROXY_AUTHORIZATION + b': Basic dXNlcjpwYXNz',
            CRLF,
        ])
        await self.assert_data_queued(server)
コード例 #5
0
 def _setUp(self, mocker: MockerFixture) -> None:
     self.mock_fromfd = mocker.patch('socket.fromfd')
     self.mock_selector = mocker.patch('selectors.DefaultSelector')
     self.fileno = 10
     self._addr = ('127.0.0.1', 54382)
     self._conn = self.mock_fromfd.return_value
     self.flags = FlagParser.initialize(threaded=True)
     self.flags.plugins = Plugins.load([
         bytes_(PLUGIN_HTTP_PROXY),
         bytes_(PLUGIN_WEB_SERVER),
     ])
     self.protocol_handler = HttpProtocolHandler(
         HttpClientConnection(self._conn, self._addr),
         flags=self.flags,
     )
     self.protocol_handler.initialize()
コード例 #6
0
    async def test_authenticated_proxy_http_tunnel(self) -> None:
        server = self.mock_server_connection.return_value
        server.connect.return_value = True
        server.buffer_size.return_value = 0
        self._conn = self.mock_fromfd.return_value
        self.mock_selector_for_client_read_and_server_write(server)

        flags = FlagParser.initialize(
            auth_code=base64.b64encode(b'user:pass'),
            threaded=True,
        )
        flags.plugins = Plugins.load([
            bytes_(PLUGIN_HTTP_PROXY),
            bytes_(PLUGIN_WEB_SERVER),
        ])

        self.protocol_handler = HttpProtocolHandler(
            HttpClientConnection(self._conn, self._addr),
            flags=flags,
        )
        self.protocol_handler.initialize()

        assert self.http_server_port is not None
        self._conn.recv.return_value = CRLF.join([
            b'CONNECT localhost:%d HTTP/1.1' % self.http_server_port,
            b'Host: localhost:%d' % self.http_server_port,
            b'User-Agent: proxy.py/%s' % bytes_(__version__),
            httpHeaders.PROXY_CONNECTION + b': Keep-Alive',
            httpHeaders.PROXY_AUTHORIZATION + b': Basic dXNlcjpwYXNz',
            CRLF,
        ])
        await self.assert_tunnel_response(server)
        self.protocol_handler.work.flush()
        await self.assert_data_queued_to_server(server)

        await self.protocol_handler._run_once()
        server.flush.assert_called_once()
コード例 #7
0
 async def test_default_web_server_returns_404(self) -> None:
     self._conn = self.mock_fromfd.return_value
     self.mock_selector.return_value.select.return_value = [
         (
             selectors.SelectorKey(
                 fileobj=self._conn.fileno(),
                 fd=self._conn.fileno(),
                 events=selectors.EVENT_READ,
                 data=None,
             ),
             selectors.EVENT_READ,
         ),
     ]
     flags = FlagParser.initialize(threaded=True)
     flags.plugins = Plugins.load([
         bytes_(PLUGIN_HTTP_PROXY),
         bytes_(PLUGIN_WEB_SERVER),
     ])
     self.protocol_handler = HttpProtocolHandler(
         HttpClientConnection(self._conn, self._addr),
         flags=flags,
     )
     self.protocol_handler.initialize()
     self._conn.recv.return_value = CRLF.join([
         b'GET /hello HTTP/1.1',
         CRLF,
     ])
     await self.protocol_handler._run_once()
     self.assertEqual(
         self.protocol_handler.request.state,
         httpParserStates.COMPLETE,
     )
     self.assertEqual(
         self.protocol_handler.work.buffer[0],
         NOT_FOUND_RESPONSE_PKT,
     )