Skip to content

Commit 4b9fec7

Browse files
authored
Merge pull request #54 from icgood/doc
Improve docs and error messages
2 parents f7f1dc5 + f9e1dc4 commit 4b9fec7

16 files changed

Lines changed: 116 additions & 81 deletions

File tree

.github/workflows/python-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
3.11
2828
- name: Install build tools
2929
run: |
30-
python -m pip install hatch coveralls
30+
python -m pip install hatch
3131
- name: Run test suites, type checks, and linters
3232
run: |
3333
hatch run all:check

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ proxy-protocol
44
PROXY protocol library with [asyncio][2] server implementation.
55

66
[![build](https://github.com/icgood/proxy-protocol/actions/workflows/python-check.yml/badge.svg)](https://github.com/icgood/proxy-protocol/actions/workflows/python-check.yml)
7-
[![Coverage Status](https://coveralls.io/repos/icgood/proxy-protocol/badge.svg)](https://coveralls.io/r/icgood/proxy-protocol)
87
[![PyPI](https://img.shields.io/pypi/v/proxy-protocol.svg)](https://pypi.python.org/pypi/proxy-protocol)
98
[![PyPI](https://img.shields.io/pypi/pyversions/proxy-protocol.svg)](https://pypi.python.org/pypi/proxy-protocol)
109
![platforms](https://img.shields.io/badge/platform-linux%20%7C%20macOS%20%7C%20windows-blueviolet)
1110
[![PyPI](https://img.shields.io/pypi/l/proxy-protocol.svg)](https://pypi.python.org/pypi/proxy-protocol)
1211

1312
#### [Specification](https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt)
14-
#### [API Documentation](http://icgood.github.io/proxy-protocol/)
13+
#### [API Reference](http://icgood.github.io/proxy-protocol/)
1514
#### [Docker Image](https://github.com/icgood/proxy-protocol/pkgs/container/proxy-protocol)
1615

1716
### Table of Contents
@@ -41,8 +40,7 @@ from proxyprotocol.sock import SocketInfo
4140

4241
async def run(host: str, port: int) -> None:
4342
pp_detect = ProxyProtocolDetect()
44-
pp_reader = ProxyProtocolReader(pp_detect)
45-
callback = reader.get_callback(on_connection)
43+
callback = ProxyProtocolReader(pp_detect).get_callback(on_connection)
4644
server = await asyncio.start_server(callback, host, port)
4745
async with server:
4846
await server.serve_forever()
@@ -59,7 +57,7 @@ read from a string.
5957
```python
6058
from proxyprotocol.version import ProxyProtocolVersion
6159

62-
pp_noop = ProxyProtocolVersion.get()
60+
pp_noop = ProxyProtocolVersion.get(None)
6361
pp_detect = ProxyProtocolVersion.get('detect')
6462
pp_v1 = ProxyProtocolVersion.get('v1')
6563
pp_v2 = ProxyProtocolVersion.get('v2')
@@ -148,4 +146,4 @@ hinting to the extent possible and common in the rest of the codebase.
148146
[4]: https://github.com/icgood/proxy-protocol/blob/main/proxyprotocol/server/echo.py
149147
[6]: https://www.python.org/dev/peps/pep-0484/
150148
[7]: http://mypy-lang.org/
151-
[8]: https://icgood.github.io/proxy-protocol/proxyprotocol.html#proxyprotocol.server.Address
149+
[8]: https://icgood.github.io/proxy-protocol/proxyprotocol.server.html#proxyprotocol.server.Address

doc/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Table of Contents
2323
proxyprotocol.dnsbl
2424
proxyprotocol.tlv
2525
proxyprotocol.reader
26+
proxyprotocol.server
2627

2728

2829
Indices and tables
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
``proxyprotocol.server``
3+
========================
4+
5+
.. automodule:: proxyprotocol.server
6+
:members:

proxyprotocol/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#: The package version string.
2-
__version__ = '0.11.0'
2+
__version__ = '0.11.1'

proxyprotocol/build.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def build_socket_result(sock: socket.socket, *,
2323
2424
Args:
2525
sock: A connected socket object.
26+
unique_id: A unique ID to associate with the connection.
27+
dnsbl: The DNSBL lookup result for the connection.
2628
2729
"""
2830
peername: SockAddr = sock.getpeername()
@@ -41,6 +43,8 @@ def build_transport_result(transport: TransportProtocol, *,
4143
4244
Args:
4345
transport: A connected transport object.
46+
unique_id: A unique ID to associate with the connection.
47+
dnsbl: The DNSBL lookup result for the connection.
4448
4549
"""
4650
sock: socket.socket = transport.get_extra_info('socket')

proxyprotocol/noop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
class ProxyProtocolNoop(ProxyProtocol):
13-
"""Implements :class:`~proxyprotocol.base.ProxyProtocol` but does not read
13+
"""Implements :class:`~proxyprotocol.ProxyProtocol` but does not read
1414
anything from the stream. A
1515
:class:`~proxyprotocol.result.ProxyResultLocal` result is always
1616
returned.

proxyprotocol/result.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import annotations
33

44
import socket
5-
import sys
65
from abc import abstractmethod, ABCMeta
76
from enum import Enum
87
from ipaddress import IPv4Address, IPv6Address
@@ -77,19 +76,32 @@ class ProxyResultType(Enum):
7776
"""The type of proxy result."""
7877

7978
#: The connection is not proxied at all.
80-
LOCAL = 1
79+
LOCAL = 1, 'AF_UNSPEC'
8180

8281
#: The connection is proxied from an unknown address family.
83-
UNKNOWN = 2
82+
UNKNOWN = 2, 'AF_UNSPEC'
8483

8584
#: The connection is proxied from an IPv4 address.
86-
IPv4 = 3
85+
IPv4 = 3, 'AF_INET'
8786

8887
#: The connection is proxied from an IPv6 address.
89-
IPv6 = 4
88+
IPv6 = 4, 'AF_INET6'
9089

9190
#: The connection is proxied from a UNIX socket.
92-
UNIX = 5
91+
UNIX = 5, 'AF_UNIX'
92+
93+
@property
94+
def family(self) -> AddressFamily:
95+
"""The address family corresponding to the proxy result type.
96+
97+
Raises:
98+
AttributeError: The address family does not exist on the current
99+
platform, e.g. :data:`~socket.AF_UNIX` on Windows.
100+
101+
"""
102+
family_attr = self.value[1]
103+
family: AddressFamily = getattr(socket, family_attr)
104+
return family
93105

94106

95107
class ProxyResult(metaclass=ABCMeta):
@@ -126,8 +138,17 @@ def dest(self) -> Address:
126138

127139
@property
128140
def family(self) -> AddressFamily:
129-
"""The original socket address family."""
130-
return socket.AF_UNSPEC
141+
"""The original socket address family.
142+
143+
See Also:
144+
:attr:`ProxyResultType.family`
145+
146+
Raises:
147+
AttributeError: An address family was proxied to a platform that
148+
does not support it. Use :attr:`.type` instead when possible.
149+
150+
"""
151+
return self.type.family
131152

132153
@property
133154
def protocol(self) -> Optional[SocketKind]:
@@ -324,10 +345,6 @@ def source(self) -> Tuple[IPv4Address, int]:
324345
def dest(self) -> Tuple[IPv4Address, int]:
325346
return self._dest
326347

327-
@property
328-
def family(self) -> AddressFamily:
329-
return socket.AF_INET
330-
331348
@property
332349
def protocol(self) -> Optional[SocketKind]:
333350
return self._protocol
@@ -384,10 +401,6 @@ def source(self) -> Tuple[IPv6Address, int]:
384401
def dest(self) -> Tuple[IPv6Address, int]:
385402
return self._dest
386403

387-
@property
388-
def family(self) -> AddressFamily:
389-
return socket.AF_INET6
390-
391404
@property
392405
def protocol(self) -> Optional[SocketKind]:
393406
return self._protocol
@@ -443,12 +456,6 @@ def source(self) -> str:
443456
def dest(self) -> str:
444457
return self._dest
445458

446-
@property
447-
def family(self) -> AddressFamily:
448-
if sys.platform == 'win32': # pragma: no cover
449-
raise AttributeError('AF_UNIX')
450-
return socket.AF_UNIX
451-
452459
@property
453460
def protocol(self) -> Optional[SocketKind]:
454461
return self._protocol

proxyprotocol/server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def pp(self) -> ProxyProtocol:
6262

6363
@property
6464
def ssl(self) -> Optional[SSLContext]:
65-
"""The: class:`~ssl.SSLContext` to use on the address."""
65+
"""The :class:`~ssl.SSLContext` to use on the address."""
6666
if self.url.scheme == 'ssl':
6767
if self._ssl is None:
6868
if self.server:

proxyprotocol/sock.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def sockname(self) -> SockAddr:
105105
"""The local address of the socket.
106106
107107
See Also:
108-
:meth:`~socket.socket.getsockname`
108+
:meth:`socket.socket.getsockname`
109109
110110
"""
111111
...
@@ -142,7 +142,7 @@ def peername(self) -> SockAddr:
142142
"""The remote address of the socket.
143143
144144
See Also:
145-
:meth:`~socket.socket.getpeername`
145+
:meth:`socket.socket.getpeername`
146146
147147
"""
148148
...
@@ -224,7 +224,8 @@ def cipher(self) -> Optional[Cipher]:
224224
@property
225225
@abstractmethod
226226
def peercert(self) -> Optional[PeerCert]:
227-
"""The :meth:`~ssl.SSLSocket.peercert` value for encrypted connections.
227+
"""The :meth:`~ssl.SSLSocket.getpeercert` value for encrypted
228+
connections.
228229
229230
Note:
230231
For proxied connections, this data may be unavailable, depending on
@@ -284,6 +285,8 @@ class SocketInfoProxy(SocketInfo):
284285
protocol result.
285286
286287
Args:
288+
transport: The :class:`~asyncio.BaseTransport` or
289+
:class:`~asyncio.StreamWriter` for the connection.
287290
result: The PROXY protocol result.
288291
289292
"""
@@ -376,8 +379,7 @@ class SocketInfoLocal(SocketInfo):
376379

377380
__slots__ = ['_transport', '_unique_id', '_dnsbl']
378381

379-
def __init__(self, transport: TransportProtocol,
380-
result: Optional[ProxyResult] = None, *,
382+
def __init__(self, transport: TransportProtocol, *,
381383
unique_id: bytes = b'', dnsbl: Optional[str] = None) -> None:
382384
super().__init__(transport)
383385
self._unique_id = unique_id

0 commit comments

Comments
 (0)