mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 21:15:18 +00:00
feat(service): warn when a proxy exit is outside GEOFENCE
This commit is contained in:
@@ -108,7 +108,8 @@ Declared at class level to configure framework behaviour:
|
|||||||
|
|
||||||
`GEOFENCE` drives an automatic proxy: on startup, if you did not pass an
|
`GEOFENCE` drives an automatic proxy: on startup, if you did not pass an
|
||||||
explicit `--proxy`, the base class does a live IP check and, if your region is
|
explicit `--proxy`, the base class does a live IP check and, if your region is
|
||||||
blocked, fetches a proxy to `GEOFENCE[0]` from your configured proxy providers.
|
blocked, fetches a proxy to `GEOFENCE[0]` from your configured proxy providers. A
|
||||||
|
proxy exit outside `GEOFENCE` logs a warning and the run continues.
|
||||||
|
|
||||||
`GEOBLOCK` is the negative form: the service works everywhere except the listed
|
`GEOBLOCK` is the negative form: the service works everywhere except the listed
|
||||||
regions. With no `GEOFENCE` to name a main region, a `GEOBLOCK` hit stops the run
|
regions. With no `GEOFENCE` to name a main region, a `GEOBLOCK` hit stops the run
|
||||||
@@ -117,7 +118,7 @@ with a "not available in your region" error; pass `--proxy` yourself.
|
|||||||
```python
|
```python
|
||||||
class EXAMPLE(Service):
|
class EXAMPLE(Service):
|
||||||
ALIASES = ("EX", "DOMAIN")
|
ALIASES = ("EX", "DOMAIN")
|
||||||
GEOFENCE = ("US", "UK")
|
GEOFENCE = ("US", "GB")
|
||||||
VAULT_TAG = "DIFFERENT_NAME"
|
VAULT_TAG = "DIFFERENT_NAME"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -124,7 +124,8 @@ resolved through the per-service `proxy_map`, and, if `GEOFENCE` is set and you
|
|||||||
IP check and auto-fetches a proxy to `GEOFENCE[0]` when it detects a geoblock. A
|
IP check and auto-fetches a proxy to `GEOFENCE[0]` when it detects a geoblock. A
|
||||||
`GEOBLOCK` hit with no `GEOFENCE` stops the run with a "not available in your region"
|
`GEOBLOCK` hit with no `GEOFENCE` stops the run with a "not available in your region"
|
||||||
error, because there is no main region to fetch a proxy for; pass `--proxy` yourself.
|
error, because there is no main region to fetch a proxy for; pass `--proxy` yourself.
|
||||||
A proxy whose exit is in a `GEOBLOCK` region stops the run the same way.
|
A proxy whose exit is in a `GEOBLOCK` region stops the run the same way. A proxy whose exit is
|
||||||
|
outside `GEOFENCE` logs a warning and the run continues.
|
||||||
See [Proxies and VPN](../guide/proxies-and-vpn.md).
|
See [Proxies and VPN](../guide/proxies-and-vpn.md).
|
||||||
|
|
||||||
!!! note "`TrackRequest`"
|
!!! note "`TrackRequest`"
|
||||||
|
|||||||
@@ -231,9 +231,10 @@ proxy. The service stops with `Proxy check failed` in two cases:
|
|||||||
unshackle tries each geolocation API in turn, and a 429 moves on to the next one. If at
|
unshackle tries each geolocation API in turn, and a 429 moves on to the next one. If at
|
||||||
least one API answers through the proxy but none of them gives an IP, the proxy passes
|
least one API answers through the proxy but none of them gives an IP, the proxy passes
|
||||||
the check without the IP comparison. unshackle does not compare the exit country with the region you asked for,
|
the check without the IP comparison. unshackle does not compare the exit country with the region you asked for,
|
||||||
because geolocation data can be out of date.
|
because geolocation data can be out of date. When the service lists the regions it supports and
|
||||||
|
the proxy exit is in none of them, unshackle logs a warning and continues.
|
||||||
|
|
||||||
Over the REST API, the server returns this failure to the client as `INVALID_PROXY`.
|
Over the REST API, the server returns a `Proxy check failed` error to the client as `INVALID_PROXY`.
|
||||||
|
|
||||||
### System proxy and environment variables
|
### System proxy and environment variables
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
"""Region checks for GEOFENCE and GEOBLOCK on the Service base class."""
|
"""Region checks for GEOFENCE and GEOBLOCK on the Service base class."""
|
||||||
|
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from tests.core.test_service_proxy_env import PROXY, Dummy
|
||||||
|
from unshackle.core import service as service_mod
|
||||||
|
from unshackle.core.api.session_log import SessionLogBuffer, capture_service_logs
|
||||||
from unshackle.core.service import Service
|
from unshackle.core.service import Service
|
||||||
|
from unshackle.core.utils import run_proxy
|
||||||
|
|
||||||
|
|
||||||
class Blocked(Service):
|
class Blocked(Service):
|
||||||
@@ -40,3 +48,67 @@ def test_geoblock_wins_over_geofence():
|
|||||||
def test_unknown_region_is_allowed():
|
def test_unknown_region_is_allowed():
|
||||||
assert Blocked.is_region_allowed("")
|
assert Blocked.is_region_allowed("")
|
||||||
assert Blocked.is_region_allowed(None)
|
assert Blocked.is_region_allowed(None)
|
||||||
|
|
||||||
|
|
||||||
|
def warnings_for(
|
||||||
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
exit_region: str | None,
|
||||||
|
class_fence: tuple[str, ...] | None = (),
|
||||||
|
own_fence: tuple[str, ...] | None = None,
|
||||||
|
block: tuple[str, ...] = (),
|
||||||
|
) -> list[str]:
|
||||||
|
"""Build a service through a proxy with the given exit: the warnings a remote session gets from it."""
|
||||||
|
|
||||||
|
class EXAMPLE(Dummy):
|
||||||
|
GEOFENCE = class_fence
|
||||||
|
GEOBLOCK = block
|
||||||
|
|
||||||
|
def __init__(self, ctx): # type: ignore[no-untyped-def]
|
||||||
|
if own_fence is not None:
|
||||||
|
self.GEOFENCE = own_fence
|
||||||
|
super().__init__(ctx)
|
||||||
|
|
||||||
|
monkeypatch.setattr(
|
||||||
|
service_mod, "verify_proxy_exit", lambda session: {"country": exit_region} if exit_region else {}
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(run_proxy, "watched", {})
|
||||||
|
monkeypatch.setattr(run_proxy, "proxy_hosts", set())
|
||||||
|
ctx = SimpleNamespace(
|
||||||
|
obj=SimpleNamespace(config={}, proxy_providers=[]), parent=SimpleNamespace(params={"proxy": PROXY})
|
||||||
|
)
|
||||||
|
buffer = SessionLogBuffer()
|
||||||
|
with capture_service_logs("EXAMPLE", buffer):
|
||||||
|
service = EXAMPLE(ctx)
|
||||||
|
assert service.current_region == exit_region
|
||||||
|
return [r["message"] for r in buffer.since(0) if r["level"] != "INFO"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_proxy_exit_outside_the_geofence_logs_one_warning(monkeypatch):
|
||||||
|
assert warnings_for(monkeypatch, "hu", ("fr", "PL")) == [
|
||||||
|
"Proxy exit region HU is outside the regions of the service (FR, PL)"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("exit_region", "fence"),
|
||||||
|
[("Fr", ("fR", "PL")), ("hu", ()), ("hu", None), (None, ("fr",))],
|
||||||
|
ids=["inside-any-case", "no-fence", "none-fence", "unknown-exit"],
|
||||||
|
)
|
||||||
|
def test_no_warning_when_the_exit_is_allowed_or_unknown(monkeypatch, exit_region, fence):
|
||||||
|
assert warnings_for(monkeypatch, exit_region, fence) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_an_instance_geofence_decides(monkeypatch):
|
||||||
|
assert warnings_for(monkeypatch, "hu", ("hu", "fr"), own_fence=("fr",)) == [
|
||||||
|
"Proxy exit region HU is outside the regions of the service (FR)"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_an_empty_instance_geofence_gives_no_warning(monkeypatch):
|
||||||
|
assert warnings_for(monkeypatch, "hu", ("fr",), own_fence=()) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_blocked_region_is_not_listed_as_supported(monkeypatch):
|
||||||
|
assert warnings_for(monkeypatch, "hu", ("us", "gb"), block=("GB",)) == [
|
||||||
|
"Proxy exit region HU is outside the regions of the service (US)"
|
||||||
|
]
|
||||||
|
|||||||
@@ -235,16 +235,20 @@ class Service(TitleSource):
|
|||||||
# plaintext-http forward-proxy requests with HTTP 407.
|
# plaintext-http forward-proxy requests with HTTP 407.
|
||||||
# Verify the proxy IP every time, because a proxy can change its exit node. A dead
|
# Verify the proxy IP every time, because a proxy can change its exit node. A dead
|
||||||
# proxy fails here, not after every service request has used up its retries.
|
# proxy fails here, not after every service request has used up its retries.
|
||||||
exit_region = verify_proxy_exit(self.session).get("country")
|
self.current_region = verify_proxy_exit(self.session).get("country")
|
||||||
self.current_region = exit_region
|
region = (self.current_region or "").upper()
|
||||||
# Only GEOBLOCK applies here: an explicit --proxy outside GEOFENCE stays the user's call.
|
blocked = [x.upper() for x in self.GEOBLOCK or ()]
|
||||||
if exit_region and any(x.lower() == exit_region.lower() for x in self.GEOBLOCK):
|
if region and region in blocked:
|
||||||
raise coded(
|
raise coded(
|
||||||
click.ClickException(
|
click.ClickException(f"Service is not available in {whose} proxy's region ({region})"),
|
||||||
f"Service is not available in {whose} proxy's region ({exit_region.upper()})"
|
|
||||||
),
|
|
||||||
APIErrorCode.GEOFENCE,
|
APIErrorCode.GEOFENCE,
|
||||||
)
|
)
|
||||||
|
# Warn only: the proxy is the user's call. Not is_region_allowed: self.GEOFENCE can be set per instance.
|
||||||
|
fence = [x.upper() for x in self.GEOFENCE or () if x.upper() not in blocked]
|
||||||
|
if region and fence and region not in fence:
|
||||||
|
self.log.warning(
|
||||||
|
f"Proxy exit region {region} is outside the regions of the service ({', '.join(fence)})"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# No proxy, use cached IP info for title caching (non-critical)
|
# No proxy, use cached IP info for title caching (non-critical)
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ class EXAMPLE(Service):
|
|||||||
|
|
||||||
# ALIASES: extra CLI tags that resolve to this service (e.g. `dl EX ...`).
|
# ALIASES: extra CLI tags that resolve to this service (e.g. `dl EX ...`).
|
||||||
ALIASES = ("EX", "DOMAIN")
|
ALIASES = ("EX", "DOMAIN")
|
||||||
# GEOFENCE: regions required; the framework warns/blocks if proxy region mismatches.
|
# GEOFENCE: regions required; the framework warns if the proxy exit region is not one of them.
|
||||||
GEOFENCE = ("US", "UK")
|
GEOFENCE = ("US", "GB")
|
||||||
# GEOBLOCK: the negative form; regions where the service refuses to work, all others allowed.
|
# GEOBLOCK: the negative form; regions where the service refuses to work, all others allowed.
|
||||||
# GEOBLOCK = ("GB",)
|
# GEOBLOCK = ("GB",)
|
||||||
# TITLE_RE: named groups (?P<...>) parsed in get_titles(). Accepts URL or bare id.
|
# TITLE_RE: named groups (?P<...>) parsed in get_titles(). Accepts URL or bare id.
|
||||||
|
|||||||
@@ -70,8 +70,8 @@ class MUSIC_EXAMPLE(Service):
|
|||||||
|
|
||||||
# ALIASES: extra CLI tags that resolve to this service (e.g. `dl MUSICEX ...`).
|
# ALIASES: extra CLI tags that resolve to this service (e.g. `dl MUSICEX ...`).
|
||||||
ALIASES = ("MUSICEX", "MUSICDOMAIN")
|
ALIASES = ("MUSICEX", "MUSICDOMAIN")
|
||||||
# GEOFENCE: regions required; the framework warns/blocks if proxy region mismatches.
|
# GEOFENCE: regions required; the framework warns if the proxy exit region is not one of them.
|
||||||
GEOFENCE = ("US", "UK")
|
GEOFENCE = ("US", "GB")
|
||||||
# GEOBLOCK: the negative form; regions where the service refuses to work, all others allowed.
|
# GEOBLOCK: the negative form; regions where the service refuses to work, all others allowed.
|
||||||
# GEOBLOCK = ("GB",)
|
# GEOBLOCK = ("GB",)
|
||||||
# TITLE_RE: named groups (?P<...>) parsed in get_titles(). Accepts URL or bare id.
|
# TITLE_RE: named groups (?P<...>) parsed in get_titles(). Accepts URL or bare id.
|
||||||
|
|||||||
Reference in New Issue
Block a user