├── src └── proxyx │ ├── py.typed │ ├── __init__.py │ └── _asgi.py ├── MANIFEST.in ├── requirements.txt ├── setup.cfg ├── example.py ├── scripts ├── lint ├── install ├── example └── check ├── .gitignore ├── ci └── azure-pipelines.yml ├── setup.py ├── README.md └── LICENSE /src/proxyx/py.typed: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | graft src 2 | include LICENSE 3 | -------------------------------------------------------------------------------- /src/proxyx/__init__.py: -------------------------------------------------------------------------------- 1 | from ._asgi import ProxyApp 2 | 3 | __all__ = ["ProxyApp"] 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | -e . 2 | 3 | # Development. 4 | uvicorn 5 | 6 | # Tooling. 7 | black 8 | flake8 9 | mypy 10 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = W503, E203, B305 3 | max-line-length = 88 4 | 5 | [mypy] 6 | disallow_untyped_defs = True 7 | ignore_missing_imports = True 8 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import proxyx 4 | 5 | app = proxyx.ProxyApp( 6 | hostname=os.environ["PROXYX_HOSTNAME"], 7 | root_path=os.environ.get("PROXYX_ROOT_PATH", ""), 8 | ) 9 | -------------------------------------------------------------------------------- /scripts/lint: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | BIN="" 4 | if [ -d venv ] ; then 5 | BIN="venv/bin/" 6 | fi 7 | 8 | SOURCE_FILES="src" 9 | 10 | set -x 11 | 12 | ${PREFIX}black --target-version=py38 $SOURCE_FILES 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Tooling. 2 | .coverage 3 | venv*/ 4 | .python-version 5 | 6 | # Caches. 7 | __pycache__/ 8 | *.pyc 9 | .mypy_cache/ 10 | .pytest_cache/ 11 | 12 | # Packaging. 13 | build/ 14 | dist/ 15 | *.egg-info/ 16 | -------------------------------------------------------------------------------- /scripts/install: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | BIN="" 4 | if [ ! -d venv ]; then 5 | python -m venv venv 6 | BIN="venv/bin/" 7 | fi 8 | 9 | set -x 10 | 11 | ${BIN}pip install -U pip 12 | ${BIN}pip install -r requirements.txt 13 | -------------------------------------------------------------------------------- /scripts/example: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | BIN="" 4 | if [ -d venv ]; then 5 | BIN="venv/bin/" 6 | fi 7 | 8 | PROXYX_HOSTNAME=${PROXYX_HOSTNAME:-www.python-httpx.org} PROXYX_ROOT_PATH=${PROXYX_ROOT_PATH:-} ${BIN}uvicorn example:app --port=${PORT:-8000} 9 | -------------------------------------------------------------------------------- /scripts/check: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | BIN="" 4 | if [ -d venv ] ; then 5 | BIN="venv/bin/" 6 | fi 7 | 8 | SOURCE_FILES="src" 9 | 10 | set -x 11 | 12 | ${BIN}black --check --diff --target-version=py38 $SOURCE_FILES 13 | ${BIN}flake8 $SOURCE_FILES 14 | ${BIN}mypy $SOURCE_FILES 15 | -------------------------------------------------------------------------------- /ci/azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | resources: 2 | repositories: 3 | - repository: templates 4 | type: github 5 | endpoint: github 6 | name: florimondmanca/azure-pipelines-templates 7 | ref: refs/tags/3.4 8 | 9 | trigger: 10 | - master 11 | 12 | pr: 13 | - master 14 | 15 | variables: 16 | PIP_CACHE_DIR: $(Pipeline.Workspace)/.cache/pip 17 | 18 | jobs: 19 | - template: job--python-check.yml@templates 20 | parameters: 21 | pythonVersion: "3.9" 22 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | from setuptools import find_packages, setup 4 | 5 | setup( 6 | name="proxyx", 7 | python_requires=">=3.9", 8 | version="0.0.1", 9 | url="https://github.com/florimondmanca/proxyx", 10 | license="MIT", 11 | description=( 12 | "Proof of concept for a lightweight HTTP/1.1 proxy service " 13 | "built with ASGI and HTTPX." 14 | ), 15 | author="Florimond Manca", 16 | author_email="florimond.manca@gmail.com", 17 | packages=find_packages("src"), 18 | package_dir={"": "src"}, 19 | install_requires=["httpx==0.19.*", "starlette==0.*"], 20 | include_package_data=True, 21 | zip_safe=False, 22 | classifiers=[ 23 | "Private :: Do Not Upload", 24 | "Intended Audience :: Developers", 25 | "Operating System :: OS Independent", 26 | "Programming Language :: Python :: 3", 27 | "Programming Language :: Python :: 3.9", 28 | ], 29 | ) 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ProxyX 2 | 3 | [![Build Status](https://dev.azure.com/florimondmanca/public/_apis/build/status/florimondmanca.proxyx?branchName=master)](https://dev.azure.com/florimondmanca/public/_build/latest?definitionId=14&branchName=master) 4 | 5 | Proof of concept for a lightweight HTTP/1.1 proxy service built with [ASGI](https://asgi.readthedocs.io) and [HTTPX](https://github.com/encode/httpx). No maintenance intended. 6 | 7 | ## Setup 8 | 9 | Clone this repository, then install dependencies: 10 | 11 | ```bash 12 | scripts/install 13 | ``` 14 | 15 | ## Example 16 | 17 | ```bash 18 | scripts/example 19 | ``` 20 | 21 | This will proxy https://www.python-httpx.org/ (the HTTPX documentation) from `localhost:8000`. 22 | 23 | Use environment variables as below to proxy a different target: 24 | 25 | ```bash 26 | PROXYX_HOSTNAME="www.example.org" PROXYX_ROOT_PATH="" scripts/example 27 | ``` 28 | 29 | ## Known limitations 30 | 31 | - Domain-level redirects are not handled (e.g. proxying `https://encode.io/{path}` won't work because this domain returns a 301 to `https://www.encode.io/{path}`). 32 | 33 | ## License 34 | 35 | MIT 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Florimond Manca 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/proxyx/_asgi.py: -------------------------------------------------------------------------------- 1 | from typing import List, Optional, Tuple 2 | 3 | import httpx 4 | from starlette.requests import Request as ASGIRequest 5 | from starlette.types import Receive, Scope, Send 6 | 7 | 8 | class ProxyApp: 9 | _http: httpx.AsyncBaseTransport 10 | 11 | def __init__(self, hostname: str, root_path: str = ""): 12 | self._hostname = hostname 13 | self._root_path = root_path 14 | 15 | async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: 16 | if scope["type"] == "lifespan": 17 | await self._lifespan(scope, receive, send) 18 | else: 19 | assert scope["type"] == "http" 20 | await self._http_request(scope, receive, send) 21 | 22 | async def _lifespan(self, scope: Scope, receive: Receive, send: Send) -> None: 23 | while True: 24 | message = await receive() 25 | if message["type"] == "lifespan.startup": 26 | self._http = httpx.AsyncHTTPTransport() 27 | await send({"type": "lifespan.startup.complete"}) 28 | elif message["type"] == "lifespan.shutdown": 29 | await self._http.aclose() 30 | await send({"type": "lifespan.shutdown.complete"}) 31 | return 32 | 33 | async def _http_request(self, scope: Scope, receive: Receive, send: Send) -> None: 34 | method, url, headers, stream = self._get_request(scope, receive) 35 | 36 | status_code, headers, stream, ext = await self._http.handle_async_request( 37 | method=method, 38 | url=url, 39 | headers=headers, 40 | stream=stream, 41 | extensions={}, 42 | ) 43 | 44 | assert ext["http_version"] == b"HTTP/1.1" 45 | 46 | await send( 47 | {"type": "http.response.start", "status": status_code, "headers": headers} 48 | ) 49 | 50 | try: 51 | async for chunk in stream: 52 | await send( 53 | {"type": "http.response.body", "body": chunk, "more_body": True} 54 | ) 55 | await send({"type": "http.response.body", "body": b"", "more_body": False}) 56 | finally: 57 | await stream.aclose() 58 | 59 | def _get_request( 60 | self, scope: Scope, receive: Receive 61 | ) -> Tuple[ 62 | bytes, 63 | Tuple[bytes, bytes, Optional[int], bytes], 64 | List[Tuple[bytes, bytes]], 65 | httpx.AsyncByteStream, 66 | ]: 67 | asgi_request = ASGIRequest(scope, receive=receive) 68 | 69 | method = asgi_request.method.encode("utf-8") 70 | 71 | url = httpx.URL( 72 | scheme="https", # Only allow proxying to HTTPS services. 73 | host=self._hostname, # Swap hostname. 74 | path=self._root_path + asgi_request.url.path, # Inject root path. 75 | query=asgi_request.url.query.encode("utf-8"), 76 | ) 77 | 78 | headers = asgi_request.headers.mutablecopy() 79 | headers["host"] = self._hostname # Swap hostname. 80 | 81 | # Build a full HTTPX request to get a proper HTTPX stream object, using 82 | # the HTTPX public API only. 83 | # (We'd actually only want `httpx._content.encode_content()`, 84 | # or `httpx._content.AsyncIterableStream`.) 85 | httpx_request = httpx.Request( 86 | method=method, 87 | url=url, 88 | content=asgi_request.stream(), 89 | ) 90 | stream = httpx_request.stream 91 | assert isinstance(stream, httpx.AsyncByteStream) 92 | 93 | return method, url.raw, headers.raw, stream 94 | --------------------------------------------------------------------------------