Skip to main content

An ASGI middleware for dynamic reverse proxy

Project description

Asgiproxify

Asgiproxify is an ASGI middleware for dynamic reverse proxy.

Installation

Of course, you can directly install from PyPI.

(venv) $ pip install asgiproxify

Getting Started

You can easily make a running example from these lines:

import uvicorn

from asgiproxify import Asgiproxify

proxify = Asgiproxify()
uvicorn.run(proxify)

Oh, you have an ASGI server already? You can set a fallback:

import uvicorn

from app import app
from asgiproxify import Asgiproxify

proxify = Asgiproxify(app) # or proxify.to(app)
uvicorn.run(proxify)

Adding a new reverse proxy endpoint is as simple as:

import uvicorn

from asgiproxify import Asgiproxify, AsgiproxifyHandler

proxify = Asgiproxify()


@proxify.register('/example/')
class FooProxyHanlder(AsgiproxifyHandler):
	pass

uvicorn.run(proxify)

This will register a handler to handle all requests to /example/*. By default, an AsgiproxifyHandler will proxy any request to example.org.

AsgiproxifyHandler can be smarter than that, like proxy to any URL you wish, like this:

import uvicorn

from asgiproxify import Asgiproxify, AsgiproxifyHandler

proxify = Asgiproxify()


@proxify.register('/LICENSE')
class FooProxyHanlder(AsgiproxifyHandler):
	def make_request_url(self):
		return 'https://www.gnu.org/licenses/gpl-3.0.txt'

uvicorn.run(proxify)

You may dynamically make revrese proxy by reading self.scope. Its content is specified in ASGI Specification.

A little example for that will be:

import uvicorn

from asgiproxify import Asgiproxify, AsgiproxifyHandler

proxify = Asgiproxify()


@proxify.register('/')
class FooProxyHanlder(AsgiproxifyHandler):
	def make_request_url(self):
		return 'https://ftp.gnu.org' + self.scope['path']

uvicorn.run(proxify)

You can do more with it by utilizing the scope and other infomation. The parameters you can tweek from the hander are request_url, request_cookies, request_headers and response_headers. If that's not enough for you, you can override make_request which will allow you to create your own aiohttp request!

You can consult proxify.py for more technical infomation.

License

This project is proudly licensed in GNU General Public License v3 or later.

Please refer to COPYING for more infomation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgiproxify-0.0.1.tar.gz (16.0 kB view hashes)

Uploaded Source

Built Distribution

asgiproxify-0.0.1-py3-none-any.whl (17.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page