Skip to main content

PROXY protocol library with asyncio server implementation

Project description

proxy-protocol

PROXY protocol library with asyncio server implementation.

build Coverage Status PyPI PyPI PyPI

Specification

API Documentation

Docker Image

Table of Contents

Install and Usage

$ pip install proxy-protocol

Integration with an asyncio.start_server based server is intended to be extremely simple. Here is an example, which will detect PROXY protocol v1 or v2.

from functools import partial

from proxyprotocol.base import ProxyProtocol
from proxyprotocol.detect import ProxyProtocolDetect
from proxyprotocol.reader import ProxyProtocolReader
from proxyprotocol.socket import SocketInfo

async def run(host: str, port: int) -> None:
    pp_detect = ProxyProtocolDetect()
    pp_reader = ProxyProtocolReader(pp_detect)
    callback = partial(on_connection, pp_reader)
    server = await asyncio.start_server(callback, host, port)
    async with server:
        await server.serve_forever()

async def on_connection(pp_reader: ProxyProtocolReader,
                        reader: StreamReader, writer: StreamWriter) -> None:
    result = await pp_reader.read(reader)
    info = SocketInfo(writer, result)
    print(info.family, info.peername)
    # ... continue using connection

To simplify PROXY protocol use based on configuration, the version can also be read from a string.

from proxyprotocol.version import ProxyProtocolVersion

pp_noop = ProxyProtocolVersion.get()
pp_detect = ProxyProtocolVersion.get('detect')
pp_v1 = ProxyProtocolVersion.get('v1')
pp_v2 = ProxyProtocolVersion.get('v2')

The pp_noop object in this example is a special case implementation that does not read a PROXY protocol header from the stream at all. It may be used to disable PROXY protocol use without complicating your server code.

You can also check out the proxyprotocol/echo.py reference implementation. If you configure your proxy to send PROXY protocol to localhost:10007, you can see it in action:

$ proxyprotocol-echo --help
$ proxyprotocol-echo detect
$ proxyprotocol-echo noop

Server Usage

Two basic server implementations are included for reference. Using the two together can demonstrate the process end-to-end: use proxyprotocol-server to proxy connections with a PROXY protocol header to proxyprotocol-echo, which then displays the original connection information.

The hostname:port arguments used by both types of servers are parsed by the Address class, which allows for customization of SSL/TLS and PROXY protocol versions.

Echo Server

The proxyprotocol-echo server expects inbound connections to provide a PROXY protocol header indicating the original source of the connection. After the header, all received data will be echoed back to the client.

proxyprotocol-echo --help
proxyprotocol-echo  # run the server

Proxy Server

The proxyprotocol-server server proxies inbound connections to another host/port endoint, prefixing the outbound connection with a PROXY protocol header to indicate the original connection information.

proxyprotocol-server --help
proxyprotocol-server --service localhost:10000 localhost:10007

Development and Testing

You will need to do some additional setup to develop and test plugins. First off, I suggest activating a venv. Then, install the test requirements and a local link to the proxy-protocol package:

$ pip install -r test/requirements.txt
$ pip install -e .

Run the tests with py.test:

$ py.test

If you intend to create a pull request, you should make sure the full suite of tests run by CI/CD is passing:

$ py.test
$ mypy --strict proxyprotocol test
$ flake8 proxyprotocol test

Type Hinting

This project makes heavy use of Python's type hinting system, with the intention of a clean run of mypy:

mypy --strict proxyprotocol test

No code contribution will be accepted unless it makes every effort to use type hinting to the extent possible and common in the rest of the codebase.

The MIT License (MIT)

Copyright (c) 2021 Ian Good

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

proxy-protocol-0.7.5.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

proxy_protocol-0.7.5-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file proxy-protocol-0.7.5.tar.gz.

File metadata

  • Download URL: proxy-protocol-0.7.5.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for proxy-protocol-0.7.5.tar.gz
Algorithm Hash digest
SHA256 84bda89d65d31df2dd7088582dab3490a4bbbf7a44d1411c070c862319208cbf
MD5 38bd24b56d63e6ad18283d94e0260003
BLAKE2b-256 7236e3d3419d1df28d58586a5b4702de63f7cdcf353cc595d2f19fd111337ab8

See more details on using hashes here.

File details

Details for the file proxy_protocol-0.7.5-py3-none-any.whl.

File metadata

  • Download URL: proxy_protocol-0.7.5-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for proxy_protocol-0.7.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e7691d766a15674e1bf36f822ec0e0149850f333aa9902633f7fa351106994ac
MD5 a1e7d1f9f27cccaf53d1e773ab6a325f
BLAKE2b-256 1744a7eda84cbfd530956bca0c948e1c803cdb808eaf14b3f3a846a651f15c12

See more details on using hashes here.

Supported by

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