Skip to content

tool

User-oriented tool library.

ProxyFilterProto

Bases: Protocol

All proxy filter must implement like this.

Source code in src/fastapi_proxy_lib/core/_tool.py
class ProxyFilterProto(Protocol):
    """All proxy filter must implement like this."""

    def __call__(self, url: httpx.URL, /) -> Union[None, str]:
        """Decide whether accept the proxy request by the given url.

        Examples:
            Refer to [`default_proxy_filter`][fastapi_proxy_lib.core._tool.default_proxy_filter]

        Args:
            url: The target url of the client request to proxy.

        Returns:
            None: should accept the proxy request.
            str: should rejetc the proxy request.
                The `str` is the reason of reject.
        """

__call__(url)

Decide whether accept the proxy request by the given url.

Examples:

Refer to default_proxy_filter

Parameters:

Name Type Description Default
url URL

The target url of the client request to proxy.

required

Returns:

Name Type Description
None Union[None, str]

should accept the proxy request.

str Union[None, str]

should rejetc the proxy request. The str is the reason of reject.

Source code in src/fastapi_proxy_lib/core/_tool.py
def __call__(self, url: httpx.URL, /) -> Union[None, str]:
    """Decide whether accept the proxy request by the given url.

    Examples:
        Refer to [`default_proxy_filter`][fastapi_proxy_lib.core._tool.default_proxy_filter]

    Args:
        url: The target url of the client request to proxy.

    Returns:
        None: should accept the proxy request.
        str: should rejetc the proxy request.
            The `str` is the reason of reject.
    """

default_proxy_filter(url)

Filter by host.

If the host of url is ip address, which is not global ip address, then will reject it.

Warning

It will consumption time: 3.22~4.7 µs ± 42.6 ns.

Parameters:

Name Type Description Default
url URL

The target url of the client request to proxy.

required

Returns:

Name Type Description
None Union[None, str]

should accept the proxy request.

str Union[None, str]

should rejetc the proxy request. The str is the reason of reject.

Source code in src/fastapi_proxy_lib/core/_tool.py
def default_proxy_filter(url: httpx.URL) -> Union[None, str]:
    """Filter by host.

    If the host of url is ip address, which is not global ip address, then will reject it.

    Warning:
        It will consumption time: 3.22~4.7 µs ± 42.6 ns.

    Args:
        url: The target url of the client request to proxy.

    Returns:
        None: should accept the proxy request.
        str: should rejetc the proxy request.
            The `str` is the reason of reject.
    """
    try:
        ip_address = ipaddress.ip_address(url.host)
    except ValueError:
        return None

    if not ip_address.is_global:
        return "Deny proxy for non-public IP addresses."

    return None