mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-27 19:35:06 +00:00
refactor(proxies): resolve every --proxy value through one shared entry
This commit is contained in:
@@ -7,7 +7,6 @@ from __future__ import annotations
|
||||
import base64
|
||||
import http.server
|
||||
import logging
|
||||
import re
|
||||
import socket
|
||||
import socketserver
|
||||
import threading
|
||||
@@ -31,7 +30,7 @@ from unshackle.core.proxies.controld import (
|
||||
local_proxy,
|
||||
parse_a_records,
|
||||
)
|
||||
from unshackle.core.proxies.resolve import REGION, resolve_proxy
|
||||
from unshackle.core.proxies.resolve import REGION_QUERY, resolve_proxy, resolve_proxy_value
|
||||
from unshackle.core.utilities import get_free_port
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
@@ -462,10 +461,10 @@ def test_a_server_gets_the_typed_proxy_only_when_this_machine_resolved_it_to_loo
|
||||
|
||||
@pytest.mark.parametrize("query", ["ca", "us:seattle", "us-ny", "uk1", "yul", "jfk", "res_yyz", "nordvpn:ca"])
|
||||
def test_a_region_query_matches(query: str) -> None:
|
||||
assert re.fullmatch(rf"(?:[a-z]+:)?{REGION}", query, re.IGNORECASE)
|
||||
assert re.fullmatch(rf"(?:[a-z]+:){{0,2}}{REGION}", f"gluetun:{query}", re.IGNORECASE)
|
||||
assert REGION_QUERY.fullmatch(query)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("uri", ["nas:3128", "localhost:8080", "proxy.example.com:8080", "1.2.3.4:8080", "us:1080"])
|
||||
def test_a_host_and_port_is_never_a_region_query(uri: str) -> None:
|
||||
assert not re.fullmatch(rf"(?:[a-z]+:){{0,2}}{REGION}", uri, re.IGNORECASE)
|
||||
resolved = resolve_proxy_value(uri, [])
|
||||
assert resolved.uri == uri and resolved.query is None
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
"""An import falls back to the export region for a proxy only when the region is a region code."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core import import_service
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("region", "proxies"), [("ca", {"all": "http://ca.proxy:8080"}), ("localhost:8080", {})])
|
||||
def test_the_export_region_becomes_a_proxy_only_when_it_is_a_region(
|
||||
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, region: str, proxies: dict
|
||||
) -> None:
|
||||
monkeypatch.setattr(import_service, "resolve_proxy_arg", lambda query: f"http://{query}.proxy:8080")
|
||||
ctx = SimpleNamespace(parent=SimpleNamespace(params={}))
|
||||
with caplog.at_level(logging.WARNING):
|
||||
session = import_service.ImportService.build_session(ctx, region) # type: ignore[arg-type]
|
||||
assert session.proxies == proxies
|
||||
assert ("continuing without one" in caplog.text) is not proxies
|
||||
@@ -1,21 +1,24 @@
|
||||
"""Unit tests for the shared proxy dispatch that dl, search and resolve_proxy use: the provider:query
|
||||
split, the named or bare provider lookup, and the line that reports which proxy provider answered."""
|
||||
"""Unit tests for the shared proxy dispatch that dl, search, the REST API and the remote client use: the
|
||||
provider:query split, the named or bare provider lookup, and the line that reports which proxy provider answered."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.proxies import Basic
|
||||
from unshackle.core.proxies.proxy import Proxy
|
||||
from unshackle.core.proxies.resolve import (
|
||||
ResolvedProxy,
|
||||
Unavailable,
|
||||
describe_proxy,
|
||||
pick_proxy,
|
||||
resolve_proxy,
|
||||
resolve_proxy_value,
|
||||
split_proxy_query,
|
||||
)
|
||||
|
||||
@@ -215,3 +218,77 @@ def test_named_query_passes_a_query_error_on_unchanged() -> None:
|
||||
|
||||
with pytest.raises(ValueError, match="^no server for us150$"):
|
||||
pick_proxy([Strict()], "us150", "strict")
|
||||
|
||||
|
||||
class Gluetun(Alpha):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
[
|
||||
(
|
||||
"US",
|
||||
ResolvedProxy(
|
||||
"http://a:b@alpha.example:80", None, "us", "Using Alpha Proxy: http://xxxxx:xxxxx@alpha.example:80"
|
||||
),
|
||||
),
|
||||
(
|
||||
"Beta:CA",
|
||||
ResolvedProxy("http://c:d@beta-ca.example:80", "Beta", "ca", "Using Beta Proxy via Dallas (server 12)"),
|
||||
),
|
||||
(
|
||||
"alpha:beta:ca",
|
||||
ResolvedProxy(
|
||||
"http://a:b@alpha-beta.example:80",
|
||||
"alpha",
|
||||
"beta:ca",
|
||||
"Using Alpha Proxy: http://xxxxx:xxxxx@alpha-beta.example:80",
|
||||
),
|
||||
),
|
||||
(
|
||||
"http://user:pass@proxy.example:8080",
|
||||
ResolvedProxy(
|
||||
"http://user:pass@proxy.example:8080",
|
||||
None,
|
||||
None,
|
||||
"Using explicit Proxy: http://xxxxx:xxxxx@proxy.example:8080",
|
||||
),
|
||||
),
|
||||
("localhost:8080", ResolvedProxy("localhost:8080", None, None, "Using a local proxy on port 8080")),
|
||||
("1.2.3.4:3128", ResolvedProxy("1.2.3.4:3128", None, None, "Using explicit Proxy: 1.2.3.4:3128")),
|
||||
("us:1080", ResolvedProxy("us:1080", None, None, "Using explicit Proxy: us:1080")),
|
||||
],
|
||||
)
|
||||
def test_resolve_proxy_value_reads_each_form(providers: list, value: str, expected: ResolvedProxy) -> None:
|
||||
providers[1].answers["beta:ca"] = "http://a:b@alpha-beta.example:80"
|
||||
assert resolve_proxy_value(value, providers) == expected
|
||||
|
||||
|
||||
def test_resolve_proxy_value_starts_a_forwarder_for_a_control_d_resolver(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from unshackle.core.proxies import controld
|
||||
|
||||
monkeypatch.setattr(controld, "local_proxy", lambda uri: "http://127.0.0.1:4000")
|
||||
resolved = resolve_proxy_value("controld://efgh456@dns.controld.com", [])
|
||||
assert resolved == ResolvedProxy("http://127.0.0.1:4000", None, None, "Using a local proxy on port 4000")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "message"), [("us", "Getting a Proxy to us..."), ("gluetun:ca", "Connecting to VPN (ca)...")]
|
||||
)
|
||||
def test_the_status_wraps_only_the_provider_lookup(providers: list, value: str, message: str) -> None:
|
||||
shown: list[str] = []
|
||||
|
||||
@contextmanager
|
||||
def status(text: str) -> Iterator[None]:
|
||||
shown.append(text)
|
||||
yield
|
||||
|
||||
resolve_proxy_value(value, [*providers, Gluetun(ca="http://g.example:8888")], status=status)
|
||||
resolve_proxy_value("http://proxy.example:8080", providers, status=status)
|
||||
assert shown == [message]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["localhost:8080", "1.2.3.4:3128"])
|
||||
def test_resolve_proxy_passes_a_host_and_port_through(providers: list, value: str) -> None:
|
||||
assert resolve_proxy(value, providers) == value
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import re
|
||||
import socket
|
||||
import ssl
|
||||
import threading
|
||||
@@ -19,7 +18,7 @@ from cryptography.x509.oid import NameOID
|
||||
|
||||
from unshackle.core.proxies import asn, surfsharkvpn
|
||||
from unshackle.core.proxies.nordvpn import NordVPN
|
||||
from unshackle.core.proxies.resolve import REGION
|
||||
from unshackle.core.proxies.resolve import REGION_QUERY
|
||||
from unshackle.core.proxies.surfsharkvpn import SurfsharkVPN
|
||||
from unshackle.core.proxies.windscribevpn import WindscribeVPN
|
||||
from unshackle.core.remote_service import resolve_remote_proxy_arg
|
||||
@@ -54,7 +53,7 @@ def known_networks(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
)
|
||||
def test_region_grammar_takes_the_selector_but_not_a_uri(value: str, is_query: bool) -> None:
|
||||
"""dl and search send a value to the proxy providers only when it matches; anything else is an explicit URI."""
|
||||
assert bool(re.fullmatch(rf"(?:[a-z]+:)?{REGION}", value, re.IGNORECASE)) is is_query
|
||||
assert bool(REGION_QUERY.fullmatch(value)) is is_query
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
Reference in New Issue
Block a user