|
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | 4 | import socket |
5 | | -import sys |
6 | 5 | from abc import abstractmethod, ABCMeta |
7 | 6 | from enum import Enum |
8 | 7 | from ipaddress import IPv4Address, IPv6Address |
@@ -77,19 +76,32 @@ class ProxyResultType(Enum): |
77 | 76 | """The type of proxy result.""" |
78 | 77 |
|
79 | 78 | #: The connection is not proxied at all. |
80 | | - LOCAL = 1 |
| 79 | + LOCAL = 1, 'AF_UNSPEC' |
81 | 80 |
|
82 | 81 | #: The connection is proxied from an unknown address family. |
83 | | - UNKNOWN = 2 |
| 82 | + UNKNOWN = 2, 'AF_UNSPEC' |
84 | 83 |
|
85 | 84 | #: The connection is proxied from an IPv4 address. |
86 | | - IPv4 = 3 |
| 85 | + IPv4 = 3, 'AF_INET' |
87 | 86 |
|
88 | 87 | #: The connection is proxied from an IPv6 address. |
89 | | - IPv6 = 4 |
| 88 | + IPv6 = 4, 'AF_INET6' |
90 | 89 |
|
91 | 90 | #: 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 |
93 | 105 |
|
94 | 106 |
|
95 | 107 | class ProxyResult(metaclass=ABCMeta): |
@@ -126,8 +138,17 @@ def dest(self) -> Address: |
126 | 138 |
|
127 | 139 | @property |
128 | 140 | 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 |
131 | 152 |
|
132 | 153 | @property |
133 | 154 | def protocol(self) -> Optional[SocketKind]: |
@@ -324,10 +345,6 @@ def source(self) -> Tuple[IPv4Address, int]: |
324 | 345 | def dest(self) -> Tuple[IPv4Address, int]: |
325 | 346 | return self._dest |
326 | 347 |
|
327 | | - @property |
328 | | - def family(self) -> AddressFamily: |
329 | | - return socket.AF_INET |
330 | | - |
331 | 348 | @property |
332 | 349 | def protocol(self) -> Optional[SocketKind]: |
333 | 350 | return self._protocol |
@@ -384,10 +401,6 @@ def source(self) -> Tuple[IPv6Address, int]: |
384 | 401 | def dest(self) -> Tuple[IPv6Address, int]: |
385 | 402 | return self._dest |
386 | 403 |
|
387 | | - @property |
388 | | - def family(self) -> AddressFamily: |
389 | | - return socket.AF_INET6 |
390 | | - |
391 | 404 | @property |
392 | 405 | def protocol(self) -> Optional[SocketKind]: |
393 | 406 | return self._protocol |
@@ -443,12 +456,6 @@ def source(self) -> str: |
443 | 456 | def dest(self) -> str: |
444 | 457 | return self._dest |
445 | 458 |
|
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 | | - |
452 | 459 | @property |
453 | 460 | def protocol(self) -> Optional[SocketKind]: |
454 | 461 | return self._protocol |
|
0 commit comments