from __future__ import annotations import json import logging import os import random import re from pathlib import Path from typing import Optional import requests from unshackle.core.config import config from unshackle.core.proxies.proxy import Proxy log = logging.getLogger("proxies.proton") class ProtonVPN(Proxy): """ Proton VPN HTTPS proxy provider. Reuses an exported account.proton.me session (the AUTH-/REFRESH- cookies) to call the VPN API, resolves a server by country/city, mints short-lived proxy credentials, and returns an authenticated HTTPS proxy URL (https://user:pass@server:4443). Query format (after the provider prefix, e.g. "protonvpn:us"): us random server in the country (paid preferred over free) us12 Proton server #12 (the number shown in the connection log) us:ny city, NordVPN-style Free Proton accounts work, limited to the free-tier exit countries. """ API_BASE = "https://account.proton.me/api" APP_VERSION = "browser-vpn@1.3.5" PROXY_PORT = 4443 SECURE_CORE_PORT = 443 SECURE_CORE_FEATURE = 1 # Features bitmask: 1=SecureCore, 2=Tor, 4=P2P, 8=Streaming, 16=IPv6 TOR_FEATURE = 2 EXCLUDE_FEATURES = SECURE_CORE_FEATURE | TOR_FEATURE # skip Secure Core + Tor; both are slow for a plain proxy TOKEN_DURATION = 1200 # free tier caps the granted lifetime lower COUNTRY_ALIASES = {"gb": "uk"} # Proton labels the United Kingdom "UK", not the ISO "GB" def __init__( self, server_map: Optional[dict[str, str]] = None, cookie_path: Optional[str] = None, cache_path: Optional[str] = None, timeout: float = 10.0, ): self.server_map = {str(k).lower().strip(): str(v).lower().strip() for k, v in (server_map or {}).items()} self.timeout = timeout self.uid: Optional[str] = None self.access_token: Optional[str] = None self.refresh_token: Optional[str] = None self.logicals: Optional[list[dict]] = None self.max_tier: Optional[int] = None self.last_name: Optional[str] = None self.last_city: Optional[str] = None self.last_host: Optional[str] = None self.cookie_path = Path(cookie_path).expanduser() if cookie_path else self.default_cookie_path() self.cache_path = Path(cache_path).expanduser() if cache_path else self.default_cache_path() def __repr__(self) -> str: try: servers = self.get_logicals() # eager so the "Loaded ProtonVPN: …" line shows the catalog size except Exception: servers = self.logicals or [] if not servers: return "Proton VPN HTTPS Proxy" countries = len({str(s.get("ExitCountry") or "").upper() for s in servers if s.get("ExitCountry")}) return ( f"{countries} Countr{'ies' if countries != 1 else 'y'} ({len(servers)} Server{'s' if servers != 1 else ''})" ) def get_proxy(self, query: str) -> Optional[str]: server = self.resolve_server(query.strip().lower()) if not server: return None creds = self.get_browser_token() if not creds: log.error("Proton: could not obtain proxy credentials") return None self.last_host = host = str(server.get("Domain") or "") port = self.SECURE_CORE_PORT if int(server.get("Features") or 0) & self.SECURE_CORE_FEATURE else self.PROXY_PORT # Username (a JWT) and Password are URL-safe, but quote so requests rebuilds Basic auth cleanly user, pwd = (requests.utils.quote(c, safe="") for c in creds) return f"https://{user}:{pwd}@{host}:{port}" def last_connection_display(self) -> Optional[str]: if not self.last_name or not self.last_host: return None label = f"{self.last_name} - {self.last_city}" if self.last_city else self.last_name return f"({label}): {self.last_host.replace('.protonvpn.net', '')}" def resolve_server(self, query: str) -> Optional[dict]: self.last_name = self.last_city = self.last_host = None query = self.server_map.get(query, query) country, city, server_num = self.parse_query(query) pool = [] if country: max_tier = self.get_max_tier() # only servers the account can actually connect to pool = [ s for s in self.get_logicals() if str(s.get("ExitCountry") or "").lower() == country and s.get("Status") == 1 and int(s.get("Tier") or 0) <= max_tier and not int(s.get("Features") or 0) & self.EXCLUDE_FEATURES ] if city: pool = self.match_city(pool, city) if not pool: log.warning("Proton: no server matched query '%s'", query) return None return self.select(pool, server_num) def parse_query(self, query: str) -> tuple[Optional[str], Optional[str], Optional[int]]: """Parse "us", "us12"/"us-12" (server #12), and "us:ny" (city).""" city = None if ":" in query: query, city = query.split(":", 1) city = city.strip() or None match = re.match(r"^([a-z]{2})-?(\d+)?$", query.strip()) if not match: return None, None, None country = self.COUNTRY_ALIASES.get(match.group(1), match.group(1)) server_num = int(match.group(2)) if match.group(2) else None valid = {str(s.get("ExitCountry") or "").lower() for s in self.get_logicals()} return (country, city, server_num) if country in valid else (None, None, None) def match_city(self, servers: list[dict], city_query: str) -> list[dict]: city_query = city_query.strip().lower() exact, prefix, substring = [], [], [] for s in servers: city = str(s.get("City") or "") slug = slugify(city) abbreviation = "".join(w[0] for w in re.findall(r"[a-z]+", city.lower())) if city_query in (slug, abbreviation): exact.append(s) elif slug.startswith(city_query): prefix.append(s) elif city_query in slug: substring.append(s) return exact or prefix or substring def select(self, pool: list[dict], server_num: Optional[int]) -> dict: if server_num is not None: matches = [s for s in pool if server_no(s) == server_num] if matches: pool = matches else: log.warning("Proton: server #%d not found (%d available), picking random", server_num, len(pool)) else: pool = [s for s in pool if int(s.get("Tier") or 0) > 0] or pool chosen = random.choice(pool) self.last_name = chosen.get("Name") self.last_city = chosen.get("City") return chosen def get_logicals(self) -> list[dict]: if self.logicals is None: response = self.api("GET", "/vpn/v1/logicals") self.logicals = (response.json().get("LogicalServers") or []) if response is not None else [] return self.logicals 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) return self.max_tier 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: return None body = response.json() if body.get("Username") and body.get("Password"): return body["Username"], body["Password"] log.error("Proton: browser/token response missing credentials") return None def api(self, method: str, path: str) -> Optional[requests.Response]: """Call the Proton API, refreshing the session once on a 401.""" if not self.access_token: self.load_session() if not self.access_token: log.error("Proton: no session available; export account.proton.me cookies to %s", self.cookie_path) return None for attempt in range(2): response = requests.request(method, f"{self.API_BASE}{path}", headers=self.headers(), timeout=self.timeout) if response.status_code == 401 and attempt == 0 and self.refresh_session(): continue if response.status_code == 401: log.error("Proton: session expired; re-export account.proton.me cookies to %s", self.cookie_path) return None if not response.ok: raise ValueError(f"Proton request failed with HTTP {response.status_code}: {path}") return response return None def headers(self) -> dict[str, str]: return { "Authorization": f"Bearer {self.access_token}", "x-pm-uid": self.uid or "", "x-pm-appversion": self.APP_VERSION, "Accept": "application/json", } def refresh_session(self) -> bool: if not self.refresh_token or not self.uid: return False response = requests.post( f"{self.API_BASE}/auth/refresh", json={ "UID": self.uid, "ResponseType": "token", "GrantType": "refresh_token", "RefreshToken": self.refresh_token, "RedirectURI": "https://protonvpn.com", }, headers={"x-pm-uid": self.uid, "x-pm-appversion": self.APP_VERSION, "Accept": "application/json"}, timeout=self.timeout, ) if not response.ok: log.warning("Proton: session refresh failed with HTTP %s", response.status_code) return False body = response.json() self.access_token = body.get("AccessToken") or self.access_token self.refresh_token = body.get("RefreshToken") or self.refresh_token self.save_session() return True def load_session(self) -> None: cached = self.load_cache() api_host = self.API_BASE.split("://", 1)[-1].split("/", 1)[0] cookie_uid = cookie_access = cookie_refresh = None for name, value, domain in self.load_cookies(): if domain and domain.lstrip(".") != api_host: continue if name.startswith("AUTH-"): cookie_uid, cookie_access = name[len("AUTH-") :], value elif name.startswith("REFRESH-"): cookie_refresh = value # Cached tokens win, but only for the same account and not older than a re-exported cookie file same_account = cookie_uid is None or cached.get("uid") == cookie_uid cookie_newer = ( self.cookie_path.is_file() and self.cache_path.is_file() and self.cookie_path.stat().st_mtime > self.cache_path.stat().st_mtime ) if cached.get("access_token") and same_account and not cookie_newer: self.uid = cached["uid"] self.access_token = cached["access_token"] self.refresh_token = cached.get("refresh_token") return self.uid, self.access_token = cookie_uid, cookie_access # A cookie-only re-export may lack REFRESH-; reuse the cache's refresh token if it's the same account self.refresh_token = cookie_refresh or (cached.get("refresh_token") if same_account else None) if self.uid and self.access_token: self.save_session() def load_cookies(self) -> list[tuple[str, str, str]]: """Return [(name, value, domain), ...] from a Netscape cookies.txt or JSON cookie list.""" if not self.cookie_path.is_file(): return [] try: content = self.cookie_path.read_text(encoding="utf-8").strip() except OSError as error: log.error("Proton: failed to read cookies file %s: %s", self.cookie_path, error) return [] try: data = json.loads(content) except json.JSONDecodeError: # Netscape: domain \t flag \t path \t secure \t expiry \t name \t value # Proton's AUTH-/REFRESH- cookies are HttpOnly, exported as "#HttpOnly_\t..." out = [] for line in content.splitlines(): line = line.removeprefix("#HttpOnly_") parts = line.split("\t") if not line.startswith("#") and len(parts) >= 7: out.append((parts[5], parts[6], parts[0])) return out items = data.get("cookies") if isinstance(data, dict) else data return [ (str(c["name"]), str(c.get("value", "")), str(c.get("domain", ""))) for c in (items or []) if isinstance(c, dict) and c.get("name") ] def load_cache(self) -> dict: if not self.cache_path.is_file(): return {} try: data = json.loads(self.cache_path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError) as error: log.warning("Proton: failed to read token cache %s: %s", self.cache_path, error) return {} return data if isinstance(data, dict) else {} def save_session(self) -> None: tokens = {"uid": self.uid, "access_token": self.access_token, "refresh_token": self.refresh_token} try: self.cache_path.parent.mkdir(parents=True, exist_ok=True) write_private(self.cache_path, json.dumps({k: v for k, v in tokens.items() if v}, indent=2)) except OSError as error: log.error("Proton: failed to save token cache %s: %s", self.cache_path, error) def default_cookie_path(self) -> Path: cookies_dir = Path(config.directories.cookies) for folder in ("vpn", "vpns"): for fname in ("protonvpn.txt", "proton.txt"): if (candidate := cookies_dir / folder / fname).is_file(): return candidate return cookies_dir / "vpn" / "protonvpn.txt" def default_cache_path(self) -> Path: return Path(config.directories.cache) / "global" / "proton_tokens.json" def slugify(value: str) -> str: return re.sub(r"[^a-z0-9]+", "-", value.lower()).strip("-") def server_no(server: dict) -> Optional[int]: """Proton's server number from the logical name, e.g. "US#231" -> 231, "US-CO#21" -> 21.""" match = re.search(r"#(\d+)", str(server.get("Name") or "")) return int(match.group(1)) if match else None def write_private(path: Path, content: str) -> None: """Write content to path created with owner-only (0600) permissions.""" if hasattr(os, "fchmod"): fd = os.open(str(path), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) try: os.fchmod(fd, 0o600) os.write(fd, content.encode("utf-8")) finally: os.close(fd) else: path.write_text(content, encoding="utf-8") try: path.chmod(0o600) except OSError: pass