feat(Basic): Allow proxy selection by index (one-indexed)

This commit is contained in:
rlaphoenix 2024-04-01 17:50:08 +01:00
parent b36befb296
commit 491a0b3a5a
2 changed files with 21 additions and 3 deletions

View File

@ -302,7 +302,9 @@ us:
de: "https://127.0.0.1:8080"
```
Note that if multiple proxies are defined for a region, then it will randomly choose which one to use.
Note that if multiple proxies are defined for a region, then by default one will be randomly chosen.
You can choose a specific one by specifying it's number, e.g., `--proxy basic:us2` will choose the
second proxy of the US list.
### nordvpn (dict)

View File

@ -1,4 +1,5 @@
import random
import re
from typing import Optional, Union
from requests.utils import prepend_scheme_if_needed
@ -25,12 +26,27 @@ class Basic(Proxy):
"""Get a proxy URI from the config."""
query = query.lower()
servers = self.countries.get(query)
match = re.match(r"^([a-z]{2})(\d+)?$", query, re.IGNORECASE)
if not match:
raise ValueError(f"The query \"{query}\" was not recognized...")
country_code = match.group(1)
entry = match.group(2)
servers: Optional[Union[str, list[str]]] = self._data.get(country_code)
if not servers:
return
raise ValueError(f"There's no proxies configured for \"{country_code}\"...")
if isinstance(servers, str):
proxy = servers
elif entry:
try:
proxy = servers[int(entry) - 1]
except IndexError:
raise ValueError(
f"There's only {len(servers)} prox{'y' if len(servers) == 1 else 'ies'} "
f"for \"{country_code}\"..."
)
else:
proxy = random.choice(servers)