forked from kenzuya/unshackle
perf(proxies): cache expressvpn and proton catalogues across requests
A config reload clears the cache; a failed answer is never kept.
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user