Compare commits

...
3 Commits
Author SHA1 Message Date
imSp4rky dc188b7276 fix(manifests): match dash representations across periods by shape, not id
Some packagers renumber AdaptationSet and Representation ids in every Period, so audio took video segments after the first Period. A match never crosses codec, size or language.
2026-11-02 17:50:35 -07:00
imSp4rky a4ba7767c5 test(manifests): add shaka packager ad-cue output to the corpus 2026-11-02 17:50:25 -07:00
imSp4rky c744e9a87b perf(proxies): cache expressvpn and proton catalogues across requests
A config reload clears the cache; a failed answer is never kept.
2026-11-02 17:11:58 -07:00
10 changed files with 380 additions and 50 deletions
+21 -2
View File
@@ -2,7 +2,8 @@
Sources: the DASH-IF test vector database (with the stream counts and codecs it expects),
the example MPDs of the MPEG DASH spec (ISO/IEC 23009-1), the yt-dlp test data, and the
stream lists of the dash.js reference player, the Shaka Player demo and the hls.js tests.
stream lists of the dash.js reference player, the Shaka Player demo and the hls.js tests, and
Shaka Packager test output (ad cues written as Period splits).
Only the manifest text is stored; index.json maps each file to its source URL, which the
parser needs to resolve relative URLs. A manifest that fails to download keeps its previous
copy. An HLS media playlist is left out, since HLS parses only multivariant playlists.
@@ -35,6 +36,10 @@ DASHJS_SOURCES = (
YTDLP_TESTDATA = "https://api.github.com/repos/yt-dlp/yt-dlp/contents/test/testdata/"
HLSJS_STREAMS = "https://raw.githubusercontent.com/video-dev/hls.js/master/tests/test-streams.js"
SHAKA_ASSETS = "https://raw.githubusercontent.com/shaka-project/shaka-player/main/demo/common/assets.js"
SHAKA_PACKAGER_TESTDATA = (
"https://raw.githubusercontent.com/shaka-project/shaka-packager/main/packager/app/test/testdata/"
)
SHAKA_PACKAGER_OUTPUTS = ("vtt-text-to-mp4-with-ad-cues",)
OUT = Path(__file__).resolve().parent.parent / "tests" / "fixtures" / "manifests"
MAX_BYTES = 500_000
DEADLINE = 60
@@ -117,6 +122,12 @@ def shaka_streams() -> list[Stream]:
return [("shaka", name.replace("\\'", "'"), url, None) for name, url in pattern.findall(text)]
def shaka_packager_streams() -> list[Stream]:
return [
("shaka-packager", name, f"{SHAKA_PACKAGER_TESTDATA}{name}/output.mpd", None) for name in SHAKA_PACKAGER_OUTPUTS
]
def kind_of(head: bytes) -> Optional[str]:
"""Tell the manifest type from the first bytes, since many manifest URLs have no telling extension."""
text = head.lstrip(b"\xef\xbb\xbf").lstrip()
@@ -167,7 +178,15 @@ def fetch(stream: Stream) -> Optional[dict[str, Any]]:
def main() -> None:
streams: dict[str, Stream] = {}
lists = dashif_streams() + mpeg_streams() + ytdlp_streams() + dashjs_streams() + shaka_streams() + hlsjs_streams()
lists = (
dashif_streams()
+ mpeg_streams()
+ ytdlp_streams()
+ dashjs_streams()
+ shaka_streams()
+ hlsjs_streams()
+ shaka_packager_streams()
)
for source, name, url, expect in lists:
url = f"https:{url}" if url.startswith("//") else url
if url not in EXCLUDED:
+165
View File
@@ -0,0 +1,165 @@
"""DASH.resolve_representation must continue a track into a later Period without crossing codec, size or language."""
from __future__ import annotations
from typing import Any
import pytest
from tests.core.test_manifest_corpus import CORPUS, INDEX, KNOWN_FAILURES, parse
from unshackle.core.manifests import DASH
from unshackle.core.utils.xml import load_xml
RENUMBERED = b"""<MPD type="static" mediaPresentationDuration="PT20S">
<Period id="0" duration="PT10S">
<AdaptationSet id="0" contentType="text" lang="en">
<Representation id="0" bandwidth="1000" codecs="wvtt" mimeType="application/mp4"/>
</AdaptationSet>
<AdaptationSet id="1" contentType="video">
<Representation id="1" bandwidth="2000000" codecs="avc1" width="720" height="540"/>
<Representation id="2" bandwidth="500000" codecs="avc1" width="640" height="480"/>
</AdaptationSet>
<AdaptationSet id="2" contentType="audio" lang="en">
<Representation id="3" bandwidth="128000" codecs="mp4a.40.2" audioSamplingRate="48000"/>
</AdaptationSet>
</Period>
<Period id="1" duration="PT10S">
<AdaptationSet id="0" contentType="audio" lang="en">
<Representation id="0" bandwidth="129000" codecs="mp4a.40.2" audioSamplingRate="48000"/>
</AdaptationSet>
<AdaptationSet id="1" contentType="video">
<Representation id="1" bandwidth="480000" codecs="avc1" width="640" height="480"/>
<Representation id="2" bandwidth="1900000" codecs="avc1" width="720" height="540"/>
<Representation id="3" bandwidth="3000000" codecs="avc1" width="720" height="540"/>
</AdaptationSet>
</Period>
</MPD>"""
def video(rep_id: str, bandwidth: str, codecs: str = "avc1.640028") -> str:
return f'<Representation id="{rep_id}" bandwidth="{bandwidth}" codecs="{codecs}" width="1920" height="1080" mimeType="video/mp4"/>'
def audio(rep_id: str) -> str:
return f'<Representation id="{rep_id}" bandwidth="128000" codecs="mp4a.40.2" audioSamplingRate="48000" mimeType="audio/mp4"/>'
def two_periods(first: str, second: str) -> bytes:
return f'<MPD><Period id="0">{first}</Period><Period id="1">{second}</Period></MPD>'.encode()
def resolve(mpd: bytes, set_index: int, rep_id: str) -> tuple[str | None, str | None, str | None] | None:
p0, p1 = load_xml(mpd).findall("Period")
found = DASH.resolve_representation(p1, rep_id, p0.findall("AdaptationSet")[set_index])
if found is None:
return None
adaptation_set, rep = found
return adaptation_set.get("lang"), rep.get("id"), DASH.get_attr("codecs", adaptation_set, rep)
def test_renumbered_audio_continues_into_audio_not_a_video_rep_with_its_id():
assert resolve(RENUMBERED, 2, "3") == ("en", "0", "mp4a.40.2")
def test_renumbered_video_continues_into_the_same_size():
assert resolve(RENUMBERED, 1, "1") == (None, "2", "avc1")
assert resolve(RENUMBERED, 1, "2") == (None, "1", "avc1")
def test_track_missing_from_the_period_is_not_matched_to_another_type():
assert resolve(RENUMBERED, 0, "0") is None
def test_untyped_sets_that_swap_order_keep_audio_on_audio():
mpd = two_periods(
f"<AdaptationSet>{video('v', '5000000')}</AdaptationSet><AdaptationSet>{audio('a')}</AdaptationSet>",
f"<AdaptationSet>{audio('a')}</AdaptationSet><AdaptationSet>{video('v', '5000000')}</AdaptationSet>",
)
assert resolve(mpd, 1, "a") == (None, "a", "mp4a.40.2")
def test_codec_missing_from_the_period_is_skipped_not_swapped():
mpd = two_periods(
f'<AdaptationSet contentType="video">{video("avc", "5000000")}</AdaptationSet>'
f'<AdaptationSet contentType="video">{video("hevc", "3000000", "hvc1.2.4.L150")}</AdaptationSet>',
f'<AdaptationSet contentType="video">{video("avc", "5000000")}</AdaptationSet>',
)
assert resolve(mpd, 1, "hevc") is None
def test_renumbered_sets_that_swap_order_keep_the_codec():
mpd = two_periods(
f'<AdaptationSet id="0" contentType="video">{video("0", "8000000", "hvc1.2.4.L150")}</AdaptationSet>'
f'<AdaptationSet id="1" contentType="video">{video("1", "8000000", "dvh1.08.06")}</AdaptationSet>',
f'<AdaptationSet id="0" contentType="video">{video("0", "8000000", "dvh1.08.06")}</AdaptationSet>'
f'<AdaptationSet id="1" contentType="video">{video("1", "8000000", "hvc1.2.4.L150")}</AdaptationSet>',
)
assert resolve(mpd, 1, "1") == (None, "0", "dvh1.08.06")
@pytest.mark.parametrize("content", ["audio", "text"])
def test_language_missing_from_the_period_is_skipped_not_swapped(content: str):
def rep(rep_id: str) -> str:
return (
audio(rep_id) if content == "audio" else f'<Representation id="{rep_id}" bandwidth="1000" codecs="wvtt"/>'
)
mpd = two_periods(
f'<AdaptationSet id="1" contentType="{content}" lang="en">{rep("x")}</AdaptationSet>',
f'<AdaptationSet id="2" contentType="{content}" lang="de">{rep("y")}</AdaptationSet>',
)
assert resolve(mpd, 0, "x") is None
def test_regional_tag_continues_into_the_same_language():
mpd = two_periods(
f'<AdaptationSet id="1" contentType="audio" lang="en">{audio("a")}</AdaptationSet>',
f'<AdaptationSet id="5" contentType="audio" lang="en-US">{audio("b")}</AdaptationSet>',
)
assert resolve(mpd, 0, "a") == ("en-US", "b", "mp4a.40.2")
def test_same_identity_sets_that_swap_order_follow_the_rep_id():
dialog = f'<AdaptationSet contentType="audio" lang="en">{audio("dialog")}</AdaptationSet>'
described = f'<AdaptationSet contentType="audio" lang="en">{audio("ad")}</AdaptationSet>'
assert resolve(two_periods(dialog + described, described + dialog), 0, "dialog") == ("en", "dialog", "mp4a.40.2")
def test_stable_ladder_follows_bandwidth_rank_when_every_rung_drifts():
mpd = two_periods(
f'<AdaptationSet id="1" contentType="video">{video("v1", "5000000")}{video("v2", "6000000")}</AdaptationSet>',
f'<AdaptationSet id="1" contentType="video">{video("v1", "3000000")}{video("v2", "4500000")}</AdaptationSet>',
)
assert resolve(mpd, 0, "v1") == (None, "v1", "avc1.640028")
assert resolve(mpd, 0, "v2") == (None, "v2", "avc1.640028")
def test_non_integer_bandwidth_does_not_raise():
mpd = two_periods(
f'<AdaptationSet id="1" contentType="video">{video("v1", "5000000")}</AdaptationSet>',
f'<AdaptationSet id="1" contentType="video">{video("v1", "5e6")}</AdaptationSet>',
)
assert resolve(mpd, 0, "v1") == (None, "v1", "avc1.640028")
def multi_period(entry: dict[str, Any]) -> bool:
periods = load_xml((CORPUS / entry["file"]).read_bytes()).findall("Period")
return sum(DASH.is_content_period(p, []) for p in periods) > 1
@pytest.mark.parametrize(
"entry",
[e for e in INDEX if e["kind"] == "dash" and e["file"] not in KNOWN_FAILURES and multi_period(e)],
ids=lambda e: e["file"],
)
def test_corpus_matches_never_cross_codec_or_size(entry: dict[str, Any]):
shape = ("codecs", "width", "height")
for track in parse(entry):
data = track.data["dash"]
want = tuple(DASH.get_attr(a, data["adaptation_set"], data["representation"]) for a in shape)
for period in data["manifest"].findall("Period"):
if period is data["period"] or not DASH.is_content_period(period, []):
continue
found = DASH.resolve_representation(period, data["representation_id"], data["adaptation_set"])
if found is not None:
assert tuple(DASH.get_attr(a, *found) for a in shape) == want
+30
View File
@@ -35,3 +35,33 @@ def test_empty_catalogue_is_not_kept() -> None:
fetch()
fetch()
assert len(calls) == 2
def test_login_bound_providers_fetch_once_across_builds(monkeypatch: pytest.MonkeyPatch, tmp_path) -> None:
from types import SimpleNamespace
from unshackle.core.proxies.expressvpn import ExpressVPN
from unshackle.core.proxies.proton import ProtonVPN
requests_made: list[str] = []
def express_request(self: ExpressVPN, method: str, url: str, **_: object) -> SimpleNamespace:
requests_made.append("express")
return SimpleNamespace(json=lambda: {"locations": [{"country_code": "ca"}]})
def proton_api(self: ProtonVPN, method: str, path: str) -> SimpleNamespace:
requests_made.append(path)
body = {"LogicalServers": [{"ExitCountry": "CA"}]} if path.endswith("logicals") else {"VPN": {"MaxTier": 0}}
return SimpleNamespace(json=lambda: body)
monkeypatch.setattr(ExpressVPN, "get_srt", lambda self: "srt")
monkeypatch.setattr(ExpressVPN, "request", express_request)
monkeypatch.setattr(ProtonVPN, "api", proton_api)
for _ in range(2):
assert ExpressVPN(cache_path=str(tmp_path / "e.json")).get_locations() == [{"country_code": "ca"}]
proton = ProtonVPN(cookie_path=str(tmp_path / "c.txt"), cache_path=str(tmp_path / "p.json"))
assert proton.get_logicals() == [{"ExitCountry": "CA"}]
assert proton.get_max_tier() == 0
assert sorted(requests_made) == ["/vpn", "/vpn/v1/logicals", "express"]
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Generated with https://github.com/shaka-project/shaka-packager version <tag>-<hash>-<test>-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd" profiles="urn:mpeg:dash:profile:isoff-live:2011" minBufferTime="PT2S" type="static" mediaPresentationDuration="PT2.736067S">
<Period id="0" duration="PT2.002S">
<AdaptationSet id="1" contentType="audio" startWithSAP="1" segmentAlignment="true">
<Representation id="0" bandwidth="133961" codecs="mp4a.40.2" mimeType="audio/mp4" audioSamplingRate="44100">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
<SegmentTemplate timescale="44100" initialization="bear-640x360-audio-init.mp4" media="bear-640x360-audio-$Number$.m4s" startNumber="1">
<SegmentTimeline>
<S t="0" d="45056"/>
<S t="45056" d="43008"/>
</SegmentTimeline>
</SegmentTemplate>
</Representation>
</AdaptationSet>
<AdaptationSet id="0" contentType="video" width="640" height="360" frameRate="30000/1001" segmentAlignment="true" par="16:9">
<Representation id="1" bandwidth="974154" codecs="avc1.64001e" mimeType="video/mp4" sar="1:1">
<SegmentTemplate timescale="30000" initialization="bear-640x360-video-init.mp4" media="bear-640x360-video-$Number$.m4s" startNumber="1">
<SegmentTimeline>
<S t="0" d="30030" r="1"/>
</SegmentTimeline>
</SegmentTemplate>
</Representation>
</AdaptationSet>
<AdaptationSet id="2" contentType="text" segmentAlignment="true">
<Role schemeIdUri="urn:mpeg:dash:role:2011" value="subtitle"/>
<Representation id="2" bandwidth="2104" codecs="wvtt" mimeType="application/mp4">
<SegmentTemplate timescale="1000" initialization="bear-english-text-init.mp4" media="bear-english-text-$Number$.m4s" startNumber="1">
<SegmentTimeline>
<S t="0" d="1000" r="1"/>
<S t="2000" d="1"/>
</SegmentTimeline>
</SegmentTemplate>
</Representation>
</AdaptationSet>
</Period>
<Period id="1" duration="PT0.734067S">
<AdaptationSet id="1" contentType="audio" startWithSAP="1" segmentAlignment="true">
<Representation id="0" bandwidth="108129" codecs="mp4a.40.2" mimeType="audio/mp4" audioSamplingRate="44100">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
<SegmentTemplate timescale="44100" presentationTimeOffset="88288" initialization="bear-640x360-audio-init.mp4" media="bear-640x360-audio-$Number$.m4s" startNumber="3">
<SegmentTimeline>
<S t="88064" d="32768"/>
</SegmentTimeline>
</SegmentTemplate>
</Representation>
</AdaptationSet>
<AdaptationSet id="0" contentType="video" width="640" height="360" frameRate="30000/1001" segmentAlignment="true" par="16:9">
<Representation id="1" bandwidth="869088" codecs="avc1.64001e" mimeType="video/mp4" sar="1:1">
<SegmentTemplate timescale="30000" presentationTimeOffset="60059" initialization="bear-640x360-video-init.mp4" media="bear-640x360-video-$Number$.m4s" startNumber="3">
<SegmentTimeline>
<S t="60060" d="22022"/>
</SegmentTimeline>
</SegmentTemplate>
</Representation>
</AdaptationSet>
<AdaptationSet id="2" contentType="text" segmentAlignment="true">
<Role schemeIdUri="urn:mpeg:dash:role:2011" value="subtitle"/>
<Representation id="2" bandwidth="2216" codecs="wvtt" mimeType="application/mp4">
<SegmentTemplate timescale="1000" presentationTimeOffset="2001" initialization="bear-english-text-init.mp4" media="bear-english-text-$Number$.m4s" startNumber="4">
<SegmentTimeline>
<S t="2001" d="1000" r="2"/>
</SegmentTimeline>
</SegmentTemplate>
</Representation>
</AdaptationSet>
</Period>
</MPD>
+7
View File
@@ -2916,6 +2916,13 @@
"name": "onDemand profile",
"url": "https://download.tsi.telecom-paristech.fr/gpac/DASH_CONFORMANCE/TelecomParisTech/mp4-onDemand/mp4-onDemand-mpd-AV.mpd"
},
{
"file": "dash/shaka-packager-vtt-text-to-mp4-with-ad-cues-7bf01ab0.mpd",
"kind": "dash",
"source": "shaka-packager",
"name": "vtt-text-to-mp4-with-ad-cues",
"url": "https://raw.githubusercontent.com/shaka-project/shaka-packager/main/packager/app/test/testdata/vtt-text-to-mp4-with-ad-cues/output.mpd"
},
{
"file": "dash/shaka-shaka-player-history-h264-vp9-av1-live-dash-88d2ae20.mpd",
"kind": "dash",
+2
View File
@@ -272,8 +272,10 @@ def reload_config(app: web.Application, *, no_key: bool, serve_widevine: bool, s
old_devices, new_devices = _device_stems(config.serve), _device_stems(serve)
vars(config).update(vars(fresh))
from unshackle.core.api.handlers import reset_remote_pool
from unshackle.core.proxies.proxy import CATALOGUES
reset_remote_pool()
CATALOGUES.clear()
app["config"]["users"] = serve_config["users"]
if serve_widevine:
app["config"]["devices"] = _device_map(serve_config["devices"])
+56 -29
View File
@@ -147,6 +147,25 @@ class RollingMerge:
return self.cursor
def rep_shape(adaptation_set: Element, rep: Element) -> tuple[Optional[str], ...]:
"""The attributes a Representation must keep to continue a track into another Period."""
return tuple(DASH.get_attr(a, adaptation_set, rep) for a in ("codecs", "width", "height", "audioSamplingRate"))
def rep_bandwidth(rep: Element) -> float:
try:
return float(rep.get("bandwidth") or 0)
except ValueError:
return 0.0
def kind_and_lang(adaptation_set: Element) -> tuple[Optional[str], str]:
"""Content type and language subtag, without the region, of an AdaptationSet."""
mime = DASH.get_attr("mimeType", adaptation_set, adaptation_set.find("Representation")) or ""
kind = adaptation_set.get("contentType") or mime.split("/")[0] or None
return kind, (adaptation_set.get("lang") or "").split("-")[0].lower()
class DASH:
def __init__(self, manifest: Element, url: str) -> None:
if manifest is None:
@@ -503,7 +522,7 @@ class DASH:
if matched_rep is None or matched_as is None:
period_id = content_period.get("id", period_idx)
log.warning(f"Representation '{rep_id}' not found in period '{period_id}', skipping")
log.debug(f"Representation '{rep_id}' not found in period '{period_id}', skipping")
continue
p_init, p_segments, p_timescale, p_durations, p_kid = DASH.get_period_segments(
@@ -1316,40 +1335,48 @@ class DASH:
period: Element, rep_id: Optional[str], adaptation_set: Element
) -> Optional[tuple[Element, Element]]:
"""
Find the (AdaptationSet, Representation) in another Period matching rep_id.
Find the (AdaptationSet, Representation) in another Period that continues rep_id.
Representation ids are only unique within an AdaptationSet, so the set matching
`adaptation_set`'s identity wins over an earlier sibling that reuses the same id
for a different track (for example, audio description vs dialog).
Ids are not trusted alone, because some packagers renumber them per Period. A match never
crosses codec, size or language; within the set, bandwidth rank decides.
"""
as_key = DASH.adaptation_set_key(adaptation_set)
source = next((r for r in adaptation_set.findall("Representation") if r.get("id") == rep_id), None)
if source is None:
return None
want = rep_shape(adaptation_set, source)
def candidates(within: Element) -> list[tuple[Element, Element]]:
found = []
for as_ in within.findall("AdaptationSet"):
if DASH.is_trick_mode(as_) or DASH.adaptation_set_key(as_) != as_key:
continue
for rep in as_.findall("Representation"):
if rep.get("id") == rep_id:
found.append((as_, rep))
break
return found
def same_shape(as_: Element) -> list[Element]:
return sorted((r for r in as_.findall("Representation") if rep_shape(as_, r) == want), key=rep_bandwidth)
matches = candidates(period)
if matches:
parent = adaptation_set.getparent()
siblings = [as_ for as_, _ in candidates(parent)] if parent is not None else []
def sets(within: Optional[Element], key: Callable[[Element], Any]) -> list[Element]:
found = within.findall("AdaptationSet") if within is not None else []
target = key(adaptation_set)
return [a for a in found if key(a) == target and not DASH.is_trick_mode(a) and same_shape(a)]
keys: tuple[Callable[[Element], Any], ...] = (
DASH.adaptation_set_key,
lambda as_: DASH.adaptation_set_key(as_)[:-1],
kind_and_lang,
)
for key in keys:
if matches := sets(period, key):
break
else:
return None
with_id = [a for a in matches if any(r.get("id") == rep_id for r in same_shape(a))]
if len(with_id) == 1:
matched_as = with_id[0]
else:
siblings = sets(adaptation_set.getparent(), key)
ordinal = siblings.index(adaptation_set) if adaptation_set in siblings else 0
return matches[min(ordinal, len(matches) - 1)]
matched_as = matches[min(ordinal, len(matches) - 1)]
for as_ in period.findall("AdaptationSet"):
if DASH.is_trick_mode(as_):
continue
for rep in as_.findall("Representation"):
if rep.get("id") == rep_id:
return as_, rep
return None
group, source_group = same_shape(matched_as), same_shape(adaptation_set)
if len(group) == len(source_group):
return matched_as, group[source_group.index(source)]
return matched_as, min(
group, key=lambda r: (abs(rep_bandwidth(r) - rep_bandwidth(source)), r.get("id") != rep_id)
)
@staticmethod
def is_descriptive(adaptation_set: Element) -> bool:
+12 -8
View File
@@ -14,7 +14,7 @@ from typing import Any, Optional
import requests
from unshackle.core.config import config
from unshackle.core.proxies.proxy import Proxy
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
log = logging.getLogger("proxies.expressvpn")
@@ -248,15 +248,19 @@ class ExpressVPN(Proxy):
def get_locations(self) -> list[dict]:
if self.locations is None:
srt = self.get_srt()
if not srt:
return []
response = self.request(
"GET", f"{self.API_BASE}/ids2/locations", headers=self.api_headers(srt), params={"protocols": "proxy"}
)
self.locations = response.json().get("locations", [])
self.locations = self.fetch_locations()
return self.locations
@cached_catalogue
def fetch_locations(self) -> list[dict]:
srt = self.get_srt()
if not srt:
return []
response = self.request(
"GET", f"{self.API_BASE}/ids2/locations", headers=self.api_headers(srt), params={"protocols": "proxy"}
)
return response.json().get("locations", [])
def get_endpoints(self, location: dict) -> list[dict]:
srt = self.get_srt()
if not srt:
+17 -10
View File
@@ -15,7 +15,7 @@ import requests
from unshackle.core.config import config
from unshackle.core.console import prompt_user
from unshackle.core.proxies.proxy import Proxy
from unshackle.core.proxies.proxy import Proxy, cached_catalogue
log = logging.getLogger("proxies.proton")
@@ -195,22 +195,29 @@ class ProtonVPN(Proxy):
return chosen
def get_logicals(self) -> list[dict]:
"""Get Proton's server list. Only a list that loaded is cached, so a later session can still fetch it."""
if self.logicals is None:
response = self.api("GET", "/vpn/v1/logicals")
if response is None:
return []
self.logicals = response.json().get("LogicalServers") or []
"""Get Proton's server list. Only a list that loaded is kept, so a later call can still fetch it."""
if not self.logicals:
self.logicals = self.fetch_logicals()
return self.logicals
@cached_catalogue
def fetch_logicals(self) -> list[dict]:
response = self.api("GET", "/vpn/v1/logicals")
return (response.json().get("LogicalServers") or []) if response is not None else []
def get_max_tier(self) -> int:
"""The account's plan tier (0=free, 2=plus). Caps which servers it can connect to."""
if self.max_tier is None:
response = self.api("GET", "/vpn")
vpn = (response.json().get("VPN") or {}) if response is not None else {}
self.max_tier = int(vpn.get("MaxTier") or 0)
self.max_tier = int(self.fetch_vpn().get("MaxTier") or 0)
return self.max_tier
@cached_catalogue
def fetch_vpn(self) -> dict:
"""The account's VPN section, kept only when it names the plan tier."""
response = self.api("GET", "/vpn")
vpn = (response.json().get("VPN") or {}) if response is not None else {}
return vpn if "MaxTier" in vpn else {}
def get_browser_token(self) -> Optional[tuple[str, str]]:
response = self.api("GET", f"/vpn/v1/browser/token?Duration={self.TOKEN_DURATION}")
if response is None:
+2 -1
View File
@@ -10,9 +10,10 @@ F = TypeVar("F", bound=Callable[..., Any])
def cached_catalogue(fetch: F) -> F:
"""Keep a proxy provider's server catalogue for ``CATALOGUE_TTL`` seconds; the next call fetches an empty answer again.
"""Keep a proxy provider's catalogue for ``CATALOGUE_TTL`` seconds; a failed (empty) answer is fetched again.
A server builds its proxy providers on every request, and each build fetches the catalogue.
One config builds every provider, so a config reload clears the cache.
"""
@functools.wraps(fetch)