forked from kenzuya/unshackle
feat(service): add GEOBLOCK negative geofence
Regions in GEOBLOCK refuse the service and everything else is allowed. A hit stops the run with a region error instead of auto-proxying; the check also covers proxy exits and the remote-dl client region.
This commit is contained in:
@@ -184,6 +184,14 @@ def categorize_exception(
|
||||
exc_str = f"{exc} {root}".lower()
|
||||
exc_type = type(root).__name__
|
||||
|
||||
if any(keyword in exc_str for keyword in ["geofence", "geoblock", "region", "not available in", "territory"]):
|
||||
return APIError(
|
||||
error_code=APIErrorCode.GEOFENCE,
|
||||
message=f"Content not available in your region: {redact_secrets(str(exc))}",
|
||||
details={**context, "reason": "geofence_restriction"},
|
||||
retryable=False,
|
||||
)
|
||||
|
||||
if "proxy" in str(root).lower():
|
||||
return APIError(
|
||||
error_code=APIErrorCode.INVALID_PROXY,
|
||||
@@ -220,14 +228,6 @@ def categorize_exception(
|
||||
http_status=503,
|
||||
)
|
||||
|
||||
if any(keyword in exc_str for keyword in ["geofence", "region", "not available in", "territory"]):
|
||||
return APIError(
|
||||
error_code=APIErrorCode.GEOFENCE,
|
||||
message=f"Content not available in your region: {redact_secrets(str(exc))}",
|
||||
details={**context, "reason": "geofence_restriction"},
|
||||
retryable=False,
|
||||
)
|
||||
|
||||
if any(keyword in exc_str for keyword in ["not found", "404", "does not exist", "invalid id"]):
|
||||
return APIError(
|
||||
error_code=APIErrorCode.NOT_FOUND,
|
||||
|
||||
@@ -207,7 +207,7 @@ def build_parent_ctx(
|
||||
|
||||
parent = click.Context(dummy)
|
||||
parent.obj = ContextData(config=service_config, cdm=cdm, proxy_providers=proxy_providers, profile=profile)
|
||||
params = {"proxy": proxy_param, "no_proxy": no_proxy}
|
||||
params = {"proxy": proxy_param, "no_proxy": no_proxy, "served": True}
|
||||
if extra_params:
|
||||
params.update(extra_params)
|
||||
parent.params = params
|
||||
@@ -357,9 +357,11 @@ def run_service_search(
|
||||
)
|
||||
service_module = Services.load(normalized_service)
|
||||
|
||||
from click import ClickException
|
||||
|
||||
try:
|
||||
service_instance = instantiate_service(parent_ctx, service_module, query)
|
||||
except ConnectionError as exc:
|
||||
except (ConnectionError, ClickException) as exc:
|
||||
raise categorize_exception(exc, {"service": normalized_service}) from exc
|
||||
except Exception as exc:
|
||||
raise APIError(
|
||||
@@ -2126,6 +2128,7 @@ async def dashboard_services_handler(request: web.Request) -> web.Response:
|
||||
"jobs": jobs.get(tag, 0),
|
||||
"aliases": list(services_module.ALIASES.get(tag, ())),
|
||||
"geofence": [],
|
||||
"geoblock": [],
|
||||
}
|
||||
if staged:
|
||||
# Only staged tags need a git call; the working tree already holds the new commit.
|
||||
@@ -2136,6 +2139,7 @@ async def dashboard_services_handler(request: web.Request) -> web.Response:
|
||||
module = services_module.MODULES.get(tag)
|
||||
if module is not None:
|
||||
row["geofence"] = list(getattr(module, "GEOFENCE", ()) or ())
|
||||
row["geoblock"] = list(getattr(module, "GEOBLOCK", ()) or ())
|
||||
rows.append(row)
|
||||
return web.json_response(rows)
|
||||
|
||||
@@ -3601,6 +3605,15 @@ def resolve_handler_proxy(
|
||||
log.debug(f"Server region lookup failed: {e!r}")
|
||||
server_region = None
|
||||
|
||||
geoblock = getattr(Services.load(normalized_service), "GEOBLOCK", ()) or ()
|
||||
if client_region.lower() in {x.lower() for x in geoblock}:
|
||||
raise APIError(
|
||||
APIErrorCode.GEOFENCE,
|
||||
f"Service is not available in your region ({client_region.upper()}). "
|
||||
"Pass --proxy with a proxy outside the blocked regions.",
|
||||
details={"service": normalized_service},
|
||||
)
|
||||
|
||||
in_client_region = bool(server_region) and server_region == client_region.lower()
|
||||
if not allowed:
|
||||
if server_region and not in_client_region:
|
||||
|
||||
@@ -201,6 +201,10 @@ async def services(request: web.Request) -> web.Response:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
geoblock:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
title_regex:
|
||||
oneOf:
|
||||
- type: string
|
||||
@@ -253,6 +257,7 @@ async def services(request: web.Request) -> web.Response:
|
||||
"tag": tag,
|
||||
"aliases": [],
|
||||
"geofence": [],
|
||||
"geoblock": [],
|
||||
"title_regex": None,
|
||||
"url": None,
|
||||
"help": None,
|
||||
@@ -270,6 +275,7 @@ async def services(request: web.Request) -> web.Response:
|
||||
|
||||
if hasattr(service_module, "GEOFENCE"):
|
||||
service_data["geofence"] = list(service_module.GEOFENCE)
|
||||
service_data["geoblock"] = list(getattr(service_module, "GEOBLOCK", ()) or ())
|
||||
|
||||
if hasattr(service_module, "TITLE_RE"):
|
||||
title_re = service_module.TITLE_RE
|
||||
@@ -2488,6 +2494,10 @@ async def dashboard_services(request: web.Request) -> web.Response:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
geoblock:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
'401':
|
||||
description: Dashboard key missing or invalid
|
||||
"""
|
||||
|
||||
@@ -106,6 +106,7 @@ class ImportService:
|
||||
|
||||
ALIASES: tuple[str, ...] = ()
|
||||
GEOFENCE: tuple[str, ...] = ()
|
||||
GEOBLOCK: tuple[str, ...] = ()
|
||||
NO_SUBTITLES: bool = False
|
||||
|
||||
def __init__(self, ctx: click.Context, service_tag: str, title: str, import_file: Optional[str]) -> None:
|
||||
|
||||
@@ -760,6 +760,7 @@ class RemoteService:
|
||||
|
||||
ALIASES: tuple[str, ...] = ()
|
||||
GEOFENCE: tuple[str, ...] = ()
|
||||
GEOBLOCK: tuple[str, ...] = ()
|
||||
NO_SUBTITLES: bool = False
|
||||
ANIME: bool = False
|
||||
DAILY: bool = False
|
||||
|
||||
@@ -150,6 +150,7 @@ class Service(metaclass=ABCMeta):
|
||||
|
||||
ALIASES: tuple[str, ...] = () # alternative tags for the service, matched without case.
|
||||
GEOFENCE: tuple[str, ...] = () # list of ip regions required to use the service. empty list == no specific region.
|
||||
GEOBLOCK: tuple[str, ...] = () # ip regions where the service refuses to work; everything else is allowed.
|
||||
ANIME: bool = False # service catalogue is anime; metadata lookups prefer AniList. Title.anime overrides per title.
|
||||
DAILY: bool = False # catalog is daily/date-based. episodes are named by air date. Title.daily overrides per title.
|
||||
# vault namespace override; when set, key vault read/write uses this tag instead of the service's own.
|
||||
@@ -185,6 +186,7 @@ class Service(metaclass=ABCMeta):
|
||||
best_available=bool(best_available),
|
||||
)
|
||||
|
||||
whose = "the server's" if ctx.parent and ctx.parent.params.get("served") else "your"
|
||||
if not ctx.parent or not ctx.parent.params.get("no_proxy"):
|
||||
if ctx.parent:
|
||||
proxy = ctx.parent.params["proxy"]
|
||||
@@ -248,13 +250,20 @@ class Service(metaclass=ABCMeta):
|
||||
if not proxy:
|
||||
# don't override the explicit proxy set by the user, even if they may be geoblocked
|
||||
with console.status("Checking if current region is Geoblocked...", spinner="dots"):
|
||||
if self.GEOFENCE:
|
||||
if self.GEOFENCE or self.GEOBLOCK:
|
||||
try:
|
||||
current_region = get_ip_info(self.session)["country"].lower()
|
||||
if any(x.lower() == current_region for x in self.GEOFENCE):
|
||||
current_region = (get_ip_info(self.session) or {}).get("country", "").lower()
|
||||
if not current_region:
|
||||
self.log.warning(f"Could not find {whose} region, so the region check does not run")
|
||||
elif self.is_region_allowed(current_region):
|
||||
self.log.info("Service is not Geoblocked in your region")
|
||||
elif not (fence_targets := [x for x in self.GEOFENCE if self.is_region_allowed(x)]):
|
||||
raise click.ClickException(
|
||||
f"Service is not available in {whose} region ({current_region.upper()}). "
|
||||
"Pass --proxy with a proxy outside the blocked regions."
|
||||
)
|
||||
else:
|
||||
requested_proxy = self.GEOFENCE[0] # first is likely main region
|
||||
requested_proxy = fence_targets[0] # first is likely main region
|
||||
self.log.info(
|
||||
f"Service is Geoblocked in your region, getting a Proxy to {requested_proxy}"
|
||||
)
|
||||
@@ -268,11 +277,12 @@ class Service(metaclass=ABCMeta):
|
||||
f"No proxy available for {requested_proxy}. "
|
||||
f"Pass --proxy with a proxy in {requested_proxy}, or the request can fail."
|
||||
)
|
||||
except click.ClickException:
|
||||
raise
|
||||
except Exception as e:
|
||||
self.log.warning(f"Failed to check geofence: {e}")
|
||||
current_region = None
|
||||
else:
|
||||
self.log.info("Service has no Geofence")
|
||||
self.log.info("Service has no region restrictions")
|
||||
|
||||
if proxy:
|
||||
self.session.proxies.update({"all": proxy})
|
||||
@@ -282,7 +292,14 @@ class Service(metaclass=ABCMeta):
|
||||
# plaintext-http forward-proxy requests with HTTP 407.
|
||||
# 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.
|
||||
self.current_region = verify_proxy_exit(self.session).get("country")
|
||||
exit_region = verify_proxy_exit(self.session).get("country")
|
||||
self.current_region = exit_region
|
||||
# Only GEOBLOCK applies here: an explicit --proxy outside GEOFENCE stays the user's call.
|
||||
if exit_region and any(x.lower() == exit_region.lower() for x in self.GEOBLOCK):
|
||||
raise click.ClickException(
|
||||
f"Service is not available in {whose} proxy's region ({exit_region.upper()}). "
|
||||
"Pass --proxy with a proxy outside the blocked regions."
|
||||
)
|
||||
else:
|
||||
# No proxy, use cached IP info for title caching (non-critical)
|
||||
try:
|
||||
@@ -292,6 +309,19 @@ class Service(metaclass=ABCMeta):
|
||||
self.log.debug(f"Failed to get cached IP info: {e}")
|
||||
self.current_region = None
|
||||
|
||||
@classmethod
|
||||
def is_region_allowed(cls, region: Optional[str]) -> bool:
|
||||
"""True when an exit IP in `region` (two-letter region code, any case) can use the service.
|
||||
|
||||
An unknown region (None or empty) is allowed, because there is nothing to compare.
|
||||
"""
|
||||
if not region:
|
||||
return True
|
||||
region = region.lower()
|
||||
if any(x.lower() == region for x in cls.GEOBLOCK):
|
||||
return False
|
||||
return not cls.GEOFENCE or any(x.lower() == region for x in cls.GEOFENCE)
|
||||
|
||||
def get_tracks_for_variants(
|
||||
self,
|
||||
title: Title_T,
|
||||
|
||||
Reference in New Issue
Block a user